Repository navigation
Expand file tree
/
Copy pathVideoGameProject-2.R
More file actions
328 lines (217 loc) · 9.71 KB
/
Copy pathVideoGameProject-2.R
File metadata and controls
328 lines (217 loc) · 9.71 KB
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
#working directory has the file in it.
games <- read.csv('games.csv')
#drop nas
games <- drop_na(games)
#make variables as numbers, but setting them as strings first so the function doesnt change the years/ratings.
games$Year_of_Release <- as.numeric(as.character(games[,3]))
games$User_Score <- as.numeric(as.character(games[,13]))
#grabbing everything above 2010, use this to take out 0 values for sales???
newdata <- subset(games, Year_of_Release >= 2010)
newdata <- newdata[-c(733,2045),]
newdata
#these are jsut linear regression.
model1 <- lm(NA_Sales~Platform+Genre+Publisher+ Rating + Developer+ User_Score + Critic_Score, data=newdata)
summary(model1)
model2 <- lm(NA_Sales~Platform+Genre+Publisher + Developer+ User_Score + Critic_Score, data=newdata)
summary(model2)
model3 <- lm(NA_Sales~Platform+Genre+Publisher, data=newdata)
summary(model3)
model4 <- lm(NA_Sales~Platform+Genre+Publisher+Developer, data=newdata)
summary(model4)
#RegSubsets.
set.seed(1)
n = nrow(newdata)
tr = sample(1:n, #Sample indices do be used in training
size = 2194, #Sample will have 5000 observation
replace = FALSE) #Without replacement
null = lm(NA_Sales~1, data=newdata[tr,]) #only has an intercept
full = glm(NA_Sales~Platform+Genre+Publisher+ Rating + Developer+ User_Score + Critic_Score, data=newdata[tr,]) #Has all the selected variables
#Variable selection
regForward = step(null, #The most simple model
scope=formula(full),#Let us analyze all models until the full model
direction="forward", #Adding variables
k=log(length(tr))) #This is BIC
regBack = step(full, #Starting with the full model
direction="backward", #And deleting variables
k=log(length(tr))) #This is BIC
reghybrid = step(null, #The most simple model
scope=formula(full), #The most complicated model
direction="both", #Add or delete variables
k=log(length(tr))) #This is BIC
##
## This is the best regression model for the NA region.
##
best_model <- lm(NA_Sales ~ Critic_Score + User_Score + Platform + Rating, data = newdata)
summary(best_model)
##
## This is the best regression model for the NA region.
##
#Lasso
#Let us scale our model matrix
X <- model.matrix(model1, data=newdata)
library(glmnet)
#Another way of choosing variables: LASSO and RIDGE
Lasso.Fit = glmnet(X[tr,],newdata$NA_Sales[tr],alpha=1)
par(mfrow=c(1,2)) #Plot window: 1 row, 2 columns
#Evaluating LASSO and RIDGE
plot(Lasso.Fit)
CV.L = cv.glmnet(X[tr,], newdata$NA_Sales[tr],alpha=1) #For Lasso
LamL = CV.L$lambda.1se
CV.L$lambda.min
coef.L = predict(CV.L,type="coefficients",s=LamL)
coef.L
lasso_model <- lm(NA_Sales ~ Critic_Score + Developer + Platform + Genre + Publisher,data = newdata)
summary(lasso_model)
#plots
plot(newdata$User_Score,newdata$NA_Sales)
#regression CV.
set.seed(1)
train = sample(1:n, #Sample indices do be used in training
size = 659, #Sample will have 5000 observation
replace = FALSE) #Without replacement
2194*.30
training.set <- newdata[-train,] # Bigger, to give a better chance to our model...
test.set <- newdata[train,] # Smaller, to test our model
#CV Best regsubset model
best_model_cv <- lm(NA_Sales ~ Critic_Score + User_Score + Platform + Rating, data = training.set)
OOS <- predict(best_model_cv, newdata = test.set)
#SofE
sum((OOS-test.set$NA_Sales)^2)
#MSE
mean((OOS-test.set$NA_Sales)^2)
#CV Lasso Model
lasso_model_cv <- lm(NA_Sales ~ Critic_Score + Developer + Platform + Genre + Publisher,data = training.set)
OOS_lasso <- predict(lasso_model_cv, newdata = test.set)
#SofE
sum((OOS_lasso-test.set$NA_Sales)^2)
#MSE
mean((OOS_lasso-test.set$NA_Sales)^2)
### do these plots again
###
###
###
##GGPLOT of NA sales and User_score
ggplot(data = newdata) +
geom_point(mapping = aes(x = User_Score, y = NA_Sales, color = Genre))
#stuff and things
ggplot(newdata, aes(x= User_Score, y= NA_Sales, colour=Genre, label=Name)) +
geom_point(size = 2,alpha = 0.6) +
theme_bw()+
geom_text(aes(label=ifelse(NA_Sales>5,as.character(Name),'')),hjust=0,vjust=0)
#Stuff and things part 2
ggplot(newdata, aes(x= User_Score, y= NA_Sales, colour=Genre, label=Name)) +
geom_point(size = 2, alpha = 0.6) +
theme_bw()+
geom_text(aes(label=ifelse(NA_Sales>5,as.character(Publisher),'')),hjust=0,vjust=0)
#linear regressions for the Europe.
modelEU <- lm(EU_Sales~Platform+Genre+Publisher+ Rating + Developer+ User_Score + Critic_Score, data=newdata)
summary(modelEU)
#RegSubsets.
set.seed(1)
n = nrow(newdata)
tr2 = sample(1:n, #Sample indices do be used in training
size = 2194, #Sample will have 5000 observation
replace = FALSE) #Without replacement
null2 = lm(EU_Sales~1, data=newdata[tr,]) #only has an intercept
full2 = glm(EU_Sales~Platform+Genre+Publisher+ Rating + Developer+ User_Score + Critic_Score, data=newdata[tr2,]) #Has all the selected variables
#Variable selection
regForward = step(null2, #The most simple model
scope=formula(full2),#Let us analyze all models until the full model
direction="forward", #Adding variables
k=log(length(tr2))) #This is BIC
regBack = step(full2, #Starting with the full model
direction="backward", #And deleting variables
k=log(length(tr2))) #This is BIC
reghybrid = step(null2, #The most simple model
scope=formula(full2), #The most complicated model
direction="both", #Add or delete variables
k=log(length(tr2))) #This is BIC
##
## This is the best regression model for the Europe region.
##
best_model_EU <- lm(EU_Sales ~ Critic_Score + User_Score + Platform + Rating, data = newdata)
summary(best_model_EU)
##
## This is the best regression model for the Europe region.
##
#regression CV for EU.
set.seed(1)
train2 = sample(1:n, #Sample indices do be used in training
size = 659, #Sample will have 5000 observation
replace = FALSE) #Without replacement
2194*.30
training.set2 <- newdata[-train2,] # Bigger, to give a better chance to our model...
test.set2 <- newdata[train2,] # Smaller, to test our model
#CV Best regsubset model
best_model_cv_EU <- lm(EU_Sales ~ Critic_Score + User_Score + Platform + Rating, data = training.set2)
OOS2 <- predict(best_model_cv_EU, test.set2)
#SofE
sum((OOS2-test.set$EU_Sales)^2)
#MSE
mean((OOS2-test.set$EU_Sales)^2)
#Linear regression for Japan
modelJP <- lm(JP_Sales~Platform+Genre+Publisher+ Rating + Developer+ User_Score + Critic_Score, data=newdata)
summary(modelJP)
#RegSubsets.
set.seed(1)
n = nrow(newdata)
tr3 = sample(1:n, #Sample indices do be used in training
size = 659, #Sample will have 5000 observation
replace = FALSE) #Without replacement
null3 = lm(JP_Sales~1, data=newdata[tr3,]) #only has an intercept
full3 = glm(JP_Sales~Platform + Genre + Publisher + Rating + Developer + User_Score + Critic_Score, data=newdata[tr3,]) #Has all the selected variables
#Variable selection
regForward = step(null3, #The most simple model
scope=formula(full3),#Let us analyze all models until the full model
direction="forward", #Adding variables
k=log(length(tr3))) #This is BIC
regBack = step(full3, #Starting with the full model
direction="backward", #And deleting variables
k=log(length(tr3))) #This is BIC
reghybrid = step(null3, #The most simple model
scope=formula(full3), #The most complicated model
direction="both", #Add or delete variables
k=log(length(tr3))) #This is BIC
##
## This is the best regression model for the JP region.
##
best_model_JP <- lm(JP_Sales ~ Critic_Score + Platform, data = newdata)
summary(best_model_JP)
##
## This is the best regression model for the JP region.
##
#regression CV for EU.
set.seed(1)
train3 = sample(1:n, #Sample indices do be used in training
size = 659, #Sample will have 5000 observation
replace = FALSE) #Without replacement
2194*.30
training.set3 <- newdata[-train3,] # Bigger, to give a better chance to our model...
test.set3 <- newdata[train3,] # Smaller, to test our model
#CV Best regsubset model
best_model_JP <- lm(JP_Sales ~ Critic_Score + Platform, data = training.set3)
OOS3 <- predict(best_model_JP, newdata = test.set3)
#SofE
sum((OOS3-test.set$JP_Sales)^2)
#MSE
mean((OOS3-test.set$JP_Sales)^2)
#stuff and things
ggplot(newdata, aes(x= User_Score, y= EU_Sales, colour=Genre, label=Name)) +
geom_point(size = 2,alpha = 0.6) +
theme_bw()+
geom_text(aes(label=ifelse(EU_Sales>3.5,as.character(Name),'')),hjust=0,vjust=0)
#Stuff and things part 3
ggplot(newdata, aes(x= User_Score, y= EU_Sales, colour=Genre, label=Name)) +
geom_point(size = 2, alpha = 0.6) +
theme_bw()+
geom_text(aes(label=ifelse(EU_Sales>3.5,as.character(Publisher),'')),hjust=0,vjust=0)
#stuff and things
ggplot(newdata, aes(x= User_Score, y= JP_Sales, colour=Genre, label=Name)) +
geom_point(size = 2,alpha = 0.6) +
theme_bw()+
geom_text(aes(label=ifelse(JP_Sales>2,as.character(Name),'')),hjust=0,vjust=0)
#Stuff and things part 4
ggplot(newdata, aes(x= User_Score, y= JP_Sales, colour=Genre, label=Name)) +
geom_point(size = 2, alpha = 0.6) +
theme_bw()+
geom_text(aes(label=ifelse(JP_Sales>2,as.character(Publisher),'')),hjust=0,vjust=0)