Skip to content
Prev 313152 / 398506 Next

scope, lme, ns, nlme, splines

On 06/12/2012 2:28 PM, Jacob Wegelin wrote:
Presumably this is a bug in nlme.  Formulas have associated 
environments.  In your example, that would be the local frame of the 
fitfunction, and splineDF lives there.  It looks as though lme is not 
looking in the formula environment for the splineDF value.

Another workaround besides the one you found is to use substitute() to 
put the value of splineDF into the formula, i.e.

fitfunction<-function(splineDF=1) {
       junk$splineDF <- splineDF
      thislm<-lm( y ~ ns(x, df=splineDF), data= junk)
      thislm
      cat("finished thislm\n")
       fixed <- eval(substitute( y ~ ns(x, df=splineDF), 
list(splineDF=splineDF)))
      thislme<-lme(
          fixed= fixed
          , random= ~ 1 | idvar
          , data= junk)
      thislme
}

It's not much better than your kludge in this example, but it might be 
more convenient if you want to work with different formulas.

Duncan Murdoch