Skip to content

Commit a85ab98

Browse files
committed
add models for 3rd chapter
1 parent 2d783a3 commit a85ab98

File tree

9 files changed

+1207
-1
lines changed

9 files changed

+1207
-1
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# clears workspace:
2+
rm(list=ls())
3+
4+
library(rstan)
5+
6+
model <- "
7+
// One-Sample Order Restricted Comparsion of Means
8+
data {
9+
int<lower=0> ndata;
10+
vector[ndata] x;
11+
}
12+
parameters {
13+
real sigmatmp;
14+
real<upper=0> delta;
15+
}
16+
transformed parameters {
17+
real<lower=0> sigma;
18+
real mu;
19+
20+
sigma <- fabs(sigmatmp);
21+
mu <- delta * sigma;
22+
}
23+
model {
24+
// Delta and sigma Come From (Half) Cauchy Distributions
25+
sigmatmp ~ cauchy(0, 1);
26+
delta ~ cauchy(0, 1)T[,0];
27+
28+
// Data
29+
x ~ normal(mu, sigma);
30+
}"
31+
32+
# Read data Dr. Smith
33+
Winter <- c(-0.05,0.41,0.17,-0.13,0.00,-0.05,0.00,0.17,0.29,0.04,0.21,0.08,0.37,
34+
0.17,0.08,-0.04,-0.04,0.04,-0.13,-0.12,0.04,0.21,0.17,0.17,0.17,
35+
0.33,0.04,0.04,0.04,0.00,0.21,0.13,0.25,-0.05,0.29,0.42,-0.05,0.12,
36+
0.04,0.25,0.12)
37+
38+
Summer <- c(0.00,0.38,-0.12,0.12,0.25,0.12,0.13,0.37,0.00,0.50,0.00,0.00,-0.13,
39+
-0.37,-0.25,-0.12,0.50,0.25,0.13,0.25,0.25,0.38,0.25,0.12,0.00,0.00,
40+
0.00,0.00,0.25,0.13,-0.25,-0.38,-0.13,-0.25,0.00,0.00,-0.12,0.25,
41+
0.00,0.50,0.00)
42+
43+
x <- Winter - Summer # allowed because it is a within-subjects design
44+
x <- x / sd(x) # standardize
45+
ndata <- length(Winter) # number of subjects
46+
47+
data <- list(x=x, ndata=ndata) # to be passed on to Stan
48+
49+
myinits <- list(
50+
list(delta=-abs(rnorm(1,0,1)), deltaprior=-abs(rnorm(1,0,1)), sigmatmp=.1),
51+
list(delta=-abs(rnorm(1,0,1)), deltaprior=-abs(rnorm(1,0,1)), sigmatmp=.2),
52+
list(delta=-abs(rnorm(1,0,1)), deltaprior=-abs(rnorm(1,0,1)), sigmatmp=.3))
53+
54+
# Parameters to be monitored
55+
parameters <- c("delta")
56+
57+
# The following command calls Stan with specific options.
58+
# For a detailed description type "?rstan".
59+
samples <- stan(model_code=model,
60+
data=data,
61+
init=myinits, # If not specified, gives random inits
62+
pars=parameters,
63+
iter=30000,
64+
chains=3,
65+
thin=1,
66+
# warmup=100, # Stands for burn-in; Default = iter/2
67+
# seed=123 # Setting seed; Default is random seed
68+
)
69+
# Now the values for the monitored parameters are in the "samples" object,
70+
# ready for inspection.
71+
72+
plot(samples)
73+
74+
# Collect posterior samples across all chains:
75+
delta.posterior <- extract(samples)$delta
76+
77+
#============ BFs based on logspline fit ===========================
78+
library(polspline) # this package can be installed from within R
79+
fit.posterior <- logspline(delta.posterior)
80+
81+
#============ BFs based on logspline fit ===========================
82+
fit.posterior <- logspline(delta.posterior,ubound=0) # NB. note the bound
83+
84+
# 95% confidence interval:
85+
x0 <- qlogspline(0.025,fit.posterior)
86+
x1 <- qlogspline(0.975,fit.posterior)
87+
88+
posterior <- dlogspline(0, fit.posterior) # this gives the pdf at point delta = 0
89+
prior <- 2*dcauchy(0) # height of order--restricted prior at delta = 0
90+
BF01 <- posterior/prior
91+
BF01
92+
93+
#============ Plot Prior and Posterior ===========================
94+
par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
95+
font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
96+
xlow <- -3
97+
xhigh <- 0
98+
yhigh <- 12
99+
Nbreaks <- 80
100+
y <- hist(delta.posterior, Nbreaks, plot=F)
101+
plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=2,
102+
xlim=c(xlow,xhigh), ylim=c(0,yhigh), xlab=" ", ylab="Density", axes=F)
103+
axis(1, at = c(-3,-2,-1,0), lab=c("-3","-2","-1","0"))
104+
axis(2)
105+
mtext(expression(delta), side=1, line = 2.8, cex=2)
106+
#now bring in log spline density estimation:
107+
par(new=T)
108+
plot(fit.posterior, ylim=c(0,yhigh), xlim=c(xlow,xhigh), lty=1, lwd=1, axes=F)
109+
points(0, dlogspline(0, fit.posterior),pch=19, cex=2)
110+
# plot the prior:
111+
par(new=T)
112+
plot ( function( x ) 2*dcauchy( x, 0, 1 ), xlow, xhigh, ylim=c(0,yhigh),
113+
xlim=c(xlow,xhigh), lwd=2, lty=1, ylab=" ", xlab = " ", axes=F)
114+
axis(1, at = c(-3,-2,-1,0), lab=c("-3","-2","-1","0"))
115+
axis(2)
116+
points(0, 2*dcauchy(0), pch=19, cex=2)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# clears workspace:
2+
rm(list=ls())
3+
4+
library(rstan)
5+
6+
model <- "
7+
// One-Sample Comparsion of Means
8+
data {
9+
int<lower=0> ndata;
10+
vector[ndata] x;
11+
}
12+
parameters {
13+
real sigmatmp;
14+
real delta;
15+
}
16+
transformed parameters {
17+
real mu;
18+
real<lower=0> sigma;
19+
20+
sigma <- fabs(sigmatmp);
21+
mu <- delta * sigma;
22+
}
23+
model {
24+
// Delta and sigma Come From (Half) Cauchy Distributions
25+
sigmatmp ~ cauchy(0, 1);
26+
delta ~ cauchy(0, 1);
27+
// Data
28+
x ~ normal(mu, sigma);
29+
}"
30+
31+
# Read data Dr. Smith
32+
Winter <- c(-0.05,0.41,0.17,-0.13,0.00,-0.05,0.00,0.17,0.29,0.04,0.21,0.08,0.37,
33+
0.17,0.08,-0.04,-0.04,0.04,-0.13,-0.12,0.04,0.21,0.17,0.17,0.17,
34+
0.33,0.04,0.04,0.04,0.00,0.21,0.13,0.25,-0.05,0.29,0.42,-0.05,0.12,
35+
0.04,0.25,0.12)
36+
37+
Summer <- c(0.00,0.38,-0.12,0.12,0.25,0.12,0.13,0.37,0.00,0.50,0.00,0.00,-0.13,
38+
-0.37,-0.25,-0.12,0.50,0.25,0.13,0.25,0.25,0.38,0.25,0.12,0.00,0.00,
39+
0.00,0.00,0.25,0.13,-0.25,-0.38,-0.13,-0.25,0.00,0.00,-0.12,0.25,
40+
0.00,0.50,0.00)
41+
42+
x <- Winter - Summer # allowed because it is a within-subjects design
43+
x <- x / sd(x) # standardize
44+
45+
ndata <- length(Winter) # number of subjects
46+
47+
data <- list(x=x, ndata=ndata) # to be passed on to Stan
48+
49+
myinits <- list(
50+
list(delta=rnorm(1,0,3), deltaprior=rnorm(1,0,3), sigmatmp = rnorm(1,0,1)),
51+
list(delta=rnorm(1,0,3), deltaprior=rnorm(1,0,3), sigmatmp = rnorm(1,0,1)),
52+
list(delta=rnorm(1,0,3), deltaprior=rnorm(1,0,3), sigmatmp = rnorm(1,0,1)))
53+
54+
# Parameters to be monitored
55+
parameters <- c("delta")
56+
57+
# The following command calls Stan with specific options.
58+
# For a detailed description type "?rstan".
59+
samples <- stan(model_code=model,
60+
data=data,
61+
init=myinits, # If not specified, gives random inits
62+
pars=parameters,
63+
iter=20000,
64+
chains=3,
65+
thin=1,
66+
# warmup=100, # Stands for burn-in; Default = iter/2
67+
# seed=123 # Setting seed; Default is random seed
68+
)
69+
# Now the values for the monitored parameters are in the "samples" object,
70+
# ready for inspection.
71+
72+
plot(samples)
73+
74+
# Collect posterior samples across all chains:
75+
delta.posterior <- extract(samples)$delta
76+
77+
#============ BFs based on logspline fit ===========================
78+
library(polspline) # this package can be installed from within R
79+
fit.posterior <- logspline(delta.posterior)
80+
81+
# 95% confidence interval:
82+
x0 <- qlogspline(0.025,fit.posterior)
83+
x1 <- qlogspline(0.975,fit.posterior)
84+
85+
posterior <- dlogspline(0, fit.posterior) # this gives the pdf at point delta = 0
86+
prior <- dcauchy(0) # height of order-restricted prior at delta = 0
87+
BF01 <- posterior/prior
88+
BF01
89+
90+
#============ Plot Prior and Posterior ===========================
91+
par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
92+
font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
93+
xlow <- -3
94+
xhigh <- 3
95+
yhigh <- 4
96+
Nbreaks <- 80
97+
y <- hist(delta.posterior, Nbreaks, plot=F)
98+
plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=2,
99+
xlim=c(xlow,xhigh), ylim=c(0,yhigh), xlab=" ", ylab="Density", axes=F)
100+
axis(1, at = c(-4,-3,-2,-1,0,1,2,3,4), lab=c("-4","-3","-2","-1","0",
101+
"1", "2", "3", "4"))
102+
axis(2)
103+
mtext(expression(delta), side=1, line = 2.8, cex=2)
104+
#now bring in log spline density estimation:
105+
par(new=T)
106+
plot(fit.posterior, ylim=c(0,yhigh), xlim=c(xlow,xhigh), lty=1, lwd=1, axes=F)
107+
points(0, dlogspline(0, fit.posterior),pch=19, cex=2)
108+
# plot the prior:
109+
par(new=T)
110+
plot(function( x ) dcauchy( x, 0, 1 ), xlow, xhigh, ylim=c(0,yhigh),
111+
xlim=c(xlow,xhigh), lwd=2, lty=1, ylab=" ", xlab = " ", axes=F)
112+
axis(1, at = c(-4,-3,-2,-1,0,1,2,3,4), lab=c("-4","-3","-2","-1","0",
113+
"1", "2", "3", "4"))
114+
axis(2)
115+
points(0, dcauchy(0), pch=19, cex=2)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# clears workspace:
2+
rm(list=ls())
3+
4+
library(rstan)
5+
6+
model <- "
7+
// Two-sample Comparison of Means
8+
data {
9+
int<lower=1> n1;
10+
int<lower=1> n2;
11+
vector[n1] x;
12+
vector[n2] y;
13+
}
14+
parameters {
15+
real mu;
16+
real sigmatmp;
17+
real delta;
18+
}
19+
transformed parameters {
20+
real<lower=0> sigma;
21+
real alpha;
22+
23+
sigma <- fabs(sigmatmp);
24+
alpha <- delta * sigma;
25+
}
26+
model {
27+
// Delta, mu, and sigma Come From (Half) Cauchy Distribution
28+
mu ~ cauchy(0, 1);
29+
sigmatmp ~ cauchy(0, 1);
30+
delta ~ cauchy(0, 1);
31+
32+
// Data
33+
x ~ normal(mu + alpha / 2, sigma);
34+
y ~ normal(mu - alpha / 2, sigma);
35+
}"
36+
37+
x <- c(70,80,79,83,77,75,84,78,75,75,78,82,74,81,72,70,75,72,76,77)
38+
y <- c(56,80,63,62,67,71,68,76,79,67,76,74,67,70,62,65,72,72,69,71)
39+
40+
n1 <- length(x)
41+
n2 <- length(y)
42+
43+
# Rescale
44+
y <- y - mean(x)
45+
y <- y / sd(x)
46+
x <- (x - mean(x)) / sd(x);
47+
48+
data <- list(x=x, y=y, n1=n1, n2=n2) # to be passed on to Stan
49+
50+
myinits <- list(
51+
list(delta=rnorm(1,0,3), deltaprior=rnorm(1,0,3), mu=rnorm(1,0,1),
52+
sigmatmp=runif(1,0,5)),
53+
list(delta=rnorm(1,0,3), deltaprior=rnorm(1,0,3), mu=rnorm(1,0,1),
54+
sigmatmp=runif(1,0,5)),
55+
list(delta=rnorm(1,0,3), deltaprior=rnorm(1,0,3), mu=rnorm(1,0,1),
56+
sigmatmp=runif(1,0,5)))
57+
58+
# Parameters to be monitored
59+
parameters <- c("delta")
60+
61+
# The following command calls Stan with specific options.
62+
# For a detailed description type "?rstan".
63+
samples <- stan(model_code=model,
64+
data=data,
65+
init=myinits, # If not specified, gives random inits
66+
pars=parameters,
67+
iter=20000,
68+
chains=3,
69+
thin=1,
70+
warmup=5000, # Stands for burn-in; Default = iter/2
71+
# seed=123 # Setting seed; Default is random seed
72+
)
73+
# Now the values for the monitored parameters are in the "samples" object,
74+
# ready for inspection.
75+
76+
plot(samples)
77+
78+
# Collect posterior samples across all chains:
79+
delta.posterior <- extract(samples)$delta
80+
81+
#============ BFs based on logspline fit ===========================
82+
library(polspline) # this package can be installed from within R
83+
fit.posterior <- logspline(delta.posterior)
84+
85+
# 95% confidence interval:
86+
x0 <- qlogspline(0.025,fit.posterior)
87+
x1 <- qlogspline(0.975,fit.posterior)
88+
89+
posterior <- dlogspline(0, fit.posterior) # this gives the pdf at point delta = 0
90+
prior <- dcauchy(0) # height of order--restricted prior at delta = 0
91+
BF01 <- posterior/prior
92+
BF01
93+
94+
#============ Plot Prior and Posterior ===========================
95+
par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
96+
font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
97+
xlow <- -3
98+
xhigh <- 3
99+
yhigh <- 2
100+
Nbreaks <- 80
101+
y <- hist(delta.posterior, Nbreaks, plot=F)
102+
plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=2,
103+
xlim=c(xlow,xhigh), ylim=c(0,yhigh), xlab=" ", ylab="Density", axes=F)
104+
axis(1, at = c(-4,-3,-2,-1,0,1,2,3,4), lab=c("-4","-3","-2","-1","0",
105+
"1", "2", "3", "4"))
106+
axis(2)
107+
mtext(expression(delta), side=1, line = 2.8, cex=2)
108+
#now bring in log spline density estimation:
109+
par(new=T)
110+
plot(fit.posterior, ylim=c(0,yhigh), xlim=c(xlow,xhigh), lty=1, lwd=1, axes=F)
111+
points(0, dlogspline(0, fit.posterior),pch=19, cex=2)
112+
# plot the prior:
113+
par(new=T)
114+
plot ( function( x ) dcauchy( x, 0, 1 ), xlow, xhigh, ylim=c(0,yhigh),
115+
xlim=c(xlow,xhigh), lwd=2, lty=1, ylab=" ", xlab = " ", axes=F)
116+
axis(1, at = c(-4,-3,-2,-1,0,1,2,3,4), lab=c("-4","-3","-2","-1","0",
117+
"1", "2", "3", "4"))
118+
axis(2)
119+
points(0, dcauchy(0), pch=19, cex=2)

0 commit comments

Comments
 (0)