-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.Rhistory
512 lines (512 loc) · 26.1 KB
/
.Rhistory
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
install.packages("DEoptimR")
install.packages("DEoptimR")
## Automatic Feature Selection
# control using random forest
control <- rfeControl(functions = rfFuncs, method="cv", number=5)
results <- rfe(finalCombined[,2:53], finalCombined[,56], sizes = c(1:52), rfeControl = control) # this will take AWHILE
results
ggplot(results)
# chosen features
predictors(results)
selected_features <- predictors(results)[1:5]
selected_features <- predictors(results)[1:10]
selected_features
control <- trainControl(method="repeatedcv", number = 5, repeats = 3)
model <- train(idnum ~., data=finalCombined, method = "knn", trControl = control)
importance <- varImp(model)
ggplot(importance)
View(finalCombined)
lenth(finalCombined)
length(finalCombined)
corr_matrix <- cor(finalCombined[,2:length(finalCombined)]) # correlations between all predictor vars
length(finalCombined)
typeof(length(finalCombined))
corr_matrix <- cor(finalCombined[,2:56) # correlations between all predictor vars
corr_matrix <- cor(finalCombined[,2:56]) # correlations between all predictor vars
?cor
finalCombNumeric <- finalCombined[, !names(finalCombined) %in% c("chas")] # removing charles river tract b/c not numeric
head(finalCombNumeric)
finalCombNumeric <- filter(finalCombNumeric, finalCombNumeric[,1:length(finalCombNumeric)] >= 0)
finalCombNumeric <- filter(finalCombNumeric, finalCombNumeric[,1:length(finalCombNumeric)] > -1)
new <- finalCombNumeric %>% filter(finalCombNumeric$m1a4 >= 0)
new <- finalCombNumeric %>% filter(finalCombNumeric$m1a4 >= 5)
new <- finalCombNumeric %>% filter(finalCombNumeric$m1a4 >= 0)
corr_matrix <- cor(finalCombNumeric[,2:56])
finalCombNumeric <- finalCombined[, names(finalCombined) %in% c("numeric")]
selected_features
ggplot(importance)
importance
predictors
top4 <- importance[1:4]
importance
importance[,0]
importance[,1]
importance[2,]
View(importance)
?varImp
glmnet_grid <- expand.grid(alpha = c(0, .1, .2, .4, .6, .8, 1),
lambda = c(1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1,1))
glmnet_ctrl <- trainControl(method = "cv", number = 10)
glmnet_fit <- train(medv ~ ., data = finalCombined[, c(selected_features, "medv")],
method = "glmnet",
preProcess = c("center", "scale"),
tuneGrid = glmnet_grid,
trControl = glmnet_ctrl)
baseline <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Baseline_Features.csv",header=TRUE,sep=",")
year1 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year1_Features.csv", header=TRUE,sep=",")
year3 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year3_Features.csv",header=TRUE,sep=",")
year5 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year5_Features.csv",header=TRUE,sep=",")
year9 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year9_Features.csv",header=TRUE,sep=",")
#View(year9)
combine0 <- baseline %>% select(idnum, m1a4, m1a11a, m1a11b) %>%
filter(idnum>-1, m1a4>-1, m1a11a>-1, m1a11b>-1)
combine1 <- year1 %>% select(idnum,m2b18a,m2b18b,m2b18c,m2b18d,m2b18e,m2b18f,m2b18g,m2b18h,m2c3a,m2c3b, m2c3c,m2c3d,m2c3e,m2c3f,m2c3h,m2c3i,m2c3j) %>% filter(m2b18a>-1,m2b18b>-1,m2b18c>-1,m2b18d>-1,m2b18e>-1,m2b18f>-1,m2b18g>-1,m2b18h>-1,m2c3a>-1,m2c3b>-1, m2c3c>-1,m2c3d>-1,m2c3e>-1,m2c3f>-1,m2c3h>-1,m2c3i>-1,m2c3j>-1)
combine3 <- year3 %>% select(idnum,m3b4a,m3b4b,m3b4c,m3b4d,m3b4e,m3b4f,m3b4g,m3b4h,m3b4i,m3b4k,m3b4l,m3b5,m3c3a,m3c3b,m3c3c,m3c3d,m3c3e,m3c3f,m3c3g,m3c3h,m3c3i,m3c3j,m3c3k,m3c3l) %>%
filter(m3b4a>-1,m3b4b>-1,m3b4c>-1,m3b4d>-1,m3b4e>-1,m3b4f>-1,m3b4g>-1,m3b4h>-1,m3b4i>-1,m3b4k>-1,m3b4l>-1,m3b5>-1,m3c3a>-1,m3c3b>-1,m3c3c>-1,m3c3d>-1,m3c3e>-1,m3c3f>-1,m3c3g>-1,m3c3h>-1,m3c3i>-1,m3c3j>-1,m3c3k>-1,m3c3l>-1)
combine5 <- year5 %>% select(idnum,m4b4a1,m4b4a2,m4b4a3,m4b4a4,m4b4a5,m4b4a6,m4b4a7,m4b4a8) %>%
filter(m4b4a1>-1,m4b4a2>-1,m4b4a3>-1,m4b4a4>-1,m4b4a5>-1,m4b4a6>-1,m4b4a7>-1,m4b4a8>-1)
combine9 <- year9 %>% select(idnum,t5c13a,t5c13b,t5c13c) %>%
filter(t5c13a>-1,t5c13b>-1,t5c13c>-1)
join1 <- inner_join(combine0,combine1, by="idnum")
join2 <- inner_join(join1,combine3, by="idnum")
join3 <- inner_join(join2,combine5, by="idnum")
finalCombined <- inner_join(join3,combine9, by="idnum")
# add outcome variables
finalCombined$t5c13a <- as.factor(finalCombined$t5c13a)
finalCombined$t5c13b <- as.factor(finalCombined$t5c13b)
finalCombined$t5c13c <- as.factor(finalCombined$t5c13c)
## Automatic Feature Selection
# control using random forest
control <- rfeControl(functions = rfFuncs, method="cv", number=5)
results <- rfe(finalCombined[,2:53], finalCombined[,56], sizes = c(1:52), rfeControl = control) # this will take AWHILE
results
ggplot(results)
# chosen features
predictors(results)
if(!require(mlbench)){install.packages("mlbench"); require(mlbench)} # common datasets to use
# some dependencies for caret that aren't automatically installed
if(!require(ModelMetrics)){install.packages("ModelMetrics"); require(ModelMetrics)}
if(!require(recipes)){install.packages("recipes"); require(recipes)}
if(!require(DEoptimR)){install.packages("DEoptimR"); require(DEoptimR)}
if(!require(caret)){install.packages("caret", dependencies = c("Depends", "Suggests")); require(caret)} # ML package and its dependencies. This will take awhile!
if(!require(dplyr)){install.packages("dplyr"); require(dplyr)}
set.seed(370)
install.packages("recipes")
data(BostonHousing)
?BostonHousing
df_boston <- BostonHousing[, !names(BostonHousing) %in% c("chas")] # removing charles river tract b/c not numeric
head(df_boston)
cutoff <- 0.6 # should be higher in practice
if(!require(mlbench)){install.packages("mlbench"); require(mlbench)} # common datasets to use
if(!require(caret)){install.packages("caret", dependencies = c("Depends", "Suggests")); require(caret)} # ML package and its dependencies. This will take awhile!
if(!require(dplyr)){install.packages("dplyr"); require(dplyr)}
set.seed(370)
data(BostonHousing)
?BostonHousing
df_boston <- BostonHousing[, !names(BostonHousing) %in% c("chas")] # removing charles river tract b/c not numeric
head(df_boston)
corr_matrix <- cor(df_boston[,1:12]) # correlations between all predictor vars
corr_matrix
cutoff <- 0.6 # should be higher in practice
highly_corr <- findCorrelation(corr_matrix, cutoff=cutoff)
print(colnames(df_boston)[highly_corr]) # age is highly correalted with pregnant
selected_features <- predictors(results)[1:5]
selected_features <- predictors(results)[1:10]
selected_features
predictors
predictors(results)
results
finalCombined
results
predictors(results)
control <- trainControl(method="repeatedcv", number = 10, repeats = 3)
model <- train(medv ~., data=df_boston, method = "knn", trControl = control)
importance <- varImp(model)
ggplot(importance)
# control using random forest
control <- rfeControl(functions = rfFuncs, method="cv", number=10)
results <- rfe(df_boston[,1:12], df_boston[,13], sizes = c(1:12), rfeControl = control) # this will take AWHILE...
results
ggplot(results)
# chosen features
predictors(results)
selected_features <- predictors(results)[1:6] #TODO
selected_features
glmnet_grid <- expand.grid(alpha = c(0, .1, .2, .4, .6, .8, 1),
lambda = c(1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1,1))
glmnet_ctrl <- trainControl(method = "cv", number = 10)
glmnet_fit <- train(medv ~ ., data = df_boston[, c(selected_features, "medv")],
method = "glmnet",
preProcess = c("center", "scale"),
tuneGrid = glmnet_grid,
trControl = glmnet_ctrl)
glmnet_fit
df_boston_all <- BostonHousing
plr_grid <- expand.grid(lambda = c(1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1,1, 1e2, 1e3, 1e4, 1e5), cp = c("bic", "aic"))
plr_ctrl <- trainControl(method = "cv", number = 10)
plr_fit <- train(chas ~ ., data = df_boston_all,
method = "plr",
preProcess = c("center", "scale"),
tuneGrid = plr_grid,
trControl = plr_ctrl)
plr_fit
baseline <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Baseline_Features.csv",header=TRUE,sep=",")
year1 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year1_Features.csv", header=TRUE,sep=",")
year3 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year3_Features.csv",header=TRUE,sep=",")
year5 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year5_Features.csv",header=TRUE,sep=",")
year9 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year9_Features.csv",header=TRUE,sep=",")
#View(year9)
combine0 <- baseline %>% select(idnum, m1a4, m1a11a, m1a11b) %>%
filter(idnum>-1, m1a4>-1, m1a11a>-1, m1a11b>-1)
combine1 <- year1 %>% select(idnum,m2b18a,m2b18b,m2b18c,m2b18d,m2b18e,m2b18f,m2b18g,m2b18h,m2c3a,m2c3b, m2c3c,m2c3d,m2c3e,m2c3f,m2c3h,m2c3i,m2c3j) %>% filter(m2b18a>-1,m2b18b>-1,m2b18c>-1,m2b18d>-1,m2b18e>-1,m2b18f>-1,m2b18g>-1,m2b18h>-1,m2c3a>-1,m2c3b>-1, m2c3c>-1,m2c3d>-1,m2c3e>-1,m2c3f>-1,m2c3h>-1,m2c3i>-1,m2c3j>-1)
combine3 <- year3 %>% select(idnum,m3b4a,m3b4b,m3b4c,m3b4d,m3b4e,m3b4f,m3b4g,m3b4h,m3b4i,m3b4k,m3b4l,m3b5,m3c3a,m3c3b,m3c3c,m3c3d,m3c3e,m3c3f,m3c3g,m3c3h,m3c3i,m3c3j,m3c3k,m3c3l) %>%
filter(m3b4a>-1,m3b4b>-1,m3b4c>-1,m3b4d>-1,m3b4e>-1,m3b4f>-1,m3b4g>-1,m3b4h>-1,m3b4i>-1,m3b4k>-1,m3b4l>-1,m3b5>-1,m3c3a>-1,m3c3b>-1,m3c3c>-1,m3c3d>-1,m3c3e>-1,m3c3f>-1,m3c3g>-1,m3c3h>-1,m3c3i>-1,m3c3j>-1,m3c3k>-1,m3c3l>-1)
combine5 <- year5 %>% select(idnum,m4b4a1,m4b4a2,m4b4a3,m4b4a4,m4b4a5,m4b4a6,m4b4a7,m4b4a8) %>%
filter(m4b4a1>-1,m4b4a2>-1,m4b4a3>-1,m4b4a4>-1,m4b4a5>-1,m4b4a6>-1,m4b4a7>-1,m4b4a8>-1)
combine9 <- year9 %>% select(idnum,t5c13a,t5c13b,t5c13c) %>%
filter(t5c13a>-1,t5c13b>-1,t5c13c>-1)
join1 <- inner_join(combine0,combine1, by="idnum")
join2 <- inner_join(join1,combine3, by="idnum")
join3 <- inner_join(join2,combine5, by="idnum")
finalCombined <- inner_join(join3,combine9, by="idnum")
# add outcome variables
finalCombined$t5c13a <- as.factor(finalCombined$t5c13a)
finalCombined$t5c13b <- as.factor(finalCombined$t5c13b)
finalCombined$t5c13c <- as.factor(finalCombined$t5c13c)
#Jared trying to do a correlation
View(finalCombined)
finalCombNumeric <- finalCombined[, names(finalCombined) %in% c("numeric")] # removing charles river tract b/c not numeric
head(finalCombNumeric)
finalCombNumeric <- filter(finalCombNumeric)
new <- finalCombNumeric %>% filter(finalCombNumeric$m1a4 >= 0)
baseline <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Baseline_Features.csv",header=TRUE,sep=",")
year1 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year1_Features.csv", header=TRUE,sep=",")
year3 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year3_Features.csv",header=TRUE,sep=",")
year5 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year5_Features.csv",header=TRUE,sep=",")
year9 <- read.csv(file="/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/Year9_Features.csv",header=TRUE,sep=",")
#View(year9)
combine0 <- baseline %>% select(idnum, m1a4, m1a11a, m1a11b) %>%
filter(idnum>-1, m1a4>-1, m1a11a>-1, m1a11b>-1)
combine1 <- year1 %>% select(idnum,m2b18a,m2b18b,m2b18c,m2b18d,m2b18e,m2b18f,m2b18g,m2b18h,m2c3a,m2c3b, m2c3c,m2c3d,m2c3e,m2c3f,m2c3h,m2c3i,m2c3j) %>% filter(m2b18a>-1,m2b18b>-1,m2b18c>-1,m2b18d>-1,m2b18e>-1,m2b18f>-1,m2b18g>-1,m2b18h>-1,m2c3a>-1,m2c3b>-1, m2c3c>-1,m2c3d>-1,m2c3e>-1,m2c3f>-1,m2c3h>-1,m2c3i>-1,m2c3j>-1)
combine3 <- year3 %>% select(idnum,m3b4a,m3b4b,m3b4c,m3b4d,m3b4e,m3b4f,m3b4g,m3b4h,m3b4i,m3b4k,m3b4l,m3b5,m3c3a,m3c3b,m3c3c,m3c3d,m3c3e,m3c3f,m3c3g,m3c3h,m3c3i,m3c3j,m3c3k,m3c3l) %>%
filter(m3b4a>-1,m3b4b>-1,m3b4c>-1,m3b4d>-1,m3b4e>-1,m3b4f>-1,m3b4g>-1,m3b4h>-1,m3b4i>-1,m3b4k>-1,m3b4l>-1,m3b5>-1,m3c3a>-1,m3c3b>-1,m3c3c>-1,m3c3d>-1,m3c3e>-1,m3c3f>-1,m3c3g>-1,m3c3h>-1,m3c3i>-1,m3c3j>-1,m3c3k>-1,m3c3l>-1)
combine5 <- year5 %>% select(idnum,m4b4a1,m4b4a2,m4b4a3,m4b4a4,m4b4a5,m4b4a6,m4b4a7,m4b4a8) %>%
filter(m4b4a1>-1,m4b4a2>-1,m4b4a3>-1,m4b4a4>-1,m4b4a5>-1,m4b4a6>-1,m4b4a7>-1,m4b4a8>-1)
combine9 <- year9 %>% select(idnum,t5c13a,t5c13b,t5c13c) %>%
filter(t5c13a>-1,t5c13b>-1,t5c13c>-1)
join1 <- inner_join(combine0,combine1, by="idnum")
join2 <- inner_join(join1,combine3, by="idnum")
join3 <- inner_join(join2,combine5, by="idnum")
finalCombined <- inner_join(join3,combine9, by="idnum")
# add outcome variables
finalCombined$t5c13a <- as.factor(finalCombined$t5c13a)
finalCombined$t5c13b <- as.factor(finalCombined$t5c13b)
finalCombined$t5c13c <- as.factor(finalCombined$t5c13c)
## Automatic Feature Selection
# control using random forest
control <- rfeControl(functions = rfFuncs, method="cv", number=5)
results <- rfe(finalCombined[,2:53], finalCombined[,56], sizes = c(1:52), rfeControl = control) # this will take AWHILE
results
ggplot(results)
# chosen features
predictors(results)
selected_features <- predictors(results)[1:5]
selected_features <- predictors(results)[1:10]
selected_features
glmnet_grid <- expand.grid(alpha = c(0, .1, .2, .4, .6, .8, 1),
lambda = c(1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1,1))
glmnet_ctrl <- trainControl(method = "cv", number = 10)
glmnet_fit <- train(t5c13c ~ ., data = finalCombined[, c(selected_features, "t5c13c")],
method = "glmnet",
preProcess = c("center", "scale"),
tuneGrid = glmnet_grid,
trControl = glmnet_ctrl)
glmnet_fit
control <- trainControl(method="repeatedcv", number = 5, repeats = 3)
model <- train(idnum ~., data=finalCombined, method = "knn", trControl = control)
importance <- varImp(model)
ggplot(importance)
selected_features_1 <- c("m1a11b","m3b4d","m3c3j","m3b4a","m3c3i","m3b4h")
selected_features_2 <- c("m3c3e","m4b4a1","m3b4h","m4b4a5")
glmnet_fit_1 <- train(t5c13c ~ ., data = finalCombined[, c(selected_features_1, "t5c13c")],
method = "glmnet",
preProcess = c("center", "scale"),
tuneGrid = glmnet_grid,
trControl = glmnet_ctrl)
glmnet_fit_1
glmnet_fit_2 <- train(t5c13c ~ ., data = finalCombined[, c(selected_features_2, "t5c13c")],
method = "glmnet",
preProcess = c("center", "scale"),
tuneGrid = glmnet_grid,
trControl = glmnet_ctrl)
glmnet_fit_2
split_proportion = 0.8 # specify proportion of data used for training
# select outcome variable
outcome <- finalCombined %>% dplyr::select(t5c13c)
# randomly select indices for train/validate set
train_ind <- createDataPartition(outcome$t5c13c, p = split_proportion, list = FALSE)
finalCombined_train <- finalCombined[train_ind,] # get training data
finalCombined_test <- finalCombined[-train_ind,] # get test data
finalCombined_x <- finalCombined %>% dplyr::select(-t5c13c) # select predictor data for test set
finalCombined_y <- finalCombined %>% dplyr::select(t5c13c) # select outcome data for test set
model_lm <- train(t5c13c ~ ., # outcome is "medv", all other columns are predictors
data = finalCombined_train, # training data
method = "lm", # model type (linear model)
trControl=ctrl)
?train
model_lm <- train(t5c13c ~ ., # outcome is "medv", all other columns are predictors
data = finalCombined_train, # training data
method = "lm", # model type (linear model)
trControl=ctrl)
finalCombined_train
names(getModelInfo())
model_lm <- train(t5c13c ~ ., # outcome is "medv", all other columns are predictors
data = finalCombined_train, # training data
method = "knn", # model type (linear model)
trControl=ctrl)
ctrl <- trainControl(method = "cv", number=5)
model_lm <- train(t5c13c ~ ., # outcome is "medv", all other columns are predictors
data = finalCombined_train, # training data
method = "knn", # model type (linear model)
trControl=ctrl) # evaluation method
model_lm$finalModel
model_lm
model_lm$finalModel
predict_finalCombined_knn <- predict(model_knn, finalCombined_x)
model_knn <- model_lm
predict_finalCombined_knn <- predict(model_knn, finalCombined_x)
postResample(predict_finalCombined_knn, finalCombined_y$t5c13c)
grid <- finalCombined_test %>%
gather_predictions(model_knn)
library(modelr)
# creating grid of data to plot results for test set
grid <- finalCombined_test %>%
gather_predictions(model_knn)
varImp(model_knn)
?aes
varImp(model_knn)
ggplot(finalCombined_test) +
geom_point() +
geom_line(data = grid, aes(y = t5c13c))
ggplot(finalCombined_test)
ggplot(finalCombined_test) +
geom_point()
if(!require(klaR)){install.packages("klaR"); require(klaR)} # only need this is dependencies of caret were not installed
model_nb <- train(t5c13c ~ . , method="nb", data=finalCombined_train, trControl = ctrl)
predict_math <- predict(model_nb, finalCombined_x)
ggplot(importance)
glmnet_fit_1
glmnet_fit_2
confusionMatrix(predict_math, finalCombined_y$t5c13c)
model_nb <- train(t5c13c ~ . , method="nb", data=finalCombined_train, trControl = ctrl)
model_nb
predict_math <- predict(model_nb, finalCombined_x)
predict_math
confusionMatrix(predict_math, finalCombined_y$t5c13c)
finalCombined_y <- finalCombined_test %>% dplyr::select(t5c13c) # select outcome data for test set
finalCombined_x <- finalCombined_test %>% dplyr::select(-t5c13c) # select predictor data for test set
predict_math <- predict(model_nb, finalCombined_x)
# performance
confusionMatrix(predict_math, finalCombined_y$t5c13c)
predict_math
finalCombined_y$t5c13c
?confusionMatrix
caret::confusionMatrix(predict_math, finalCombined_y$t5c13c)
fullSet <- inner_join(fullSet, year3)
fullSet <- inner_join(baseline, year1)
fullSet <- inner_join(fullSet, year3)
fullSet <- inner_join(fullSet, year5)
fullSet <- inner_join(fullSet, year9)
fullSet$t5c13a <- as.factor(fullSet$t5c13a)
fullSet$t5c13b <- as.factor(fullSet$t5c13b)
fullSet$t5c13c <- as.factor(fullSet$t5c13c)
outcome1 <- fullSet %>% dplyr::select(t5c13c)
train_ind1 <- createDataPartition(outcome1$t5c13c, p = split_proportion, list = FALSE)
fullSet_train <- fullSet[train_ind1,] # get training data
fullSet_test <- fullSet[-train_ind1,] # get test data
fullSet_x <- fullSet_test %>% dplyr::select(-t5c13c) # select predictor data for test set
fullSet_y <- fullSet_test %>% dplyr::select(t5c13c) # select outcome data for test set
ctrl <- trainControl(method = "cv", number=5)
model2_nb <- train(t5c13c ~ . , method="nb", data=fullSet_train, trControl = ctrl)
predict_math1 <- predict(model2_nb, fullSet_x)
model2_nb
model2_nb
model2_nb <- train(t5c13c ~ . , method="nb", data=fullSet_train, trControl = ctrl)
View(fullSet)
#Split data into train and test set
# splitting boston data into train+validate and test sets
split_proportion = 0.8 # specify proportion of data used for training
# select outcome variable
outcome <- finalCombined %>% dplyr::select(t5c13c)
# randomly select indices for train/validate set
train_ind <- createDataPartition(outcome$t5c13c, p = split_proportion, list = FALSE)
finalCombined_train <- finalCombined[train_ind,] # get training data
finalCombined_test <- finalCombined[-train_ind,] # get test data
finalCombined_x <- finalCombined_test %>% dplyr::select(-t5c13c) # select predictor data for test set
finalCombined_y <- finalCombined_test %>% dplyr::select(t5c13c) # select outcome data for test set
#################
if(!require(klaR)){install.packages("klaR"); require(klaR)} # only need this is dependencies of caret were not installed
# fit model
ctrl <- trainControl(method = "cv", number=5)
model_nb <- train(t5c13c ~ . , method="nb", data=finalCombined_train, trControl = ctrl)
# predictions using model
predict_math <- predict(model_nb, finalCombined_x)
# performance
caret::confusionMatrix(predict_math, finalCombined_y$t5c13c)
fullSet[fullSet < 0] <- NA
control <- rfeControl(functions = rfFuncs, method="cv", number=5)
results <- rfe(finalCombined[,2:53], finalCombined[,56], sizes = c(1:52), rfeControl = control) # this will take AWHILE
results
ggplot(results)
fullSet <- inner_join(baseline, year1)
fullSet <- inner_join(fullSet, year3)
fullSet <- inner_join(fullSet, year5)
fullSet <- inner_join(fullSet, year9)
fullSet$t5c13a <- as.factor(fullSet$t5c13a)
fullSet$t5c13b <- as.factor(fullSet$t5c13b)
fullSet$t5c13c <- as.factor(fullSet$t5c13c)
fullSet <- fullSet %>% filter(t5c13a>0, t5c13b>0, t5c13c>0)
fullSet$t5c13a <- as.factor(fullSet$t5c13a)
fullSet$t5c13b <- as.factor(fullSet$t5c13b)
fullSet$t5c13c <- as.factor(fullSet$t5c13c)
View(fullSet)
fullSet <- inner_join(baseline, year1)
fullSet <- inner_join(fullSet, year3)
fullSet <- inner_join(fullSet, year5)
fullSet <- inner_join(fullSet, year9)
fullSet <- fullSet %>% filter(t5c13a>0, t5c13b>0, t5c13c>0)
fullSet <- fullSet %>% filter(t5c13a>0, t5c13b>0, t5c13c>0)
fullSet$t5c13a <- as.factor(fullSet$t5c13a)
fullSet$t5c13b <- as.factor(fullSet$t5c13b)
fullSet$t5c13c <- as.factor(fullSet$t5c13c)
ullSet["t5c13c"]
fullSet["t5c13c"]
fullSet[,4:370]
2 + 2
results <- rfe(fullSet[,4:370], fullSet["t5c13c"], sizes = c(1:52), rfeControl = control) # this will take AWHILE
ullSet[,4:370]
fullSet[,4:370]
test <- fullSet[,4:370]
test <- fullSet["t5c13c"
results <- rfe(fullSet[,4:370], fullSet["t5c13c"], sizes = c(1:52), rfeControl = control) # this will take AWHILE
test <- fullSet["t5c13c"]
control <- rfeControl(functions = rfFuncs, method="cv", number=5)
results <- rfe(finalCombined[,2:53], finalCombined[,56], sizes = c(1:52), rfeControl = control) # this will take AWHILE
fullSet <- na.omit(fullSet)
fullSet[fullSet < 0] <- NA
results <- rfe(fullSet[,4:370], fullSet["t5c13c"], sizes = c(1:52), rfeControl = control) # this will take AWHILE
results <- rfe(fullSet[,4:370], fullSet["t5c13c"], sizes = c(1:367), rfeControl = control) # this will take AWHILE
fullSet[fullSet < 0] <- 0
results <- rfe(fullSet[,4:370], fullSet["t5c13c"], sizes = c(1:367), rfeControl = control) # this will take AWHILE
###### (Full Set)
fullSet <- inner_join(baseline, year1)
fullSet <- inner_join(fullSet, year3)
fullSet <- inner_join(fullSet, year5)
fullSet <- inner_join(fullSet, year9)
fullSet <- fullSet %>% filter(t5c13a>0, t5c13b>0, t5c13c>0)
fullSet <- na.omit(fullSet)
fullSet[fullSet < 0] <- 0
fullSet$t5c13a <- as.factor(fullSet$t5c13a)
fullSet$t5c13b <- as.factor(fullSet$t5c13b)
fullSet$t5c13c <- as.factor(fullSet$t5c13c)
results <- rfe(fullSet[,4:370], fullSet["t5c13c"], sizes = c(1:367), rfeControl = control) # this will take AWHILE
typeof(fullSet["t5c13c"])
typeof(fullSet[,106])
results <- rfe(fullSet[,4:370], fullSet[,106], sizes = c(1:367), rfeControl = control) # this will take AWHILE
ggplot(results)
predictors(results)
selected_features <- predictors(results)[1:5]
results
caret::confusionMatrix(predict_math1, fullSet$t5c13c)
predict_math1 <- predict(model2_nb, fullSet_x)
model2_nb <- train(t5c13c ~ . , method="nb", data=fullSet_train, trControl = ctrl)
predict_math1 <- predict(model2_nb, fullSet_x)
caret::confusionMatrix(predict_math1, fullSet$t5c13c)
model2_nb <- train(t5c13c ~ . , method="nb", data=fullSet_train, trControl = ctrl)
split_proportion = 0.8 # specify proportion of data used for training
# select outcome variable
outcome <- finalCombined %>% dplyr::select(t5c13c)
# randomly select indices for train/validate set
train_ind <- createDataPartition(outcome$t5c13c, p = split_proportion, list = FALSE)
finalCombined_train <- finalCombined[train_ind,] # get training data
finalCombined_test <- finalCombined[-train_ind,] # get test data
finalCombined_x <- finalCombined_test %>% dplyr::select(-t5c13c) # select predictor data for test set
finalCombined_y <- finalCombined_test %>% dplyr::select(t5c13c) # select outcome data for test set
finalCombined_x <- finalCombined_test %>% dplyr::select(-t5c13c) # select predictor data for test set
finalCombined_y <- finalCombined_test %>% dplyr::select(t5c13c) # select outcome data for test set
ctrl <- trainControl(method = "cv", number=5)
model_nb <- train(t5c13c ~ . , method="nb", data=finalCombined_train, trControl = ctrl)
predict_math <- predict(model_nb, finalCombined_x)
caret::confusionMatrix(predict_math, finalCombined_y$t5c13c)
x <- select(finalCombined, t5v13c) %>% filter(t5c13c == 3)
x
x <- select(finalCombined, t5c13c) %>% filter(t5c13c == 3)
finalCombined
finalCombined$t5c13c
x <- select(finalCombined, t5c13c) %>% filter(t5c13c == 3)
x <- finalCombined %>% filter(t5c13c == 3)
length(x)
finalCombined
x <- finalCombined %>% filter(t5c13c == 4)
length(x)
x <- finalCombined %>% filter(t5c13c == 5)
length(x)
finalCombined
nrows(x)
typeof(x)
sapply(x, NROW)
x <- finalCombined %>% filter(t5c13c == 4)
sapply(x, NROW)
x <- finalCombined %>% filter(t5c13c == 3)
sapply(x, NROW)
x <- finalCombined %>% filter(t5c13c == 2)
sapply(x, NROW)
x <- finalCombined %>% filter(t5c13c == 1)
sapply(x, NROW)
x1[1:50]
head(x1,4)
head(x,20)
head(x,20)
head(x,20)
?head
x1 <- x[sample(nrow(x),20),]
x <- finalCombined %>% filter(t5c13c == 2)
x2 <- x[sample(nrow(x),20),]
x <- finalCombined %>% filter(t5c13c == 3)
x3 <- x[sample(nrow(x),20),]
x <- finalCombined %>% filter(t5c13c == 4)
x4 <- x[sample(nrow(x),20),]
x <- finalCombined %>% filter(t5c13c == 5)
x5 <- x[sample(nrow(x),20),]
new_x <- inner_join(x1,x2)
new_x <- inner_join(new_x,x3)
new_x <- inner_join(new_x,x4)
new_x <- inner_join(new_x,x5)
new_x <- merge(x1,x2)
new_x <- merge(new_x,x3)
new_x <- merge(new_x,x4)
new_x <- merge(new_x,x5)
new_x <- merge(x1,x2)
x1,x2
x1
typeof(x1)
new_x <- append(x1,x2)
new_x <- append(new_x,x3)
new_x <- append(new_x,x4)
new_x <- append(new_x,x5)
new_x <- append(x1,x2,x3)
new_x <- append(x1,x2)
new_x <- rbind(x1,x2)
new_x <- rbind(new_x,x3)
new_x <- rbind(new_x,x4)
new_x <- rbind(new_x,x5)
outcome <- new_x %>% dplyr::select(t5c13c)
train_ind <- createDataPartition(outcome$t5c13c, p = split_proportion, list = FALSE)
finalCombined_train <- new_x[train_ind,] # get training data
finalCombined_test <- new_x[-train_ind,] # get test data
finalCombined_x <- finalCombined_test %>% dplyr::select(-t5c13c) # select predictor data for test set
finalCombined_y <- finalCombined_test %>% dplyr::select(t5c13c) # select outcome data for test set
ctrl <- trainControl(method = "cv", number=5)
model_nb <- train(t5c13c ~ . , method="nb", data=finalCombined_train, trControl = ctrl)
predict_math <- predict(model_nb, finalCombined_x)
caret::confusionMatrix(predict_math, finalCombined_y$t5c13c)