-
Notifications
You must be signed in to change notification settings - Fork 0
/
Predicting Medal Counts by Countries in Upcoming Olympics Games 3 - Model Building 1.R
329 lines (273 loc) · 17.2 KB
/
Predicting Medal Counts by Countries in Upcoming Olympics Games 3 - Model Building 1.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Data Loading and Preprocessing
## Read the CSV file
summer_olympic <- read.csv("C:\\Users\\wodnj\\OneDrive\\바탕 화면\\Data Analysis and Regression\\DSC 423 - Final Project\\Data File\\final_summer.csv")
## Remove unnecessary columns
summer_olympic <- subset(summer_olympic, select = -c(Country, Olympic, Year))
## Convert categorical variables into dummy variables
dummy_summer_olympic <- as.data.frame(model.matrix(NumberofMedals ~ . -1, data = summer_olympic))
dummy_summer_olympic <- subset(dummy_summer_olympic, select = -c(ClimateDwa))
dummy_summer_olympic$NumberofMedals <- summer_olympic$NumberofMedals
# Model Building 1
## Load the library
library(car)
## Initial model
model1 <- lm(NumberofMedals ~ ., data = dummy_summer_olympic)
summary(model1)
## Check multicollinearity
vif(model1)
## Remove insignificant variables based on the p-value and domain knowledge
model2 <- lm(NumberofMedals ~ GDP + Population + ClimateBWh + ClimateCfa + ClimateCfb +
ClimateCsa + ClimateDfa + ClimateDfb + ClimateDfc + PoliticalSystemcommunism +
PoliticalSystemdemocracy + PoliticalSystemmilitarydictatorship + HostCountryyes +
Participationyes + LifeExp + DeathRate + BirthRate + MortalityRateMale +
MortalityRateFemale + YoungFertilityRate + NetMigration,
data = dummy_summer_olympic)
summary(model2)
## Model selection
stepwise_model2 <- step(model2, direction = "both", trace = FALSE)
summary(stepwise_model2)
## Check multicollinearity
vif(stepwise_model2)
## Remove the variable with the highest VIF value
model3 <- lm(NumberofMedals ~ GDP + Population + ClimateCsa + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + LifeExp + BirthRate +
MortalityRateMale + YoungFertilityRate + NetMigration + PoliticalSystemdemocracy,
data = dummy_summer_olympic)
summary(model3)
## Check multicollinearity
vif(model3)
## Remove the variable with the highest VIF value
model4 <- lm(NumberofMedals ~ GDP + Population + ClimateCsa + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + MortalityRateMale +
YoungFertilityRate + NetMigration + PoliticalSystemdemocracy,
data = dummy_summer_olympic)
summary(model4)
## Check multicollinearity
vif(model4)
## Remove the variable with the highest VIF value
model5 <- lm(NumberofMedals ~ GDP + Population + ClimateCsa + ClimateDfc +
PoliticalSystemcommunism + HostCountryyes + Participationyes + BirthRate +
MortalityRateMale + YoungFertilityRate + NetMigration,
data = dummy_summer_olympic)
summary(model5)
## Check multicollinearity
vif(model5)
## Remove the most insignificant variable based on the p-value
model6 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism + HostCountryyes +
Participationyes +BirthRate + MortalityRateMale + YoungFertilityRate + NetMigration,
data = dummy_summer_olympic)
summary(model6)
## Check multicollinearity
vif(model6)
## Remove the most insignificant variable based on the p-value
model7 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism + HostCountryyes +
Participationyes + BirthRate + YoungFertilityRate + NetMigration,
data = dummy_summer_olympic)
summary(model7)
## Check multicollinearity
vif(model7)
## Create interaction term variable
model8 <- lm(NumberofMedals ~ (GDP + Population + ClimateDfc + PoliticalSystemcommunism + HostCountryyes +
Participationyes + BirthRate + YoungFertilityRate + NetMigration)^2,
data = dummy_summer_olympic)
summary(model8)
## Model selection
stepwise_model8 <- step(model8, direction = "both", trace = FALSE)
summary(stepwise_model8)
## Create second-order term variable
model9 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism + HostCountryyes +
Participationyes + BirthRate + YoungFertilityRate + NetMigration +
I(GDP^2) + I(Population^2) + I(ClimateDfc^2) + I(PoliticalSystemcommunism^2) + I(HostCountryyes^2) +
I(Participationyes^2) + I(BirthRate^2) + I(YoungFertilityRate^2) + I(NetMigration^2),
data = dummy_summer_olympic)
summary(model9)
## Generate a new model with significant interaction and second-order terms
model10 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:Population + GDP:PoliticalSystemcommunism + GDP:Participationyes + GDP:BirthRate +
GDP:YoungFertilityRate + GDP:NetMigration + Population:ClimateDfc +
Population:PoliticalSystemcommunism + Population:HostCountryyes + Population:Participationyes +
Population:BirthRate + Population:YoungFertilityRate + Population:NetMigration +
ClimateDfc:HostCountryyes + ClimateDfc:Participationyes + ClimateDfc:NetMigration +
PoliticalSystemcommunism:HostCountryyes + PoliticalSystemcommunism:Participationyes +
PoliticalSystemcommunism:BirthRate + HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate +
BirthRate:YoungFertilityRate + YoungFertilityRate:NetMigration + I(Population^2),
data = dummy_summer_olympic)
summary(model10)
## Model selection
stepwise_model10 <- step(model10, direction = "both", trace = FALSE)
summary(stepwise_model10)
## Remove insignificant variables based on the p-value
model11 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism + HostCountryyes +
Participationyes + BirthRate + YoungFertilityRate + NetMigration + GDP:PoliticalSystemcommunism +
GDP:Participationyes + GDP:BirthRate + Population:BirthRate + Population:YoungFertilityRate +
Population:NetMigration + ClimateDfc:HostCountryyes + ClimateDfc:Participationyes +
ClimateDfc:NetMigration + PoliticalSystemcommunism:HostCountryyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration + Participationyes:BirthRate +
Participationyes:YoungFertilityRate + YoungFertilityRate:NetMigration + I(Population^2),
data = dummy_summer_olympic)
summary(model11)
## Remove insignificant variables based on the p-value
model12 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism + HostCountryyes +
Participationyes + BirthRate + YoungFertilityRate + NetMigration + GDP:PoliticalSystemcommunism +
GDP:Participationyes + GDP:BirthRate + Population:BirthRate + Population:YoungFertilityRate +
ClimateDfc:HostCountryyes + ClimateDfc:Participationyes + PoliticalSystemcommunism:HostCountryyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration + Participationyes:BirthRate +
Participationyes:YoungFertilityRate + YoungFertilityRate:NetMigration + I(Population^2),
data = dummy_summer_olympic)
summary(model12)
# Cross-validation 1
## Load the library
library(caret)
## Set up cross-validation parameters
train_control12 <- trainControl(method = "cv", number = 10)
## Train a model using cross-validation
cv_model12 <- train(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism + HostCountryyes +
Participationyes + BirthRate + YoungFertilityRate + NetMigration + GDP:PoliticalSystemcommunism +
GDP:Participationyes + GDP:BirthRate + Population:BirthRate + Population:YoungFertilityRate +
ClimateDfc:HostCountryyes + ClimateDfc:Participationyes + PoliticalSystemcommunism:HostCountryyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration + Participationyes:BirthRate +
Participationyes:YoungFertilityRate + YoungFertilityRate:NetMigration + I(Population^2),
data = dummy_summer_olympic, method = "lm", trControl = train_control12)
print(cv_model12)
# Checking residuals 1
## Calculate residuals and predicted values
residuals12 <- residuals(model12)
predicted_values12 <- predict(model12)
## Q-Q plot of residuals
qqnorm(residuals12)
qqline(residuals12)
## Scatter plot of residuals against predicted values
plot(predicted_values12, residuals12, xlab = "Predicted values", ylab = "Residuals")
## Box plot of residuals
boxplot(residuals12)
## Histogram of residuals
hist(residuals12, main = "Histogram of Residuals")
# Model Building 2
## Remove insignificant variable based on domain knowledge
model13 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + GDP:Participationyes + GDP:BirthRate +
Population:BirthRate + Population:YoungFertilityRate + ClimateDfc:HostCountryyes +
ClimateDfc:Participationyes + PoliticalSystemcommunism:HostCountryyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic)
summary(model13)
## Remove insignificant variable based on the p-value
model14 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + GDP:Participationyes + Population:BirthRate +
Population:YoungFertilityRate + ClimateDfc:HostCountryyes + ClimateDfc:Participationyes +
PoliticalSystemcommunism:HostCountryyes + PoliticalSystemcommunism:Participationyes +
PoliticalSystemcommunism:BirthRate + HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic)
summary(model14)
## Remove insignificant variable based on the p-value
model15 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + Population:BirthRate + Population:YoungFertilityRate +
ClimateDfc:HostCountryyes + ClimateDfc:Participationyes + PoliticalSystemcommunism:HostCountryyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration + Participationyes:BirthRate +
Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic)
summary(model15)
## Remove insignificant variable based on the p-value
model16 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + Population:BirthRate + ClimateDfc:HostCountryyes +
ClimateDfc:Participationyes + PoliticalSystemcommunism:Participationyes +
PoliticalSystemcommunism:BirthRate + HostCountryyes:YoungFertilityRate +
HostCountryyes:NetMigration + Participationyes:BirthRate +
Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic)
summary(model16)
## Remove insignificant variable based on the p-value
model17 <- lm(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + Population:BirthRate + ClimateDfc:Participationyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic)
summary(model17)
# Cross-validation 2
## Set up cross-validation parameters
train_control17 <- trainControl(method = "cv", number = 10)
## Train a model using cross-validation
cv_model17 <- train(NumberofMedals ~ GDP + Population + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + Population:BirthRate + ClimateDfc:Participationyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic, method = "lm", trControl = train_control17)
print(cv_model17)
# Checking residuals 2
## Calculate residuals and predicted values
residuals17 <- residuals(model17)
predicted_values17 <- predict(model17)
## Q-Q plot of residuals
qqnorm(residuals17)
qqline(residuals17)
## Scatter plot of residuals against predicted values
plot(predicted_values17, residuals17, xlab = "Predicted values", ylab = "Residuals")
## Box plot of residuals
boxplot(residuals17)
## Histogram of residuals
hist(residuals17, main = "Histogram of Residuals")
# Model Building 3
## Log transformation
model18 <- lm(NumberofMedals ~ log(GDP) + log(Population) + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + Population:BirthRate + ClimateDfc:Participationyes +
PoliticalSystemcommunism:Participationyes + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic)
summary(model18)
## Remove insignificant variables based on the p-value
model19 <- lm(NumberofMedals ~ log(GDP) + log(Population) + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic)
summary(model19)
# Cross-validation 3
## Set up cross-validation parameters
train_control19 <- trainControl(method = "cv", number = 10)
## Train a model using cross-validation
cv_model19 <- train(NumberofMedals ~ log(GDP) + log(Population) + ClimateDfc + PoliticalSystemcommunism +
HostCountryyes + Participationyes + BirthRate + YoungFertilityRate + NetMigration +
GDP:PoliticalSystemcommunism + PoliticalSystemcommunism:BirthRate +
HostCountryyes:YoungFertilityRate + HostCountryyes:NetMigration +
Participationyes:BirthRate + Participationyes:YoungFertilityRate + I(Population^2),
data = dummy_summer_olympic, method = "lm", trControl = train_control19)
print(cv_model19)
# Checking residuals 3
## Calculate residuals and predicted values
residuals19 <- residuals(model19)
predicted_values19 <- predict(model19)
## Q-Q plot of residuals
qqnorm(residuals19)
qqline(residuals19)
## Scatter plot of residuals against predicted values
plot(predicted_values19, residuals19, xlab = "Predicted values", ylab = "Residuals")
## Box plot of residuals
boxplot(residuals19)
## Histogram of residuals
hist(residuals19, main = "Histogram of Residuals")
# Prediction
## Read the CSV file
predict_summer <- read.csv("C:\\Users\\wodnj\\OneDrive\\바탕 화면\\Data Analysis and Regression\\DSC 423 - Final Project\\Data File\\predict_summer.csv")
## Predict the number of medals
predict_medals <- predict(model19, newdata = predict_summer)
print(predict_medals)