Søren Højsgaard
2007-May-31 09:19 UTC
[R] predict.nls - gives error but only on some nls objects
Dear list, I have encountered a problem with predict.nls (Windows XP, R.2.5.0), but I am not sure if it is a bug... On the nls man page, an example is: DNase1 <- subset(DNase, Run == 1) fm2DNase1 <- nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(xmid = 0, scal = 1)) alg = "plinear", trace = TRUE) Now consider prediction:> predict(fm2DNase1)[1] 0.001424337 0.001424337 0.028883648 0.028883648 .....> predict(fm2DNase1,newdata=fm2DNase1)Error in if (sum(wrong) == 1) stop(gettextf("variable '%s' was fitted with class \"%s\" but class \"%s\" was supplied", : missing value where TRUE/FALSE needed What causes the trouble is the call to .checkMFClasses(cl, newdata) in predict.nls. Incidently, on the predict.nls page the example works:> fm <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) > predict(fm)[1] 7.887449 12.524977 15.251673 16.854870 17.797490 18.677580> predict(fm,newdata=BOD)[1] 7.887449 12.524977 15.251673 16.854870 17.797490 18.677580 attr(,"gradient") A lrc [1,] 0.4120369 5.977499 [2,] 0.6542994 7.029098 .... Is there a bug, or am I overlooking something?? Regards Søren [[alternative HTML version deleted]]
Prof Brian Ripley
2007-May-31 12:28 UTC
[R] predict.nls - gives error but only on some nls objects
Why do you think feeding a model fit (fm2DNase1) is suitable 'newdata'?.>From the help pagenewdata: A named list or data frame in which to look for variables with which to predict. If 'newdata' is missing the fitted values at the original data points are returned. It is the unsuitable 'newdata' that is causing the error. On Thu, 31 May 2007, S?ren H?jsgaard wrote:> Dear list, > I have encountered a problem with predict.nls (Windows XP, R.2.5.0), but I am not sure if it is a bug... > > On the nls man page, an example is: > > DNase1 <- subset(DNase, Run == 1) > fm2DNase1 <- nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)), > data = DNase1, > start = list(xmid = 0, scal = 1)) > alg = "plinear", trace = TRUE) > > Now consider prediction: > >> predict(fm2DNase1) > [1] 0.001424337 0.001424337 0.028883648 0.028883648 ..... > >> predict(fm2DNase1,newdata=fm2DNase1) > Error in if (sum(wrong) == 1) stop(gettextf("variable '%s' was fitted with class \"%s\" but class \"%s\" was supplied", : > missing value where TRUE/FALSE needed > > What causes the trouble is the call to .checkMFClasses(cl, newdata) in predict.nls. > > > Incidently, on the predict.nls page the example works: > >> fm <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) >> predict(fm) > [1] 7.887449 12.524977 15.251673 16.854870 17.797490 18.677580 >> predict(fm,newdata=BOD) > [1] 7.887449 12.524977 15.251673 16.854870 17.797490 18.677580 > attr(,"gradient") > A lrc > [1,] 0.4120369 5.977499 > [2,] 0.6542994 7.029098 > .... > > Is there a bug, or am I overlooking something?? > > Regards > S?ren > > > [[alternative HTML version deleted]] > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595