-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modeling.R
142 lines (106 loc) · 5.15 KB
/
Modeling.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
# install.packages('forecast')
library(forecast)
# Set to current working directory
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
co2_emission <- read.csv("Actual_CO2.csv")
# To get global CO2 Emission and convert into time series object
accumulated_co2_by_year <- aggregate(CO2.emission ~ Year, data = co2_emission, sum)
annualized_co2_by_year <- aggregate(Actual_CO2_by_year ~ Year, data = co2_emission, sum)
# Convert the data into time series object
accumulated_co2_time_series = ts(accumulated_co2_by_year$CO2.emission, start=1750)
accumulated_co2_time_series
annualized_co2_time_series = ts(annualized_co2_by_year$Actual_CO2_by_year, start=1750)
annualized_co2_time_series
plot(accumulated_co2_time_series)
plot(annualized_co2_time_series)
# ddata <- decompose(annualized_co2_time_series)
# plot(ddata)
# # Setup the model - for report purpose only
# accumulated_linear_model = tslm(accumulated_co2_time_series ~ trend)
# accumulated_arima_model = auto.arima(accumulated_co2_time_series)
# accumulated_naive_model = naive(accumulated_co2_time_series, h = 30)
# accumulated_tbats_model = tbats(accumulated_co2_time_series)
# accumulated_holt_model = holt(accumulated_co2_time_series, h = 30)
#
# annualized_linear_model = tslm(annualized_co2_time_series ~ trend)
# annualized_arima_model = auto.arima(annualized_co2_time_series)
# annualized_naive_model = naive(annualized_co2_time_series, h = 30)
# annualized_tbats_model = tbats(annualized_co2_time_series)
# annualized_holt_model = holt(annualized_co2_time_series, h = 30)
# 1. Linear Regression
accumulated_linear_model = tslm(accumulated_co2_time_series ~ trend)
accumulated_linear_predicted = forecast(accumulated_linear_model, h = 30)
accumulated_linear_predicted
annualized_linear_model = tslm(annualized_co2_time_series ~ trend)
annualized_linear_predicted = forecast(annualized_linear_model, h = 30)
annualized_linear_predicted
# 1.2 Model Evaluation
accumulated_accuracy_table = accuracy(accumulated_linear_predicted)
summary(accumulated_linear_model)
checkresiduals(accumulated_linear_model)
plot(accumulated_linear_predicted)
annualized_accuracy_table = accuracy(annualized_linear_predicted)
summary(annualized_linear_model)
checkresiduals(annualized_linear_model)
plot(annualized_linear_predicted)
# 2. ARIMA
accumulated_arima_model = auto.arima(accumulated_co2_time_series)
accumulated_arima_predicted = forecast(accumulated_arima_model, h = 30)
accumulated_arima_predicted
annualized_arima_model = auto.arima(annualized_co2_time_series)
annualized_arima_predicted = forecast(annualized_arima_model, h = 30)
annualized_arima_predicted
# 2.2 Model Evaluation
accumulated_accuracy_table = rbind(accumulated_accuracy_table, accuracy(accumulated_arima_predicted))
summary(accumulated_arima_model)
checkresiduals(accumulated_arima_model)
plot(accumulated_arima_predicted)
annualized_accuracy_table = rbind(annualized_accuracy_table, accuracy(annualized_arima_predicted))
summary(annualized_arima_model)
checkresiduals(annualized_arima_model)
plot(annualized_arima_predicted)
# 3. Naive Forecasting Method
accumulated_naive_model = naive(accumulated_co2_time_series, h = 30)
annualized_naive_model = naive(annualized_co2_time_series, h = 30)
# 3.2 Model Evaluation
accumulated_accuracy_table = rbind(accumulated_accuracy_table, accuracy(accumulated_naive_model))
summary(accumulated_naive_model)
checkresiduals(accumulated_naive_model)
plot(accumulated_naive_model)
annualized_accuracy_table = rbind(annualized_accuracy_table, accuracy(annualized_naive_model))
summary(annualized_naive_model)
checkresiduals(annualized_naive_model)
plot(annualized_naive_model)
# 4. TBATS
accumulated_tbats_model = tbats(accumulated_co2_time_series)
accumulated_tbats_predicted = forecast(accumulated_tbats_model, h = 30)
accumulated_tbats_predicted
annualized_tbats_model = tbats(annualized_co2_time_series)
annualized_tbats_predicted = forecast(annualized_tbats_model, h = 30)
annualized_tbats_predicted
# 4.2 Model Evaluation
accumulated_accuracy_table = rbind(accumulated_accuracy_table, accuracy(accumulated_tbats_predicted))
summary(accumulated_tbats_model)
checkresiduals(accumulated_tbats_model)
plot(accumulated_tbats_predicted)
annualized_accuracy_table = rbind(annualized_accuracy_table, accuracy(annualized_tbats_predicted))
summary(annualized_tbats_model)
checkresiduals(annualized_tbats_model)
plot(annualized_tbats_predicted)
# 5. Holt's Method
accumulated_holt_model = holt(accumulated_co2_time_series, h = 30)
annualized_holt_model = holt(annualized_co2_time_series, h = 30)
# 5.2 Model Evaluation
accumulated_accuracy_table = rbind(accumulated_accuracy_table, accuracy(accumulated_holt_model))
summary(accumulated_holt_model)
checkresiduals(accumulated_holt_model)
plot(accumulated_holt_model)
annualized_accuracy_table = rbind(annualized_accuracy_table, accuracy(annualized_holt_model))
summary(annualized_holt_model)
checkresiduals(annualized_holt_model)
plot(annualized_holt_model)
# Accuracy Table
rownames(accumulated_accuracy_table) <- c("Linear Regression", "ARIMA", "Naive", "TBATS", "Holt")
accumulated_accuracy_table
rownames(annualized_accuracy_table) <- c("Linear Regression", "ARIMA", "Naive", "TBATS", "Holt")
annualized_accuracy_table