Skip to content

Commit 5de6427

Browse files
committed
update MPT models
1 parent c962bc2 commit 5de6427

File tree

3 files changed

+115
-86
lines changed

3 files changed

+115
-86
lines changed

Bayesian_Cognitive_Modeling/CaseStudies/MPT/MPT_1_Stan.R

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ n <- sum(k)
3838
data <- list(k=k, n=n) # To be passed on to Stan
3939

4040
myinits <- list(
41-
list(c=.5, r=.5, u=.5))
41+
list(c=.5, r=.5, u=.5),
42+
list(c=.2, r=.2, u=.2),
43+
list(c=.8, r=.8, u=.8))
4244

4345
parameters <- c("c", "r", "u") # Parameters to be monitored
4446

@@ -48,8 +50,8 @@ samples_1 <- stan(model_code=model,
4850
data=data,
4951
init=myinits, # If not specified, gives random inits
5052
pars=parameters,
51-
iter=11000,
52-
chains=1,
53+
iter=6000,
54+
chains=3,
5355
thin=1,
5456
warmup=1000, # Stands for burn-in; Default = iter/2
5557
# seed=123 # Setting seed; Default is random seed
@@ -63,8 +65,8 @@ samples_2 <- stan(fit=samples_1,
6365
data=data,
6466
init=myinits, # If not specified, gives random inits
6567
pars=parameters,
66-
iter=11000,
67-
chains=1,
68+
iter=6000,
69+
chains=3,
6870
thin=1,
6971
warmup=1000, # Stands for burn-in; Default = iter/2
7072
# seed=123 # Setting seed; Default is random seed
@@ -74,12 +76,12 @@ k <- c(243, 64, 65, 48)
7476
n <- sum(k)
7577
data <- list(k=k, n=n) # To be passed on to Stan
7678

77-
samples_3 <- stan(fit=samples_1,
79+
samples_6 <- stan(fit=samples_1,
7880
data=data,
7981
init=myinits, # If not specified, gives random inits
8082
pars=parameters,
81-
iter=11000,
82-
chains=1,
83+
iter=6000,
84+
chains=3,
8385
thin=1,
8486
warmup=1000, # Stands for burn-in; Default = iter/2
8587
# seed=123 # Setting seed; Default is random seed
@@ -93,19 +95,31 @@ u1 <- extract(samples_1)$u
9395
c2 <- extract(samples_2)$c
9496
r2 <- extract(samples_2)$r
9597
u2 <- extract(samples_2)$u
96-
c3 <- extract(samples_3)$c
97-
r3 <- extract(samples_3)$r
98-
u3 <- extract(samples_3)$u
98+
c6 <- extract(samples_6)$c
99+
r6 <- extract(samples_6)$r
100+
u6 <- extract(samples_6)$u
99101

100-
windows(14, 7)
101-
layout(matrix(1:3, 1, 3, byrow=T))
102-
plot(density(c3), xlim=c(0, 1), lty="dotted")
102+
windows(10, 5)
103+
layout(matrix(1:3, 1, 3, byrow=TRUE))
104+
par(cex=1.1, mar=c(2, 2, 1, 1), mgp=c(.8, .1, 0))
105+
106+
plot(density(c6), xlim=c(0, 1), ylim=c(0, 15), lty="dotted",
107+
ylab="Probability Density", xlab="c", main="", yaxt="n", xaxt="n")
103108
lines(density(c2), lty="dashed")
104109
lines(density(c1))
105-
plot(density(r3), xlim=c(0, 1), lty="dotted")
110+
axis(1, seq(0, 1, by=.2), tick=FALSE)
111+
legend(0, 15.5, c("Trial1", "Trial 2", "Trial 6"),lty = c(1, 2, 3),col=c("black"),
112+
text.col = "black", bty = "n")
113+
114+
plot(density(r6), xlim=c(0, 1), ylim=c(0, 15), lty="dotted", ylab="", xlab="r",
115+
yaxt="n", xaxt="n", main="")
106116
lines(density(r2), lty="dashed")
107117
lines(density(r1))
108-
plot(density(u3), xlim=c(0, 1), lty="dotted")
118+
axis(1, seq(0, 1, by=.2), tick=FALSE)
119+
120+
plot(density(u6), xlim=c(0, 1), ylim=c(0, 15), lty="dotted", ylab="", xlab="u",
121+
yaxt="n", xaxt="n", main="")
109122
lines(density(u2), lty="dashed")
110123
lines(density(u1))
124+
axis(1, seq(0, 1, by=.2), tick=FALSE)
111125

Bayesian_Cognitive_Modeling/CaseStudies/MPT/MPT_2_Stan.R

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -91,47 +91,33 @@ generated quantities {
9191
sigmau <- xiuhat * sqrt(Sigma[3,3]);
9292
}"
9393

94-
trial_1 <- list(subjs=21, items=20, response=structure(
95-
.Data=c(2,4,4,10,2,1,3,14,2,2,5,11,6,0,4,10,1,0,4,15,1,0,2,17,1,2,4,13,4,1,
96-
6,9,5,1,4,10,1,0,9,10,5,0,3,12,0,1,6,13,1,5,7,7,1,1,4,14,2,2,3,13,2,
97-
1,5,12,2,0,6,12,1,0,5,14,2,1,8,9,3,0,2,15,1,2,3,14),
98-
.Dim = c(4, 21)))
99-
100-
trial_2 <- list(subjs=21, items=20, response=structure(
101-
.Data = c(7,5,3,5,5,2,3,10,6,2,7,5,9,4,2,5,2,2,7,9,1,3,3,13,5,0,5,10,7,3,4,
102-
6,7,3,6,4,4,1,10,5,9,1,2,8,3,1,6,10,3,5,9,3,2,0,6,12,8,0,3,9,3,2,
103-
7,8,7,1,5,7,2,1,6,11,5,3,5,7,5,0,6,9,6,2,2,10),
104-
.Dim = c(4, 21)))
105-
106-
trial_6 <- list(subjs=21, items=20, response=structure(
107-
.Data = c(14,3,1,2,12,3,1,4,18,0,1,1,15,3,0,2,7,1,10,2,3,6,11,0,8,4,3,5,17,
108-
1,1,1,13,4,3,0,11,6,1,2,16,1,2,1,10,1,3,6,7,13,0,0,8,4,3,5,16,1,
109-
1,2,5,4,7,4,15,0,5,0,6,3,6,5,17,2,0,1,17,1,0,2,8,3,6,3),
110-
.Dim = c(4, 21)))
111-
112-
response_1 <- t(trial_1$response)
113-
response_2 <- t(trial_2$response)
114-
response_6 <- t(trial_6$response)
94+
### Riefer et al (2002) data:
95+
load("dataMPT.Rdata")
11596

11697
nparams <- 3 # Number of free parameters per participant: c_i, r_i, u_i
11798
I <- diag(3) # Identity matrix for Wishart
11899

119-
k <- response_1
120-
nsubjs <- nrow(k) # Number of word pairs per participant
121-
122-
# To be passed on to Stan
123-
data <- list(k=k, nparams=nparams, nsubjs=nsubjs, I=I)
124-
125100
myinits <- list(
126-
list(deltahat=matrix(0, 21, 3), Sigma=diag(3),
127-
muchat=0, murhat=0, muuhat=0, xichat=1, xirhat=1, xiuhat=1))
128-
101+
list(deltahat=matrix(rnorm(21 * 3), 21, 3), Sigma=diag(3),
102+
muchat=rnorm(1), murhat=rnorm(1), muuhat=rnorm(1),
103+
xichat=runif(1), xirhat=runif(1), xiuhat=runif(1)),
104+
list(deltahat=matrix(rnorm(21 * 3), 21, 3), Sigma=diag(3),
105+
muchat=rnorm(1), murhat=rnorm(1), muuhat=rnorm(1),
106+
xichat=runif(1), xirhat=runif(1), xiuhat=runif(1)),
107+
list(deltahat=matrix(rnorm(21 * 3), 21, 3), Sigma=diag(3),
108+
muchat=rnorm(1), murhat=rnorm(1), muuhat=rnorm(1),
109+
xichat=runif(1), xirhat=runif(1), xiuhat=runif(1)))
110+
129111
# Parameters to be monitored
130112
parameters <- c("muc", "mur", "muu", "sigmac", "sigmar", "sigmau", "rho")
131113

132114
# Run higher iterations for better estimate
133-
myiterations <- 3300
134-
mywarmup <- 300
115+
myiterations <- 2100
116+
mywarmup <- 100
117+
118+
k <- response_1
119+
nsubjs <- nrow(k) # Number of word pairs per participant
120+
data <- list(k=k, nparams=nparams, nsubjs=nsubjs, I=I) # To be passed on to Stan
135121

136122
# The following command calls Stan with specific options.
137123
# For a detailed description type "?stan".
@@ -140,37 +126,37 @@ samples_1 <- stan(model_code=model,
140126
init=myinits, # If not specified, gives random inits
141127
pars=parameters,
142128
iter=myiterations,
143-
chains=1,
129+
chains=3,
144130
thin=1,
145131
warmup=mywarmup, # Stands for burn-in; Default = iter/2
146132
# seed=123 # Setting seed; Default is random seed
147133
)
148134

149135
k <- response_2
150136
nsubjs <- nrow(k) # Number of word pairs per participant
151-
# To be passed on to Stan
152-
data <- list(k=k, nparams=nparams, nsubjs=nsubjs, I=I)
137+
data <- list(k=k, nparams=nparams, nsubjs=nsubjs, I=I) # To be passed on to Stan
138+
153139
samples_2 <- stan(fit=samples_1,
154140
data=data,
155141
init=myinits, # If not specified, gives random inits
156142
pars=parameters,
157143
iter=myiterations,
158-
chains=1,
144+
chains=3,
159145
thin=1,
160146
warmup=mywarmup, # Stands for burn-in; Default = iter/2
161147
# seed=123 # Setting seed; Default is random seed
162148
)
163149

164150
k <- response_6
165151
nsubjs <- nrow(k) # Number of word pairs per participant
166-
# To be passed on to Stan
167-
data <- list(k=k, nparams=nparams, nsubjs=nsubjs, I=I)
152+
data <- list(k=k, nparams=nparams, nsubjs=nsubjs, I=I) # To be passed on to Stan
153+
168154
samples_6 <- stan(fit=samples_1,
169155
data=data,
170156
init=myinits, # If not specified, gives random inits
171157
pars=parameters,
172158
iter=myiterations,
173-
chains=1,
159+
chains=3,
174160
thin=1,
175161
warmup=mywarmup, # Stands for burn-in; Default = iter/2
176162
# seed=123 # Setting seed; Default is random seed
@@ -188,35 +174,64 @@ muc6 <- extract(samples_6)$muc
188174
mur6 <- extract(samples_6)$mur
189175
muu6 <- extract(samples_6)$muu
190176

177+
rhocr1 <- extract(samples_1)$rho[, 1, 2]
178+
rhocu1 <- extract(samples_1)$rho[, 1, 3]
179+
rhoru1 <- extract(samples_1)$rho[, 2, 3]
180+
rhocr2 <- extract(samples_2)$rho[, 1, 2]
181+
rhocu2 <- extract(samples_2)$rho[, 1, 3]
182+
rhoru2 <- extract(samples_2)$rho[, 2, 3]
183+
rhocr6 <- extract(samples_6)$rho[, 1, 2]
184+
rhocu6 <- extract(samples_6)$rho[, 1, 3]
185+
rhoru6 <- extract(samples_6)$rho[, 2, 3]
186+
191187
#### Plots posteriors of the group--level c, r, and u parameters
192-
windows(14,7)
193-
layout(matrix(1:3,1,3,byrow=T))
194-
par(mar = c(4.25, 3, 1, 1))
195-
par(cex.axis=0.9)
196-
plot(density((muc1)),ylim = c(0,6),xlim=c(0,1), axes=F,xlab="c",ylab="",
197-
main="")
198-
axis(1,at=seq(0,1,0.2))
199-
axis(2,at = c(0,12),labels=F,lwd.ticks=0)
200-
mtext("Density",2,1,cex=0.55,las=0)
201-
par(cex=0.60)
202-
legend(0.3, 10, c("Trial1","Trial 2","Trial 3"),lty = c(1,2,3),col=c("black"),
203-
text.col = "black")
204-
par(cex=0.65)
205-
lines(density((muc2)),lty=2)
206-
lines(density((muc6)),lty=3)
207-
208-
plot(density((mur1),to=0.75),ylim = c(0,13),xlim=c(0,1), axes=F,xlab="r",
209-
ylab="",main="")
210-
axis(1,at=seq(0,1,0.2))
211-
axis(2,at = c(0,13),labels=F,lwd.ticks=0)
212-
mtext("Density",2,1,cex=0.55,las=0)
213-
lines(density((mur2)),lty=2)
214-
lines(density((mur6)),lty=3)
215-
216-
plot(density((muu1)),ylim = c(0,9),xlim=c(0,1), axes=F,xlab="u",ylab="",
217-
main="")
218-
axis(1,at=seq(0,1,0.2))
219-
axis(2,at = c(0,10),labels=F,lwd.ticks=0)
220-
mtext("Density",2,1,cex=0.55,las=0)
221-
lines(density((muu2)),lty=2)
222-
lines(density((muu6)),lty=3)
188+
windows(10, 5)
189+
layout(matrix(1:3, 1, 3, byrow=TRUE))
190+
par(cex=1.1, mar=c(2, 2, 1, 1), mgp=c(.8, .1, 0))
191+
192+
plot(density(muc6), xlim=c(0, 1), ylim=c(0, 7), lty="dotted",
193+
ylab="Probability Density", xlab=expression(mu[c]), main="",
194+
yaxt="n", xaxt="n")
195+
lines(density(muc2), lty="dashed")
196+
lines(density(muc1))
197+
axis(1, seq(0, 1, by=.2), tick=FALSE)
198+
199+
plot(density(mur6), xlim=c(0, 1), ylim=c(0, 15), lty="dotted", ylab="",
200+
xlab=expression(mu[r]), yaxt="n", xaxt="n", main="")
201+
lines(density(mur2), lty="dashed")
202+
lines(density(mur1))
203+
axis(1, seq(0, 1, by=.2), tick=FALSE)
204+
legend(0, 15.5, c("Trial 1", "Trial 2", "Trial 6"), lty = c(1, 2, 3),
205+
col=c("black"), text.col = "black", bty = "n")
206+
207+
plot(density(muu6), xlim=c(0, 1), ylim=c(0, 9), lty="dotted", ylab="",
208+
xlab=expression(mu[u]), yaxt="n", xaxt="n", main="")
209+
lines(density(muu2), lty="dashed")
210+
lines(density(muu1))
211+
axis(1, seq(0, 1, by=.2), tick=FALSE)
212+
213+
#### Plots posteriors for the correlations
214+
windows(10, 5)
215+
layout(matrix(1:3, 1, 3, byrow=TRUE))
216+
par(cex=1.1, mar=c(2, 2, 1, 1), mgp=c(.8, .1, 0))
217+
218+
plot(density(rhocr6), xlim=c(-1, 1), ylim=c(0, 1.5), lty="dotted",
219+
ylab="Probability Density", xlab=expression(italic(rho[cr])), main="",
220+
yaxt="n", xaxt="n")
221+
lines(density(rhocr2), lty="dashed")
222+
lines(density(rhocr1))
223+
axis(1, seq(-1, 1, by=.5), tick=FALSE)
224+
225+
plot(density(rhocu6), xlim=c(-1, 1), ylim=c(0, 1.5), lty="dotted", ylab="",
226+
xlab=expression(italic(rho[cu])), yaxt="n", xaxt="n", main="")
227+
lines(density(rhocu2), lty="dashed")
228+
lines(density(rhocu1))
229+
axis(1, seq(-1, 1, by=.5), tick=FALSE)
230+
legend(-1, 1.55, c("Trial 1", "Trial 2", "Trial 6"), lty = c(1, 2, 3),
231+
col=c("black"), text.col = "black", bty = "n")
232+
233+
plot(density(rhoru6), xlim=c(-1, 1), ylim=c(0, 1.5), lty="dotted", ylab="",
234+
xlab=expression(italic(rho[ru])), yaxt="n", xaxt="n", main="")
235+
lines(density(rhoru2), lty="dashed")
236+
lines(density(rhoru1))
237+
axis(1, seq(-1, 1, by=.5), tick=FALSE)
65.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)