问题
As said in the help(predict.nls), when se.fit=TRUE,the standard errors of the predictions should be calculated. However, my codes in the following do not display that, but only the predictions.
alloy <- data.frame(x=c(10,30,51,101,203,405,608,810,1013,2026,4052,6078,
8104,10130),
y=c(0.3561333,0.3453,0.3355,0.327453,0.3065299,0.2839316,
0.2675214,0.2552821,0.2455726,0.2264957,0.2049573,
0.1886496,0.1755897,0.1651624))
model <- nls(y ~ a * x^(-b), data=alloy, start=list(a=.5, b=.1))
predict(model, se=TRUE)
What's wrong with my codes? Thank you!
回答1:
Nothing. The last line of the Description
section of ?predict.nls
says:
At present ‘se.fit’ and ‘interval’ are ignored.
Perhaps the deltaMethod
function in the car
package would help. (Results of library("sos"); findFn("{delta method} nls")
...)
update: I didn't get it work with car::deltaMethod
on my first try. Here's an attempt to use the deltavar
function from the emdbook
package. The attach()/detach()
stuff is a horrible hack, but it's what I could get to work quickly (a with
-based solution didn't work because of non-standard evaluation). Improvements welcome.)
attach(alloy)
> deltavar(a*x^(-b),meanval=coef(model),Sigma=vcov(model))
[1] 1.445018e-04 6.956934e-05 4.897363e-05 3.287949e-05 2.527709e-05
[6] 2.379668e-05 2.487129e-05 2.626029e-05 2.762930e-05 3.300235e-05
[11] 3.933191e-05 4.317493e-05 4.588509e-05 4.795406e-05
> detach(alloy)
Caveats:
- these are variances, not standard errors;
- this is a delta-method approximation, which may be inaccurate in cases of strong nonlinearity or strongly non-Gaussian error (I believe Brian Ripley has stated in the past that he doesn't consider delta-method approximations reliable enough for general use, but he's a purist)
- I haven't checked that this is a sensible answer
来源:https://stackoverflow.com/questions/12425920/predict-cannot-display-the-standard-errors-of-the-predictions-with-se-fit-true