0
votes

I have been using ts objects for a while but not too much xts, so i was trying to calculate mean, sum, last, first... (FUN) for every year in economic time series. When i try it i have weird result.

This is a quick example: Considering x and y like monthly series, for two consecutive years:

x <- 1:12
y <- 1:12
serie <- c(x,y)

Then i create a ts object

serie <- ts(serie,1990,,12)

now i convert it to xts object

library(xts)

serie.xts <- as.xts(serie)

plot(serie.xts)

as far i see, the series is properly created

serie.xts

The series looks ok

ene 1990    1
feb 1990    2
mar 1990    3
abr 1990    4
may 1990    5
jun 1990    6
jul 1990    7
ago 1990    8
sep 1990    9
oct 1990   10
nov 1990   11
dic 1990   12
ene 1991    1
feb 1991    2
mar 1991    3
abr 1991    4
may 1991    5
jun 1991    6
jul 1991    7
ago 1991    8
sep 1991    9
oct 1991   10
nov 1991   11
dic 1991   12

Then comes my problem

for example, i try to get the last value for every year, as far i know this is what this function do:

apply.yearly(serie.xts,FUN = "last")

but i get this:

ene 1990    1
ene 1991    1
dic 1991   12

I was expecting this: dic 1990 12 dic 1991 12

What am i doing wrong? Is this the way this function works? What this function do actually?

i don't understand what is this result. Please help. And thanks for reading my question.

R 3.3.3 xts 0.9-7 zoo 1.8-0

1
i try this, but also give me the same result: period.apply(INDEX = endpoints(x = serie.xts, on = "years"), x = serie.xts, FUN = "last") - Victor Espinoza
I tried your code, and it works fine for me. I would suggest to convert the class of your index from yearmon to Date like this index(serie.xts) <- as.Date(index(serie.xts)) - sechstein
That is weird. I try in another pc and i have the same wrong result. Are you using the same version? - Victor Espinoza
On macOS Sierra, R 3.3.3 xts 0.9-7, zoo 1.7-14 - sechstein
The only difference is the zoo version. Maybe this update broke something. - Victor Espinoza

1 Answers

0
votes

This is a bug in xts 0-9.7 that has been fixed in the development version. Run one of these commands to install from GitHub:

remotes::install_github("joshuaulrich/xts")
# or
devtools::install_github("joshuaulrich/xts")

Then the apply.yearly call will work as you expect:

serie <- ts(c(1:12,1:12),1990,,12)
library(xts)
serie.xts <- as.xts(serie)
apply.yearly(serie.xts, last)
#          [,1]
# Dec 1990   12
# Dec 1991   12