简体   繁体   中英

36 Months rolling regressions, with at least 24 observations

For instance, say data starts from January 1999, then running the first regression on 36 months window from January 1999 to December 2001, requires that the output should come only if minimum 24 months data is available. Otherwise that stock should be skipped for that particular regression. Next rolling regression will start from February 1999 and end at January 2002 (taking care of the minimum number of observations) and so on till March 2020.Since, it's a monthly rolling regression, the regression output would be reported on monthly basis starting from December 2001 to March 2020. The required regression equation that is needed to be run:

E(ri)= α +β1(rmt) + β2(rmt-1) + ut

E(ri) = Expected excess returns on securities α = intercept, β1=Coefficient of excess market returns, β2= Coefficient of lagged excess market returns, rm t = excess returns on Markets at time t, rm t-1 = excess returns on Market at time t-1, ut = error term

The below code was run but it doesnt look at visiting multiple stocks and doesnt check the condition

Using this on R studio

library(ggplot2)
library(tseries)

spy <- get.hist.quote(instrument="SPY", start="2003-01-01",
                      end=Sys.Date(), quote="AdjClose",
                      provider="yahoo", origin="1970-01-01",
                      compression="d", retclass="zoo")
ief <- get.hist.quote(instrument="IEF", start="2003-01-01",
                      end=Sys.Date(), quote="AdjClose",
                      provider="yahoo", origin="1970-01-01",
                      compression="d", retclass="zoo")
z <- merge.zoo(spy,ief)

rollingbeta <- rollapply(z.logrtn,
                         width=262,
                         FUN = function(Z)
                         {
                           t = lm(formula=SPY~IEF, data = as.data.frame(Z), na.rm=T);
                           return(t$coef)
                         },
                         by.column=FALSE, align="right")

在此处输入图像描述

I recommend you runner package. There is an example of rolling regression depending on date in the vignette. Your example can be solved in following way:

# work with df
df <- as.data.frame(z)
df$date <- as.Date(rownames(df))

# calculate rolling coefficients 
library(runner)
df$intercept <- runner(
  df,
  idx = "date",
  k = "36 months",
  f = function(x) {
    if (nrow(x) < 24) return(NA)
    t <- lm(Adjusted.spy ~ Adjusted.ief, x)
    t$coef[1]
  }  
)


df$slope <- runner(
  df,
  idx = "date",
  k = "36 months",
  f = function(x) {
    if (nrow(x) < 24) return(NA)
    t <- lm(Adjusted.spy ~ Adjusted.ief, x)
    t$coef[2]
  }  
)


plot(y = df$slope, x = df$date)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM