library(rstan)
## Loading required package: Rcpp
## Loading required package: inline
##
## Attaching package: 'inline'
##
## The following object is masked from 'package:Rcpp':
##
## registerPlugin
##
## rstan (Version 2.7.0-1, packaged: 2015-07-17 18:12:01 UTC, GitRev: 05c3d0058b6a)
## For execution on a local, multicore CPU with excess RAM we recommend calling
## rstan_options(auto_write = TRUE)
## options(mc.cores = parallel::detectCores())
explicit_sum <- "
data {
int<lower=0> N;
vector[N] v1;
vector[N] v2;
vector[N] v3;
vector[N] v4;
vector[N] y;
}
parameters {
real<lower=0.0001> sigma;
real c[4];
}
transformed parameters {
vector[N] theta;
theta <- c[1] * v1 + c[2] * v2 + c[3] * v3 + c[4] * v4;
}
model {
c ~ normal(0, 10);
sigma ~ cauchy(0, 25);
y ~ normal(theta, sigma);
}
"
sum_via_matrix <- "
data {
int<lower=0> N;
matrix[4, N] V;
row_vector[N] y;
}
parameters {
real<lower=0.0001> sigma;
row_vector[4] c;
}
transformed parameters {
row_vector[N] theta;
theta <- c * V;
}
model {
c ~ normal(0, 10);
sigma ~ cauchy(0, 25);
y ~ normal(theta, sigma);
}
"
set.seed(42)
N <- 10000
y <- rnorm(N)
v1 <- rnorm(N)
v2 <- rnorm(N)
v3 <- rnorm(N)
v4 <- rnorm(N)
V <- matrix( c(v1, v2, v3, v4), byrow = T, ncol=N)
fit1 <- stan(model_code = explicit_sum, seed = 42)
## COMPILING THE C++ CODE FOR MODEL 'explicit_sum' NOW.
## During startup - Warning messages:
## 1: Setting LC_CTYPE failed, using "C"
## 2: Setting LC_COLLATE failed, using "C"
## 3: Setting LC_TIME failed, using "C"
## 4: Setting LC_MESSAGES failed, using "C"
## 5: Setting LC_MONETARY failed, using "C"
##
## SAMPLING FOR MODEL 'explicit_sum' NOW (CHAIN 1).
##
## Chain 1, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 1, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 1, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 1, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 1, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 1, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 1, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 1, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 1, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 10.6505 seconds (Warm-up)
## # 10.2337 seconds (Sampling)
## # 20.8842 seconds (Total)
##
##
## SAMPLING FOR MODEL 'explicit_sum' NOW (CHAIN 2).
##
## Chain 2, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 2, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 2, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 2, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 2, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 2, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 2, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 2, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 2, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 2, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 2, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 2, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 10.5785 seconds (Warm-up)
## # 9.91802 seconds (Sampling)
## # 20.4965 seconds (Total)
##
##
## SAMPLING FOR MODEL 'explicit_sum' NOW (CHAIN 3).
##
## Chain 3, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 3, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 3, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 3, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 3, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 3, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 3, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 3, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 3, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 3, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 3, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 3, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 10.5117 seconds (Warm-up)
## # 9.75371 seconds (Sampling)
## # 20.2654 seconds (Total)
##
##
## SAMPLING FOR MODEL 'explicit_sum' NOW (CHAIN 4).
##
## Chain 4, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 4, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 4, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 4, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 4, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 4, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 4, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 4, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 4, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 4, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 4, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 4, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 10.5888 seconds (Warm-up)
## # 10.645 seconds (Sampling)
## # 21.2338 seconds (Total)
fit2 <- stan(model_code = sum_via_matrix, seed = 42)
## COMPILING THE C++ CODE FOR MODEL 'sum_via_matrix' NOW.
## During startup - Warning messages:
## 1: Setting LC_CTYPE failed, using "C"
## 2: Setting LC_COLLATE failed, using "C"
## 3: Setting LC_TIME failed, using "C"
## 4: Setting LC_MESSAGES failed, using "C"
## 5: Setting LC_MONETARY failed, using "C"
##
## SAMPLING FOR MODEL 'sum_via_matrix' NOW (CHAIN 1).
##
## Chain 1, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 1, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 1, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 1, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 1, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 1, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 1, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 1, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 1, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 12.9331 seconds (Warm-up)
## # 14.6358 seconds (Sampling)
## # 27.5689 seconds (Total)
##
##
## SAMPLING FOR MODEL 'sum_via_matrix' NOW (CHAIN 2).
##
## Chain 2, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 2, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 2, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 2, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 2, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 2, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 2, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 2, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 2, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 2, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 2, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 2, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 13.636 seconds (Warm-up)
## # 14.3789 seconds (Sampling)
## # 28.0149 seconds (Total)
##
##
## SAMPLING FOR MODEL 'sum_via_matrix' NOW (CHAIN 3).
##
## Chain 3, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 3, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 3, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 3, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 3, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 3, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 3, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 3, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 3, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 3, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 3, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 3, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 12.9012 seconds (Warm-up)
## # 11.9196 seconds (Sampling)
## # 24.8208 seconds (Total)
##
##
## SAMPLING FOR MODEL 'sum_via_matrix' NOW (CHAIN 4).
##
## Chain 4, Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 4, Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 4, Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 4, Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 4, Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 4, Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 4, Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 4, Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 4, Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 4, Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 4, Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 4, Iteration: 2000 / 2000 [100%] (Sampling)
## # Elapsed Time: 12.5604 seconds (Warm-up)
## # 14.1871 seconds (Sampling)
## # 26.7476 seconds (Total)
c1_1 <- extract(fit1, "c[1]")$c
mean(c1_1)
## [1] -0.007307882
sd(c1_1)
## [1] 0.01011407
c1_2 <- extract(fit2, "c[1]")$c
mean(c1_2)
## [1] -0.007190169
sd(c1_2)
## [1] 0.01010169