[R] predict.nls - gives error but only on some nls objects
Søren Højsgaard
Soren.Hojsgaard at agrsci.dk
Thu May 31 23:54:09 CEST 2007
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)
> 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...
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
More information about the R-help
mailing list