-
Notifications
You must be signed in to change notification settings - Fork 1
/
Twitter_data_results_pred_errors.R
309 lines (192 loc) · 13 KB
/
Twitter_data_results_pred_errors.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
set.seed(005)
setwd("/home/b/benda002/rFiles/bagging/Results_twitter_data/pred")
source("/home/b/benda002/rFiles/bagging/source_functions/testing_fn.R")
source("/home/b/benda002/rFiles/bagging/source_functions/pred_fn.r")
library(ranger)
library(rpart)
library(dplyr)
exact2 <-read.csv("exact2.csv")
exact2$gun_pred <- factor(exact2$gun_pred)
exact2$rep_pred <- factor(exact2$rep_pred)
exact2$male_pred <- factor(exact2$male_pred)
exact2$older_pred <- factor(exact2$older_pred)
exact2$ppp10012 <- as.numeric(exact2$ppp10012)
simDat <- exact2%>%select( ppp10012, tweet_count , older_pred, male_pred, gun_pred , rep_pred)
formula_simDat <- ppp10012 ~ tweet_count + older_pred + male_pred + gun_pred + rep_pred
n <- nrow(simDat)
k = .7
n_train <- floor(.7*n)
M_naive_pred_error <- numeric(8)
M_naive_rf_pred_error <- numeric(8)
M1_pred_error <- numeric(8)
M_rf1_pred_error <- numeric(8)
M2_pred_error <- numeric(8)
Op_M_naive_pred_error <- numeric(8)
Op_M_naive_rf_pred_error <- numeric(8)
Op_M1_pred_error <- numeric(8)
Op_M_rf1_pred_error <- numeric(8)
Op_M2_pred_error <- numeric(8)
Op_mean_M_naive_pred_error <- numeric(8)
Op_mean_M_naive_rf_pred_error <- numeric(8)
Op_mean_M1_pred_error <- numeric(8)
Op_mean_M_rf1_pred_error <- numeric(8)
Op_mean_M2_pred_error <- numeric(8)
pred_error_sum <- as.data.frame(list(M_naive_pred_error = M_naive_pred_error,
M_naive_rf_pred_error = M_naive_rf_pred_error,
M1_pred_error = M1_pred_error,
M_rf1_pred_error = M_rf1_pred_error,
M2_pred_error = M2_pred_error,
Op_M_naive_pred_error=Op_M_naive_pred_error,
Op_M_naive_rf_pred_error = Op_M_naive_rf_pred_error,
Op_M1_pred_error = Op_M1_pred_error,
Op_M_rf1_pred_error = Op_M_rf1_pred_error,
Op_M2_pred_error = Op_M2_pred_error,
Op_mean_M_naive_pred_error = Op_mean_M_naive_pred_error,
Op_mean_M_naive_rf_pred_error = Op_mean_M_naive_rf_pred_error,
Op_mean_M1_pred_error = Op_mean_M1_pred_error,
Op_mean_M_rf1_pred_error = Op_mean_M_rf1_pred_error,
Op_mean_M2_pred_error = Op_mean_M2_pred_error
))
max_iter = 100
for(iter in 1:max_iter){
idx = sample(1:n, n_train,replace = FALSE)
simDat <- model.frame(formula_simDat, simDat)
trainData = simDat[idx,]
testData = simDat[-idx, ]
n_test <- nrow(testData)
Kappa <- c(0,.10,.15,.2,.25,.3,.35,.4)
Alpha <- rep(.5, n_train)
wghts <- rep(1, n_train)
M_naive_sum <- M_naive_rf_sum <- M1_sum <- M_rf1_sum <- M2_sum <- M_op_naive_sum <- M_op_naive_rf_sum <-
M1_op_sum <- M_op_rf1_sum <- M2_op_sum <- M_op_mean_naive_sum <- M_op_mean_naive_rf_sum <-
M1_op_mean_sum <- M_op_mean_rf1_sum <- M2_op_mean_sum <- as.data.frame(
cbind( numeric(n_test), numeric(n_test), numeric(n_test), numeric(n_test),
numeric(n_test),numeric(n_test), numeric(n_test),numeric(n_test)))
simDat <- model.frame(formula_simDat, simDat)
Kappa <- c(0,.10,.15,.2,.25,.3,.35,.4)
Alpha <- rep(.5, n_train)
wghts <- rep(1, n_train)
for(i in 1:8){
kappa <- Kappa[i]
simDat_pdat <- trainData
simDat_pdat$ppp10012 <- y.perm(trainData$ppp10012, kappa)
running_time_Alpha_op <- system.time(Alpha_op <- rep(optimal_Alpha(formula_simDat, data = simDat_pdat)%>%mean(), n_test))
running_time_Alpha_mean <- system.time( Alpha_mean <- rep(optimal_mean_alpha(formula_simDat, data = simDat_pdat), n_test))
running_time_bagging <- system.time( m_naive <- pred_bag_trees(formula_simDat, data = simDat_pdat, newdata = testData, wghts = wghts))
M_naive_pred_error[i] <- mse(m_naive, testData$ppp10012)
M_naive_sum[,i] <- M_naive_sum[,i] + m_naive
running_time_ranger <- system.time( m_naive_rf <- predict(ranger(formula_simDat, simDat_pdat), data = testData)$predictions)
M_naive_rf_pred_error[i] <- mse(m_naive_rf, testData$ppp10012)
M_naive_rf_sum[, i] = M_naive_rf_sum[, i] + m_naive_rf
running_time_adj1 <- system.time(m1 <- pred_adj_bag_trees1(formula_simDat, data = simDat_pdat, newdata = testData, ntrees=100, wghts,
Alpha))
M1_pred_error[i] <- mse(m1, testData$ppp10012)
M1_sum[, i] = M1_sum[, i] + m1
running_time_adj_ranger <- system.time( m_rf1 <- pred_adj_rf1(formula_simDat, data = simDat_pdat, newdata = testData, Alpha))
M_rf1_pred_error[i] <- mse(m_rf1, testData$ppp10012)
M_rf1_sum[, i] = M_rf1_sum[, i] + m_rf1
running_time_adj2 <- system.time( m2 <- pred_adj_bag_trees2(formula_simDat, data = simDat_pdat,newdata = testData, ntrees=100, wghts, Alpha))
M2_pred_error[i] <- mse(m2, testData$ppp10012)
M2_sum[, i] = M2_sum[, i] + m2
# Alpha adjustment using Alpha = Optimal Alpha
running_time_alpha_adj <- system.time(Op_m_naive <- adj_alpha_method(formula_simDat, data = testData, Alpha = Alpha_op,
mu_ytildex = m_naive))
Op_M_naive_pred_error[i] <- mse( Op_m_naive, testData$ppp10012)
M_op_naive_sum[, i] = M_op_naive_sum[, i] + Op_m_naive
Op_m_naive_rf <- adj_alpha_method(formula_simDat, data = testData,
Alpha = Alpha_op, mu_ytildex = m_naive_rf)
Op_M_naive_rf_pred_error[i] <- mse( Op_m_naive_rf, testData$ppp10012)
M_op_naive_rf_sum[, i] = M_op_naive_rf_sum[, i] + Op_m_naive_rf
Op_m1 <- adj_alpha_method(formula_simDat,data = testData, Alpha = Alpha_op, mu_ytildex = m1)
Op_M1_pred_error[i] <- mse( Op_m1, testData$ppp10012)
M1_op_sum[, i] = M1_op_sum[, i] + Op_m1
Op_m_rf1 <- adj_alpha_method(formula_simDat, data = testData,
Alpha = Alpha_op, mu_ytildex = m_rf1)
Op_M_rf1_pred_error[i] <- mse( Op_m_rf1, testData$ppp10012)
M_op_rf1_sum[, i] = M_op_rf1_sum[, i] + Op_m_rf1
Op_m2 <- adj_alpha_method(formula_simDat, data = testData,
Alpha = Alpha_op, mu_ytildex = m2)
Op_M2_pred_error[i] <- mse( Op_m2, testData$ppp10012)
M2_op_mean_sum[, i] = M2_op_mean_sum[, i] + Op_m2
# Alpha adjustment method using alpha_mean_optimal
Op_mean_m_naive <- adj_alpha_method(formula_simDat, data = testData, Alpha = Alpha_mean,
mu_ytildex = m_naive)
Op_mean_M_naive_pred_error[i] <- mse( Op_mean_m_naive, testData$ppp10012)
M_op_mean_naive_sum[, i] = M_op_mean_naive_sum[, i] + Op_mean_m_naive
Op_mean_m_naive_rf <- adj_alpha_method(formula_simDat, data = testData,
Alpha = Alpha_mean, mu_ytildex = m_naive_rf)
Op_mean_M_naive_rf_pred_error[i] <- mse( Op_mean_m_naive_rf, testData$ppp10012)
M_op_mean_rf1_sum[, i] = M_op_mean_rf1_sum[, i] + Op_mean_m_naive_rf
Op_mean_m1 <- adj_alpha_method(formula_simDat,data = testData,
Alpha = Alpha_mean, mu_ytildex = m1)
Op_mean_M1_pred_error[i] <- mse( Op_mean_m1, testData$ppp10012)
M1_op_mean_sum[, i] = M1_op_mean_sum[, i] + Op_mean_m1
Op_mean_m_rf1 <- adj_alpha_method(formula_simDat, data = testData,
Alpha = Alpha_mean, mu_ytildex = m_rf1)
Op_mean_M_rf1_pred_error[i] <- mse( Op_mean_m_rf1, testData$ppp10012)
M_op_mean_rf1_sum[, i] = M_op_mean_rf1_sum[, i] + Op_mean_m_rf1
Op_mean_m2 <- adj_alpha_method(formula_simDat, data = testData,
Alpha = Alpha_mean, mu_ytildex = m2)
Op_mean_M2_pred_error[i] <- mse(Op_mean_m2, testData$ppp10012)
M2_op_mean_sum[, i] = M2_op_mean_sum[, i] + Op_mean_m2
}
table_simDat_pred_error <- as.data.frame(list(M_naive_pred_error = M_naive_pred_error,
M_naive_rf_pred_error = M_naive_rf_pred_error,
M1_pred_error = M1_pred_error,
M_rf1_pred_error = M_rf1_pred_error,
M2_pred_error = M2_pred_error,
Op_M_naive_pred_error=Op_M_naive_pred_error,
Op_M_naive_rf_pred_error = Op_M_naive_rf_pred_error,
Op_M1_pred_error = Op_M1_pred_error,
Op_M_rf1_pred_error = Op_M_rf1_pred_error,
Op_M2_pred_error = Op_M2_pred_error,
Op_mean_M_naive_pred_error = Op_mean_M_naive_pred_error,
Op_mean_M_naive_rf_pred_error = Op_mean_M_naive_rf_pred_error,
Op_mean_M1_pred_error = Op_mean_M1_pred_error,
Op_mean_M_rf1_pred_error = Op_mean_M_rf1_pred_error,
Op_mean_M2_pred_error = Op_mean_M2_pred_error))
pred_error_sum <- table_simDat_pred_error + pred_error_sum
}
table_simDat_pred_error <- pred_error_sum/max_iter
table_M_naive_predictions <- M_naive_sum/max_iter
table_M_naive_rf_predictions <- M_naive_rf_sum/max_iter
table_M1_predictions <- M1_sum/max_iter
table_M2_predictions <- M2_sum/max_iter
table_M_rf1_predictions <- M_rf1_sum/max_iter
table_M_op_naive_predictions <- M_op_naive_sum/max_iter
table_M_op_naive_rf_predictions <- M_op_naive_rf_sum/max_iter
table_M1_op_predictions <- M1_op_sum/max_iter
table_M2_op_predictions <- M2_op_sum/max_iter
table_M_op_rf1_predictions <- M_op_rf1_sum/max_iter
table_M_op_mean_naive_predictions <- M_op_mean_naive_sum/max_iter
table_M_op_mean_naive_rf_predictions <- M_op_mean_naive_rf_sum/max_iter
table_M1_op_mean_predictions <- M1_op_mean_sum/max_iter
table_M2_op_mean_predictions <- M2_op_mean_sum/max_iter
table_M_op_mean_rf1_predictions <- M_op_mean_rf1_sum/max_iter
write.csv(table_M_naive_predictions, "table_M_naive_predictions_50_replicates.csv")
write.csv(table_M_naive_rf_predictions,"table_M_naive_rf_predictions_50_replicates.csv")
write.csv(table_M1_predictions,"table_M1_predictions_50_replicates.csv")
write.csv(table_M2_predictions,"table_M2_predictions_50_replicates.csv")
write.csv(table_M_rf1_predictions,"table_M_rf1_predictions_50_replicates.csv")
write.csv(table_M_op_naive_predictions, "table_M_op_naive_predictions_50_replicates.csv")
write.csv(table_M_op_naive_rf_predictions,"table_M_op_naive_rf_predictions_50_replicates.csv")
write.csv(table_M1_op_predictions,"table_M1_op_predictions_50_replicates.csv")
write.csv(table_M2_op_predictions,"table_M2_op_predictions_50_replicates.csv")
write.csv(table_M_op_rf1_predictions,"table_M_op_rf1_predictions_50_replicates.csv")
write.csv(table_M_op_mean_naive_predictions, "table_M_op_mean_naive_predictions_50_replicates.csv")
write.csv(table_M_op_mean_naive_rf_predictions,"table_M_op_mean_naive_rf_predictions_50_replicates.csv")
write.csv(table_M1_op_mean_predictions,"table_M1_op_mean_predictions_50_replicates.csv")
write.csv(table_M2_op_mean_predictions,"table_M2_op_mean_predictions_50_replicates.csv")
write.csv(table_M_op_mean_rf1_predictions,"table_M_op_mean_rf1_predictions_50_replicates.csv")
table_simDat_pred_error <- apply(table_simDat_pred_error,1, round, 2)
running_time <- as.data.frame(list( running_time_Alpha_op = running_time_Alpha_op[3],
running_time_Alpha_mean = running_time_Alpha_mean[3],
running_time_bagging = running_time_bagging[3],
running_time_ranger = running_time_ranger[3],
running_time_adj1 = running_time_adj1[3],
running_time_adj_ranger = running_time_adj_ranger[3],
running_time_adj2 = running_time_adj2[3],
running_time_alpha_adj = running_time_alpha_adj[3]
))
write.csv(table_simDat_pred_error,"table_simDat_pred_error_50_new.csv")
write.csv(running_time, "running_time_simData_50_new.csv")