[R-SIG-Finance] Error in lm prediction

Joshua Ulrich josh.m.ulrich at gmail.com
Fri Jul 28 14:09:44 CEST 2017


On Mon, Jul 24, 2017 at 1:10 PM, Ed Herranz <ed.herranz at gmail.com> wrote:
> 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
>
Bad guess. :)

library(xts)
data(sample_matrix)
xtsObject <- as.xts(sample_matrix)
xtsObject$t <- seq_len(nrow(xtsObject))-1
lm(Open ~ t, data=xtsObject)

> 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.
>>
It's very difficult to help if you do not provide a reproducible
example.  Most people do not have, and will not spend, the time it
takes to imagine and create data required to reproduce the issue you
describe.

Please see: https://stackoverflow.com/q/5963269/271616

>>
>> --
>> 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]]
>
> _______________________________________________
> 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.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2017 | www.rinfinance.com

On Mon, Jul 24, 2017 at 1:10 PM, Ed Herranz <ed.herranz at gmail.com> wrote:
> 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]]
>
> _______________________________________________
> 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.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2017 | www.rinfinance.com



More information about the R-SIG-Finance mailing list