I am having trouble working with a list of xts objects. I get different and strange behavior when running lapply
on the list elements, and sometimes the program segfaults. I am running R 2.12.2 on Ubuntu 9.10, although I was having similar issues running 2.13.1 on Windows XP.
I am attempting to split a dataframe, which contains quarterly financial data, into multiple time series. I am splitting the dataframe on CIK code, which is an integer. The dataframe is:
> 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
...
The code that I am using to create the list of xts objects is:
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)
I am not sure that it is strictly necessary to convert to a list, but I feel comfortable with lists.
This creates the following data structure, which appears correct to me:
> 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"
...
The problem occurs when I try to run lapply
on CQT
. After experiencing problems multiple times, I have condensed my testing code to:
lapply(CQT,function(x) {
lag.xts(x[,"prccq"],1)
}
)
I figure if I can get this working, then I might be on the right track.
This code only segfaults sometimes. For instance, on the most recent iteration that I ran (for the purposes of posting), the code gets through a number of records just fine. For instance:
$`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"
...
However, it will then throw:
$`6720`
Error in vector(storage.mode(x)) :
vector: cannot make a vector of mode 'NULL'.
This is not always the error: sometimes I get complaints about characters or something else, and it has never halted on the same record twice. There does not appear to be anything wrong with this particular record:
> 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"
I am kind of at my wit's end over this. I am not sure if I am coding incorrectly (I was not able to find good examples of working with lists of xts objects), or if there is a problem with my xts package. I have reinstalled xts by removing the package and then reinstalling, using the R-开发者_如何学JAVAForge repo, so I should have the most recent version.
Please let me know if there is any additional information I can provide.
The issue is that lag's C code is now in the zoo package, with the patch applied to the R-forge sources - not yet to the CRAN version. This was fixed about a week ago.
Update your version of zoo (from R-forge, version number may be the same still) and you should find that it works.
精彩评论