0

Yesterday, I was successfully working with lag, calculating month over month investment returns, as so:

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

Now I'm trying to calculate the two month change. Seems simple enough. I changed my lag parameters:

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

But... this generates the error:

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

I can't find much on 'hasTsp.' I understand that there could be nulls in the set causing this, but it wasn't an issue/ something I had to handle in the first instance. What am I missing?

Edit:

Here is my full code listing:

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))

Session info:

> 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
StatsViaCsh
  • 2,600
  • 10
  • 44
  • 63
  • Without your data it will be difficult to tell why it fails. I just tested your code on toy data an it works. So you'll have to provide a reproductible example if you want people to help you – dickoa Jul 25 '13 at 17:27

1 Answers1

1

Use lapply instead of sapply and it will works.

Lets load the data first.

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" ...

Now we can test if it works.

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 tend to simplify the result into a matrix/vector whereas lapply always return a list and in this case is not possible to return a matrix.

More info here : R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate

If for any reason you want to return a matrix like object, you can proceed like this

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" ...

Finally, here is my session info

## 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 
Community
  • 1
  • 1
dickoa
  • 18,217
  • 3
  • 36
  • 50
  • @StatsViaCsh Can you post sessionInfo with package loaded please ? – dickoa Jul 25 '13 at 18:49
  • Session info per your request. – StatsViaCsh Jul 25 '13 at 18:52
  • @StatsViaCsh I ran this code and it works, please try to run the same code to see if it works for you. Something missing in your search space is the `tseries` package, how can you use `get.hist.quote` without it ? – dickoa Jul 25 '13 at 19:00
  • I think that was it. I was missing tseries. I had been using it all day but it somehow got unchecked. Actually, I thought zoo extended it. Anyway, thank you for your help. – StatsViaCsh Jul 25 '13 at 19:05
  • @StatsViaCsh You're welcome. I love RStudio too though I use emacs ESS but I don't like this package checking things in Rstudio because you can easily forget which packages you loaded or not. Make sure to call the necessary package at the begining of your script. – dickoa Jul 25 '13 at 19:07