修复滚动线性回归的起点

时间:2017-12-03 13:46:05

标签: r linear-regression

采取两个系列

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

使用上面的日期,让我们为我们的例子做准备:

# 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")

一切都很好,它的短样本,所以我们只运行2天的回归宽度。所以我想做的是修复回归的起点,然后让它在整个样本上运行。这与选择滑动窗口形成对比,即宽度为2将运行1:2 ..然后2:3,3:4等...其中固定窗口以1:2,1:3,1:4等运行......

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

使用read.zoo阅读数据以创建两个动物园系列,将它们组合在一起创建both并使用rollapplyr的宽度向量在1:nrow(both)上运行linreg运行宽度为1的第一个回归,第二个宽度为2的回归,等等。Coefs定义为使用截距返回系数对第二列的第一列进行回归。 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]) 是系数的n乘2动物园系列。

rollapplyr

z也适用于普通矩阵和数据帧。请注意,如果coredata(z)是动物园对象,则time(z)fortify.zoo(z)分别是其数据(作为普通向量或矩阵)和索引。 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" 是其数据框表示。

注意:使用的输入是:

Type: Exception

Message: Session: Configured save path 'C:\Windows\TEMP' is not writable by the PHP process.