Hello, How do I specify the formula and random effects without a startup object ? I thought it would be a mixture of nls and lme. after trying very hard, I ask for help on using nlme. Can someone hint me to some examples? I constructed a try using the example from nls: #variables are density, conc and Run #all works fine with nls DNase1 <- subset(DNase, Run == 1 ) fm2DNase1 <- nls( density ~ 1/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(xmid = 0, scal = 1), trace = TRUE) #Now I want to do a mixed model with covariate Run #how is the syntax? DNase12 <- subset(DNase, Run == 1 | Run == 2) fm2DNase1 <- nlme( density ~ 1/(1 + exp((xmid - log(conc))/scal)), data = DNase12, fixed = conc ~ 1, random = Run ~ 1, start = list(xmid = 0, scal = 1) ) #gives: Error in eval(expr, envir, enclos) : object "xmid" not found Thomas
On 9/11/06, Thomas Wutzler <thomas.wutzler at web.de> wrote:> Hello, > > > How do I specify the formula and random effects without a startup object > ? I thought it would be a mixture of nls and lme. > after trying very hard, I ask for help on using nlme. > > Can someone hint me to some examples?There are quite a few examples in the book Pinheiro and Bates (2000), "Mixed-effects Models in S and S-PLUS"> I constructed a try using the example from nls: > > #variables are density, conc and Run > #all works fine with nls > DNase1 <- subset(DNase, Run == 1 ) > fm2DNase1 <- nls( density ~ 1/(1 + exp((xmid - log(conc))/scal)), > data = DNase1, > start = list(xmid = 0, scal = 1), > trace = TRUE) > > #Now I want to do a mixed model with covariate RunDo you mean that "Run" is the grouping factor for the random effects? Your formulas for fixed and random in the function call below don't make sense. The left hand side of each formula should be an expression that involves parameters xmid and/or scal, not the covariate conc or the grouping factor Run. Also the starting estimates should either be a named numeric vector or a list with a component called "fixed" that is a named numeric vector.> #how is the syntax? > DNase12 <- subset(DNase, Run == 1 | Run == 2) > fm2DNase1 <- nlme( density ~ 1/(1 + exp((xmid - log(conc))/scal)), > data = DNase12, > fixed = conc ~ 1, > random = Run ~ 1, > start = list(xmid = 0, scal = 1) > ) > #gives: Error in eval(expr, envir, enclos) : object "xmid" not found> library(nlme) > fm2DNase1 <- nlme( density ~ 1/(1 + exp((xmid - log(conc))/scal)),+ data = DNase, + fixed = xmid + scal ~ 1, + random = xmid ~ 1|Run, + start = list(fixed = c(xmid = 0, scal = 1))) By the way, that model doesn't make sense. The fitted values from the model must be in the range [0,1] but the observed values exceed 2> range(DNase$density)[1] 0.011 2.003 Also, it is not a good idea to fit a nonlinear mixed effects model when you have only two levels of the grouping factor. You are estimating variances. Doing so with only two distinct groups is quite inaccurate.
<in line> Douglas Bates wrote:> On 9/11/06, Thomas Wutzler <thomas.wutzler at web.de> wrote: > >> Hello, >> >> >> How do I specify the formula and random effects without a startup object >> ? I thought it would be a mixture of nls and lme. >> after trying very hard, I ask for help on using nlme. >> >> Can someone hint me to some examples? >> > > There are quite a few examples in the book Pinheiro and Bates (2000), > "Mixed-effects Models in S and S-PLUS" >Permit me to offer a personal testimonial: If you do more statistics than just this one data set, I believe you will be amply rewarded for investing money and time in that book. The value of this investment will be further enhanced by working line by line (as you read the text) through the files "ch01.R", "ch02.R", ..., "ch06.R" and "ch08.R" in "~library\nlme\scripts" in your R installation directory. Spencer Graves> ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Dear Douglas Bates and Spencer Graves, thank you for your replies. In the meantime I got the book and will recommend it to my colleages :) I learned the answer to my problem and want to post it here: If you use ungrouped data (a plain dataframe) you must specifiy the grouping variable in the random argument behind "|" The fixed argument is used to distuingish coefficients from data inputs. In its simple form, it is not a list (as in nls), but a formula with all coefficients seperated by + on the left side and ~ 1 as right hand side (other forms allow e.g. including additional covariates) The random argument is used to specify those coefficients that have a random effect associated. Again (in its simple form) it is not a list but a formula with all coefficients on the left side seperated by + on the left side and ~ 1 on the right side (other forms allow nested effects, different correlation matrices, etc.) The grouping variable is specified on the far right behind "|". My previous example does not make sense, because there are no repetitions for the runs, so I switch to the Loblolly example of the online help. In online-help-examples, the grouping variable is not specified in the call to lme, because it is already specified in the grouped data (see ?groupedData). In the online help example, "Seed" is the grouping factor. I did not understand the online help first, because the grouping variable was not specified with the call to nlme. the example specifies the call: fm1 <- nlme(height ~ SSasymp(age, Asym, R0, lrc), data = Loblolly, fixed = Asym + R0 + lrc ~ 1, random = Asym ~ 1, start = c(Asym = 103, R0 = -8.5, lrc = -3.3)) without grouped data (athough I recommend using grouped data after reading the book), the call can be formulated as: l2 <- data.frame( height=Loblolly$height, age=Loblolly$age, Seed=Loblolly$Seed) fm2 <- nlme(height ~ Asym+(R0-Asym)*exp(-exp(lrc)*age) ,data = l2 ,fixed = Asym + R0 + lrc ~ 1 ,random = Asym ~ 1 | Seed ,start = c(Asym = 103, R0 = -8.5, lrc = -3.3) ) Thanks, Thomas
Thomas Wutzler <twutz <at> bgc-jena.mpg.de> writes:> without grouped data (athough I recommend using grouped data after > reading the book), the call can be formulated as:Well, this is probably the only point where I disagree with Douglas Bates. I found using grouped data introduces a level of indirection that makes life more difficult. And, if you check the examples in this list, very few of them use grouped data. So I would wish that in the coming book there were more examples without grouped data, as I know that finding the right syntax for these is difficult when the book mainly hast groupedData examples. Dieter