我一直在与 zoo
合作,利用我的时间序列数据的滞后和差分。我不使用由公司和日期组成的面板数据集。单独落后于每个公司然后合并结果变得非常麻烦。 R 中是否有任何可以处理面板数据的好包?我目前了解 plm
。其他的? plm
有一个奇怪的问题,即lag
顺序(即-1 vs +1)与zoo
和ts
完全相反code> 因此我预见到 future 会令人头痛。有没有人喜欢的包?
最佳答案
ddply
函数,在 plyr
包裹,
通常使这种手术无痛
(但在大型数据集上可能会很慢)。
# Sample data
library(quantmod)
d <- NULL
for(s in c("^GSPC","^N225")) {
tmp <- getSymbols(s,auto.assign=FALSE)
tmp <- Ad(tmp)
names(tmp) <- "price"
tmp <- data.frame( date=index(tmp), id=s, price=coredata(tmp) )
d[[s]] <- tmp
}
d <- do.call(rbind, d)
rownames(d) <- NULL
# Sample computations: lag the prices and compute the logarithmic returns
library(plyr)
d <- ddply(
d, "id",
mutate,
previous_price = lag(xts(price,date)),
log_return = log(price / previous_price)
)
关于r - R 中的面板数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798441/