-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeMachineLearningProject.R
More file actions
364 lines (272 loc) · 13.5 KB
/
codeMachineLearningProject.R
File metadata and controls
364 lines (272 loc) · 13.5 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
data<- read.csv('/Users/apple/Destop/speeddating_cleanedd.csv',sep = ';')
head(data)
# Iterate through the last three columns and convert b'1' to 1 and b'0' to 0
for (i in (ncol(data) - 2):ncol(data)) {
data[, i] <- ifelse(data[, i] == "b'1'", '1', ifelse(data[, i] == "b'0'", '0', data[, i]))
}
data <- data[, -1]
for (i in 1:nrow(data)) {
data[i, 1] <- ifelse(data[i, 1] == "b'male'", 'M', ifelse(data[i, 1] == "b'female'", 'F', data[i, 1]))
}
for (i in 1:nrow(data)) {
data[i, 3] <- ifelse(data[i, 3] == "b'1'", '1', ifelse(data[i, 3] == "b'0'", "0", data[i, 3]))
}
data_new<- na.omit(data)
data_new$decision<- as.factor(data_new$decision)
data_new$gender<- as.factor(data_new$gender)
data_new$decision_o<- as.factor(data_new$decision_o)
data_new$match<- as.factor(data_new$match)
data_new$samerace<- as.factor(data_new$samerace)
#removing some columns
data_new <- subset(data_new, select = -c(sports, tvsports, exercise, dining, museums, art, hiking, gaming, clubbing, reading, tv, theater, movies, concerts, music, shopping, yoga))
str(data_new)
xtabs(~gender + decision, data=data_new)
data_decision<-subset(data_new, select = -c(decision_o, match))
## general_df is the data set with our chosen variables
general_df<- subset(data_new, select = -c(gender,decision_o, match, pref_o_attractive, pref_o_sincere, pref_o_intelligence, pref_o_funny, pref_o_ambitious, pref_o_shared_interests, attractive_o, sinsere_o, intelligence_o, funny_o, ambitous_o, shared_interests_o))
str(general_df)
model_all<- glm(decision~., data= general_df, family= binomial)
## assumption check
# car::vif(model_all) # Only attractive_important has high VIF! Might want to remove
# Perform Leave-One-Out Cross-Validation (LOOCV)
accuracies <- c()
for (i in 1:nrow(general_df)) {
# Extract validation observation
validation_data <- general_df[i, ]
# Extract train data
train_data <- general_df[-i, ]
# Train your logistic regression model using the training data
model<- glm(formula = decision ~., data = train_data, family = binomial)
# Make prediction on the validation observation
predicted_prob <- predict(model, newdata = validation_data, type = "response")
binary_predictions <- ifelse(predicted_prob >= 0.5, 1, 0)
y_actual<- general_df$decision[i]
accuracies[i] <- ifelse(y_actual == binary_predictions, 1, 0)
}
# Calculate average accuracy across all validation observations
avg_accuracy <- mean(accuracies)
avg_accuracy
## backward regression
MASS::stepAIC(model_all, direction = "backward")
#LOOCV
mses <- numeric(length = nrow(general_df))
accuracies <- c()
# Perform Leave-One-Out Cross-Validation (LOOCV)
for (i in 1:nrow(general_df)) {
# Extract validation observation
validation_data <- general_df[i, ]
# Extract train data
train_data <- general_df[-i, ]
# Train your logistic regression model using the training data
model<- glm(formula = decision ~ importance_same_race + importance_same_religion +
sincere_important + funny_important + shared_interests_important +
attractive + sincere + funny + attractive_partner + sincere_partner +
intelligence_partner + ambition_partner + interests_correlate +
expected_happy_with_sd_people + expected_num_matches + like +
guess_prob_liked + met, family = binomial, data = train_data)
# Make prediction on the validation observation
predicted_prob <- predict(model, newdata = validation_data, type = "response")
binary_predictions <- ifelse(predicted_prob >= 0.5, 1, 0)
y_actual<- general_df$decision[i]
accuracies[i] <- ifelse(y_actual == binary_predictions, 1, 0)
# Calculate MSE for the validation observation
mse <- (as.numeric(validation_data$decision) - predicted_prob)^2
# Store MSE
mses[i] <- mse
}
# Calculate average MSE and accuracies across all validation observations
avg_mse <- mean(mses)
avg_accuracy <- mean(accuracies)
avg_accuracy
#accuracy
model<- glm(formula = decision ~ importance_same_race + importance_same_religion +
sincere_important + funny_important + shared_interests_important +
attractive + sincere + funny + attractive_partner + sincere_partner +
intelligence_partner + ambition_partner + interests_correlate +
expected_happy_with_sd_people + expected_num_matches + like +
guess_prob_liked + met, family = binomial, data = general_df)
y_pred<- predict(model, type="response")
binary_predictions <- ifelse(y_pred >= 0.5, 1, 0)
y_actual<- general_df$decision
table(binary_predictions, y_actual)
accuracy<- (512+352)/(512+352+93+91)
accuracy
##############################
# General; Accuracies
# Before Stepwise: 0.81393
# After Stepwise: 0.81584
women_df <- data_new[data_new$gender == "F", ]
men_df <- data_new[data_new$gender == "M", ]
women_df_decision<- subset(women_df, select = -c(gender,decision_o, match, pref_o_attractive, pref_o_sincere, pref_o_intelligence, pref_o_funny, pref_o_ambitious, pref_o_shared_interests, attractive_o, sinsere_o, intelligence_o, funny_o, ambitous_o, shared_interests_o))
model_all_women<- glm(decision~., data= women_df_decision, family= binomial)
accuracies <- c()
# Perform Leave-One-Out Cross-Validation (LOOCV)
for (i in 1:nrow(women_df_decision)) {
# Extract validation observation
validation_data <- women_df_decision[i, ]
# Extract train data
train_data <- women_df_decision[-i, ]
# Train your logistic regression model using the training data
model<- glm(formula = decision ~., family = binomial,
data = train_data)
# Make prediction on the validation observation
predicted_prob <- predict(model, newdata = validation_data, type = "response")
binary_predictions <- ifelse(predicted_prob >= 0.5, 1, 0)
y_actual<- validation_data$decision
mse <- (as.numeric(validation_data$decision) - predicted_prob)^2
# Store MSE & Accuracies
mses[i] <- mse
accuracies[i] <- ifelse(y_actual == binary_predictions, 1, 0)
}
avg_accuracy <- mean(accuracies)
avg_accuracy
MASS::stepAIC(model_all_women, direction = "backward")
model_women<- glm(formula = decision ~ importance_same_race + attractive_important +
sincere_important + intellicence_important + ambtition_important +
attractive + intelligence + funny + attractive_partner +
sincere_partner + shared_interests_partner + interests_correlate +
expected_happy_with_sd_people + expected_num_interested_in_me +
expected_num_matches + like + guess_prob_liked + met, family = binomial,
data = women_df_decision)
summary(model_women)
#LOOCV
mses <- numeric(length = nrow(women_df_decision))
accuracies <- c()
# Perform Leave-One-Out Cross-Validation (LOOCV)
for (i in 1:nrow(women_df_decision)) {
# Extract validation observation
validation_data <- women_df_decision[i, ]
# Extract train data
train_data <- women_df_decision[-i, ]
# Train your logistic regression model using the training data
model<- glm(formula = decision ~ importance_same_race + attractive_important +
sincere_important + intellicence_important + ambtition_important +
attractive + intelligence + funny + attractive_partner +
sincere_partner + shared_interests_partner + interests_correlate +
expected_happy_with_sd_people + expected_num_interested_in_me +
expected_num_matches + like + guess_prob_liked + met, family = binomial,
data = train_data)
# Make prediction on the validation observation
predicted_prob <- predict(model, newdata = validation_data, type = "response")
binary_predictions <- ifelse(predicted_prob >= 0.5, 1, 0)
y_actual<- validation_data$decision
# Calculate MSE for the validation observation
mse <- (as.numeric(validation_data$decision) - predicted_prob)^2
# Store MSE & accuracies
mses[i] <- mse
accuracies[i] <- ifelse(y_actual == binary_predictions, 1, 0)
}
# Calculate average MSE across all validation observations
avg_mse <- mean(mses)
print("Women: Average MSE, after stepwise")
avg_mse
avg_accuracy <- mean(accuracies)
print("Women: Accuracy, after stepwise")
avg_accuracy
########################
# Women: Accuracies
# Before stepwise: 0.83050
# After stepwise: 0.83804
#accuracy
model_women<- glm(formula = decision ~ importance_same_race + attractive_important +
sincere_important + intellicence_important + ambtition_important +
attractive + intelligence + funny + attractive_partner +
sincere_partner + shared_interests_partner + interests_correlate +
expected_happy_with_sd_people + expected_num_interested_in_me +
expected_num_matches + like + guess_prob_liked + met, family = binomial,
data = women_df_decision)
y_pred<- predict(model_women, type="response")
binary_predictions <- ifelse(y_pred >= 0.5, 1, 0)
y_actual<- women_df_decision$decision
table(binary_predictions, y_actual)
accuracy<- (305+151)/531
accuracy
men_df_decision<- subset(men_df, select = -c(gender,decision_o, match, pref_o_attractive, pref_o_sincere, pref_o_intelligence, pref_o_funny, pref_o_ambitious, pref_o_shared_interests, attractive_o, sinsere_o, intelligence_o, funny_o, ambitous_o, shared_interests_o))
model_all_men<- glm(decision~., data= men_df_decision, family= binomial)
# Perform Leave-One-Out Cross-Validation (LOOCV)
accuracies <- c()
for (i in 1:nrow(men_df_decision)) {
# Extract validation observation
validation_data <- men_df_decision[i, ]
# Extract train data
train_data <- men_df_decision[-i, ]
# Train your logistic regression model using the training data
model<- glm(formula = decision ~., family = binomial, data = train_data)
# Make prediction on the validation observation
predicted_prob <- predict(model, newdata = validation_data, type = "response")
binary_predictions <- ifelse(predicted_prob >= 0.5, 1, 0)
y_actual<- validation_data$decision
accuracies[i] <- ifelse(y_actual == binary_predictions, 1, 0)
# Calculate MSE for the validation observation
mse <- (as.numeric(validation_data$decision) - predicted_prob)^2
# Store MSE
mses[i] <- mse
}
# Calculate average MSE across all validation observations
avg_mse <- mean(mses)
print("Men: Average MSE, before stepwise")
avg_mse
avg_accuracy <- mean(accuracies)
print("Men: Accuracy, before stepwise")
avg_accuracy
MASS::stepAIC(model_all_men, direction = "backward")
model_men<- glm(formula = decision ~ importance_same_race + importance_same_religion +
attractive_important + sincere_important + intellicence_important +
funny_important + shared_interests_important + attractive +
sincere + funny + ambition + attractive_partner + intelligence_partner +
ambition_partner + interests_correlate + expected_happy_with_sd_people +
expected_num_interested_in_me + expected_num_matches + like +
guess_prob_liked + met, family = binomial, data = men_df_decision)
summary(model_men)
#LOOCV
mses <- numeric(length = nrow(women_df_decision))
accuracies <- c()
# Perform Leave-One-Out Cross-Validation (LOOCV)
for (i in 1:nrow(men_df_decision)) {
# Extract validation observation
validation_data <- men_df_decision[i, ]
# Extract train data
train_data <- men_df_decision[-i, ]
# Train your logistic regression model using the training data
model<- glm(formula = decision ~ importance_same_race + importance_same_religion +
attractive_important + sincere_important + intellicence_important +
funny_important + shared_interests_important + attractive +
sincere + funny + ambition + attractive_partner + intelligence_partner +
ambition_partner + interests_correlate + expected_happy_with_sd_people +
expected_num_interested_in_me + expected_num_matches + like +
guess_prob_liked + met, family = binomial, data = train_data)
# Make prediction on the validation observation
predicted_prob <- predict(model, newdata = validation_data, type = "response")
binary_predictions <- ifelse(predicted_prob >= 0.5, 1, 0)
y_actual<- validation_data$decision
accuracies[i] <- ifelse(y_actual == binary_predictions, 1, 0)
# Calculate MSE for the validation observation
mse <- (as.numeric(validation_data$decision) - predicted_prob)^2
# Store MSE
mses[i] <- mse
}
# Calculate average MSE across all validation observations
avg_mse <- mean(mses)
print("Men: Average MSE, after stepwise")
avg_mse
avg_accuracy <- mean(accuracies)
print("Men: Accuracy, after stepwise")
avg_accuracy
######################
# Men; Accuracies
# Before stepwise: 0.83752
# After stepwise: 0.83559
#accuracy
model_men<- glm(formula = decision ~ importance_same_race + importance_same_religion +
attractive_important + sincere_important + intellicence_important +
funny_important + shared_interests_important + attractive +
sincere + funny + ambition + attractive_partner + intelligence_partner +
ambition_partner + interests_correlate + expected_happy_with_sd_people +
expected_num_interested_in_me + expected_num_matches + like +
guess_prob_liked + met, family = binomial, data = men_df_decision)
y_pred<- predict(model_men, type="response")
binary_predictions <- ifelse(y_pred >= 0.5, 1, 0)
y_actual<- men_df_decision$decision
table(binary_predictions, y_actual)
accuracy<-(230+217)/517
accuracy #slightly higher