Hadley Wickham
2004-Jan-21 22:00 UTC
[R] Problem with lme, ns and df (variable scoping problem?)
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
Prof Brian Ripley
2004-Jan-22 01:03 UTC
[R] Problem with lme, ns and df (variable scoping problem?)
Use substitute, something like lml <- eval(substitute(lmList(y ~ ns(x,df=d.f) | c, a), list(d.f.=df))) This sort of thing is in any case necessary to get a sensible call component recorded in the fitted object. On Thu, 22 Jan 2004, Hadley Wickham wrote:> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595