-
Notifications
You must be signed in to change notification settings - Fork 0
/
Furniture.R
178 lines (125 loc) · 4.92 KB
/
Furniture.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
library(forecast)
library(readxl)
library(dplyr)
setwd('C:/Users/tiwar/Downloads/')
getwd()
salesdata <- read_excel("SalesData.xlsx", sheet=1, col_names = TRUE)
View(salesdata)
salesdata$Order.Date.Month<-format(salesdata$OrderDate,"%Y-%m")
df_cat <- salesdata %>%
select('Category','Sales','Order.Date.Month')
summary(df_cat)
df_cat$Sales <- as.numeric((df_cat$Sales))
str(df_cat)
Sales<-df_cat%>%
group_by(Order.Date.Month, Category)%>%
summarise(Sales.Per.Month = sum(Sales))%>%subset(Category == 'Furniture')
head(Sales)
plot(decompose(salests))
#salest<-data.frame(salesdata$OrderDate,salesdata$Sales)
####salests<-ts(salest[,-1],frequency =24,start=c(2014, 01),end=c(2018, 01))
salests<-ts(Sales$Sales.Per.Month,frequency =12,start=c(2014, 01),end=c(2018, 01))
head(salests)
hwin_Additive <- ets(salests_train, model = "ZNA")
hwin_Multiplicative<- ets(salests_train, model = "ZNM")
hwin <- hw(salests_train, lambda = "auto", h=12)
acf(hwin$fitted)
pacf(hwin$fitted)
acf(hwin$residuals)
hwin_2 <- hw(salests_train,seasonal = "multiplicative", h=12)
hwin_2 <- hw(salests_train,seasonal = "additive", h=12)
ets(salests)
BoxCox.lambda(salests)
salests<- salests^-0.57
autoplot(salests)
plot(salests,xlab="Year",ylab="Sales (USD)",main="Superstore Sales,Jan2014-December2017")
plot(decompose(salests))
seasonplot(salests,ylab="Sales (USD)",col=c(1,2,3,4),main="Seasonalplot:Superstore Sales,Jan2014-December2017",year.labels=TRUE)
Acf(salests)
Pacf(salests)
cycle(salests)
boxplot(salests~cycle(salests))
library(zoo)
accuracy(arima(salests, order = c(0,0,1)))
accuracy(ma(salests))
annual_sales = aggregate(salests)
plot.ts(annual_sales, col = "blue", main = "Yearly Beer sales time series data between 2014 and 2017")
decompose_salesdata <- decompose(salests,type = "additive")
seasonal_sales <- as.ts(decompose_salesdata$seasonal)
trend_sales <- as.ts(decompose_salesdata$trend)
random_sales <- as.ts(decompose_salesdata$random)
plot.ts(seasonal_sales, main = "Seasonal Component")
plot.ts(trend_sales, main = "Trend Component")
plot.ts(random_sales, main = "Randon Component")
plot (decompose(salests, type="additive"))
salests_seasonallyadjusted <- salests - seasonal_sales
plot.ts(salests_seasonallyadjusted, main = "Seasonal Adjusted Plot of sales")
####modified
####salests_hw <- hw(salests,seasonal="additive", h=24)
####salests_hw$model
####plot(salests_hw$model)
####plot(salests_hw)
####plot(salests)
####accuracy(salests_hw)
####plot(salests_hw$residuals)
####abline(0, 0)
####Acf(salests_hw$residuals)
####Acf(salests_hw$fitted)
####Data partition
####data partition
####Validation data set has been chosen to be 6 months because the last 1 year shows
####relatively very less variation in the sales. If sales for 1 year are taken to validate
####the model, difference in the forecast and actual values is very large owing to
####the large variation in the training data of the previous months.
nvalid <- 12
ntrain <- length(salests) - nvalid
salests_train <- window(salests,start=c(2014, 01),end=c(2014,ntrain))
salests_valid <- window(salests,start=c(2014,ntrain+1),end=c(2014,ntrain+nvalid))
# plot the series and its ACF, demonstrating trend and seasonality, indicating
# starting with d=0 and D=1
plot(salests_train, xlab = "Time", ylab = "Sales",bty = "l")
salests_train %>% Acf()
salests_train %>% diff() %>% Acf()
salests_train %>% diff(lag=12) %>% Acf()
salests_train %>% diff(lag=12) %>% diff() %>% Acf()
####Pacf for p,P=(0,0)
salests_train %>% Pacf()
salests_train %>% diff() %>% Pacf()
salests_train %>% diff(lag=12) %>% Pacf()
#salests_train %>% diff(lag=12) %>% diff() %>% Pacf()
####Acf for q,Q=(0,0)
####plot ARIMA
arima_model <- arima(salests_train,order=c(0,0,0),seasonal = c(0,1,0))
arima_model_transform <- arima(salests_train,order=c(0,0,0),seasonal = c(0,1,0),transform.pars = TRUE)
checkresiduals(arima_model)
accuracy(arima_model)
BoxCox.lambda(salests_train)
auto_arima_model <- auto.arima(salests_train, lambda = 0.945681, stepwise = FALSE)
accuracy(auto_arima_model)
checkresiduals(auto_arima_model)
auto.arima(salests_train)
arima_model
forecast_data <- forecast(arima_model,h=12)
forecast_data_auto <- forecast(auto_arima_model,h=12)
accuracy(forecast_data,salests_valid)
accuracy(forecast_data_auto,salests_valid)
plot(forecast_data)
lines(salests_valid,col="green")
####defining hw here
salests_hw <- hw(salests_train,seasonal="additive", h=12)
salests_hw$model
plot(salests_hw$model)
plot(salests_hw)
plot(salests)
accuracy(salests_hw)
plot(salests_hw$residuals)
abline(0, 0)
Acf(salests_hw$residuals)
forecast_data_hw <- forecast(salests_hw,h=12)
accuracy(forecast_data_hw,salests_valid)
plot(forecast_data_hw)
lines(salests_valid,col="green")
####Naive forecast
naive_model <- naive(salests_train,h=12)
plot(naive_model)
lines(salests_valid,col="green")