[R] predict.nls - gives error but only on some nls objects
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Jun 1 00:28:20 CEST 2007
On Thu, 31 May 2007, Søren Højsgaard wrote:
> Dear Prof Ripley and the list
>
> Apologies for an unfortunate typing error. What I meant was:
>
>> predict(fm2DNase1)
>
> [1] 0.001424337 0.001424337 0.028883648 0.028883648 0.119576734 0.119576734 0.382791057 ...
>
>> nd <- as.data.frame(DNase1)
DNase1 is already a data frame.
>> class(nd)
> [1] "data.frame"
>
>> predict(fm2DNase1,newdata=nd)
>
> 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
>
> I know I don't have to supply newdata to just get the fitted values, but I want to enter another dataframe later...
It's picky: you need to supply just the named variables needed to fit the
model, here
predict(fm2DNase1, newdata=DNase1[2:3])
(That's not obvious from the documentation, of course.)
I've made it less picky in R-patched, but nls is not written in the same
way as 'standard' R modelling functions like lm, which would use
model.frame to extract the variables from 'newdata'. Doing so is not
entirely straightforward as there is no record of what variables are
needed for the RHS alone. The less picky form is also less safe: it is
too easy to get the wrong variables since the variables used for fitting
are visible in the environment used for prediction.
This needs something of a rethink in the light of the experience of years
of other prediction methods, but until then use with care.
>
> Regards
> Søren
>
>
> ________________________________
>
> Fra: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sendt: to 31-05-2007 14:28
> Til: Søren Højsgaard
> Cc: r-help at stat.math.ethz.ch
> Emne: Re: [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 page
>
> newdata: 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
>
>
--
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
More information about the R-help
mailing list