# nlme beginner question [closed]

I am trying to fit a mixed model to my data using the nlme library in R.

Here's what I have:

df <- data.frame(phen = phen.info.box[,2], hap = as.factor(mouse.haps), strain = as.factor(mouse.strains))

fit <- nlme(model = phen ~ hap + (1|strain),
data = df,
fixed = hap ~ 1,
random = strain ~ 1,
start = rep(1, 832),
correlation = K)


And the error I get is:

Error in getGroups.data.frame(dataMix, eval(parse(text = paste("~1", deparse(groups[[2]]), :
invalid formula for groups


I would like to do more complex things down the road, but I'm having trouble just getting this basic model running. I have read through the nlme documentation and can't seem to figure out this "invalid formula" error. I got the same error with the line "random = strain ~ 1" omitted. I read in the documentation that simply putting "fixed = var ~ 1" signals a single fixed effect, which is what I want.

I have to use nlme instead of lme4 because, as far as I understand, lme4 won't let me specify a variance-covariance matrix on the random effects and that's important to me (see "correlation = K" line.)

OK, I'm getting downvotes for this and I'm not sure why. I assume it's people thinking "this question is too easy to even be asked -- the asker must not have tried to solve it at all!". I have been looking through documentation and examples online and I cannot, for the life of me see the difference between how I'm doing it and this example I found online:

nlme.fit <- nlme(lgcopy ~ logexp2(p1,b1,p2,b2,day),
fixed = p1+b1+p2+b2 ~1,random = p1+b1+p2+b2 ~1,
data =aids.dat,start=c(start))


which apparently works.

## closed as off-topic by Macro, Nick Cox, gung♦, COOLSerdash, Matt KrauseAug 10 '13 at 15:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions about programming are off-topic here unless they involve statistical analysis in some fashion, but they can be asked on Stack Overflow." – Macro, Nick Cox, gung, COOLSerdash, Matt Krause
If this question can be reworded to fit the rules in the help center, please edit the question.

• Isn't (1|strain) only used in lmer (from package lme4) formulas, but not in nlme (from package nlme) formulas? What happens if you remove that part from the current formula? – JTT Aug 9 '13 at 18:45
• I removed the (1|strain) and replaced it with just strain. Good catch, but same error persists. – rcorty Aug 9 '13 at 18:52
• Can you post your data, dput(df), so we can run the code? There may be a problem with your formula phen ~ hap + (1|strain) or as you say you modified it to phen ~ hap + strain. As the help for nlme states it is looking for a nonlinear model formula and it looks like you are providing it with a linear forumula (where the coefficients are assumed rather than specified). – Jean V. Adams Aug 9 '13 at 19:47
• Jean, great idea. I've now switched to using "lme" and am playing with that. – rcorty Aug 9 '13 at 20:21
• dput was too long, even for df[1:3,], so here's str: 'data.frame': 832 obs. of 3 variables: $phen : num 81.8 73.8 80.5 86 81.3 ...$ hap : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 2 2 1 1 1 1 ... \$ strain: Factor w/ 45 levels "1","2","3","4",..: 1 1 2 2 3 3 2 2 2 2 ... – rcorty Aug 9 '13 at 20:26