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

Daily to Weekly and Daily to Monthly Aggregation #51

Open taranpreetk opened 1 year ago

taranpreetk commented 1 year ago

Salutation for the day.

Thank you for the fantastic package design.

I was unsure if this package could aggregate time series from daily to weekly and daily to monthly because when I attempt to do so, I receive an error saying there isn't an option for "weekly" or "monthly".

I have put a reproducible example for this.

` library(Sim.DiffProc) library(stats) library(tempdisagg) set.seed(1234)

dates <- seq(as.Date("2019-01-01"), as.Date("2023-12-31"), by = "1 day")

X <- GBM(N = length(dates)-1, M = 1, x0 = 50, y= 200) daily_data <- data.frame(dates,X) daily_data <- ts(daily_data$X1, start = c(2019,1,1), end = c(2022,12,31), frequency = 365) weekly_data <- ta(daily_data, conversion = "sum", to = "weekly") monthly_data <- ta(daily_data, conversion = "sum", to = "monthly") ` Error in Console: daily_to_weekly_and_monthly_error

I'm not sure how to get this working as it is needed for the use case I'm working on. Any help on this will be appreciated.

Thanks in advance.

christophsax commented 1 year ago

The main point of tempdisagg, is to disaggregate:

What you are up to it to aggregate, which is a far easier task. tsbox can solve the problem you are struggling with:

library(dplyr)
library(tsbox)

dates = seq(as.Date("2019-01-01"), as.Date("2023-12-31"), by = "1 day")
daily <- tibble(
  time = dates,
  value = rnorm(length(dates))
) 

daily
#> # A tibble: 1,826 × 2
#>    time         value
#>    <date>       <dbl>
#>  1 2019-01-01  0.350 
#>  2 2019-01-02 -1.12  
#>  3 2019-01-03  0.384 
#>  4 2019-01-04 -0.0618
#>  5 2019-01-05  0.764 
#>  6 2019-01-06  1.09  
#>  7 2019-01-07  0.660 
#>  8 2019-01-08 -0.411 
#>  9 2019-01-09  0.893 
#> 10 2019-01-10  0.0300
#> # … with 1,816 more rows

tsbox::ts_frequency(daily, to = "month")
#> # A tibble: 60 × 2
#>    time          value
#>    <date>        <dbl>
#>  1 2019-01-01  0.111  
#>  2 2019-02-01  0.183  
#>  3 2019-03-01 -0.108  
#>  4 2019-04-01 -0.103  
#>  5 2019-05-01  0.00898
#>  6 2019-06-01  0.111  
#>  7 2019-07-01 -0.141  
#>  8 2019-08-01  0.213  
#>  9 2019-09-01 -0.100  
#> 10 2019-10-01  0.134  
#> # … with 50 more rows
tsbox::ts_frequency(daily, to = "year")
#> # A tibble: 5 × 2
#>   time          value
#>   <date>        <dbl>
#> 1 2019-01-01  0.0375 
#> 2 2020-01-01  0.00706
#> 3 2021-01-01  0.0208 
#> 4 2022-01-01 -0.00932
#> 5 2023-01-01  0.0706
tsbox::ts_frequency(daily, to = "week")
#> # A tibble: 260 × 2
#>    time         value
#>    <date>       <dbl>
#>  1 2019-01-06  0.342 
#>  2 2019-01-13  0.313 
#>  3 2019-01-20 -0.226 
#>  4 2019-01-27 -0.108 
#>  5 2019-02-03 -0.292 
#>  6 2019-02-10  0.292 
#>  7 2019-02-17  0.658 
#>  8 2019-02-24 -0.392 
#>  9 2019-03-03  0.0588
#> 10 2019-03-10 -0.581 
#> # … with 250 more rows

And if you need seem instead of averages:

tsbox::ts_frequency(daily, to = "week", aggregate = "sum")
#> # A tibble: 260 × 2
#>    time        value
#>    <date>      <dbl>
#>  1 2019-01-06  2.40 
#>  2 2019-01-13  2.19 
#>  3 2019-01-20 -1.59 
#>  4 2019-01-27 -0.759
#>  5 2019-02-03 -2.05 
#>  6 2019-02-10  2.04 
#>  7 2019-02-17  4.61 
#>  8 2019-02-24 -2.74 
#>  9 2019-03-03  0.412
#> 10 2019-03-10 -4.06 
#> # … with 250 more rows

Created on 2023-04-20 with reprex v2.0.2