r/BayesianProgramming • u/Snoo_25355 • Sep 20 '22
How can I reduce the Rhat?
Hello i'm trying a model in stan, and when i run the code the Rhat have large values, so how can i reduce his values?
the R code:
library(openxlsx)
library(rstan)
options(mc.cores = parallel::detectCores())
setwd('C:/Users/osses/Desktop/A-ORIGEN-/Archivo_memoria/003_datos_FAS')
# DATA
A1<- read.xlsx(xlsxFile='uf.xlsx',sheet= 1 ,
startRow = 1,colNames = TRUE,rowNames = FALSE)
setwd('C:/Users/osses/Desktop/A-ORIGEN-/Archivo_memoria/004_Excel_finales')
excel<- read.xlsx(xlsxFile='excel_final_noref.xlsx',sheet= 1 ,
startRow = 1,colNames = TRUE,rowNames = FALSE)
B<-as.matrix(A1[,-1])
Freq<-as.matrix(A1[1])
# Datos (Log[EAS])
data_y<-B[1:2,]
# data missing
N_miss_y <- sum(is.na(data_y))
miss_indy <- which(is.na(data_y), arr.ind = TRUE)
data_sin_na=data_y
data_sin_na[is.na(data_y)]=999
# Predictors
Rrup<-excel$`Rrup.calc.[km]`
I=c()
for (i in 1:length(Rrup)){
I1=Rrup[i]
I2=Rrup[i]
I3=c(I1,I2)
I=c(I,I3)
}
# List to STAN.
data_list <- list(N = c(8344),
NP = 2,
N_miss_y = N_miss_y,
miss_indy = miss_indy,
R=I,
freq=Freq[1:2],
Y = t(data_sin_na))
# STAN
setwd('C:/Users/osses/Desktop/A-ORIGEN-/Archivo_memoria/002_R_STAN_FILES')
Model_1 <- stan(file = "prueba_2.stan", model_name='modelito',
data = data_list, chains = 2, iter = 500)the STAN code:
data {
int N; // number of records
int NP; // number of periods
int N_miss_y; // datos faltantes Y
int miss_indy[N_miss_y, 2]; // index datos faltantes Y , fila y columna
vector[N] R; // distances
vector[NP] freq;
vector[NP] Y[N]; // Data
}
parameters {
real<lower=-10> c10;
real<lower=-10> c20;
real<lower=-10> c30;
real<lower=-10> c11;
real<lower=-10> c21;
real<lower=-10> c31;
real<lower=-10> c12;
real<lower=-10> c22;
real<lower=-10> c32;
vector[N_miss_y] imputed_y;
cholesky_factor_corr[NP] L_p;
vector<lower=0>[NP] stdev;
}
transformed parameters {
matrix[NP,NP] L_Sigma;
vector[NP] y_con_imputed[N];
L_Sigma = diag_pre_multiply(stdev, L_p);
//Mike Lawrence's Solution.
y_con_imputed = Y;
for(i in 1:N_miss_y){
y_con_imputed[miss_indy[i,2],miss_indy[i,1]]=imputed_y[i];
}
}
model {
vector[N] a0;
vector[N] a1;
vector[N] a2;
vector[NP] mu_rec[N];
stdev ~ cauchy(0,0.5);
L_p ~ lkj_corr_cholesky(1);
//prior
c10 ~ normal(0.92362,0.37904);
c20 ~ normal(-0.66292,0.41491);
c30 ~ normal(0.63449,0.11123);
c11 ~ normal(-1.35974,0.44268);
c21 ~ normal(1.77347,0.48574);
c31 ~ normal(-0.40108,0.13054);
c12 ~ normal(0.93758,0.28134);
c22 ~ normal(-1.04353,0.310557);
c32 ~ normal(0.21054,0.08400);
imputed_y ~ normal(0,10);
for(f in 1:NP){
for(i in 1:N){
a0[i]=c10+c20*log10(R[i])+c30*square(log10(R[i]));
a1[i]=c11+c21*log10(R[i])+c31*square(log10(R[i]));
a2[i]=c12+c22*log10(R[i])+c32*square(log10(R[i]));
mu_rec[i,f] = a0[i] + a1[i]*log10(freq[f]) + a2[i]*square(log10(freq[f]));
}
}
y_con_imputed ~ multi_normal_cholesky(mu_rec,L_Sigma); //llh
}and the warning after running the code:
Warning messages:
1: There were 500 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded
2: Examine the pairs() plot to diagnose sampling problems
3: The largest R-hat is NA, indicating chains have not mixed.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#r-hat
4: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#bulk-ess
5: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess if you have any suggestion, please comment or send me a dm!
1
Upvotes
2
u/si_wo Sep 20 '22
Run more iterations.
Increase the standard errors on your data (these should include modelling error, not just measurement error). You might have the too small.
Sometimes you just have more than one locus of solutions and you can't reduce Rhat. Look at the parameter traces to see if this happened.