[R] problem with lm and predict - no predictions made
Petr PIKAL
petr.pikal at precheza.cz
Thu Jul 3 14:38:44 CEST 2008
Hi
r-help-bounces at r-project.org napsal dne 03.07.2008 09:41:17:
> On Wed, Jul 02, 2008 at 08:55:54PM -0500, Erik Iverson wrote:
> > Hello -
> >
> > Keld Jørn Simonsen wrote:
> > >Hi
> > >
> > >I have a problem with lm and predict
> > >
> > >I have
> > >
> > >us
> > > [1] 2789.53 3128.43 3255.03 3536.68 3933.18 4220.25 4462.83
4739.48
> > > [9] 5103.75 5484.35 5803.08 5995.93 6337.75 6657.40 7072.23
7397.65
> > >[17] 7816.83 8304.33 8746.98 9268.43 9816.98 10127.95 10469.60
> > >10960.75
> > >[25] 11685.93 12433.93 13194.70 13843.83
> > >
> > >
> > > us.p
> > > [1] 227.62 229.92 232.13 234.25 236.31 238.42 240.59 242.75 244.97
247.29
> > >[11] 250.05 253.39 256.78 260.15 263.33 266.46 269.58 272.82 276.02
279.20
> > >[21] 282.31 285.25 288.10 290.85 293.53 296.26 299.08 301.97
> > >
> > > us.l = lm(log(us) ~ log(us.p))
> > >>predict(us.l,n.ahead=5)
There is no n.ahead parameter in predict.lm function. You has to tell
predict at what values do you want a prediction at some prespecified
values.
predict(us.l, newdata=data.frame(us.p=seq(200, 320,10)))
However lm is probably not the best way for analysing time series. Maybe
you shall look to arima or other ts modeling tools.
Regards
Petr
> > > 1 2 3 4 5 6 7 8
> > >8.079754 8.131908 8.181531 8.228692 8.274111 8.320224 8.367224
8.413588
> > > 9 10 11 12 13 14 15 16
> > >8.460813 8.509709 8.567285 8.636117 8.705057 8.772694 8.835719
8.897015
> > > 17 18 19 20 21 22 23 24
> > >8.957402 9.019376 9.079867 9.139289 9.196752 9.250495 9.302067
9.351347
> > > 25 26 27 28
> > >9.398927 9.446950 9.496094 9.545979
> > >
> > >
> > >Why does predict not give me any predictions? The result of predict()
is
> > >same lenght (28) as the us and us.p variables.
> >
> > The version of 'predict' being called on 'us.l' (i.e., predict.lm) is
> > doing predictions, and it should be giving you a result of identical
> > length as your original vectors. What are you expecting here? Your
> > usage of the 'n.ahead' parameter suggests to me you might be wanting
to
> > fit your model using a different function than 'lm', and use its
> > corresponding prediction function.
>
> Yes, what I have is actually some time series since 1980 on various
> countries - here USA production and population. I would like to estimate
> a model and then extrapolate for future years. Maybe predict.lm() is not
> the right function for that. What would be an adequate function then?
>
> best regards
> keld
>
> ______________________________________________
> 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