-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework4.R
306 lines (187 loc) · 7.16 KB
/
homework4.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
if (!require("apaTables")) install.packages("apaTables")
options(scipen = 999)
library(tidyverse)
library(car)
library(psych)
library(emmeans)
library(apaTables)
library(rties)
library(brms)
library(BayesFactor)
#I saw this package on twitter that apparently has one fucntion to check model assumptions?
library(easystats)
#Load data and functions
natdis<-read.csv("naturalDisaster.csv", stringsAsFactors = TRUE)
source("userfunctions.R")
#First look at the data
str(natdis)
summary(natdis)
head(natdis)
natdis_uncentered<-read.csv("naturalDisaster.csv", stringsAsFactors = TRUE)
varofint<-subset(natdis, select = c(pts, aaq, age, hurte, mindful, therap, wome, race))
histAll(varofint)
#women therapy and race should not be int. lets make them factors
natdis$therapyF <- factor(natdis$therapy, levels = c(0,1), labels = c("No", "Yes"))
natdis$genderF <- factor(natdis$women, levels = c(0,1), labels = c("Man", "Woman"))
natdis$raceF <-factor(natdis$race, levels = c(0,1,2), labels = c("White", "Black", "Asian"))
#Look at data again (pts ~ aaq + women + age + therapy + race)
histAll(natdis_uncentered)
plot(pts ~ aaq, data = natdis)
plot(pts ~ age, data = natdis)
plot(pts ~ genderF, data = natdis)
plot(pts ~ therapyF, data = natdis)
plot(pts ~ raceF, data = natdis)
genderD<- natdis %>%
group_by(genderF) %>%
get_summary_stats(pts); genderD
raceD<- natdis %>%
group_by(raceF) %>%
get_summary_stats(pts); raceD
#data looks normalish. should center aaq and age. maybe more when I add my own variables later
natdis<- natdis %>% mutate(
age_c = age - mean(age, na.rm = TRUE),
aaq_c = aaq - mean(aaq, na.rm = TRUE))
quant <- subset(natdis, select = c(pts, aaq, age, mindful, hurte))
pairs.panels(quant)
apaTables::apa.cor.table(quant, filename = "homework4cortable.docx")
#lets build model 1
m1f<- glm(pts ~ aaq_c + genderF + raceF + age_c + therapyF, data = natdis,
family = "gaussian", na.action = na.exclude)
#i want to try the new way of checking model assumptions
check_model(m1f)
#the chart is a bit ugly but i think this package is cool/saves time
summary(m1f)
F_test(m1f)
r_sq(m1f)
#super mega significant model
#now to add my new variables. I think mindfulness and hurricane are promising
#first center the variables/look at plots
plot(pts ~ mindful, data = natdis)
plot(pts ~ hurte, data = natdis)
natdis<- natdis %>% mutate(
mindful_c = mindful - mean(mindful, na.rm = TRUE),
hurte_c = hurte - mean(hurte, na.rm = TRUE))
m2f<- glm(pts ~ aaq_c + genderF + raceF + age_c + therapyF +mindful_c + hurte_c , data = natdis,
family = "gaussian", na.action = na.exclude)
#i want to try the new way of checking model assumptions
check_model(m2f)
#the chart is a bit ugly but i think this package is cool/saves time
summary(m2f)
F_test(m2f)
r_sq(m2f)
# also significant.
#bayes time >:)
b1 <- brm(pts ~ 0 + aaq_c + genderF + raceF + age_c + therapyF, data = natdis,
family = "skew_normal", chains = 4, iter = 2000, seed = 123)
saveRDS(b1, file = "b1_hw4.rds")
b2 <- brm(pts ~ 0 + aaq_c + genderF + raceF + age_c + therapyF +mindful_c + hurte_c , data = natdis,
family = "skew_normal", chains = 4, iter = 2000, seed = 123)
saveRDS(b2, file = "b2_hw4.rds")
b1 <- readRDS("/Users/daphnehansell/Documents/GitHub/datascienceb/b1_hw4.rds")
b2 <- readRDS("/Users/daphnehansell/Documents/GitHub/datascience/b2_hw4.rds")
#Establish if an effect exists
lmBF(pts ~ aaq_c + genderF + raceF + age_c + therapyF, data = natdis)
lmBF(pts ~ aaq_c + genderF + raceF + age_c + therapyF +mindful_c + hurte_c , data = natdis)
#strong evidence for both over the null
#goal 2 nhst. check model assumptions the old fashioned way
# 1. Normality of Residual
hist(residuals(m1f), probability = T) #histogram overall
summary(residuals(m1f))
# Superimpose density curve
lines(density(residuals(m1f), na.rm=T), lwd = 2, col = "red")
# Superimpose best fitting normal curve
curve(dnorm(x, mean = mean(residuals(m1f), na.rm=T), sd = sd(residuals(m1f), na.rm=T)),
lty = 2, lwd = 2, add = TRUE, col = "blue")
#bit skewed. weird qqplot
## By Group:
qqPlot(residuals(m1f), groups = natdis$pts)
#this code didn't work because there are too many variables in my big dataframe
natdis_sub <- natdis[, c("pts", "aaq_c", "genderF", "raceF", "age_c", "therapyF", "mindful_c", "hurte_c")]
qqPlot(residuals(m1f), groups = natdis_sub$pts)
#still too big, out of ideas
qqPlot(residuals(m1f))
### 2. Homoscedasticity (Constant Variance):
car::residualPlots(m1f,
pch=20, col="gray",
fitted = T,
ask = F, layout = c(1,2),
tests = F, quadratic = F)
Anova(m1f, type = "III", test.statistic = "F")
sr2(m1f)
#still seems like only aaq is predictive
#second model
hist(residuals(m2f), probability = T) #histogram overall
summary(residuals(m2f))
# Superimpose density curve
lines(density(residuals(m2f), na.rm=T), lwd = 2, col = "red")
# Superimpose best fitting normal curve
curve(dnorm(x, mean = mean(residuals(m2f), na.rm=T), sd = sd(residuals(m1f), na.rm=T)),
lty = 2, lwd = 2, add = TRUE, col = "blue")
#yeah looks pretty normal, bit skewed
### 2. Homoscedasticity (Constant Variance):
car::residualPlots(m2f,
pch=20, col="gray",
fitted = T,
ask = F, layout = c(1,2),
tests = F, quadratic = F)
Anova(m2f, type = "III", test.statistic = "F")
sr2(m2f)
#aaq and hurte are the only significant IVs. but they seem very significant
#both are continuous variables so no post-hoc controls required i think
#model comparisons
F_change(m1f, m2f)
m1fr2<-r_sq(m1f)
m2fr2<-r_sq(m2f)
#model 2 does explain more variance in pts
#back to bayes
plot(b1)
plot(b2)
summary(b1)
summary(b2)
pp_check(b1, ndraws=30)
pp_check(b2, ndraws=30) # checking predictive accuracy
## Normality of Residuals
pp_check(b1, type="error_hist", ndraws=20, set.seed(293)) # overall
pp_check(b1, type="error_hist_grouped", ndraws=5, group = "pts", freq = T, set.seed(293))
qqPlot(residuals(b1)) #doing ungrouped because of the maximum issue
#these residuals are kinda weird. whats with the hump
pp_check(b2, type="error_hist", ndraws=20, set.seed(293)) # overall
pp_check(b2, type="error_hist_grouped", ndraws=5, group = "pts", freq = T, set.seed(293))
qqPlot(residuals(b2)) #doing ungrouped because of the maximum issue
#weird hump again...
#Constant Variance
ggplot(natdis, aes(x = pts, y = residuals(b1)[,1]))+
geom_point(size=2)+geom_jitter(width=0.1)
ggplot(natdis, aes(x = pts, y = residuals(b2)[,1]))+
geom_point(size=2)+geom_jitter(width=0.1)
# rope
r1 <- rope(b1)
r1
plot(r1)
r2 <- rope(b2)
r2
plot(r2)
#PROBABILIY OF DIRECTION
pd1 <- p_direction(b1)
pd1
plot(pd1)
pd2 <- p_direction(b2)
pd2
plot(pd2)
#cohen's d
difYN1 <- summary(b1)$fixed[6,1]
sigma1 <- summary(b1)$spec_pars[1,1]
cd1<-difYN1/sigma1
difYN <- summary(b2)$fixed[6,1]
sigma <- summary(b2)$spec_pars[1,1]
cd<-difYN/sigma
#model comparison NHST
F_change(m1f, m2f)
#model 2 seems better
r_sq(mf1)
r_sq(m22)
#Bayesian
## Goal 4: Model Comparison
b1 <- add_criterion(b1, criterion = "waic")
b2 <- add_criterion(b2, criterion = "waic")
loo_compare(b1, b2, criterion = "waic")