-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_posttest_analyses.R
More file actions
168 lines (139 loc) · 6.15 KB
/
Copy path02_posttest_analyses.R
File metadata and controls
168 lines (139 loc) · 6.15 KB
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
rm(list=ls())
load('postdatL.rda')
############################
### Log-linear modelling ###
############################
tabdat = xtabs(~ Correct + IsCorrect + OldNew + GROUP, data=postdatL)
ll.indep = loglin(tabdat, list(c(1), c(2,3,4)), param=TRUE, fit=TRUE)
ll.indep$lrt # [1] 832.9085
1 - pchisq(ll.indep$lrt, ll.indep$df) # [1] 0
ll.32 = loglin(tabdat, list(c(1,3,2), c(2,3,4)), param=TRUE, fit=TRUE)
ll.32$lrt # [1] 13.12575
1 - pchisq(ll.32$lrt, ll.32$df) # [1] 0.01067743
ll.32and4 = loglin(tabdat, list(c(1,2,3), c(1,4), c(2,3,4)), param=TRUE, fit=TRUE)
ll.32and4$lrt # [1] 2.853522
1 - pchisq(ll.32and4$lrt, ll.32and4$df) # [1] 0.4147649
# does this model improve fit significantly?
1 - pchisq(abs(ll.32and4$lrt-ll.32$lrt),
abs(ll.32and4$df-ll.32$df))
# [1] 0.001350477 --- YES
### Plots ###
require(ggplot2)
require(grid)
freqdat = data.frame(cbind(data.frame(tabdat),
'loglinFreq'=data.frame(ll.32and4$fit)[,5]))
freqdat$SameCorr = interaction(freqdat$OldNew, freqdat$IsCorrect)
freqdat$SameCorr = factor(freqdat$SameCorr,
levels=c('old.correct','old.incorrect','new.correct','new.incorrect'))
lex = droplevels(freqdat[freqdat$GROUP=='LEX',])
gramm = droplevels(freqdat[freqdat$GROUP=='GRAM',])
p1 <- ggplot(data=lex, aes(x=SameCorr, y=loglinFreq, fill=factor(Correct))) +
geom_bar(position=position_dodge(.9), width=0.5, stat='identity') +
scale_x_discrete(name='Item', guide=guide_axis(n.dodge=2),
breaks=c('old.correct','old.incorrect','new.correct','new.incorrect'),
labels=c('seen.gramm','seen.ungramm','unseen.gramm','unseen.ungramm')) +
scale_y_continuous('Predicted Frequency', limits=c(0, 650)) +
scale_fill_manual(values=c('#b2182b','#1b7837'),
name='Participants\'\nanswers:',
breaks=c(0,1),
labels=c('incorrect','correct')) +
ggtitle('Lexical training') +
theme(legend.position='none',
plot.title=element_text(hjust=0.5),
axis.text=element_text(size=12),
axis.title=element_text(size=14),
legend.text=element_text(size=10))
p2 <- ggplot(data=gramm, aes(x=SameCorr, y=loglinFreq, fill=factor(Correct))) +
geom_bar(position=position_dodge(.9), width=0.5, stat='identity') +
scale_x_discrete(name='Item', guide=guide_axis(n.dodge=2),
breaks=c('old.correct','old.incorrect','new.correct','new.incorrect'),
labels=c('seen.gramm','seen.ungramm','unseen.gramm','unseen.ungramm')) +
scale_y_continuous('Predicted Frequency', limits=c(0, 650)) +
scale_fill_manual(values=c('#b2182b','#1b7837'),
name='Participants\'\nanswers:',
breaks=c(0,1),
labels=c('incorrect','correct')) +
ggtitle('Grammatical training') +
theme(plot.title=element_text(hjust=0.5),
axis.text=element_text(size=12),
axis.title=element_text(size=14),
legend.text=element_text(size=10))
pushViewport(viewport(
layout=grid.layout(1, 2, heights=unit(c(5), 'null'),
widths=unit(c(5,6.18), 'null'))))
print(p1, vp=viewport(layout.pos.row=1, layout.pos.col=1))
print(p2, vp=viewport(layout.pos.row=1, layout.pos.col=2))
################################################################################
#######################################
### Logistic mixed effect modelling ###
#######################################
rm(list=ls())
require(brms)
require(posterior)
load('postdatL.rda')
# releveling and sum-coding
postdatL$GROUP = relevel(postdatL$GROUP, ref='LEX')
contrasts(postdatL$GROUP) = 'contr.sum'
contrasts(postdatL$OldNew) = 'contr.sum'
contrasts(postdatL$IsCorrect) = 'contr.sum'
# scaling the order
postdatL$Order.z = as.numeric(scale(postdatL$Order))
# # Note: running this model can take a lot of time!
# summary.brmMain <- summary(brmMain <- brm(Correct ~
# Order.z +
# GROUP + OldNew + IsCorrect +
# s(SentenceID, bs='re') +
# s(Order.z, ID, bs='fs', m=1),
# family='bernoulli',
# data=postdatL,
# chains=4, iter=4000, cores=4,
# save_pars=save_pars(all=TRUE),
# control=list(adapt_delta=0.98,
# max_treedepth=10)))
# save(brmMain, file='brmMain.rda')
# save(summary.brmMain, file='summary.brmMain.rda')
load('summary.brmMain.rda')
summary.brmMain
# Smooth Terms:
# Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
# sds(sSentenceID_1) 0.90 0.10 0.73 1.11 1.00 2528 4234
# sds(sOrder.zID_1) 0.29 0.20 0.01 0.72 1.00 2354 3481
# sds(sOrder.zID_2) 3.81 0.51 2.86 4.88 1.00 3669 5626
#
# Population-Level Effects:
# Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
# Intercept 0.48 0.13 0.23 0.74 1.00 2182 3625
# Order.z 0.06 0.12 -0.17 0.30 1.00 2154 3633
# GROUP1 -0.12 0.06 -0.23 -0.01 1.00 5083 5905
# OldNew1 0.74 0.12 0.51 0.99 1.00 1757 2660
# IsCorrect1 0.65 0.12 0.42 0.89 1.00 1953 3332
### Plots ###
require(ggplot2)
require(grid)
load('brmMain.rda')
condition = make_conditions(brmMain, 'GROUP')
combinedPlot = plot(conditional_effects(brmMain,
effects=c('OldNew:IsCorrect'), conditions=condition),
ask=FALSE, plot=FALSE)
comboPlot = combinedPlot[[1]] +
scale_x_discrete(name='Seen or unseen items',
breaks=c('old','new'),
labels=c('seen','unseen')) +
scale_y_continuous('Correct answers', limits=c(0.30, 1.00)) +
scale_fill_manual(name='Item\ngrammaticality:',
breaks=c('correct','incorrect'),
labels=c('grammatical','ungrammatical'),
values=c('#b2182b','#1b7837')) +
scale_color_manual(name='Item\ngrammaticality:',
breaks=c('correct','incorrect'),
labels=c('grammatical','ungrammatical'),
values=c('#b2182b','#1b7837')) +
facet_wrap(.~GROUP,
labeller=as_labeller(c(LEX='Lexical training',GRAM='Grammatical training'))) +
guides(fill=guide_legend(title='Item\ngrammaticality:'),
color=guide_legend(title='Item\ngrammaticality:')) +
theme(plot.title=element_text(size=14,hjust=0.5),
axis.text=element_text(size=12),
axis.title=element_text(size=14),
legend.text=element_text(size=10))
plot(comboPlot)