[R-SIG-Finance] Error in lm prediction
Ed Herranz
ed.herranz at gmail.com
Mon Jul 24 20:10:49 CEST 2017
Hi Amol,
My guess is that you can't use lm() directly on xts objects. See this:
https://stackoverflow.com/questions/21692560/linear-regression-with-xts-object
Regards,
-Ed
On Sun, Jul 16, 2017 at 4:31 PM, amol gupta <amolgupta87 at gmail.com> wrote:
> Hi
>
> I am most likely committing an error in trying to predict using linear
> regression lm model. please help me figure out what am I doing wrong. I am
> trying to regress a index and its constituents. Here is the code
>
>
> #split ts inttwo parts
> a<-300;
>
> x1<-x[1:a,];
> y1<-y[1:a,];
>
> x2<-x[(a+1):nrow(x),];
> y2<-y[(a+1):nrow(y),];
>
>
> #regression
> m1<-lm( y1~x1)
> r1<-residuals(m1)
> coef(m1)
>
> ##out of sample
> y_hat<-predict.lm(m1,x2);
> r2<-y_hat-y2;
>
>
> x,y are xts. X contains multiple time series. The y_ hat turns out to be of
> 300 samples only, whereas x2 contains 1400 samples.
>
> Please help me figure out how to predict using model that I have found
> using regression.
>
>
> --
> Regards
> Amol
> +91-9897860992
> +91-8889676918
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list