[R] as.formula and lme ( Fixed effects: Error in as.vector(x, "list") : cannot coerce to vector)
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Aug 12 21:29:55 CEST 2005
You can use substitute() to get the actual formula in your call.
demo.lm2 <-
eval(substitute(lme(fma, data=demo, random=~1|subj), list(fma=fma)))
works for me in your example.
On Fri, 12 Aug 2005, Liu Song wrote:
> This is a continuing issue with the one on the list a long time ago (I
> couldn't find a solution to it from the web):
> --------------------------------------------------------------------------
>> Using a formula converted with as.formula with lme leads
>> to an error message. Same works ok with lm, and with
>> lme and a fixed formula.
>>
>> # demonstrates problems with lme and as.formula
>> demo<-data.frame(x=1:20,y=(1:20)+rnorm(20),subj=as.factor(rep(1:2,10)))
>> demo.lm1<-lme(y~x,data=demo,random=~1|subj)
>> print(summary(demo.lm1))
>> newframe<-data.frame(x=1:5,subj=rep(1,5))
>> predict(demo.lm1,newframe,level=0)
>>
>> fma<-as.formula("y~x")
>> demo.lm<-lm(fma,data=demo) # ok
>> predict(demo.lm,newframe,level=0) # ok
>> demo.lm2<-lme(fma,data=demo,random=~1|subj) # looks ok, but isn't
>> print(summary(demo.lm2))
>>
>> #Fixed effects: Error in as.vector(x, "list") : cannot coerce to vector
>> #predict(demo.lm2,newframe,level=0) # does not work
>
> Thanks for the detailed report. I can reproduce the problem. It
> appears that there needs to be an eval early in the lme function so it
> stores the formula for the fixed effects, not the name of the formula.
> ------------------------------------------------------------------------
> Now I follow the suggestion and use :
> demo.lm2<-lme(eval(fma),data=demo,random=~1|subj)
> #works ok, even with summary(demo.lm2)
> #predict(demo.lm2,newframe,level=0)
> #Error in eval() : Argument "expr" is missing, with no default
>
> How can I fix this problem and get the predict() function to work?
> Thank you!
>
> ********************************************************************
> Song Liu
> School of Statistics
> 313 FordH
> 224 Church St. SE
> Minneapolis,MN 55455
>
> ______________________________________________
> 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
>
--
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
More information about the R-help
mailing list