【发布时间】:2011-04-15 09:18:36
【问题描述】:
或者换一种说法:我怎样才能保持我的 ts 索引? 大多数时候,我在计算中使用时间序列,它不再是 ts 对象。编写函数返回一个ts对象并保留索引信息时应该遵循什么策略?
例如:
#standard Hodrick Prescott Filter
hpfilter <- function(x,lambda=1600){
eye <- diag(length(x))
result <- solve(eye+lambda*crossprod(diff(eye,lag=1,d=2)),x)
### this is what I am talking about :)
### intuitively i´d maybe add something like this
result <- ts(result,start=start(x),end=end(x),frequency=frequency(x))
###
return(result)
}
但是,我觉得这个笨拙和繁琐。有没有更优雅的方法(也许我应该上课..)?
【问题讨论】:
标签: r indexing time-series