質問編集履歴

1

内容

2016/06/26 04:30

投稿

kkoorroo
kkoorroo

スコア8

test CHANGED
File without changes
test CHANGED
@@ -26,11 +26,7 @@
26
26
 
27
27
  ```R
28
28
 
29
- #### R2Winbugs Code for a Bayesian hierarchical test of the Geurts data.
30
29
 
31
- #### Do children with ADHD perform worse on the Wisconsin Card Sorting Task
32
-
33
- #### than children who develop typically (i.e., normal controls)?
34
30
 
35
31
 
36
32
 
@@ -47,208 +43,6 @@
47
43
  bugsdir <- "C:/Program Files/WinBUGS14"
48
44
 
49
45
 
50
-
51
- ### Geurts data:
52
-
53
- # Normal Controls:
54
-
55
- num.errors <- c(15,10, 61,11, 60, 44, 63, 70, 57,11, 67, 21, 89,12, 63, 11, 96,10, 37,19, 44,18, 78, 27, 60,14)
56
-
57
- nc <- c(89,74,128,87,128,121,128,128,128,78,128,106,128,83,128,100,128,73,128,86,128,86,128,100,128,79)
58
-
59
- kc <- nc - num.errors
60
-
61
- nsc <- length(kc)
62
-
63
- # ADHD:
64
-
65
- num.errors <- c(88, 50, 58,17, 40, 18,21, 50, 21, 69,19, 29,11, 76, 46, 36, 37, 72,27, 92,13, 39, 53, 31, 49,
66
-
67
- 57,17,10,12,21, 39, 43, 49,17, 39,13, 68, 24, 21,27, 48, 54, 41, 75, 38, 76,21, 41, 61,24, 28,21)
68
-
69
- na <- c(128,128,128,86,128,117,89,128,110,128,93,107,87,128,128,113,128,128,98,128,93,116,128,116,128,
70
-
71
- 128,93,86,86,96,128,128,128,86,128,78,128,111,100,95,128,128,128,128,128,128,98,127,128,93,110,96)
72
-
73
- ka <- na - num.errors
74
-
75
- nsa <- length(ka)
76
-
77
-
78
-
79
- # two-sided p-value = .72
80
-
81
- t.test(kc/nc, ka/na, alternative = c("two.sided"), paired=F)
82
-
83
-
84
-
85
- data <- list("nc","kc","nsc","na","ka","nsa") # to be passed on to WinBUGS
86
-
87
-
88
-
89
- myinits <- list(
90
-
91
- list(mu = 0, sigma = 1, delta = 0),
92
-
93
- list(mu = -.8, sigma = 2, delta = -.5),
94
-
95
- list(mu = .8, sigma = 1.5, delta = .5))
96
-
97
-
98
-
99
- parameters <- c("delta")
100
-
101
-
102
-
103
- # The following command calls WinBUGS with specific options.
104
-
105
- # For a detailed description see Sturtz, Ligges, & Gelman (2005).
106
-
107
- samples <- bugs(data, inits=myinits, parameters,
108
-
109
- model.file ="Geurts.txt",
110
-
111
- n.chains=3, n.iter=100000, n.burnin=1000, n.thin=1,
112
-
113
- DIC=T, bugs.directory=bugsdir, #DIC=T or else error message
114
-
115
- codaPkg=F, debug=T)
116
-
117
- # Now the values for delta are in the "samples" object, ready for inspection.
118
-
119
-
120
-
121
- # Collect posterior samples across all chains:
122
-
123
- delta.posterior <- samples$sims.list$delta
124
-
125
-
126
-
127
- #============ Unrestricted analysis ===========================
128
-
129
- #============ BFs based on logspline fit ===========================
130
-
131
- library(polspline) # this package can be installed from within R
132
-
133
- fit.posterior <- logspline(delta.posterior)
134
-
135
-
136
-
137
- # 95% confidence interval:
138
-
139
- x0 <- qlogspline(0.025,fit.posterior)
140
-
141
- x1 <- qlogspline(0.975,fit.posterior)
142
-
143
-
144
-
145
- posterior <- dlogspline(0, fit.posterior) # this gives the pdf at point delta = 0
146
-
147
- prior <- dnorm(0) # height of order-restricted prior at delta = 0
148
-
149
- BF01 <- posterior/prior
150
-
151
- # BF01 = 3.96, BF10 = 0.25
152
-
153
-
154
-
155
- #####################################################################
156
-
157
- ### Plot Prior and Posterior
158
-
159
- #####################################################################
160
-
161
- par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
162
-
163
- font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
164
-
165
- Nbreaks <- 80
166
-
167
- y <- hist(delta.posterior, Nbreaks, plot=F)
168
-
169
- plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=2,
170
-
171
- xlim=c(-3,3), ylim=c(0,2), xlab=" ", ylab="Density", axes=F)
172
-
173
- axis(1, at = c(-3,-2,-1,0,1,2,3), lab=c("-3","-2", "-1", "0", "1", "2", "3"))
174
-
175
- axis(2)
176
-
177
- mtext(expression(delta), side=1, line = 2.8, cex=2)
178
-
179
- #now bring in log spline density estimation:
180
-
181
- par(new=T)
182
-
183
- plot(fit.posterior, ylim=c(0,2), xlim=c(-3,3), lty=1, lwd=1, axes=F)
184
-
185
- points(0, dlogspline(0, fit.posterior),pch=19, cex=2)
186
-
187
- # plot the prior:
188
-
189
- par(new=T)
190
-
191
- plot ( function( x ) dnorm( x, 0, 1 ), -3, 3, xlim=c(-3,3), ylim=c(0,2), lwd=2, lty=1, ylab=" ", xlab = " ")
192
-
193
- points(0, dnorm(0), pch=19, cex=2)
194
-
195
-
196
-
197
- text(0.3, 1.5, labels = "Posterior", cex = 1.5, pos=4)
198
-
199
- text(1, 0.3, labels = "Prior", cex=1.5, pos=4)
200
-
201
-
202
-
203
-
204
-
205
- Geurts.txtは以下の内容です(これは自分で書きました).
206
-
207
- # Geurts
208
-
209
- model{
210
-
211
- for (i in 1:nsc){
212
-
213
- kc[i] ~ dbin(thetac[i], nc[i])
214
-
215
- thetac[i] <- phi(phic[i])
216
-
217
- phic[i] ~ dnorm(muc, lambda)
218
-
219
- }
220
-
221
- for (j in 1:nsa){
222
-
223
- ka[j] ~ dbin(thetaa[j], na[j])
224
-
225
- thetaa[j] <- phi(phia[j])
226
-
227
- phia[j] ~ dnorm(mua, lambda)
228
-
229
- }
230
-
231
- muc <- mu+alpha/2
232
-
233
- mua <- mu-alpha/2
234
-
235
- # Priors
236
-
237
- mu ~ dnorm(0,1)
238
-
239
- sigma ~ dunif(0,10)
240
-
241
- alpha <- delta*sigma
242
-
243
- lambda <- pow(sigma,-2)
244
-
245
- delta ~dnorm(0,1)
246
-
247
- # sampling from Prior Distribution for Delta
248
-
249
- deltaprior ~ dnorm(0,1)
250
-
251
- }
252
46
 
253
47
 
254
48