[R] FW: predict.lm() does not take ts objects in formula
Duncan Mackay
dulcalma at bigpond.com
Mon Feb 24 05:25:40 CET 2014
I forgot to send this to the list
It may make things easier
Duncan
-----Original Message-----
From: Duncan Mackay [mailto:dulcalma at bigpond.com]
Sent: Monday, 24 February 2014 10:01
To: 'C W'
Subject: RE: [R] predict.lm() does not take ts objects in formula
Hi
You have not told us what newdata is
To sort things out try
library(zoo)
> x <- as.yearqtr(1900 + seq(0, 19)/4)
> dat = data.frame(t = t, q = q,dat)
> z = zoo(dat,x)
> z
t q t.1 q.1 dat
1900 Q1 1 1 1 1 -0.78964685
1900 Q2 2 2 2 2 0.48781464
1900 Q3 3 3 3 3 2.16803254
1900 Q4 4 4 4 4 0.50069461
1901 Q1 5 1 5 1 0.62021020
1901 Q2 6 2 6 2 -0.96590321
1901 Q3 7 3 7 3 0.16265471
1901 Q4 8 4 8 4 -2.07823754
1902 Q1 9 1 9 1 0.48522682
1902 Q2 10 2 10 2 0.69676878
1902 Q3 11 3 11 3 0.18551392
1902 Q4 12 4 12 4 0.70073352
1903 Q1 13 1 13 1 0.31168103
1903 Q2 14 2 14 2 0.76046236
1903 Q3 15 3 15 3 1.84246363
1903 Q4 16 4 16 4 1.11236284
1904 Q1 17 1 17 1 0.03266396
1904 Q2 18 2 18 2 -1.11444896
1904 Q3 19 3 19 3 0.41805782
1904 Q4 20 4 20 4 -0.40023524
> predict(lm(dat ~ t+q, z))
1900 Q1 1900 Q2 1900 Q3 1900 Q4 1901 Q1 1901 Q2
1901 Q3
-0.78964685 0.48781464 2.16803254 0.50069461 0.62021020 -0.96590321
0.16265471
1901 Q4 1902 Q1 1902 Q2 1902 Q3 1902 Q4 1903 Q1
1903 Q2
-2.07823754 0.48522682 0.69676878 0.18551392 0.70073352 0.31168103
0.76046236
1903 Q3 1903 Q4 1904 Q1 1904 Q2 1904 Q3 1904 Q4
1.84246363 1.11236284 0.03266396 -1.11444896 0.41805782 -0.40023524
if you have to convert
> as.ts(z)
t q t.1 q.1 dat
1900 Q1 1 1 1 1 -0.78964685
1900 Q2 2 2 2 2 0.48781464
1900 Q3 3 3 3 3 2.16803254
1900 Q4 4 4 4 4 0.50069461
1901 Q1 5 1 5 1 0.62021020
1901 Q2 6 2 6 2 -0.96590321
1901 Q3 7 3 7 3 0.16265471
1901 Q4 8 4 8 4 -2.07823754
1902 Q1 9 1 9 1 0.48522682
1902 Q2 10 2 10 2 0.69676878
1902 Q3 11 3 11 3 0.18551392
1902 Q4 12 4 12 4 0.70073352
1903 Q1 13 1 13 1 0.31168103
1903 Q2 14 2 14 2 0.76046236
1903 Q3 15 3 15 3 1.84246363
1903 Q4 16 4 16 4 1.11236284
1904 Q1 17 1 17 1 0.03266396
1904 Q2 18 2 18 2 -1.11444896
1904 Q3 19 3 19 3 0.41805782
1904 Q4 20 4 20 4 -0.40023524
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of C W
Sent: Monday, 24 February 2014 09:34
To: r-help
Subject: [R] predict.lm() does not take ts objects in formula
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!
Mike
[[alternative HTML version deleted]]
______________________________________________
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