运行 时间序列每个月的回归 - apply.monthly 不起作用

Run regression for each month of a timeseries - apply.monthly does not work

我想对一年时间序列的每个月应用分位数回归(具有一个因变量和一个自变量),这样我将收到 12 个系数。 我的数据集由 return_2000_xts 给出,rq() 是分位数回归的函数。 我的数据集采用 xts 格式,包括每天 returns 的银行股票。

我尝试使用 apply.monthly():

apply.monthly(return_2000_xts, 
    rq(esb.eu ~ hsbc.uk, data = return_2000_xts, tau = 0.95))

不幸的是,我收到以下错误消息:

Error in get(as.character(FUN), mode = "function", envir = envir) : object 'FUN' of mode 'function' was not found

我的代码可能有什么问题?

一个可重现的例子会很好,但你可能想要

apply.monthly(return_2000_xts, 
              FUN=rq,
              formula = esb.eu ~ hsbc.uk, 
              data = return_2000_xts, tau = 0.95)

... 也就是说,您只将 rq 函数作为参数传递,然后将其他参数添加到 rq() 作为附加参数(对应于 ... 参数apply.monthly())

我不完全确定出了什么问题,也许 apply.monthly() 正在剥离一些属性,但回到基础似乎可行。

library(xts)
library(quantreg)

data(sample_matrix)
xt <- as.xts(sample_matrix)

f <- as.character(index(xt), format="%Y-%m")
xt.ym <- split(xt, f)
lapply(xt.ym, FUN=function(x) rq(Open ~ Close, data=x, tau=0.95))

仅供参考,这是行不通的,但感觉应该如此

apply.monthly(xt, FUN=function(x) rq(Open ~ Close, data=x))

Error in coredata.xts(x) : currently unsupported data type


我明白了为什么 apply.monthly() 不起作用。它想要 return 一个 xts 对象,但是无法将回归对象列表强制转换为 xts,因此它会抛出错误。但是,如果我们将回归输出限制为可以强制执行的内容,f.ex.

,它将起作用
apply.monthly(xt, FUN=function(x) rq(Open ~ Close, data=x)$coef)
#            (Intercept)     Close
# 2007-01-31   12.224046 0.7564106
# 2007-02-28   -6.326472 1.1242798
# 2007-03-31   -2.108973 1.0432247
# 2007-04-30    5.739395 0.8840677
# 2007-05-31    2.453616 0.9495129
# 2007-06-30   17.380465 0.6342055