Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ta with firstand last #22

Closed
christophsax opened this issue Sep 14, 2015 · 3 comments
Closed

ta with firstand last #22

christophsax opened this issue Sep 14, 2015 · 3 comments
Labels
Milestone

Comments

@christophsax
Copy link
Owner

undesirable

atsq <- ts(1:20,start=c(2001,1),freq=4)
btsq <- window(atsq,start=tsp(atsq)[1]+0.25,end=tsp(atsq)[2]-0.25,freq=4)
ta(btsq, conversion="first", to="annual")
ta(btsq, conversion="last", to="annual”)

incorrect

library(tempdisagg)
maintainer("tempdisagg")

atsq <- ts(1:20,start=c(2001,1),freq=4)
atsq[1] <- NA
atsq

# insert a NA in the first observation of atsq
# result ?? The NA in output of "last" should be 4
ta(atsq, conversion="last", to="annual")
# OK
ta(atsq, conversion="first", to="annual")

# same but with a 0
# result ok
atsq[1] <- 0
ta(atsq, conversion="first", to="annual")
ta(atsq, conversion="last", to="annual")
@christophsax
Copy link
Owner Author

From Berend Hasselman:

library(tempdisagg)
maintainer("tempdisagg")

atsq <- ts(1:20,start=c(2001,1),freq=4)
atsq

# btsq starts in quarter 2 and ends in quarter 3
btsq <- window(atsq,start=tsp(atsq)[1]+0.25,end=tsp(atsq)[2]-0.25,freq=4)
btsq
# ok
ta(btsq, conversion="sum", to="annual")
ta(btsq, conversion="average", to="annual")

# weird ?
ta(btsq, conversion="first", to="annual")
ta(btsq, conversion="last", to="annual")

# insert a NA in the first observation of atsq
# results different

atsq[1] <- NA
# result ?? The NA in output of "last" should be 4
ta(atsq, conversion="last", to="annual")
# OK
ta(atsq, conversion="first", to="annual")

# same but with a 0
# result ok
atsq[1] <- 0
ta(atsq, conversion="first", to="annual")
ta(atsq, conversion="last", to="annual")


# BETTER!!
# partly as suggested by Christoph Sax
# And this gives the correct result
# first quarter
btsq[cycle(btsq)==1]
# 4th quarter
btsq[cycle(btsq)==4]

# as timeseries
# My extra stuff to make the result an R ts
fstart <- floor(tsp(btsq)[1]+ which(cycle(btsq)==1)[1] / frequency(btsq) + 1e-8)
fstart
flast <- floor(tsp(btsq)[1]+ which(cycle(btsq)==4)[1] / frequency(btsq) + 1e-8)
flast

ts(btsq[cycle(btsq)==1],start=fstart,frequency=1)
ts(btsq[cycle(btsq)==4],start=flast,frequency=1)

# Should be better now
atsq[1] <- NA
fstart <- floor(tsp(atsq)[1]+ which(cycle(atsq)==1)[1] / frequency(atsq) + 1e-8)
fstart
flast <- floor(tsp(atsq)[1]+ which(cycle(atsq)==4)[1] / frequency(atsq) + 1e-8)
flast

ts(atsq[cycle(atsq)==1],start=fstart,frequency=1)
ts(atsq[cycle(atsq)==4],start=flast,frequency=1)

@christophsax christophsax added this to the 0.30 milestone Mar 14, 2016
christophsax pushed a commit that referenced this issue Mar 26, 2016
adresses #22
but needs some testing
@christophsax
Copy link
Owner Author

Sorry for the long wait! Finally could address it. One difficulty was that it should also work when the low frequency is not annual, e.g., quarterly. Should be ok with the latest dev version, but probably needs some testing.

devtools::install_github("christophsax/tempdisagg")
library(tempdisagg)

> x <- AirPassengers
> x[length(x)] <- NA
> ta(x, conversion = "first", to = 4)
     Qtr1 Qtr2 Qtr3 Qtr4
1949  112  129  148  119
1950  115  135  170  133
1951  145  163  199  162
1952  171  181  230  191
1953  196  235  264  211
1954  204  227  302  229
1955  242  269  364  274
1956  284  313  413  306
1957  315  348  465  347
1958  340  348  491  359
1959  360  396  548  407
1960  417  461  622  461
> 
> x <- window(AirPassengers, start = c(1949, 5))
> ta(x, conversion = "last", to = 4)
     Qtr1 Qtr2 Qtr3 Qtr4
1949       135  136  118
1950  141  149  158  140
1951  178  178  184  166
1952  193  218  209  194
1953  236  243  237  201
1954  235  264  259  229
1955  267  315  312  278
1956  317  374  355  306
1957  356  422  404  336
1958  362  435  404  337
1959  406  472  463  405
1960  419  535  508  432

@christophsax
Copy link
Owner Author

  • fix
  • testing on travis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant