Last active
August 16, 2018 12:26
-
-
Save ito4303/55448f9005910a26956239924c4d4cfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rstan) | |
options(mc.cores = parallel::detectCores()) | |
rstan_options(auto_write = TRUE) | |
## | |
set.seed(20180816) | |
N <- 100 | |
Y <- rnorm(N, 4, 1) | |
stan_code1 <- " | |
data { | |
int<lower = 0> N; | |
vector[N] Y; | |
} | |
parameters { | |
real beta[2]; | |
real<lower = 0> sigma; | |
} | |
model { | |
Y ~ normal(beta[1] + beta[2], sigma); | |
} | |
" | |
fit1 <- stan(model_code = stan_code1, data = list(N = N, Y = Y), | |
iter = 2000, warmup = 1000) | |
print(fit1) | |
rstan::traceplot(fit1) | |
## | |
stan_code2 <- " | |
data { | |
int<lower = 0> N; | |
vector[N] Y; | |
} | |
parameters { | |
real beta[2]; | |
real<lower = 0> sigma; | |
} | |
model { | |
Y ~ normal(beta[1] + beta[2], sigma); | |
beta ~ normal(0, 10); | |
sigma ~ cauchy(0, 5); | |
} | |
" | |
fit2 <- stan(model_code = stan_code2, data = list(N = N, Y = Y), | |
iter = 2000, warmup = 1000) | |
print(fit2) | |
rstan::traceplot(fit2) | |
## | |
set.seed(20180817) | |
N <- 100 | |
U <- 4 | |
Y <- runif(N, 0, U) | |
stan_code3 <- " | |
data { | |
int<lower = 0> N; | |
real<lower = 0> U; | |
vector<lower = 0, upper = U>[N] Y; | |
} | |
parameters { | |
real beta; | |
real<lower = 0> sigma; | |
} | |
model { | |
for (n in 1:N) | |
Y[n] ~ normal(beta, sigma) T[0, U]; | |
} | |
" | |
fit3 <- stan(model_code = stan_code3, data = list(N = N, U = U, Y = Y), | |
iter = 2000, warmup = 1000) | |
print(fit3) | |
rstan::traceplot(fit3) | |
## | |
stan_code4 <- " | |
data { | |
int<lower = 0> N; | |
real<lower = 0> U; | |
vector<lower = 0, upper = U>[N] Y; | |
} | |
parameters { | |
real beta; | |
real<lower = 0> sigma; | |
} | |
model { | |
for (n in 1:N) | |
Y[n] ~ normal(beta, sigma) T[0, U]; | |
beta ~ normal(0, 10); | |
sigma ~ cauchy(0, 5); | |
} | |
" | |
fit4 <- stan(model_code = stan_code4, data = list(N = N, U = U, Y = Y), | |
iter = 2000, warmup = 1000) | |
print(fit4) | |
rstan::traceplot(fit4) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment