Hello, I am working through StatisticalRethinking books with **multi-level varying slopes with two clusters, cross classification** model

I run the stan model which finished sampling about two minutes.

To build the model with Turing.jl, I refer to StatisticalRethinkingJulia/TuringModels.jl, however, the sampling time shows ETA 5 days. So I am not sure which part I did wrong. Furthermore, I also refer to performances tips of Turing Performance Tips to make the the types are stable. But the sampling time of ETA 5 days compared to stan’s 2 minutes indicate something is wrong with my model. I spent a few hours tweaking the model specifications but it did not help at all.

Stan code saved in “varyingSlopes.stan”

```
data{
int L[504];
int block_id[504];
int actor[504];
int tid[504];
}
parameters{
vector[4] alpha[7];
vector[4] beta[6];
vector[4] g;
corr_matrix[4] Rho_actor;
corr_matrix[4] Rho_block;
vector<lower=0>[4] sigma_actor;
vector<lower=0>[4] sigma_block;
}
model{
vector[504] p;
sigma_block ~ exponential( 1 );
sigma_actor ~ exponential( 1 );
Rho_block ~ lkj_corr( 4 );
Rho_actor ~ lkj_corr( 4 );
g ~ normal( 0 , 1 );
beta ~ multi_normal( rep_vector(0,4) , quad_form_diag(Rho_block , sigma_block) );
alpha ~ multi_normal( rep_vector(0,4) , quad_form_diag(Rho_actor , sigma_actor) );
for ( i in 1:504 ) {
p[i] = g[tid[i]] + alpha[actor[i], tid[i]] + beta[block_id[i], tid[i]];
p[i] = inv_logit(p[i]);
}
L ~ binomial( 1 , p );
}
```

To sample the model in R

```
library(rethinking)
data(chimpanzees)
d = chimpanzees
d$block_id = d$block
d$treatment = 1L + d$prosoc_left + 2L*d$condition
dat = list(
L = d$pulled_left,
tid = d$treatment,
actor = d$actor,
block_id = as.integer(d$block_id) )
varyingSlopes = stan_model("varyingSlopes.stan")
fit = sampling(varyingSlopes, data = dat, seed = 803214053, control = list(adapt_delta = 0.9))
```

The model finished sampling in about 2 minutes.

The turing model adapted from StatisticalRethinkingJulia/TuringModels.jl

```
# ## Data
using Pkg;
Pkg.activate(".");
import CSV;
import TuringModels;
using DataFrames;
data_path = joinpath(TuringModels.project_root, "data", "chimpanzees.csv");
df = CSV.read(data_path, DataFrame; delim=';');
df.block_id = df.block;
df.treatment = 1 .+ df.prosoc_left .+ 2*df.condition;
# ## Model
using Turing;
@model function gdemo(actor, block, treatment, pulled_left, ::Type{TV}=Matrix{Float64}) where {TV}
## fixed priors
gamma ~ filldist(Normal(0, 1),4)
Rho_block ~ LKJ(4,2)
Rho_actor ~ LKJ(4,2)
sigma_block ~ filldist(truncated(Cauchy(0,2),0,Inf), 4)
sigma_actor ~ filldist(truncated(Cauchy(0,2),0,Inf), 4)
## adaptive priors
Sigma_block = sigma_block .* Rho_block .* sigma_block'
Sigma_block = (Sigma_block'+Sigma_block)/2
Sigma_actor = sigma_actor .* Rho_actor .* sigma_actor'
Sigma_actor = (Sigma_actor'+Sigma_actor)/2
β = TV(undef, (4, 6))
for i in 1:size(β, 2)
β[:, i] ~ MvNormal(zeros(4), Sigma_block)
end
α = TV(undef, (4, 7))
for i in 1:size(α, 2)
α[:, i] ~ MvNormal(zeros(4), Sigma_actor)
end
logit_p = gamma[treatment] .+ α[treatment,actor] .+ β[treatment,block]
pulled_left .~ BinomialLogit.(1, logit_p)
end;
## Output
julia> chns = sample(
gdemo(df.actor, df.block_id, df.treatment, df.pulled_left),
NUTS(0.95),
1000
)
┌ Info: Found initial step size
└ ϵ = 0.0125
Sampling 1%|▍ | ETA: 5 days, 12:21:51
```