-1

I have the weekly data:

weekly <- structure(list(date = c("2013-01-07", "2013-01-10", "2013-01-21", 
"2018-01-23", "2018-02-00", "2013-02-11", "2013-02-13", "2013-02-25", 
"2013-03-00", "2013-03-11", "2013-03-13", "2013-03-25", "2018-00-01", 
"2018-00-08", "2018-00-15", "2018-00-22", "2018-00-29", "2018-05-06", 
"2018-05-13"), count = c(1750L, 1993L, 1816L, 1264L, 2042L, 1989L, 
2186L, 2118L, 2081L, 2110L, 2151L, 2069L, 1898L, 1862L, 1952L, 
1891L, 1758L, 1169L, 2009L)), row.names = c(NA, -19L), class = "data.frame")

I want to convert this data to the time series ts object so that I can forecast the data.

I saw this question but it didn't help me.

The code below creates data.frame with date and count

agr <-aggregate(input[1], input[2], FUN =  function(df) count=length(df))
colnames(agr)[2] <- "count"

The below the weekly data created:

weekly <- agr %>%
  tq_transmute(select     = count,
               mutate_fun = apply.weekly,
               FUN        = sum)

Now I want to convert this weekly data to time series data so that I can apply ARIMA model.

Artem
  • 3,304
  • 3
  • 18
  • 41
Techie VISH
  • 21
  • 2
  • 6

1 Answers1

2

You can convert the weekly data frame into time series object by using ts function with fractional frequency using lubridate package. Then you can use HoltWinters to predict next e.g. 3 weeks. Please see the code below:

weekly <- structure(list(date = c("2013-01-07", "2013-01-10", "2013-01-21", 
                                  "2018-01-23", "2018-02-00", "2013-02-11", "2013-02-13", "2013-02-25", 
                                  "2013-03-00", "2013-03-11", "2013-03-13", "2013-03-25", "2018-00-01", 
                                  "2018-00-08", "2018-00-15", "2018-00-22", "2018-00-29", "2018-05-06", 
                                  "2018-05-13"), 
                         count = c(1750L, 1993L, 1816L, 1264L, 2042L, 1989L, 
                                                           2186L, 2118L, 2081L, 2110L, 2151L, 2069L, 1898L, 1862L, 1952L, 
                                                           1891L, 1758L, 1169L, 2009L)), 
                    row.names = c(NA, -19L), 
                    class = "data.frame")

library(lubridate)
weekly_ts <- ts(weekly$count, 
   freq=365.25/7, 
   start= decimal_date(ymd(weekly[1, 1])))

#weekly_ts<- ts(weekly$count, frequency = 52)


m <- HoltWinters(weekly_ts, alpha = TRUE, beta = TRUE, gamma = FALSE)

library(forecast)
p <- predict(m, 3, prediction.interval = TRUE)
plot(m, p)

Output: graph

Artem
  • 3,304
  • 3
  • 18
  • 41
  • The above answer has frequency=1, start=1 and end=19. I want a weekly analysis which starts from 12/2017 and ends with 7/2018(I posted the picture of the data but now it has been edited by @Artem). I have 32 weeks of data. I want to use ARIMA model and to forecast for the next 8 weeks. For weekly data I have to use frequency = 52. I am not able to decompose the data because of the less than 2 cycle of seasonal data. I skipped the decomposition and used auto.arima() function with the above data. This lead to a straight horizontal line of forecast which almost coincide with the regression line. – Techie VISH Sep 25 '18 at 09:02
  • Edited w/frequency 52. You can try HoltWinters: `HoltWinters(weekly_ts, alpha = TRUE, beta = TRUE, gamma = FALSE)`. (But unfortunality there is no magic, to capture seasonality when you have less than a cycle is quite a challange ;) – Artem Sep 25 '18 at 09:29
  • Thanks for the answer @Artem. – Techie VISH Sep 25 '18 at 10:11