0

I am trying to use to predict function to predict 100 points new points. I have a data.frame with one vector that is 100 doubls long.

I am trying the predict function: predict(model, newdata=mydat)

The function only returns a vector of length four. This could be due to the fact that the model was made only with four points, but I am unsure.

EDIT:

Creation of mydat

mydat <- data.frame(V1 = seq(0, max(myExperimentSummary$V1), length.out = 100))

The model I am using

model
#Nonlinear regression model
#  model: mean ~ (1/(1 + exp(-b * (V1 - c))))
#   data: myExperimentSummary
#      b       c 
#-0.6721  3.2120 
# residual sum-of-squares: 0.04395
# 
#Number of iterations to convergence: 1 
#Achieved convergence tolerance: 5.204e-06

EDIT2: Fixing the typos

EDIT3:

fitcoef = nlsLM(mean~(a/(1+exp(-b*(V5-c)))), data = myExperimentSummary,
                start=c(a=1,b=.1,c=25))

fitmodel = nls(mean~(1/(1+exp(-b*(V1-c)))), data = myExperimentSummary,
               start=coef(fitcoef))

mydat <- data.frame(V1 = seq(0, max(myExperimentSummary$V1), length.out = 100))

predict(fitmodel, mydat)
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Nicholas Hayden
  • 473
  • 1
  • 3
  • 24
  • 1
    you need to show what model you are predicting, and show how you generate mydat. – user2957945 Sep 16 '16 at 00:18
  • 1
    I'll edit the post to add those – Nicholas Hayden Sep 16 '16 at 00:20
  • 1
    okay, your newdata has to match the variables in the rhs of the equation. Your newdata has `S` but the model has `V1` – user2957945 Sep 16 '16 at 00:22
  • 1
    i think this should probably be closed as a typo – user2957945 Sep 16 '16 at 00:24
  • @user2957945 I remade `mydat` with the proper `V1` and the predict function is returning still only 4 values. – Nicholas Hayden Sep 16 '16 at 00:27
  • well your code should run... must be something you are not showing us. Is it possible to show a full example please – user2957945 Sep 16 '16 at 00:29
  • 1
    @ZheyuanLi; i thought that but the model summary displys the dataframe name – user2957945 Sep 16 '16 at 00:30
  • @ZheyuanLi I made the model with your advice as `model=nls(mean~(1/(1+exp(-b*(V1-c)))),data=myExperimentSummary,start=coef(fitcoef))` Comments are taking a while to commit – Nicholas Hayden Sep 16 '16 at 00:40
  • @ZheyuanLi I really appreciate your help. I'll accept your answer. However, I think my problem lies in that the predict function/model is referring to the original modeled data. I can put a vector of size 1 and it will still return a vector of size 4 that are the same numbers. – Nicholas Hayden Sep 16 '16 at 00:53
  • 1
    This exchange demonstrates why a minimal reproducible example is almost always helpful, rather than plain English descriptions of what OP is trying to do. @Nicholas, please provide code that enables others to reproduce your problem, this will help us help you in an efficient way instead of going back-and-forth. – Weihuang Wong Sep 16 '16 at 00:56
  • @WeihuangWong I think, also, its is my lack of knowledge. Will do. – Nicholas Hayden Sep 16 '16 at 01:02
  • 1
    @ZheyuanLi I have no idea how it happen but as I was parse through my commands and making the edits it worked. I have no idea. Thank a bunch for your help. Sorry for being annoying – Nicholas Hayden Sep 16 '16 at 01:21

1 Answers1

1

If your data are still as in your previous question:

dat <- read.table(text = " V1  N mean  
                          0.1  9 0.9 
                            1  9 0.8 
                           10  9 0.1 
                            5  9 0.2",
                  header = TRUE)

model <- nls(mean ~ -a/(1 + exp(-b * (V1-o))), data = dat,
             start=list(a=-1.452, b=-0.451, o=1.292))

Then I can not reproduce your problem:

mydat <- data.frame(V1 = seq(0, max(dat$V1), length.out = 100))

y <- predict(model, mydat)

length(y)
# [1] 100
Community
  • 1
  • 1
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248