if (FALSE) {
if (require(rstan)) {
model <- custommodel("data {
int N;
vector[N] y;
vector[N] x;
}
parameters {
real alpha;
real beta;
real sigma;
}
model {
alpha ~ normal(0,10);
beta ~ normal(0,10);
sigma ~ cauchy(0,5);
for (n in 1:N)
y[n] ~ normal(alpha + beta * x[n], sigma);
}")
N <- 100
alpha <- 1
beta <- -1
sigma <- 0.5
x <- runif(N)
y <- rnorm(N, alpha + beta * x, sigma)
dat <- list(N=N, y=y, x=x)
params <- c("alpha", "beta", "sigma")
## compile on 1st time only
fit0 <- stan.fit(dat, params, model)
## reuse compiled fit0
fit <- stan.fit(dat, params, model, fit=fit0)
sm <- stan.model(fit)
summary(fit)
sm
## data cloning
dcdat <- dclone(dat, n.clones=2, multiply="N")
dcfit <- stan.fit(dcdat, params, model, fit=fit0)
summary(dcfit)
nclones(dcfit)
## using parallel options
cl <- makeCluster(2)
## cannot utilize compiled fit0
fit2 <- stan.parfit(cl=cl, dat, params, model)
stopCluster(cl)
if (.Platform$OS.type != "windows") {
## utilize compiled fit0
fit3 <- stan.parfit(cl=2, dat, params, model, fit=fit0)
}
}
}
Run the code above in your browser using DataLab