[英]Rolling regression xts object in R
I am attempting to perform a rolling 100 day regression on an xts object and return the t statistic of the slope coefficient for all dates. 我正在尝试对xts对象执行滚动100天回归并返回所有日期的斜率系数的t统计量。 I have an xts object, prices:
我有一个xts对象,价格:
> tail(prices)
DBC EEM EFA GLD HYG IEF IWM IYR MDY TLT
2012-11-02 27.14 41.60 53.69 162.60 92.41 107.62 81.19 64.50 179.99 122.26
2012-11-05 27.37 41.80 53.56 163.23 92.26 107.88 81.73 64.02 181.10 122.95
2012-11-06 27.86 42.13 54.07 166.30 92.40 107.39 82.34 64.16 182.69 121.79
2012-11-07 27.34 41.44 53.26 166.49 91.85 108.29 80.34 63.84 178.90 124.00
2012-11-08 27.38 40.92 52.78 167.99 91.55 108.77 79.21 63.19 176.37 125.84
2012-11-09 27.60 41.00 52.80 167.82 91.39 108.78 79.38 62.98 176.80 125.98
And a function to generate the t value from a regression of the last 100 prices and return as xts object with the same column names as prices: 还有一个根据最近100个价格的回归生成t值并以与价格相同的列名称作为xts对象返回的函数:
compute.regression <- function(x)
{
last100 = last(x,100)
fit <- lm (last100~time(last100))
tval <- unlist(lapply( coef(summary(fit)), "[" ,"(Intercept)" ,"t value"))
tval.mat <-as.matrix(tval)
tval.mat2 <- matrix(c(tval.mat), nrow=1, ncol=10)
new2 <- xts(tval.mat2, Sys.Date())
colnames(new2) <- tickers
return(new2)
}
> compute.regression(prices)
IWM EFA HYG EEM IYR IEF TLT DBC GLD MDY
2012-11-10 -7.642781 -14.33474 -16.28911 -14.52982 -15.85337 5.732489 -8.026495 -1.960392 -11.82474 8.686045
However, this function only returns the t values for the current date. 但是,此函数仅返回当前日期的t值。 I need a xts object that includes the t values for all dates included in the prices xts object.
我需要一个xts对象,其中包括价格xts对象中包含的所有日期的t值。 I am attempting to use
rollapply
but not getting anywhere: 我正在尝试使用
rollapply
但没有到达任何地方:
fit.r <- function (x)
{
hard <- lm (x~time(x))
return(hard)
}
compute.r <- function(x)
{
l100.ra <- rollapply(x, 100, fit.r, fill=NA, align='right')
tval <- unlist(lapply( coef(summary(l100.ra)), "[" ,"(Intercept)" ,"t value"))
tval.mat <-as.matrix(tval)
tval.mat2 <- matrix(c(tval.mat), nrow=1, ncol=10)
new2 <- xts(tval.mat2, Sys.Date())
colnames(new2) <- tickers
return(new2)
}
But this returns the error: 但这返回错误:
compute.r(prices) Error in zoo(rval, index(x)[i]) : “x” : attempt to define invalid zoo object
zoo(rval,index(x)[i])中的compute.r(prices)错误:“ x”:尝试定义无效的Zoo对象
I know the problem is in the l100.ra line but cannot fix it. 我知道问题出在l100.ra行中,但无法解决。 Any thoughts?
有什么想法吗?
EDIT 1 编辑1
The function provided below: 下面提供的功能:
x <- prices
f <- function (x) {
res <- coef(summary(lm(x ~ time(x)))
sapply(res, "[" ,"time(x)" ,"t value")
}
r <- rollapplyr(x, 100, f, by.column=FALSE)
works well but changes the date/time format to that below: 效果很好,但是将日期/时间格式更改为以下格式:
> last(r,5)
Response MDY Response TLT
1352160000 15.38380 -7.913764
1352246400 14.81888 -7.957261
1352332800 13.96203 -7.666699
1352419200 13.28624 -7.299532
1352678400 12.75266 -7.100558
I've been reading up on timestamps but don't understand why the conversion took place. 我一直在阅读时间戳,但不明白为什么会发生转换。 Also, when I try:
另外,当我尝试:
last(index(r),5)
[1] "2012-11-06 GMT" "2012-11-07 GMT" "2012-11-08 GMT" "2012-11-09 GMT" "2012-11-12 GMT"
It shows the timestamps in POSIXct
format. 它以
POSIXct
格式显示时间戳。 Any clues here? 这里有什么线索吗? I need to convert back to the format in prices above.
我需要转换回上述价格格式。
Your rollapply
call should call a function that returns the t-values. 您的
rollapply
调用应调用一个返回t值的函数。 Right now it returns a lm
object, which is not a valid value for the coredata
of a zoo object. 现在,它返回一个
lm
对象,这对于Zoo对象的coredata
来说不是有效值。
Make your function only return the t-values and it will work. 使您的函数仅返回t值,它将起作用。
library(quantmod)
getSymbols("SPY;IEF")
x <- merge(Cl(SPY),Cl(IEF))
f <- function (x) {
res <- coef(summary(lm(x ~ time(x))))
sapply(res, "[" ,"(Intercept)" ,"t value")
}
r <- rollapplyr(x, 100, f, by.column=FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.