[R] Problem with lme, ns and df (variable scoping problem?)

Hadley Wickham h.wickham at auckland.ac.nz
Wed Jan 21 23:00:32 CET 2004


Hi,

I'm experimenting with random effect natural splines, and I've 
encountered an odd problem.

library(nlme); library(splines)
a <- data.frame(x = 1:10, y = 1:10 + runif(10, min=-3, max=3), c = 
rep(c(1,2),5))
df <- 10   
lml <- lmList(y ~ ns(x,df=df) | c, a)

Error in df - 1 : non-numeric argument to binary operator

I presume this is because the formula isn't evaluated until it's deep in 
the bowels of lmList, where a local df exists.  So I try again with a 
variable name that probably doesn't exist elsewhere:

d.f <- 10
lml <- lmList(y ~ ns(x,df=d.f) | c, a)

This works, but now when I calculate the mixed model, I get a different 
error.

lme <- lme(lml, random=pdIdent(~c))
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ

Neither of these problems occur if I specify df directly (eg. df=10)

Can anyone offer a work around?

Thanks.

Hadley




More information about the R-help mailing list