christophsax / tempdisagg

Methods for Temporal Disaggregation and Interpolation of Time Series
http://cran.r-project.org/web/packages/tempdisagg
37 stars 5 forks source link

no smoothing if conversion = "first"? #32

Closed christophsax closed 5 years ago

christophsax commented 7 years ago

Got this from here, and was surprised that there is no smoothing if conversion = "first".

m1 <- td(sales.a ~ 1, to = "monthly", conversion = "first",  method = "denton-cholette")
plot(predict(m1))

m2 <- td(sales.a ~ 1, to = "monthly", conversion = "sum",  method = "denton-cholette")
plot(predict(m2))

Is this a feature or a bug?

christophsax commented 5 years ago

As mentioned above, for conversion = "first", we get an unsmoothed result, contrary to what we get with conversion = "average":

library(tsbox)
library(tempdisagg)
data(swisspharma)

m.first <- td(sales.a ~ 1, to = "monthly", conversion = "first",  method = "denton-cholette")
m.avg <- td(sales.a ~ 1, to = "monthly", conversion = "average",  method = "denton-cholette")
ts_plot(first = predict(m.first), average = predict(m.avg))

But this is the correct result. It is the result of minimizing the sum of squared deviations between the differences of the resulting series and the indicator series. You can get a smoother series by minimizing the sum of squared deviations between the second differences (h = 2).

m.first.h2 <- td(sales.a ~ 1, to = "monthly", conversion = "first",  method = "denton-cholette", h = 2)
ts_plot(first_h2 = predict(m.first.h2), first = predict(m.first))

But note that this is not optimal with respect to the first differences (h = 1):

sum(diff(predict(m.first))^2, na.rm = TRUE)
#> [1] 4742.853
sum(diff(predict(m.first.h2))^2, na.rm = TRUE)
#> [1] 5730.583

Smoothness can only be bought by increasing the square sum of the deviations of the first differences. If you want a smoother series, use h = 2.

Created on 2019-08-19 by the reprex package (v0.3.0)