-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path4-fit-linear-bird-model.R
155 lines (118 loc) · 4.5 KB
/
4-fit-linear-bird-model.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
#################################################
# Simulate data then fit/assess linear regression
#################################################
# simulate data -----------------------------------------------------------
#from 3-bird-simulation.R
#set seed for reproducing results
set.seed(1)
#number of data points
N <- 30
#simulate predictors (standardized)
food_std <- rnorm(N, 0, 1)
#generating intercept and slope values
alpha <- 40
beta <- 3
#simulate linear predictor
mu <- alpha + beta * food_std
#process error (wrt relationship between food and bird weight)
sigma <- 3
#simulate y values
bird_weight <- rnorm(N, mu, sigma)
# load packages -----------------------------------------------------------
library(rstan)
#library(shinystan)
library(MCMCvis)
# organize data ------------------------------------------------------------
DATA <- list(N = N,
y = bird_weight,
x = food_std)
# run Stan model ----------------------------------------------------------
fit <- rstan::stan(file = '~/Google_Drive/Teaching/UCLA_Bayes_Stan_2022/Scripts/morning_session/4-linear-bird-model.stan',
data = DATA,
chains = 4,
iter = 2000,
warmup = 1000,
pars = c('alpha',
'beta',
'sigma',
'mu',
'yrep'))
# look at the output ------------------------------------------------------
#shinystan - might skip due to time
# shinystan::launch_shinystan(fit)
#posterior chains and densities
MCMCvis::MCMCtrace(fit,
params = c('alpha', 'beta', 'sigma'),
pdf = FALSE,
Rhat = TRUE,
n.eff = TRUE)
# posterior predictive check ----------------------------------------------
#extract posterior for yrep (data simulated from posterior)
yrep_ch <- MCMCvis::MCMCchains(fit, params = 'yrep')
#make sure that data generated from posterior samples looks similar to observed data
plot(density(bird_weight), lwd = 2,
ylim = c(0, 0.18),
xlab = 'y',
main = 'Posterior predictive check')
for (i in 1:150)
{
lines(density(yrep_ch[i,]), col = rgb(1,0,0,0.1))
}
# summarize model ---------------------------------------------------------
#MCMCsummary - check for Rhat <= 1.01 and n.eff > 400
MCMCvis::MCMCsummary(fit,
params = c('alpha', 'beta', 'sigma'),
round = 2)
# examine posterior -------------------------------------------------------
#extract posterior chains for alpha
alpha_ch <- MCMCvis::MCMCchains(fit, params = 'alpha')
#summary of posterior chains - matches summary output
mean(alpha_ch)
quantile(alpha_ch, probs = c(0.025, 0.5, 0.975))
# check prior posterior overlap ----------------------------------------
#check to make sure prior is not having an outsized effect on posterior
MCMCvis::MCMCtrace(fit,
params = 'alpha',
priors = rnorm(4000, 50, 15),
type = 'density',
pdf = FALSE,
Rhat = TRUE,
n.eff = TRUE,
post_zm = FALSE)
MCMCvis::MCMCtrace(fit,
params = 'beta',
priors = rnorm(4000, 0, 10),
type = 'density',
pdf = FALSE,
Rhat = TRUE,
n.eff = TRUE,
post_zm = FALSE)
PP <- rnorm(4000, 0, 10)
MCMCvis::MCMCtrace(fit,
params = 'sigma',
priors = PP[which(PP > 0)],
type = 'density',
pdf = FALSE,
Rhat = TRUE,
n.eff = TRUE,
post_zm = FALSE)
# plot model fit ----------------------------------------------------------
#calculate quantiles of mu
mu_q <- MCMCvis::MCMCpstr(fit, params = 'mu',
func = function(x) quantile(x,
probs = c(0.025, 0.5, 0.975)))[[1]]
#order of food_std for plotting (to make sure line goes from smallest to largest)
idx <- order(food_std)
#plot data
plot(food_std, bird_weight,
pch = 19,
col = 'grey60',
xlab = 'Bird food (std)',
ylab = 'Bird weight (g)')
#plot model fit and 95% CI - NOTHING SPECIAL ABOUT 95% CI!
lines(food_std[idx], mu_q[idx,1],
lty = 2, lwd = 3)
lines(food_std[idx], mu_q[idx,2],
col = 'red', lwd = 3)
lines(food_std[idx], mu_q[idx,3],
lty = 2, lwd = 3)