在 R 中使用 xts 对象列表时出现分段错误

发布于 2024-11-28 22:09:33 字数 3361 浏览 1 评论 0原文

我在处理 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" 
...

当我尝试在 CQT 上运行 lapply 时,就会出现问题。在多次遇到问题后,我将我的测试代码浓缩为:

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 包是否有问题。我已经通过删除软件包然后使用 R-Forge 存储库重新安装来重新安装了 xts,因此我应该拥有最新版本。

如果我可以提供任何其他信息,请告诉我。

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)

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:

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:

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)

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:

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:

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)

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:

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:

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)

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:

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-Forge repo, so I should have the most recent version.

Please let me know if there is any additional information I can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2024-12-05 22:09:33

问题是 lag 的 C 代码现在位于 zoo 包中,补丁已应用于 R-forge 源代码 - 尚未应用于 CRAN 版本。这是大约一周前修复的。

更新您的 zoo 版本(来自 R-forge,版本号可能仍然相同),您应该会发现它可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文