-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.R
289 lines (249 loc) · 15.2 KB
/
analysis.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
# Author: Khang Tong, as part of the project "Understanding the Efficacy of Phishing Training in Practice"
library(lme4)
library(dplyr)
# Formatting functions
pvalFormat = function(p.values, method = 'none', replace = FALSE, math = TRUE){
## Formats p-values for reports, can report adjusted pvalues
## Inputs:
## - p.value: numeric p-value
## - method: pvalue adjustment, passed to p.adjust.methods
## - replace: if TRUE, replaces p-values with their adjusted value
## Outputs:
## - out: formatted p-value
p.values <- suppressWarnings(as.numeric(p.values))
out <- rep(NA, length(p.values))
sig <- p.adjust(p.values, method)
if(replace) p.values <- sig
for(i in 1:length(p.values)){
if(is.na(p.values[i])){out[i] <- NA}else{
if(p.values[i] >= .001){
out[i] <- paste('$', formatC(p.values[i], format = 'f', digits = 3), '$', sep = '')
}
if(p.values[i] < .001){
out[i] <- '< $0.001$'
}
if(sig[i] > 0.01 & sig[i] <= 0.05){
out[i] <- paste(out[i], '*', sep = '')
}
if(sig[i] > 0.001 & sig[i] <= 0.01) {
out[i] <- paste(out[i], '**', sep = '')
}
if(sig[i] <= 0.001){
out[i] <- paste(out[i], '***', sep = '')
}}
}
out[is.na(out)] <- '-'
return(out)
}
printMod = function(mod, mod.null = NULL, row.names = NULL, caption = NULL, digits = 3, tex = F, gee.se = 'sandwich',
overall.test = F, ci = F, ci.profile = T, level = .95, d.f. = F, cat.cov.test = F, lmer.norm.p = F,
csv = NULL, ...){
## A function to print regression tables in LaTeX and to csv (truncated to only include glmerMod needed for this file).
## Inputs:
## - mod: regression model
## - row.names: a character vector, giving row names for the final printed table
## - caption: a character string, caption passed to latex(...)
## - digits: number of digits to print
## - tex: logical, should the latex(...) function be run, or should a matrix be returned
## - gee.se: a character string, 'sandwich' or 'naive'
## - overall.test: logical, likelihood ratio / conditional F test for overall model fit
## - ci: logical, should confidence intervals be printed for each parameter
## - ci.profile: logical, should profile likelihood be used for computing confidence intervals
## - level: level of confidence interval, ignored unless ci = T
## - d.f.: logical, should the degress of freedom be printed in the regression table - currently unemplemented
## - cat.cov.test: logical, should categorical covariates be tested with something like ANOVA / LRT
## - lmer.norm.p: logical, should a normal approximation be used for p-values in lmer() models
## - ...: additional parameters passed to latex(...)
## Cleaning and defining unique model classes
if(length(class(mod)) == 1){
mC <- class(mod)
}
if(mC == 'glmerMod'){
out <- summary(mod)$coefficients
out.t <- out[, 3]
out[, c(1:3)] <- formatC(out[, c(1:3), drop = F], format = 'f', digits = digits)
out[, 1:3] <- paste('$', out[, 1:3, drop = F], '$', sep = '')
out[, 4] <- pvalFormat(out[, 4])
colnames(out) <- c('Estimate', 'Std. Error', 'z-value', 'p-value')
if(summary(mod)$family == 'binomial'){
if(summary(mod)$link == 'logit'){
ccs <- summary(mod)$coefficients
ints <- cbind(ccs[, 1] - abs(qnorm((1 - level) / 2)) * ccs[, 2], ccs[, 1] + abs(qnorm((1 - level) / 2)) * ccs[, 2])
CI <- exp(cbind(OR = ccs[, 1], ints))
CI <- cbind(paste('$', formatC(CI[, 1], format = 'f', digits = 3), '$', sep = ''),
paste('($', formatC(CI[, 2], format = 'f', digits = 3), '$, $',
formatC(CI[, 3], format = 'f', digits = 3), '$)', sep = ''))
out <- cbind(out, CI); out[1, 5:6] <- c('-', '-')
colnames(out)[5:6] <- c('Odds Ratio', paste(round(level * 100), '\\% CI', sep = ''))
} else {
if(ci) {
ccs <- summary(mod)$coefficients
ints <- cbind(ccs[, 1] - abs(qnorm((1 - level) / 2)) * ccs[, 2], ccs[, 1] + abs(qnorm((1 - level) / 2)) * ccs[, 2])
ints <- paste('($', formatC(ints[, 1], digits = digits, format = 'f'), '$, $',
formatC(ints[, 2], digits = digits, format = 'f'), '$)', sep = '')
out <- cbind(out[, 1:2], ints, out[, 3:ncol(out)])
colnames(out)[3] <- paste(round(level * 100), '\\% CI', sep = '')
}
}
} else {
if(ci){
ccs <- summary(mod)$coefficients
ints <- cbind(ccs[, 1] - abs(qnorm((1 - level) / 2)) * ccs[, 2], ccs[, 1] + abs(qnorm((1 - level) / 2)) * ccs[, 2])
ints <- paste('($', formatC(ints[, 1], digits = digits, format = 'f'), '$, $',
formatC(ints[, 2], digits = digits, format = 'f'), '$)', sep = '')
out <- cbind(out[, 1:2], ints, out[, 3:ncol(out), drop = F])
colnames(out)[3] <- paste(round(level * 100), '\\% CI', sep = '')
}
}
if(cat.cov.test){
tt1 <- rep('', nrow(out))
tt2 <- rep('', nrow(out))
tAOV <- anova(mod)
int.cor <- ifelse(rownames(out)[1] == '(Intercept)', 2, 1)
for(i in which(tAOV$npar > 1)){
tAOV. <- anova(update(mod), update(mod, as.formula(paste('. ~ . -', rownames(tAOV)[i]))))
tt1[sum(tAOV$npar[1:i]) + int.cor - tAOV$npar[i]] <- paste('$\\chi^{2}_{', tAOV.$'Chi Df'[2],
'} = ', formatC(tAOV.$Chisq[2], format = 'f', digits = digits), '$', sep = '')
tt2[sum(tAOV$npar[1:i]) + int.cor - tAOV$npar[i]] <- pvalFormat(tAOV.[2, 'Pr(>Chisq)'])
}
out <- cbind(out, tt1, tt2)
colnames(out)[(ncol(out) - 1):ncol(out)] <- c('$\\chi^2$ value', 'p-value')
}
if(!is.null(mod.null)){
lrt_stat <- anova(mod, mod.null)
out <- cbind(out, c('', paste('$\\chi^2_{', abs(diff(lrt_stat$Df)), '} = ', formatC(lrt_stat$Chisq[2], format = 'f', digits = digits),
'$', sep = ''), rep('', nrow(out) - 2)))
out <- cbind(out, c('', pvalFormat(lrt_stat$'Pr(>Chisq)'[2]),
rep('', nrow(out) - 2)))
colnames(out)[c(ncol(out) - 1, ncol(out))] <- c('$\\Delta$Dev.', 'p-value')
}
if(overall.test){
LRT <- anova(update(mod), update(mod, as.formula(paste('. ~ . - (', paste(rownames(anova(mod)), collapse = ' + '), ')'))))
out <- cbind(out, c('', paste('$\\chi^2_{', abs(diff(LRT$Df)), '} = ',
formatC(LRT$Chisq[2], format = 'f', digits = 2), '$', sep = ''),
rep('', nrow(out) - 2)))
out <- cbind(out, c('', pvalFormat(LRT$'Pr(>Chisq)'[2]),
rep('', nrow(out) - 2)))
colnames(out)[(ncol(out) - 1):ncol(out)] <- c('Overall $\\chi^2$', 'Pr($>$$\\chi^2$)')
}
}
if(!is.null(row.names)) rownames(out) <- row.names
if(tex){
latex(out,
file = '',
title = '',
where = '!htp',
col.just = rep('c', ncol(out)),
caption = caption,
insert.bottom = "Significance codes: ***$0.001$, **$0.01$, *$0.05$.",
...)
} else{return(out)}
if(!is.null(csv)) write.csv(gsub('$', '', out, fixed = T), file = csv, quote = F)
}
# 4. Annual Security Awareness Training
fit.glmer.q1 = glmer(fail ~ days.since.annual.training + cumul.prev.fail +
month + phish.label + track + (1 | user),
data = df, family = binomial)
# 5. Embedded Phishing Training
fit.glmer.q2 = glmer(fail ~ treatment + cumul.prev.fail + month + phish.label +
track + (1 | user),
data=df, family = binomial)
# 6. Embedded Training Engagement
## OVERALL
### Cumulative number of acknowledgements
fit.glmer.q3.cumul.ack = glmer(fail ~ cumul.ack + cumul.prev.fail + month +
phish.label + track + (1 | user),
data = df, family = binomial)
### Completed at least 1 previous training
fit.glmer.q3.prev.ack = glmer(fail ~ prev.ack + cumul.prev.fail + month +
phish.label + track + (1 | user),
data = df, family = binomial)
### Cumulative time training (30s)
fit.glmer.q3.train.time = glmer(fail ~ cumul.training.time30 + cumul.prev.fail +
month + phish.label + track + (1 | user),
data = df, family = binomial)
## Models with interaction term between training type and engagement variable
fit.glmer.interaction.cumul.ack = glmer(fail ~ cumul.ack*training.type2 + cumul.prev.fail +
month + phish.label + track + (1 | user),
data = df, family = binomial)
tbl.fit.glmer.interaction.cumul.ack = printMod(fit.glmer.interaction.cumul.ack, cat.cov.test=T)
fit.glmer.interaction.prev.ack = glmer(fail ~ prev.ack*training.type2 + cumul.prev.fail +
month + phish.label + track + (1 | user),
data = df, family = binomial)
tbl.fit.glmer.interaction.prev.ack = printMod(fit.glmer.interaction.prev.ack, cat.cov.test=T)
fit.glmer.interaction.train.time = glmer(fail ~ cumul.training.time30*training.type2 + cumul.prev.fail +
month + phish.label + track + (1 | user),
data = df, family = binomial)
tbl.fit.glmer.interaction.train.time = printMod(fit.glmer.interaction.train.time, cat.cov.test=T)
### Generate summary table for interaction models
est.interactive.cumul.ack = summary(fit.glmer.interaction.cumul.ack)$coefficients[2,1] + summary(fit.glmer.interaction.cumul.ack)$coefficients[21,1]
or.interactive.cumul.ack = paste0("$", exp(est.interactive.cumul.ack) %>% round(3) %>% format(nsmall=2), "$")
se.interactive.cumul.ack = sqrt(vcov(fit.glmer.interaction.cumul.ack)[2,2] + vcov(fit.glmer.interaction.cumul.ack)[21,21] + 2*vcov(fit.glmer.interaction.cumul.ack)[2,21])
ci.interactive.cumul.ack = paste0("($", exp(est.interactive.cumul.ack - 1.965*se.interactive.cumul.ack) %>% round(3) %>% format(nsmall=2), "$, $", exp(est.interactive.cumul.ack + 1.965*se.interactive.cumul.ack) %>% round(3) %>% format(nsmall=2), "$)")
est.interactive.prev.ack = summary(fit.glmer.interaction.prev.ack)$coefficients[2,1] + summary(fit.glmer.interaction.prev.ack)$coefficients[21,1]
or.interactive.prev.ack = paste0("$", exp(est.interactive.prev.ack) %>% round(3) %>% format(nsmall=2), "$")
se.interactive.prev.ack = sqrt(vcov(fit.glmer.interaction.prev.ack)[2,2] + vcov(fit.glmer.interaction.prev.ack)[21,21] + 2*vcov(fit.glmer.interaction.prev.ack)[2,21])
ci.interactive.prev.ack = paste0("($", exp(est.interactive.prev.ack - 1.965*se.interactive.prev.ack) %>% round(3) %>% format(nsmall=2), "$, $", exp(est.interactive.prev.ack + 1.965*se.interactive.prev.ack) %>% round(3) %>% format(nsmall=2), "$)")
est.interactive.train.time = summary(fit.glmer.interaction.train.time)$coefficients[2,1] + summary(fit.glmer.interaction.train.time)$coefficients[21,1]
or.interactive.train.time = paste0("$", exp(est.interactive.train.time) %>% round(3) %>% format(nsmall=2), "$")
se.interactive.train.time = sqrt(vcov(fit.glmer.interaction.train.time)[2,2] + vcov(fit.glmer.interaction.train.time)[21,21] + 2*vcov(fit.glmer.interaction.train.time)[2,21])
ci.interactive.train.time = paste0("($", exp(est.interactive.train.time - 1.965*se.interactive.train.time) %>% round(3) %>% format(nsmall=2), "$, $", exp(est.interactive.train.time + 1.965*se.interactive.train.time) %>% round(3) %>% format(nsmall=2), "$)")
tbl.summary = data.frame(
or.overall = c(tbl.fit.glmer.q3.prev.ack["prev.ackYes", "Odds Ratio"],
tbl.fit.glmer.q3.cumul.ack["cum.ack", "Odds Ratio"],
tbl.fit.glmer.q3.train.time["cum.training.time30", "Odds Ratio"]),
ci.overall = c(tbl.fit.glmer.q3.prev.ack["prev.ackYes", "95\\% CI"],
tbl.fit.glmer.q3.cumul.ack["cum.ack", "95\\% CI"],
tbl.fit.glmer.q3.train.time["cum.training.time30", "95\\% CI"]),
p.overall = c(tbl.fit.glmer.q3.prev.ack["prev.ackYes", "p-value"],
tbl.fit.glmer.q3.cumul.ack["cum.ack", "p-value"],
tbl.fit.glmer.q3.train.time["cum.training.time30", "p-value"]),
or.static = c(tbl.fit.glmer.interaction.prev.ack["prev.ackYes", "Odds Ratio"],
tbl.fit.glmer.interaction.cumul.ack["cumul.ack", "Odds Ratio"],
tbl.fit.glmer.interaction.train.time["cumul.train.time30", "Odds Ratio"]),
ci.static = c(tbl.fit.glmer.interaction.prev.ack["prev.ackYes", "95\\% CI"],
tbl.fit.glmer.interaction.cumul.ack["cumul.ack", "95\\% CI"],
tbl.fit.glmer.interaction.train.time["cumul.train.time30", "95\\% CI"]),
or.interactive = c(or.interactive.prev.ack,
or.interactive.cumul.ack,
or.interactive.cumul.train),
ci.interactive = c(ci.interactive.prev.ack,
ci.interactive.cumul.ack,
ci.interactive.cumul.train),
p.interactive = c(tbl.fit.glmer.interaction.prev.ack["prev.ackYes:training.type2interactive", "p-value"],
tbl.fit.glmer.interaction.cumul.ack["cumul.ack:training.type2interactive", "p-value"],
tbl.fit.glmer.interaction.train.time["cumul.train.time30:training.type2interactive", "p-value"]))
rownames(tbl.summary) = c("prev.ackYes", "cumul.ack", "cumul.train.time30")
colnames(tbl.summary) = c("OR","95\\% CI", "P-value", "OR", "95\\% CI", "OR", "95\\% CI", "P-value for difference in OR")
df.n = data.frame(training.type = c("Overall", "Static", "Interactive"),
n.recs =
c(dim(df)[1],
dim(df %>% filter(training.type %in% c("generic.static", "contextual.static")))[1],
dim(df %>% filter(training.type %in% c("generic.interactive", "contextual.interactive")))[1]),
n.id =
c(dim(unique(df%>%select(user)))[1],
dim(unique(df%>% filter(training.type %in% c("generic.static", "contextual.static")) %>%select(user)))[1],
dim(unique(df%>% filter(training.type %in% c("generic.interactive", "contextual.interactive")) %>%select(user)))[1]))
clabel2 = c(
paste0("Overall (n = ", df.n$n.id[1], ", ", df.n$n.recs[1], ")"),
paste0("Static (n = ", df.n$n.id[2], ", ", df.n$n.recs[2], ")"),
paste0("Interactive (n = ", df.n$n.id[3], ", ", df.n$n.recs[3], ")"))
# Power analysis
p1 = seq(0.1, 0.5, by=0.05) #Baseline failure rate
delta = 0.05 # Minimum detectable effect
alpha = 0.05
power = 0.9
n=c()
for (i in 1:length(p1)){
out = power.prop.test(p1=p1[i], p2=p1[i]-delta, sig.level = alpha, power=power, alternative="two.sided")
n = c(n, out$n)
}
table.power = data.frame(p = p1, n = n %>% ceiling())
colnames(table.power) = c("Baseline failure rate", "Sample size per group")
n=c()
for (i in 1:length(p1)){
out = power.prop.test(p1=p1[i], p2=p1[i]-delta, sig.level = alpha/3, power=power, alternative="two.sided")
n = c(n, out$n)
}
table.power2 = data.frame(p = p1, n = n %>% ceiling())
colnames(table.power2) = c("Baseline failure rate", "Sample size per group")