尝试使用滞后,但得到错误:hasTsp(x)中的错误:尝试在NULL上设置属性

时间:2013-07-25 17:18:57

标签: r

昨天,我成功地使用滞后计算月度投资回报,因此:

monthlyPctChgs = sapply(importData, function(x) diff(x) / lag(x, k=-1)) #ok

现在我正在计算两个月的变化。看起来很简单。我改变了滞后参数:

monthlyPctChgs = sapply(importData, function(x) diff(x, lag =-2) / lag(x, k=-2))

但是......这会产生错误:

Error in hasTsp(x) : attempt to set an attribute on NULL

我找不到'hasTsp'。我知道集合中可能存在空值导致这种情况,但这不是我在第一个实例中必须处理的问题。我错过了什么?

修改

以下是我的完整代码清单:

symbs = c('SPY', 'XLE', 'XLF', 'XLP', 'XLI', 'XLY', 'XLV', 'XLK', 'XLU')    
symbs = symbs[, 1]
importData = vector('list', length(symbs))

#Get monthly pricing data.
for (sIdx in 1:length(symbs)){
  #Import the data for each symbol into the list.
  importData[[sIdx]] = get.hist.quote(instrument= symbs[sIdx], start="2000-01-01",
         end="2013-07-15", quote="AdjClose", provider="yahoo", origin="1970-01-01",
         compression="m", retclass="zoo")
}

#Calculate performances on each symbol.
monthlyPctChgs = sapply(importData, function(x) diff(x, lag =2) / lag(x, k=-2))

会话信息:

> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] zoo_1.7-10

loaded via a namespace (and not attached):
[1] grid_3.0.1      lattice_0.20-15 tools_3.0.1

1 个答案:

答案 0 :(得分:1)

使用lapply代替sapply,它会起作用。

让我们先加载数据。

require(tseries)
require(zoo)
symbs = c('SPY', 'XLE', 'XLF', 'XLP', 'XLI', 'XLY', 'XLV', 'XLK', 'XLU')    

importData <- lapply(symbs, function(sIdx)
                  get.hist.quote( instrument = sIdx, start="2000-01-01",
                  end="2013-07-15", quote="AdjClose", provider="yahoo",
                                 origin="1970-01-01",
         compression = "m", retclass="zoo"))
names(importData) <- symbs
str(importData, max.level = 1)
## List of 9
##  $ SPY:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 109 107 118 114 112 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLE:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 22.5 21.5 24.1 23.7 26.5 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLF:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 17.8 15.9 18.7 18.9 19.3 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLP:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 17.8 15.7 16.3 17.1 18.4 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLI:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 21.9 20.7 23.6 23.9 23.8 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLY:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 23.7 22.4 25.5 24.9 23.6 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLV:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 25.3 23.7 25.8 25.5 24.8 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLK:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 44.6 49.3 53.5 48.5 43.5 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...
##  $ XLU:‘zoo’ series from 2000-01-03 to 2013-07-01
##   Data: num [1:163, 1] 17.6 15.4 17 18.1 18.1 ...
##   ..- attr(*, "dimnames")=List of 2
##   Index:  Date[1:163], format: "2000-01-03" ...

现在我们可以测试它是否有效。

monthlyPctChgs <- lapply(importData, function(x) diff(x, lag =2) / lag(x, k=-2))
str(monthlyPctChgs)
## List of 9
##  $ SPY:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] 0.0803 0.05827 -0.05032 0.00369 0.00366 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLE:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] 0.073 0.1037 0.1012 0.056 -0.0912 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLF:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] 0.0529 0.1897 0.0321 -0.027 0.0347 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLP:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] -0.0864 0.0889 0.1284 0.1319 0.0174 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLI:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] 0.0738 0.1528 0.0106 -0.0431 -0.0248 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLY:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] 0.076 0.1158 -0.0738 -0.1054 -0.039 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLV:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] 0.0186 0.0769 -0.0384 -0.0247 -0.0129 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLK:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] 0.1976 -0.0158 -0.1862 -0.0146 0.0411 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...
##  $ XLU:‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1] -0.0324 0.1729 0.0629 -0.0359 -0.0188 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr "AdjClose"
##   Index:  Date[1:161], format: "2000-03-01" ...

sapply倾向于将结果简化为矩阵/向量,而lapply总是返回一个列表,在这种情况下不可能返回矩阵。

此处有更多信息:R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate

如果出于任何原因想要返回像object这样的矩阵,可以像这样继续

monthly_pctchgs <- do.call(cbind, monthlyPctChgs)
colnames(monthly_pctchgs) <- symbs
str(monthly_pctchgs)
## ‘zoo’ series from 2000-03-01 to 2013-07-01
##   Data: num [1:161, 1:9] 0.0803 0.05827 -0.05032 0.00369 0.00366 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:9] "SPY" "XLE" "XLF" "XLP" ...
##   Index:  Date[1:161], format: "2000-03-01" "2000-04-03" "2000-05-01" ...

最后,这是我的会话信息

## sessionInfo()
## R version 3.0.1 Patched (2013-07-22 r63380)
## Platform: x86_64-unknown-linux-gnu (64-bit)

## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods  
## [7] base     

## other attached packages:
## [1] zoo_1.7-10      tseries_0.10-32

## loaded via a namespace (and not attached):
## [1] compiler_3.0.1  grid_3.0.1      lattice_0.20-15
## [4] quadprog_1.5-5  tools_3.0.1