简体   繁体   中英

Rolling time-series regressions by group

I am working on the following data in R:

# Getting stock data
library(quantmod)
tickers <- c("ASC.OL",  "AFG.OL",   "AKA.OL",   "AKER.OL",  "AKERBP.OL",    "AKSO.OL",  "AKVA.OL",  "AMSC.OL",  "APP.OL",   "AQUA.OL",  "ARCHER.OL",    "ARCUS.OL", "AFK.OL",   "ASETEK.OL",    "ATEA.OL",  "ATLA-NOK.OL",  "AURG.OL",  "AUSS.OL",  "AVANCE.OL",    "AVM.OL",   "AXA.OL",   "B2H.OL",   "BAKKA.OL", "BEL.OL",   "BERGEN.OL",    "BGBIO.OL", "BIOTEC.OL",    "BON.OL",   "BOR.OL",   "BRG.OL",   "BOUVET.OL",    "BWLPG.OL", "BWO.OL",   "BMA.OL",   "COV.OL",   "CXENSE.OL",    "DAT.OL",   "DESSC.OL", "DNB.OL",   "DNO.OL",   "DOF.OL",   "EIOF.OL",  "EKO.OL",   "EMGS.OL",  "EMAS.OL",  "ENTRA.OL", "EPR.OL",   "FAR.OL",   "FOE.OL",   "FRO.OL",   "FUNCOM.OL",    "GIG.OL",   "RISH.OL",  "GJF.OL",   "GOGL.OL",  "GOD.OL",   "GSF.OL",   "GYL.OL",   "HNA.OL",   "HNB.OL",   "HAVI.OL",  "HYARD.OL", "HELG.OL",  "HEX.OL",   "HIDDN.OL", "HLNG.OL",  "HSPG.OL",  "IMSK.OL",  "IDEX.OL",  "INC.OL",   "ISSG.OL",  "INSR.OL",  "IOX.OL",   "ITX.OL",   "ITE.OL",   "JIN.OL",   "JAEREN.OL",    "KID.OL",   "KIT.OL",   "KOA.OL",   "KOG.OL",   "KVAER.OL", "LSG.OL",   "LINK.OL",  "MHG.OL",   "MEDI.OL",  "MELG.OL",  "MULTI.OL", "NAPA.OL",  "NAVA.OL",  "NEL.OL",   "NEXT.OL",  "NGT.OL",   "NANO.OL",  "NOD.OL",   "NHY.OL",   "NSG.OL",   "NRS.OL",   "NAS.OL",   "NOR.OL",   "NOFI.OL",  "NPRO.OL",  "NRC.OL",   "NTS.OL",   "OCY.OL",   "OTS.OL",   "ODL.OL",   "ODF.OL",   "ODFB.OL",  "OLT.OL",   "OPERA.OL", "ORK.OL",   "PEN.OL",   "PARB.OL",  "PGS.OL",   "PDR.OL",   "PHO.OL",   "PLCS.OL",  "POL.OL",   "PRS.OL",   "PROTCT.OL",    "QFR.OL",   "QEC.OL",   "RAKP.OL",  "REACH.OL", "REC.OL",   "RENO.OL",  "SDSD.OL",  "SAFE.OL",  "SALM.OL",  "SADG.OL",  "SAS-NOK.OL",   "SSO.OL",   "SCHA.OL",  "SCHB.OL",  "SBX.OL",   "SDRL.OL",  "SBO.OL",   "SEVDR.OL", "SEVAN.OL", "SIOFF.OL", "SKBN.OL",  "SKI.OL",   "SKUE.OL",  "SOLON.OL", "SOFF.OL",  "SOFFB.OL", "SOLV.OL",  "SONG.OL",  "SBVG.OL",  "NONG.OL",  "RING.OL",  "MING.OL",  "SRBANK.OL",    "SOAG.OL",  "MORG.OL",  "SOR.OL",   "SVEG.OL",  "SPOG.OL",  "SPU.OL",   "STL.OL",   "SNI.OL",   "STB.OL",   "STORM.OL", "STRONG.OL",    "SUBC.OL",  "TIL.OL",   "TRVX.OL",  "TEAM.OL",  "TECH.OL",  "TEL.OL",   "TGS.OL",   "SSC.OL",   "THIN.OL",  "TOM.OL",   "TOTG.OL",  "TRE.OL",   "TTS.OL",   "VEI.OL",   "VVL.OL",   "WWL.OL",   "WEIFA.OL", "WRL.OL",   "WWI.OL",   "WWIB.OL",  "WILS.OL",  "XXL.OL",   "YAR.OL",   "ZAL.OL")
dataEnv <- new.env()
out <- sapply(tickers, function(s) tryCatch({ getSymbols(s , env = dataEnv) }, error = function(e) NA))
plist <- eapply(dataEnv, Ad)
pframe <- do.call(merge, plist)
names(pframe) <- gsub(".Adjusted", "", names(pframe))
names(pframe) <- gsub(".X", "", names(pframe))
pframe <- log(pframe)
pframe <- diff(pframe, lag = 1)
pframe<-t(pframe)
library(reshape2)
pframe<- melt(pframe)
library(data.table)
setnames(pframe, old=c("Var1","Var2", "value"), new=c("Ticker", "Date", "Returns"))

# Getting index data
index <- c("^GSPC")
dataEnv1 <- new.env()
out1 <- sapply(index, function(s) tryCatch({ getSymbols(s , env = dataEnv1) }, error = function(e) NA))
index <- eapply(dataEnv1, Ad)
index <- do.call(merge, index)
names(index) <- gsub(".Adjusted", "", names(index))
names(index) <- gsub(".X", "", names(index))
index <- log(index)
index <- diff(index, lag = 1)
index<-t(index)
index<- melt(index)
library(data.table)
setnames(index, old=c("Var1","Var2", "value"), new=c("Index", "Date", "Index_Returns"))

# Getting curr data
curr <- c("NOK=X")
dataEnv1 <- new.env()
out1 <- sapply(curr, function(s) tryCatch({ getSymbols(s , env = dataEnv1) }, error = function(e) NA))
curr <- eapply(dataEnv1, Ad)
curr <- do.call(merge, curr)
names(curr) <- gsub(".Adjusted", "", names(curr))
names(curr) <- gsub(".X", "", names(curr))
curr <- log(curr)
curr <- diff(curr, lag = 1)
curr<-t(curr)
curr<- melt(curr)
library(data.table)
setnames(curr, old=c("Var1","Var2", "value"), new=c("NOK", "Date", "NOK_Returns"))

NOK_index <- merge(index, curr, by="Date")
NOK_index$Index <- NULL
NOK_index$NOK <- NULL

Final <- merge(pframe, NOK_index, by="Date")
Final <- na.omit(Final)

I am trying to run rolling time-series regressions by group, in the dataset called "Final". The group-id is the variable called "Ticker". The window I want is to be based on at least 120 observations at most 240. I want to regress "Return" on "Market_ret". Currently working on the regression code below, trying to make the regressions rolling. Any suggestion?

library(dplyr)
library(magrittr)   
library(broom)
## Fit models
fitted_model <- Final %>% group_by(Ticker) %>% do(tidy(lm(Returns ~ Index_Returns + NOK_Returns, data = .)))

I tried the code below, but it's taking to much time (got the following message: 1% ~5 h remaining, cancelled it before it completed).

library(dplyr)
library(magrittr)   
library(broom)
## Fit models
fitted_model <- Final %>% group_by(Ticker) %>% do(tidy(rollapply(. , width=262, FUN = function(Z) { t = lm(formula=Returns ~ Index_Returns + NOK_Returns, data = as.data.frame(Z), na.rm=T); return(t$coef) }, by.column=FALSE, align="right")))

If you want to run OLS regressions over groups, you can try to use the dplyr package. Using the do() function will run your models and store them in a variable called model.

library(dplyr)
library(magrittr)    
## Fit models
fitted_model <- Final %>%
    group_by(Ticker) %>%
    do(model = lm(Returns ~ Market_ret, data = .))

To extract the coefficients check into the broom package in R. Running tidy(model) should do the trick.

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