I have a daily frequency dataframe which I am trying to convert into weekly timeseries to decompose(). I have been able to convert it into monthly but same process doesn't work as expected for weekly.
Code that I have attempted for both monthly & weekly:
Data:
library(tidyverse)
library(quantmod)
library(zoo)
library(xts)
adani_green_df <- read.csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/adani_daily_data.csv")
Monthly: (work as expected)
adani_monthly_zoo <- adani_green_df %>%
select(date,CLOSE) %>%
set_names(.,c("date","Close")) %>%
read.zoo(.,format = "%Y-%m-%d") %>%
to.monthly() %>%
Cl() %>%
as.ts()
adani_monthly_zoo
########### output #############
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2018 27.15 61.20 57.70 41.00 39.55 46.75 41.90
2019 35.70 33.05 37.25 40.25 43.10 44.45 46.75 46.20 58.25 89.65 137.20 166.50
2020 189.45 154.90 153.65 210.25 247.85 358.70 340.65 453.60 737.85 856.25 1120.80 1052.55
2021 1003.75 1168.05 1104.30 1019.00 1267.25 1116.90 888.20 1066.85 1147.25 1146.35 1291.20 1327.75
2022 1878.75 1839.10 1913.40 2887.30 1898.80 1929.00 2168.45 2436.70 2347.00
Weekly: (Resulting Data below isn't compiled as it is for monthly in above)
adani_weekly_zoo <- adani_green_df %>%
select(date,CLOSE) %>%
set_names(.,c("date","Close")) %>%
read.zoo(.,format = "%Y-%m-%d") %>%
to.weekly() %>%
Cl() %>%
as.ts()
adani_weekly_zoo
########### output #############
Time Series:
Start = 17704
End = 19254
Frequency = 1
[1] 29.45 NA NA NA NA NA NA 27.15 NA NA NA NA
[13] NA NA 30.05 NA NA NA NA NA NA 31.50 NA NA
[25] NA NA NA NA 35.30 NA NA NA NA NA NA 53.00
[37] NA NA NA NA NA NA 70.80 NA NA NA NA NA
[49] NA 66.90 NA NA NA NA NA NA 55.05 NA NA NA
Decompose:
decompose() is working on monthly timeseries that is created above but not working on weekly timeseries.
# works
adani_monthly_zoo %>%
decompose() %>%
plot()
# doesn't work
adani_weekly_zoo %>%
decompose() %>%
plot()
Error in decompose(.) : time series has no or less than 2 periods
