| title : “Time Serries Forecasting” |
| author : “Recrafted by bambangpe” |
| output : html_document |
monthly_milk <- read.csv("D:/data/monthly_milk.csv")
# Milk production per cow per month
head(monthly_milk)
## month milk_prod_per_cow_kg
## 1 1962-01-01 265.05
## 2 1962-02-01 252.45
## 3 1962-03-01 288.00
## 4 1962-04-01 295.20
## 5 1962-05-01 327.15
## 6 1962-06-01 313.65
daily_milk <- read.csv("D:/data/daily_milk.csv")
# Milk production per cow per milking
head(daily_milk)
## date_time milk_prod_per_cow_kg
## 1 1975-01-01 05:00:00 11.21745
## 2 1975-01-01 17:00:00 10.67182
## 3 1975-01-02 05:00:00 10.90791
## 4 1975-01-02 17:00:00 11.03970
## 5 1975-01-03 05:00:00 12.53303
## 6 1975-01-03 17:00:00 10.69446
class(monthly_milk)
## [1] "data.frame"
Date classclass(monthly_milk$month)
## [1] "factor"
monthly_milk$month_date <- as.Date(monthly_milk$month, format = "%Y-%m-%d")
# Check it worked
class(monthly_milk$month_date)
## [1] "Date"
format()format(monthly_milk$month_date, format = "%Y-%B-%u")
format(monthly_milk$month_date, format = "%Y-%B-%u")
class(format(monthly_milk$month_date, format = "%Y-%B-%u")) # class is no longer `Date`
## [1] "character"
head(daily_milk)
## date_time milk_prod_per_cow_kg
## 1 1975-01-01 05:00:00 11.21745
## 2 1975-01-01 17:00:00 10.67182
## 3 1975-01-02 05:00:00 10.90791
## 4 1975-01-02 17:00:00 11.03970
## 5 1975-01-03 05:00:00 12.53303
## 6 1975-01-03 17:00:00 10.69446
head(daily_milk)
## date_time milk_prod_per_cow_kg
## 1 1975-01-01 05:00:00 11.21745
## 2 1975-01-01 17:00:00 10.67182
## 3 1975-01-02 05:00:00 10.90791
## 4 1975-01-02 17:00:00 11.03970
## 5 1975-01-03 05:00:00 12.53303
## 6 1975-01-03 17:00:00 10.69446
class(daily_milk$date_time)
## [1] "factor"
daily_milk$date_time_posix <- as.POSIXct(daily_milk$date_time, format = "%Y-%m-%d %H:%M:%S")
class(daily_milk$date_time_posix)
## [1] "POSIXct" "POSIXt"
## [1] "01/Jan/1962-1" "01/Feb/1962-4" "01/Mar/1962-4" "01/Apr/1962-7"
## [5] "01/May/1962-2" "01/Jun/1962-5"
## [1] "character"
monthly_milk$good_date <- as.Date(monthly_milk$bad_date, format = "%d/%b/%Y-%u")
head(monthly_milk$good_date)
## [1] "1962-01-01" "1962-02-01" "1962-03-01" "1962-04-01" "1962-05-01"
## [6] "1962-06-01"
class(monthly_milk$good_date)
## [1] "Date"
## `geom_smooth()` using formula 'y ~ x'
monthly_milk$year <- format(monthly_milk$month_date, format = "%Y")
monthly_milk$month_num <- format(monthly_milk$month_date, format = "%m")
colortools packageTransform to ts class
monthly_milk_ts <- ts(monthly_milk$milk_prod, start = 1962, end = 1975, freq = 12)
# Specify start and end year, measurement frequency (monthly = 12)
stl()monthly_milk_stl <- stl(monthly_milk_ts, s.window = "period")
monthplot(monthly_milk_ts, choice = "seasonal") # variation in milk production for each month
seasonplot(monthly_milk_ts) #ok
# Split data into testing and training
monthly_milk_model <- window(x = monthly_milk_ts, start=c(1962), end=c(1970))
monthly_milk_test <- window(x = monthly_milk_ts, start=c(1970))
milk_ets_auto <- ets(monthly_milk_model)
milk_ets_mmm <- ets(monthly_milk_model, model = "MMM")
milk_ets_zzz<- ets(monthly_milk_model, model = "ZZZ")
milk_ets_mmm_damped <- ets(monthly_milk_model, model = "MMM", damped = TRUE)
milk_ets_fc <- forecast(milk_ets_auto, h = 60) # `h = 60` means that the forecast will be 60 time periods long, in our case a time period is one month
milk_ets_mmm_fc <- forecast(milk_ets_mmm, h = 60)
milk_ets_zzz_fc <- forecast(milk_ets_zzz, h = 60)
milk_ets_mmm_damped_fc <- forecast(milk_ets_mmm_damped, h = 60)
forecast_all <- rbind(milk_ets_fc_df, milk_ets_mmm_fc_df, milk_ets_zzz_fc_df, milk_ets_mmm_damped_fc_df)
accuracy(milk_ets_fc, monthly_milk_test) accuracy(milk_ets_mmm_fc, monthly_milk_test) accuracy(milk_ets_zzz_fc, monthly_milk_test) accuracy(milk_ets_mmm_damped_fc, monthly_milk_test)
milk_ets_fc_df %>%
filter(Month == "Jan 1975") %>%
select(Month, Point_Forecast)
## Month Point_Forecast
## 1 Jan 1975 383.7897
milk_ets_zzz_fc_df %>%
filter(Month == "Jan 1975") %>%
select(Month, Point_Forecast)
## Month Point_Forecast
## 1 Jan 1975 383.7897