我在处理xts对象列表时遇到问题。在列表元素上运行lapply
时会出现不同且奇怪的行为,有时程序会出现段错误。我在Ubuntu 9.10上运行R 2.12.2,虽然我在Windows XP上遇到类似的问题2.13.1。
我正在尝试将包含季度财务数据的数据框拆分为多个时间序列。我在CIK代码上拆分数据帧,这是一个整数。数据框是:
> head(CQ[,c("datadate","fqtr","cik","ibq","mkvaltq","prccq","sic")])
datadate fqtr cik ibq mkvaltq prccq sic
3 2009-12-31 1 61478 3.400 601.12800 6.21 3661
4 2010-03-31 2 61478 -13.000 709.07000 7.31 3661
5 2010-06-30 3 61478 75.900 718.77000 7.41 3661
6 2010-09-30 4 61478 10.900 1231.52400 12.67 3661
7 2004-03-31 3 319126 0.424 9.73455 1.05 3861
8 2004-06-30 4 319126 0.407 13.90650 1.50 3861
...
我用来创建xts对象列表的代码是:
CQT<-by(CQ[c("datadate","ibq","cik","mkvaltq","prccq","sic","fqtr")],CQ$cik,function(x)
{
xts(x,order.by=x$datadate,frequency=4)
}
)
CQT<-as.list(CQT)
我不确定转换为列表是否完全必要,但我对列表感到满意。
这会创建以下数据结构,这对我来说是正确的:
> head(CQT)
$`20`
datadate ibq cik mkvaltq prccq sic fqtr
2004-03-31 "2004-03-31" "1.422" "20" " 53.75880" " 21.8000" "3823" "1"
2004-06-30 "2004-06-30" "1.389" "20" " 55.04400" " 22.0000" "3823" "2"
2004-09-30 "2004-09-30" "1.562" "20" " 55.69816" " 22.1200" "3823" "3"
2004-12-31 "2004-12-31" "2.237" "20" " 67.11840" " 26.5500" "3823" "4"
2005-03-31 "2005-03-31" "1.643" "20" " 77.28716" " 30.4400" "3823" "1"
2005-06-30 "2005-06-30" "1.916" "20" " 75.12520" " 29.3000" "3823" "2"
...
当我尝试在lapply
上运行CQT
时,会出现问题。在多次遇到问题后,我将测试代码浓缩为:
lapply(CQT,function(x) {
lag.xts(x[,"prccq"],1)
}
)
我想如果能让这个工作起来,那么我可能会走上正轨。
这段代码有时只会出现段错误。例如,在我运行的最新迭代中(出于发布目的),代码很好地通过了许多记录。例如:
$`6494`
prccq
2004-03-31 NA
2004-06-30 "0.240"
2004-09-30 "0.150"
2004-12-31 "0.090"
2005-03-31 "0.062"
...
然而,它会抛出:
$`6720`
Error in vector(storage.mode(x)) :
vector: cannot make a vector of mode 'NULL'.
这并不总是错误:有时我会收到有关字符或其他内容的投诉,并且它从未在同一记录上停止过两次。此特定记录似乎没有任何问题:
> CQT$"6720"
datadate ibq cik mkvaltq prccq sic fqtr
2004-03-31 "2004-03-31" " 10.740" "6720" "559.8638" "16.03" "3949" "1"
2004-06-30 "2004-06-30" " 6.178" "6720" "558.6060" "15.70" "3949" "2"
2004-09-30 "2004-09-30" " 13.198" "6720" "667.6474" "14.31" "3949" "3"
2004-12-31 "2004-12-31" " 8.825" "6720" "743.1205" "15.88" "3949" "4"
2005-03-31 "2005-03-31" " 2.324" "6720" "643.6650" "13.75" "3949" "1"
2005-06-30 "2005-06-30" " 1.453" "6720" "594.0200" "12.68" "3949" "2"
2005-09-30 "2005-09-30" " 16.740" "6720" "534.5802" "11.40" "3949" "3"
2005-12-31 "2005-12-31" "-232.078" "6720" "474.1590" "10.11" "3949" "4"
2006-03-31 "2006-03-31" " 3.642" "6720" "589.5614" "12.55" "3949" "1"
2006-06-30 "2006-06-30" " 2.143" "6720" "514.9567" "10.94" "3949" "2"
2006-09-30 "2006-09-30" " 21.518" "6720" "552.9757" "11.73" "3949" "3"
2006-12-31 "2006-12-31" " 10.385" "6720" "651.7707" "13.19" "3949" "4"
2007-03-31 "2007-03-31" " 4.767" "6720" "597.7659" "12.09" "3949" "1"
我很想知道这件事。我不确定我编码是否错误(我无法找到使用xts对象列表的好例子),或者我的xts包有问题。我已经通过删除软件包重新安装了xts,然后使用R-Forge repo重新安装,所以我应该有最新的版本。
如果我能提供任何其他信息,请告诉我。
答案 0 :(得分:5)
问题是,滞后的C代码现在位于 zoo 包中,补丁应用于R-forge源 - 尚未进入CRAN版本。这是在一周前修复的。
更新 zoo 的版本(来自R-forge,版本号可能仍然相同),您应该会发现它有效。