Reputation: 1305
take two series
xiv <- read.table("D:/R Projects/Final Scripts/VIX_term_structure/xiv.txt", header=FALSE, stringsAsFactors=FALSE)
2010-12-02 6.559722e-02
2010-12-03 5.868252e-02
2010-12-06 1.911059e-02
2010-12-07 9.420547e-03
2010-12-08 1.734460e-02
2010-12-09 2.258762e-02
2010-12-10 8.547732e-03
2010-12-13 -1.418142e-02
2010-12-14 -6.724549e-03
2010-12-15 -2.176708e-02
2010-12-16 1.340342e-02
2010-12-17 2.195712e-02
2010-12-20 2.646760e-02
2010-12-21 1.640722e-02
2010-12-22 2.594454e-03
2010-12-23 -3.210416e-02
2010-12-27 -2.665218e-02
spy <- read.table("D:/R Projects/Final Scripts/VIX_term_structure/spy.txt", header=FALSE, stringsAsFactors =FALSE)
2010-12-02 1.280823e-02
2010-12-03 2.692895e-03
2010-12-06 -1.058301e-03
2010-12-07 5.706029e-04
2010-12-08 3.663117e-03
2010-12-09 3.894063e-03
2010-12-10 5.817504e-03
2010-12-13 6.424447e-04
2010-12-14 8.838366e-04
2010-12-15 -4.588375e-03
2010-12-16 5.817468e-03
2010-12-17 1.064127e-03
2010-12-20 2.413213e-03
2010-12-21 6.340508e-03
2010-12-22 3.109997e-03
2010-12-23 -1.431073e-03
2010-12-27 3.984963e-04
Using the date above, lets prepare it for our example:
# Prepare data for reproducible example
spy <- read.table("D:/R Projects/Final Scripts/VIX_term_structure/spy.txt", header=FALSE, stringsAsFactors =FALSE)
xiv <- read.table("D:/R Projects/Final Scripts/VIX_term_structure/xiv.txt", header=FALSE, stringsAsFactors=FALSE)
colnames(spy)[1] <- "Date"
colnames(spy)[2] <- "SPY"
colnames(xiv)[1] <- "Date"
colnames(xiv)[2] <- "XIV"
library(lubridate)
spy$Date <- ymd(spy$Date) # convert to date format
xiv$Date <- ymd(xiv$Date)
df <- merge(spy,xiv, by='Date') # Merge two series to one data frame
# Package roll for rolling linear regression
library(roll)
runs <- roll::roll_lm(x=as.matrix(df$SPY), y=as.matrix(df$XIV),width = 2, intercept = TRUE)
head(runs)
beta.spy.independant <- as.data.frame(runs$coefficients[, "x1"])
colnames(beta.spy.independant)[1] = "beta"
plot(beta.spy.independant$beta,type="l")
All is fine and well, its short sample so we only run a regression width of 2 days. So what I wish to do is fix the start point of the regression and then allow it to run over the full sample. This is in contrast to picking a sliding window ie a width of 2 would run 1:2.. then 2:3, 3:4 etc... where a fixed window runs 1:2, 1:3, 1:4 etc...
How can I achieve this?
Upvotes: 2
Views: 294
Reputation: 269431
Read the data in using read.zoo
to create two zoo series, cbind them together to create both
and run rollapplyr
on that using a width vector of 1:nrow(both)
which says to run the first regression with a width of 1, the second with a width of 2, etc. linreg
is defined to regress the first column on the second column with an intercept returning the coefficients. Coefs
is an n by 2 zoo series of the coefficients.
library(zoo)
# ser1 <- read.zoo("myfile")
ser1 <- read.zoo(text = Lines1)
ser2 <- read.zoo(text = Lines2)
both <- cbind(ser1, ser2)
n <- nrow(both)
linreg <- function(m) if (is.null(dim(m))) NA else coef(lm(as.data.frame(m)))
Coefs <- rollapplyr(both, 1:n, linreg, by.column = FALSE)
plot(Coefs[, 2])
rollapplyr
also works on plain matrices and on data frames. Note that if z
is a zoo object then coredata(z)
and time(z)
are its data (as a plain vector or matrix) and index, respectively. fortify.zoo(z)
is its data frame representation.
Note: Input used is:
Lines1 <- "
2010-12-02 6.559722e-02
2010-12-03 5.868252e-02
2010-12-06 1.911059e-02
2010-12-07 9.420547e-03
2010-12-08 1.734460e-02
2010-12-09 2.258762e-02
2010-12-10 8.547732e-03
2010-12-13 -1.418142e-02
2010-12-14 -6.724549e-03
2010-12-15 -2.176708e-02
2010-12-16 1.340342e-02
2010-12-17 2.195712e-02
2010-12-20 2.646760e-02
2010-12-21 1.640722e-02
2010-12-22 2.594454e-03
2010-12-23 -3.210416e-02
2010-12-27 -2.665218e-02"
Lines2 <- "
2010-12-02 1.280823e-02
2010-12-03 2.692895e-03
2010-12-06 -1.058301e-03
2010-12-07 5.706029e-04
2010-12-08 3.663117e-03
2010-12-09 3.894063e-03
2010-12-10 5.817504e-03
2010-12-13 6.424447e-04
2010-12-14 8.838366e-04
2010-12-15 -4.588375e-03
2010-12-16 5.817468e-03
2010-12-17 1.064127e-03
2010-12-20 2.413213e-03
2010-12-21 6.340508e-03
2010-12-22 3.109997e-03
2010-12-23 -1.431073e-03
2010-12-27 3.984963e-04"
Upvotes: 2