-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModeling2.Rmd
366 lines (261 loc) · 15.1 KB
/
Modeling2.Rmd
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
Jared
```{R}
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)}
if(!require(ModelMetrics)){install.packages("ModelMetrics"); require(ModelMetrics)}
if(!require(recipes)){install.packages("recipes"); require(recipes)}
if(!require(DEoptimR)){install.packages("DEoptimR"); require(DEoptimR)}
set.seed(370)
```
```{r}
#Read in data
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=",")
```
```{r}
#Select top 56 features and remove missing values
temp_parent_involvement <- year9 %>% select(idnum,t5c13a,t5c13b,t5c13c)
#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")
# prepare new columns
# total activities
parent_involvement <- finalCombined %>% mutate(tally = 0)
# total grade
parent_involvement <- parent_involvement %>% mutate(score = t5c13a + t5c13b + t5c13c)
# add outcome variables
parent_involvement$t5c13a <- as.factor(parent_involvement$t5c13a)
parent_involvement$t5c13b <- as.factor(parent_involvement$t5c13b)
parent_involvement$t5c13c <- as.factor(parent_involvement$t5c13c)
#Subsets with grades from year 5 and year 9 isolated
finalSet <- read.csv(file="/Users/grant/Documents/INFO370/fragile_families/project-fragile-families/data/FeaturesWithYear9Year5MartialStatus.csv")
library(dplyr)
year9grades <- filter(finalSet, t5c13a>-1, t5c13b>-1, t5c13c>-1)
year5grades <- filter(finalSet, kind_a5>-1, kind_a6>-1, kind_a7>-1)
```
```{r}
#### Version 1: Popularity of activites from Yuxing's feature selection
# ## SET UP
# top DPW - hand picked features in days/per/week format
#need more features
features <- c('m4b4a2','m3c3l','m4b4a8','m3b4l','m3b4a','m2c3j','m2c3f','m3c3g','m2c3e','m3b4d','m2c3a')
#
# top YN - hand picked features in yes no format
#
#
### View(parent_involvement)
# ## FUNCTIONS
# COMPUTED MEAN
computed_mean <- function(column) {
response <- parent_involvement[[column]]
weighted_mean <- (sum(response==0)*0 + sum(response==1)*1 + sum(response==2)*2 + sum(response==3)*3 + sum(response==4)*4 + sum(response==5)*5 + sum(response==6)*6 +sum(response==7)*7)/length(response)
fltr <- response > weighted_mean
parent_involvement[fltr,"tally"] <- parent_involvement[fltr,"tally"] + 1
return(parent_involvement)
}
# ## COMPUTATIONS
# Number of activities
weighted_means <- c(1:length(features))
for(i in 1:length(features)){
column_name = features[i]
parent_involvement <- computed_mean(column_name)
}
# Percentage of activities
length <- length(features)
parent_involvement <- parent_involvement %>% mutate(percentage = tally/11.0)
```
```{r}
#synthesize marital status to M (married) and D (divorced). This finished set can be found under data/FeaturesWithYear9Year5MartialStatus.csv"
marriageCol5 <- ifelse(year5grades$m4a4 == 1, "Married",
ifelse(year5grades$m4a4 == 2, "Married",
ifelse(year5grades$m4a4 == 3, "Divorced",
ifelse(year5grades$m4a4 == 4, "Divorced",
ifelse(year5grades$m4a4 == 5, "Divorced",
ifelse(year5grades$m4a4 == 6, "Divorced", NA))))))
marriageCol9 <- ifelse(year9grades$m5a4 == 1, "Married",
ifelse(year9grades$m5a4 == 2, "Married",
ifelse(year9grades$m5a4 == 3, "Divorced",
ifelse(year9grades$m5a4 == 4, "Divorced",
ifelse(year9grades$m5a4 == 5, "Divorced",
ifelse(year9grades$m5a4 == 6, "Divorced", NA))))))
year5grades$marrystat <- marriageCol5
year9grades$marrystat <- marriageCol9
#Change school performance to a binary value, 0 for below average, 1 for above average
year9gradesHighLow <- year9grades
year9gradesHighLow <- year9gradesHighLow %>% filter(t5c13c != 3, t5c13a != 3, t5c13b != 3)
year9gradesHighLow$abovebelowMath <- ifelse(year9gradesHighLow$t5c13c == 1, 0,
ifelse(year9gradesHighLow$t5c13c == 2, 0,
ifelse(year9gradesHighLow$t5c13c == 4, 1,
ifelse(year9gradesHighLow$t5c13c == 5, 1,
-1
))))
year9gradesHighLow$abovebelowReading <- ifelse(year9gradesHighLow$t5c13a == 1, 0,
ifelse(year9gradesHighLow$t5c13a == 2, 0,
ifelse(year9gradesHighLow$t5c13a == 4, 1,
ifelse(year9gradesHighLow$t5c13a == 5, 1,
-1
))))
year9gradesHighLow$abovebelowScience <- ifelse(year9gradesHighLow$t5c13b == 1, 0,
ifelse(year9gradesHighLow$t5c13b == 2, 0,
ifelse(year9gradesHighLow$t5c13b == 4, 1,
ifelse(year9gradesHighLow$t5c13b == 5, 1,
-1
))))
year9gradesHighLow$abovebelowScore <- ifelse(year9gradesHighLow$score == 1, 0,
ifelse(year9gradesHighLow$score == 2, 0,
ifelse(year9gradesHighLow$score == 3, 0,
ifelse(year9gradesHighLow$score == 4, 0,
ifelse(year9gradesHighLow$score == 5, 0,
ifelse(year9gradesHighLow$score == 6, 0,
ifelse(year9gradesHighLow$score == 7, 0,
ifelse(year9gradesHighLow$score == 9, 1,
ifelse(year9gradesHighLow$score == 10, 1,
ifelse(year9gradesHighLow$score == 11, 1,
ifelse(year9gradesHighLow$score == 12, 1,
ifelse(year9gradesHighLow$score == 13, 1,
ifelse(year9gradesHighLow$score == 14, 1,
ifelse(year9gradesHighLow$score == 15, 1, NA))))))))))))))
#remove NA values
year9gradesHighLow <- year9gradesHighLow[!is.na(year9gradesHighLow$score), ]
```
```{r}
#### Verison 2: Popularity of all activities in the days/week format
# total grade
# Our predictor variables
features2 <- c('f1g9a', 'f1g9b', 'f1g9c', 'f1g9d', 'f1g9e', 'f1g9f', 'f1g9g', 'f1g9h', 'f1g9i', 'f1g9j', 'f1g9k', 'f1g9l','m2b18a', 'm2b18b', 'm2b18c', 'm2b18d', 'm2b18e', 'm2b18f', 'm2b18g', 'm2b18h','m2c3a','m3b4a', 'm3b4b', 'm3b4c', 'm3b4d', 'm3b4e', 'm3b4f', 'm3b4g', 'm3b4h', 'm3b4i', 'm3b4j', 'm3b4k', 'm3b4l', 'm3b4m','m3c3a','m3c3b', 'm3c3c', 'm3c3d', 'm3c3e', 'm3c3f', 'm3c3g', 'm3c3h', 'm3c3i', 'm3c3j', 'm3c3k', 'm3c3l', 'm3c3m','f3c3a', 'f3c3b', 'f3c3c', 'f3c3d', 'f3c3e', 'f3c3f', 'f3c3g', 'f3c3h', 'f3c3i', 'f3c3j', 'f3c3k', 'f3c3l', 'f3c3m','f3b4a', 'f3b4b', 'f3b4c', 'f3b4d', 'f3b4e', 'f3b4f', 'f3b4g', 'f3b4h', 'f3b4i', 'f3b4j', 'f3b4k', 'f3b4l', 'f3b4m','m4c3a', 'm4c3b', 'm4c3c', 'm4c3d', 'm4c3e', 'm4c3f','m4c3g', 'm4c3h','f4b4a1', 'f4b4a2', 'f4b4a3', 'f4b4a4', 'f4b4a5', 'f4b4a6', 'f4b4a7', 'f4b4a8', 'f4c3a', 'f4c3b', 'f4c3c', 'f4c3d', 'f4c3e', 'f4c3f', 'f4c3g', 'f4c3h','f2b36a', 'f2b36b', 'f2b36c', 'f2b36d', 'f2b36e', 'f2b36f', 'f2b36g', 'f2b36h', 'f2c3a', 'f2c3b', 'f2c3c', 'f2c3d', 'f2c3e', 'f2c3f', 'f2c3g', 'f2c3h', 'f2c3i', 'f2c3j','m2c3b', 'm2c3c', 'm2c3d', 'm2c3e', 'm2c3f', 'm2c3g', 'm2c3h', 'm2c3i', 'm2c3j')
parent_involvement2 <- read.csv("/Users/grant/Documents/INFO370/fragile_families/project-fragile-families/data/FeaturesSelected.csv")
## convert 200's values
# 201 => 7
# 202 => 3
# 203 => 2
# 204 => 1
# 205 => 0
parent_involvement2[parent_involvement2==205] <- 0
parent_involvement2[parent_involvement2==204] <- 1
parent_involvement2[parent_involvement2==203] <- 2
parent_involvement2[parent_involvement2==202] <- 3
parent_involvement2[parent_involvement2==201] <- 7
# remove negative values
parent_involvement2[parent_involvement2<0] <- NA
# grades and total grade
combine9_2 <- year9 %>% select(idnum,t5c13a,t5c13b,t5c13c) %>%
filter(t5c13a>-1,t5c13b>-1,t5c13c>-1)
combine9_2 <- combine9 %>% mutate(score=t5c13a + t5c13b + t5c13c)
#temp_save_df <- parent_involvement2
combine9_2$t5c13a <- as.factor(combine9_2$t5c13a)
combine9_2$t5c13b <- as.factor(combine9_2$t5c13b)
combine9_2$t5c13c <- as.factor(combine9_2$t5c13c)
combine9_2$score <- as.factor(combine9_2$score)
# merge
typeof(parent_involvement2$idnum)
combine9_2$idnum <- as.numeric(combine9_2$idnum)
parent_involvement2 <- inner_join(combine9_2, parent_involvement2, by="idnum")
parent_involvement2 <- parent_involvement2 %>% mutate(tally = 0)
#temp_save_df <- inner_join(combine9_2, parent_involvement2, by="idnum")
#temp_save_df_2 <- inner_join(combine9_2, parent_involvement2, by="idnum")
#temp_save_df <- temp_save_df %>% mutate(tally = 0)
#temp_save_df_2 <- temp_save_df_2 %>% mutate(tally = 0)
View(parent_involvement2)
# function for determining effort to tally activity and then tallying.
computed_mean2 <- function(column) {
response <- na.omit(parent_involvement2[[column_name]])
weighted_mean <- (sum(response==0)*0 + sum(response==1)*1 + sum(response==2)*2 + sum(response==3)*3 + sum(response==4)*4 + sum(response==5)*5 + sum(response==6)*6 +sum(response==7)*7)/length(response)
fltr <- response > weighted_mean
parent_involvement2[fltr,"tally"] <- parent_involvement2[fltr,"tally"] + 1
return(parent_involvement2)
}
# Add tallies for doing activities
for(i in 1:length(features2)){
column_name = features2[i]
parent_involvement2 <- computed_mean2(column_name)
}
# check data
View(parent_involvement2)
parent_involvement2 <- parent_involvement2 %>% mutate(percentage = tally/124.0)
#parent_involvement2 <- as.numeric(parent_involvement2$tally)
#parent_involvement2 <- as.numeric(parent_involvement2$t5c13c)
```
``` {r}
#Decision tree and confusion matrix
if(!require(rpart.plot)){install.packages("rpart.plot"); require(rpart.plot)} # only need this is dependencies of caret were not installed
ctrl <- trainControl(method = 'cv', number=5)
model_dec_tree <- train(t5c13c ~ .,
data = parent_involvement2,
method = "rpart",
trControl = ctrl,
tuneLength = 10) # try 10 reasonable parameter values. No more guessing parameter ranges!
model_dec_tree
# looking at decision tree
prp(parent_involvement2$t5c13c)
# predictions using model
predict_iris <- predict(model_dec_tree, newdata = iris_test_x)
# performance (via confusion matrix)
confusionMatrix(predict_iris, iris_test_y$Species)
```
```{r}
#Clean year 5 data
finalSet <- read.csv("/Users/jaredpraino/Documents/University of Washington/Senior Year/INFO370/final_project/project-fragile-families/data/FeaturesWithYear9Year5MartialStatus.csv")
features_year_5 <- c('m4b4a2','m3c3l','m4b4a8','m3b4l','m3b4a','m2c3j','m2c3f','m3c3g','m2c3e','m3b4d','m2c3a')
year9grades <- filter(finalSet, t5c13a>-1, t5c13b>-1,t5c13c>-1)
year5grades <- filter(finalSet, kind_a5>-1, kind_a6>-1, kind_a7>-1)
year5grades <- year5grades %>% mutate(tally = 0)
# total grade
year5grades <- year5grades %>% mutate(scoreKind = t5c13a + t5c13b + t5c13c)
# add outcome variables
year5grades$t5c13a <- as.factor(year5grades$t5c13a)
year5grades$t5c13b <- as.factor(year5grades$t5c13b)
year5grades$t5c13c <- as.factor(year5grades$t5c13c)
year5grades[year5grades==205] <- 0
year5grades[year5grades==204] <- 1
year5grades[year5grades==203] <- 2
year5grades[year5grades==202] <- 3
year5grades[year5grades==201] <- 7
# remove negative values
year5grades[year5grades<0] <- NA
computed_mean_year_5 <- function(column) {
response <- na.omit(year5grades[[column_name]])
weighted_mean <- (sum(response==0)*0 + sum(response==1)*1 + sum(response==2)*2 + sum(response==3)*3 + sum(response==4)*4 + sum(response==5)*5 + sum(response==6)*6 +sum(response==7)*7)/length(response)
fltr <- response > weighted_mean
year5grades[fltr,"tally"] <- year5grades[fltr,"tally"] + 1
return(year5grades)
}
# Add tallies for doing activities
for(i in 1:length(features_year_5)){
column_name = features_year_5[i]
year5grades <- computed_mean_year_5(column_name)
}
#graph year 5 scores vs tallies of activities
ggplot(data = year5grades, aes(x = scoreKind, y = tally)) + geom_point() + geom_jitter(width = 0.2) +
geom_smooth(method = "lm", color="black", aes(group=1))
cor(year5grades$score,year5grades$tally)
```
```{r}
#chi squared values
marriedYear9 <- filter(year9gradesHighLow, marrystat == "M")
divorcedYear9 <- filter(year9gradesHighLow, marrystat == "D")
count(marriedYear9, var = abovebelowScore)
count(divorcedYear9, var = abovebelowScore)
belowMarryRatio <- 166/687
aboveMarryRatio <- 521/687
belowDivRation <- 270/680
aboveDivRatio <- 410/680
paste("Below Marry Ratio: " ,belowMarryRatio)
paste("Above Marry Ratio: " , aboveMarryRatio)
paste("Below Divorce Ratio: " , belowDivRation)
paste("Above Divorce Ratio: " , aboveDivRatio)
```