[R] Help on R Coding

Yihui Xie xieyihui at gmail.com
Sun Oct 5 08:54:22 CEST 2008


You used variable names 'Date' and 'Cult' in lm2, but *different*
names 'Cultc52' and 'Dated16' for prediction.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China



On Sun, Oct 5, 2008 at 11:33 AM, andyer weng <nzandyer at gmail.com> wrote:
> Hi all,
>
> I am kind of stuck of  using Predict function in R to make prediction
> for a model with continuous variable and categorial variables. i have
> no problem making the model, the model is e.g.
>
> cabbage.lm2<- lm(VitC ~ HeadWt + Date + Cult)
>
> HeadWt is a continuous variable, Date and Culte are factors. Date have
> three levels inside (d16,d20,d21), Cult has two levels(c39,c52). I
> need to calculate a confidence interval for the mean VitC for each
> combination of Date and Cult, fixing the value of HeadWt at the mean
> for the corresponding cell. I have already proved that Cult and Date
> are not interacted. the mean of HeadWt is also found. e.g.2.59
>
> when i type
>
>> new.df<-data.frame(HeadWt=2.59,Cultc52=1,Dated16=1)
>> predict(cabbage.lm2,new.df, interval="confidence")
>
> it has error comes up like this:
> Error in model.frame.default(Terms, newdata, na.action = na.action,
> xlev = object$xlevels) :
>  variable lengths differ (found for 'Cult')
> In addition: Warning message:
> 'newdata' had 1 rows but variable(s) found have 60 rows
>
>
> Is there anything I have done wrong?? Please help with the coding.
>
> Thank you so much!!!
>
> All the best.
>
> Andyer.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list