[R] Rollapply

Brecknock, Peter Peter.Brecknock at bp.com
Thu Jan 14 20:48:29 CET 2010


For anyone who may be interested .....

Gabor Grothendieck suggested a link and then provided additional help resulting in the following. Any mistakes are mine.

The code will allow you to build a rolling regression and to pass a (different) predictor to that regression model.  

##### DATA ######
df = data.frame(x = c(70.67,70.54,69.87,69.51,70.69,72.66,72.65,73.36),
                y = c(78.01,77.07,77.35,76.72,77.49,78.70,77.78,79.58))
pred2 = c(70,71,72,73,72,70)

width = 3

#### FUNCTIONS #####
embed.data.frame <- function(df,width)
   apply(embed(1:nrow(df),width),1,function(idx)df[idx,])

f<-function(df,pred2){
  model <- lm(y ~ x, data = df) 
  v <- coefficients(model) 
  p <- predict(model, data.frame(x=pred2),se=TRUE)
data.frame(intcpt=v[1],slope=v[2],modfit=p$fit,modse=p$se.fit,row.names=NULL) 
 }

##### MAPPLY ####
mapply(f, embed.data.frame(df,width), pred2)


Thanks Gabor.

-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] 
Sent: Wednesday, January 13, 2010 4:02 PM
To: Brecknock, Peter
Cc: r-help at r-project.org
Subject: Re: [R] Rollapply

See:

http://tolstoy.newcastle.edu.au/R/help/04/03/1446.html

On Wed, Jan 13, 2010 at 3:45 PM, Pete B <Peter.Brecknock at bp.com> wrote:
>
> Hi
>
> I would like to understand how to extend the function (FUN) I am using in
> rollapply below.
>
> ######################################
> With the following simplified data, test1 yields parameters for a rolling
> regression
>
> data = data.frame(Xvar=c(70.67,70.54,69.87,69.51,70.69,72.66,72.65,73.36),
>               Yvar =c(78.01,77.07,77.35,76.72,77.49,78.70,77.78,79.58))
> data.z = zoo(d)
>
> test1 = rollapply(data.z, width=3,
>          FUN = function(z) coef(lm(z[,1]~z[,2],
>          data=as.data.frame(z))), by.column = FALSE, align = "right")
>
> print(test1)
>
> ######################################
>
> Rewriting this to call myfn1 gives test2 (and is consistent with test1
> above)
>
> myfn1 = function(mydata){
>      dd = as.data.frame(mydata)
>      l = lm(dd[,1]~dd[,2], data=dd)
>      c = coef(l)
>    }
>
> test2 = rollapply(data.z, width=3,
>     FUN= myfn1, by.column = FALSE, align = "right")
>
> print(test2)
>
> ######################################
>
> I would like to be able to use the predict function to obtain a prediction
> (and its std error) from the rolling regression I have just calculated.
>
> My effort below issues a warning that 'newdata' had 1 row but variable(s)
> found have 3 rows.
> (if I run this outside of rollapply I don't get this warning)
>
> Also, I don't see the predicted value or its se with print(fm2[[1]]). Again,
> if I run this outside of rollapply I am able to extract the predicted value.
>
>
> Xpred=c(70.67)
>
> myfn2 = function(mydata){
>      dd = as.data.frame(mydata)
>      l = lm(dd[,1]~dd[,2], data=dd)
>      c = coef(l)
>      p = predict(l, data.frame(Xvar=Xpred),se=T)
>      ret=c(l,c,p)
>    }
>
> fm2 = rollapply(data.z, width=3,
>     FUN= myfn2, by.column = FALSE, align = "right")
>
> print(fm2[[1]])
>
>
> Any insights would be gratefully received.
>
> Best regards
>
> Pete
> --
> View this message in context: http://n4.nabble.com/Rollapply-tp1013345p1013345.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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