R中的滚动回归xts对象 [英] Rolling regression xts object in R

查看:60
本文介绍了R中的滚动回归xts对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 xts 对象执行滚动 100 天回归并返回所有日期的斜率系数的 t 统计量.我有一个 xts 对象,价格:

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. I have an xts object, prices:

> 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

还有一个函数,用于从最近 100 个价格的回归中生成 t 值,并作为与价格具有相同列名的 xts 对象返回:

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:

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

但是,此函数仅返回当前日期的 t 值.我需要一个 xts 对象,其中包含价格 xts 对象中包含的所有日期的 t 值.我正在尝试使用 rollapply 但没有得到任何地方:

However, this function only returns the t values for the current date. I need a xts object that includes the t values for all dates included in the prices xts object. I am attempting to use rollapply but not getting anywhere:

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)
}

但这会返回错误:

compute.r(价格)zoo(rval, index(x)[i]) 中的错误:x":尝试定义无效的动物园对象

compute.r(prices) Error in zoo(rval, index(x)[i]) : "x" : attempt to define invalid zoo object

我知道问题出在 l100.ra 行中,但无法修复.有什么想法吗?

I know the problem is in the l100.ra line but cannot fix it. Any thoughts?

编辑 1

提供的功能如下:

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"

它以 POSIXct 格式显示时间戳.这里有什么线索吗?我需要转换回上面价格中的格式.

It shows the timestamps in POSIXct format. Any clues here? I need to convert back to the format in prices above.

推荐答案

您的 rollapply 调用应该调用一个返回 t 值的函数.现在它返回一个 lm 对象,这不是动物园对象的 coredata 的有效值.

Your rollapply call should call a function that returns the t-values. Right now it returns a lm object, which is not a valid value for the coredata of a zoo object.

让你的函数只返回 t 值,它就会起作用.

Make your function only return the t-values and it will work.

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)

这篇关于R中的滚动回归xts对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆