[R] Fwd: Simple problem with lm/predict

Michael Bedward michael.bedward at gmail.com
Sun Aug 15 13:18:43 CEST 2010


Hi Nick,

Normally the data you provide to predict has the same var names that
you used in your regression formula
e.g. predict( extrapolate1, newdata=list(t3=something, t4=something.else) )

Or you could have a two col matrix containing t3 and t4 values and
provide that...
my.matrix <- matrix( c(newt3, newt4), ncol=2)
colnames(my.matrix) <- c("t3", "t4")
predict( extrapolate1, newdata=my.matrix)

Does that help ?
Michael

On 15 August 2010 05:31, Nick Torenvliet <nick.torenvliet at gmail.com> wrote:
> Hi all,
>
> I've got an xts time series with monthly OHLC Dow Jones industrial index
> data from 1980 to present, the data is in stored in x.
>
> I've done an OLS fit on the data in 1982::1994 and stored it in extrapolate1
> (x[,4] contains the closing value for the index).
>
>> t3 <- seq(1980,1994,length = length(x["1980::1994",4]))
>> t4<-t3^2
>> extrapolate1 <- lines(lm(x["1980::1994",4]~t3+t4)$fit)
>> extrapolate1
>
> Call:
> lm(formula = x["1980::1994", 4] ~ t3 + t4)
>
> Coefficients:
> (Intercept)           t3           t4
>  3.161e+07   -3.205e+04    8.125e+00
>
> The plot comes up with the appropriate line fit for 1980::1994, so I can
> safely say the model/fit is correct.
>
> I want to plot the predicted response of extrapolate into 1995 and beyond
> and after much googling have got about this close --
>
>> new.t <- seq(1995,len=2*12,by = 1/12)
>> new.dat <- data.frame(Time = new.t, Seas = rep(1:12,2))
>> predict(extrapolate1,new.dat)[1:24]
>       1        2        3        4        5        6        7        8
> 704.9639 714.5726 724.2807 734.0882 743.9951 754.0014 764.1071 774.3122
>       9       10       11       12       13       14       15       16
> 784.6168 795.0207 805.5240 816.1267 826.8288 837.6303 848.5312 859.5315
>      17       18       19       20       21       22       23       24
> 870.6313 881.8304 893.1289 904.5268 916.0241 927.6209 939.3170 951.1125
> Warning message:
> 'newdata' had 24 rows but variable(s) found have 180 rows
>
> This is straight from the net, the trouble is with how new.dat is set up but
> I can't figure it out...
>
> Regards,
>
> Nick
>
>        [[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