Skip to content

Instantly share code, notes, and snippets.

@SCgeeker
Created April 12, 2017 10:18
Show Gist options
  • Save SCgeeker/83ddf45357aef1206d3801f0fe0ec9db to your computer and use it in GitHub Desktop.
Save SCgeeker/83ddf45357aef1206d3801f0fe0ec9db to your computer and use it in GitHub Desktop.
Reproduce de Schoot et al. (2014) Bayesian Analysis
## Model from appendix 3
```
model{
# There are 20 individuals.
for (i in 1:n) {
# The dependent variable has a mean (beta) and prior precision (tau).
y[i] ~ dnorm(beta[i], tau)
# Beta consists of an intercept: mu (which is, without any predictors in the model equal to the mean of our dependent variable)
beta[i] <- mu [i]}
# Mu (=mean of dependent variable) has a normal prior distribution with a mean of 80 and a prior precision of .01 and the prior is limited to obtain scores between 40 and 180. The prior precision of the dependent variable, tau, has a inverse gamma prior distribution.
mu ~ dnorm(80,.01)
#(40,180)
tau ~ dgamma(0.01, 0.01)
}
```
## R script to call WinBUGS
```
# clears workspace:
rm(list=ls())
# sets working directories:
setwd("D:/core/LAB/Analysis/R/Bayes/8steps")
library(R2WinBUGS)
bugsdir <- "D:/Apps/WinBUGS14"
y <- c(106.2451019,
130.0098114,
105.0036545,
108.0337601,
100.6291046,
96.66761017,
107.7711563,
102.5716476,
96.08522034,
119.5092163,
72.42178345,
130.16362,
92.96968079,
95.4822998,
105.3284225,
93.37817383,
87.68320465,
122.9314728,
78.78004456,
88.33502197)
n <- length(y)
data <- list("y", "n") # to be passed on to WinBUGS
myinits <- list(
list(mu = 100, sigma = 100))
# parameters to be monitored:
parameters <- c("mu", "tau")
samples = bugs(data, inits=myinits, parameters,
model.file ="deSchoot.txt",
n.chains=1, n.iter=1000, n.burnin=1, n.thin=1,
DIC=T, bugs.directory=bugsdir,
codaPkg=F, debug=T)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment