Stuart Rosen
2011-Apr-20 15:04 UTC
[R] How can I 'predict' from an nls model with a fit specified for separate groups?
Following an example on p 111 in 'Nonlinear Regression with R' by Ritz & Streibig, I have been fitting nls models using square brackets with the grouping variable inside. In their book is this example, in which 'state' is a factor indicating whether a treatment has been used or not: > Puromycin.m1 <- nls(rate ~ Vm[state] * + conc/(K[state] + conc), data = Puromycin, + start = list(K = c(0.1, 0.1), + Vm = c(200, 200))) What I cannot figure out is how to specify the value of the grouping variable in a 'predict' statement. In my own example, I can only seem to get the predictions for the 1st specified level of the grouping variable. I promise that I have read the documentation, and have tried a number of things, but cannot get the correct predictions. Thank you for any help. Yours - Stuart -- /*------------------------------------------------*/ Stuart Rosen, PhD Professor of Speech and Hearing Science UCL Speech, Hearing and Phonetic Sciences
peter dalgaard
2011-Apr-20 17:34 UTC
[R] How can I 'predict' from an nls model with a fit specified for separate groups?
On Apr 20, 2011, at 17:04 , Stuart Rosen wrote:> Following an example on p 111 in 'Nonlinear Regression with R' by Ritz & Streibig, I have been fitting nls models using square brackets with the grouping variable inside. In their book is this example, in which 'state' is a factor indicating whether a treatment has been used or not: > > > Puromycin.m1 <- nls(rate ~ Vm[state] * > + conc/(K[state] + conc), data = Puromycin, > + start = list(K = c(0.1, 0.1), > + Vm = c(200, 200))) > > What I cannot figure out is how to specify the value of the grouping variable in a 'predict' statement. In my own example, I can only seem to get the predictions for the 1st specified level of the grouping variable. I promise that I have read the documentation, and have tried a number of things, but cannot get the correct predictions.What's the problem? If I continue your example with plot(Puromycin$conc, predict(Puromycin.m1), col=(1:2)[Puromycin$state]) I get a black and a red set of points, distinctly different. It's difficult to say what you did wrong in the "number of things" that you tried, but I suspect you didn't set up your newdata properly:> myconc <- seq(0,1.2,,11)> lv <- levels(Puromycin$state) > predict(Puromycin.m1, newdata=data.frame(conc=myconc,state=factor("untreated",levels=lv)))[1] 0.0000 114.6850 133.7022 141.5248 145.7897 148.4743 150.3196 151.6661 [9] 152.6918 153.4993 154.1514> predict(Puromycin.m1, newdata=data.frame(conc=myconc,state=factor("treated",levels=lv)))[1] 0.0000 138.6154 167.8413 180.5289 187.6203 192.1490 195.2916 197.6000 [9] 199.3674 200.7641 201.8956 -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com