[R] predict.lm() does not take ts objects in formula
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Feb 24 00:49:30 CET 2014
On Sun, Feb 23, 2014 at 6:34 PM, C W <tmrsg11 at gmail.com> wrote:
> Hello,
> I don't know how to use predict.lm() for ts object.
>
> Here's the time series regression.
> y = t + Q1 + Q2 + Q3 + Q4
>
> Here's my R code,
>
>> dat <- rnorm(20)
>> tsdat <- ts(dat, start=c(1900, 1), freq=4)
>> q <- as.factor(rep(1:4, 5))
>> t <- 1:20
>> lm(tsdat~t+q)
>
> Call:
> lm(formula = tsdat ~ t + q)
>
> Coefficients:
> (Intercept) t q2 q3 q4
> -0.167030 -0.009484 0.507132 0.113818 -0.734521
>
>> predict(model, newdata=newdata)
> 1 2 3 4 5 6
> -0.17651394 0.32113359 -0.08166464 -0.93948758 -0.21445060 0.28319692
> 7 8 9 10 11 12
> -0.11960131 -0.97742425 -0.25238727 0.24526025 -0.15753798 -1.01536092
> 13 14 15 16 17 18
> -0.29032394 0.20732358 -0.19547465 -1.05329759 -0.32826061 0.16938691
> 19 20
> -0.23341132 -1.09123426
> Warning message:
> 'newdata' had 4 rows but variables found have 20 rows
>
>
> I am aware predict.lm() requires all data to be in data frame format.
> Could someone tell me what is wrong? Thanks!
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Try this:
predict(model)
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list