Skip to content

xts, period.apply question

3 messages · Subhrangshu Nandi, msalese, Joshua Ulrich

#
Is anyone else having issues with the latest update of xts and/or zoo? Some
of my functions that use *apply.weekly* or *apply.daily* are not working any
more. I'm attaching a sample dataset to help you replicate the error. Below
is a copy of my RConsole:

*> str(Data.xts)*
An ?xts? object from 2010-03-23 04:20:00 to 2011-05-06 15:10:00 containing:
  Data: num [1:17902, 1:2] 1508 1508 1507 1507 1506 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "Close" "Close.1"
  Indexed by objects of class: [POSIXt,POSIXct] TZ:
  Original class: 'data.frame'
  xts Attributes:
List of 2
 $ tclass   : chr [1:2] "POSIXt" "POSIXct"
 $ na.action:Class 'omit'  atomic [1:1] 1
  .. ..- attr(*, "index")= num 1.27e+09
*> head(Data.xts)*
                       Close Close.1
2010-03-23 04:20:00 1507.750  537.75
2010-03-23 04:30:00 1507.875  538.50
2010-03-23 04:40:00 1506.875  538.25
2010-03-23 04:50:00 1507.000  538.00
2010-03-23 05:00:00 1506.500  538.00
2010-03-23 05:10:00 1506.500  538.00
*> fn_checkApply*
*function(Data=Data_D){*
*
*
*        MaxDiff <- max(Data[,1]) - max(Data[,2])*
*        return(MaxDiff)*
*}*
*> apply.daily(x=Data.xts, FUN=fn_checkApply)*
*Error in dimnames(x) <- dn : *
*  length of 'dimnames' [2] not equal to array extent*
*> traceback()*
*3: `colnames<-`(`*tmp*`, value = c("Close", "Close.1"))*
*2: period.apply(x, ep, FUN, ...)*
*1: apply.daily(x = Data.xts, FUN = fn_checkApply)*


Any help would be greatly appreciated. Everything was working fine until I
updated the packages on 05/02.
#
Hi, I'm new to R and I'm learning Rmetrics library but here I wrote some code
using quantmod/xts.
#############################################################
R.version
               _                            
platform       x86_64-pc-linux-gnu          
arch           x86_64                       
os             linux-gnu                    
system         x86_64, linux-gnu            
status                                      
major          2                            
minor          13.0                         
year           2011                         
month          04                           
day            13                           
svn rev        55427                        
language       R                            
version.string R version 2.13.0 (2011-04-13)
######################################################
R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=C             
LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=C       

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

other attached packages:
 [1] quantmod_0.3-15    TTR_0.20-2         xts_0.8-0          zoo_1.6-5         
 [5] Defaults_1.1-1     fOptions_2110.78   fBasics_2110.79    MASS_7.3-13       
 [9] timeSeries_2130.92 timeDate_2130.93  

loaded via a namespace (and not attached):
[1] grid_2.13.0     lattice_0.19-26 tools_2.13.0   
#######################################################
running on :
######################################################
Linux plutok 2.6.32-31-generic #61-Ubuntu SMP Fri Apr 8 18:25:51 UTC 2011
x86_64 GNU/Linux
Distributor ID:	Ubuntu
Description:	Ubuntu 10.04.2 LTS
Release:	10.04
Codename:	lucid
#######################################################
Here the code that to me semes to work fine:
######################################################??
#Load quantmod library ---> call xts/zoo too 
library(quantmod) 
#download data from yahoo (Generali Assurance - Italian Company )
getSymbols(Symbols="G.MI",src="yahoo")
#define function to compute max relative drawdown
relMxDD<-function(x){
  rdd<-cummax(log(x))-log(x)
  rdd<-1-(1/exp(rdd))
  max(rdd)
}
# apply relMxDD monthly
maxRelDD.sample<-period.apply(x=Cl(G.MI),endpoints(x=Cl(G.MI),on="months"),FUN=relMxDD)
#the same if you use 
#apply.monthly(x=Cl(G.MI),FUN=relMxDD))
plot(maxRelDD.sample,type='h')
hist(x=maxRelDD.sample)





--
View this message in context: http://r.789695.n4.nabble.com/xts-period-apply-question-tp3511933p3512019.html
Sent from the Rmetrics mailing list archive at Nabble.com.
1 day later
#
Hi Subhrangshu,

This is a bug that was introduced when I attempted to change
period.apply to nicely handle functions that provide multi-column
output.  I've patched on R-forge -- revision 593.

The R-forge servers will re-build the package soon, at which point can
install via:
R> install.packages("xts", repos="http://r-forge.r-project.org").

Thanks for the report!

Best,
--
Joshua Ulrich ?| ?FOSS Trading: www.fosstrading.com
On Tue, May 10, 2011 at 8:50 AM, Subhrangshu Nandi <nands31 at gmail.com> wrote: