Removing atypical internal lines from the chain convergence graph using a traceplot function

I am making the convergence graph of the chains generated using the traceplot function. However, see what unusual lines are appearing on the chart. How would you go about removing them?

data: dados.csv - Google Drive

Below are the codes.


dados = read.table("dados.csv", header = T, sep=";", dec = ",")
dados$periodo = as.factor(dados$periodo)
dados <- dados %>% mutate(proporcao =  (dados$resposta)/60)
dados <- dados %>% mutate(logdose = log(dados$concentracao))
dados <- dados %>% mutate(Período = dados$periodo)
dados<- mutate(dados, 
dados <- dados %>% dplyr::mutate(Period = ifelse(periodo %in% c("24h","24h","24h","24h","24h",
                                                                "72h","72h","72h","72h","72h"),c("Experiment Duration: 24h",
                                                                                                 "Experiment Duration: 24h",
                                                                                                 "Experiment Duration: 24h",
                                                                                                 "Experiment Duration: 24h",
                                                                                                 "Experiment Duration: 24h",
                                                                                                 "Experiment Duration: 48h",
                                                                                                 "Experiment Duration: 48h",
                                                                                                 "Experiment Duration: 48h",
                                                                                                 "Experiment Duration: 48h",
                                                                                                 "Experiment Duration: 48h",
                                                                                                 "Experiment Duration: 72h",
                                                                                                 "Experiment Duration: 72h",
                                                                                                 "Experiment Duration: 72h",
                                                                                                 "Experiment Duration: 72h",

dados2 = dados[c(1:5),]
n = length(dados2$logdose)
y = dados2$resposta

logistic_example24 <- "data {
int<lower=0> N;
vector[N] x;
int<lower=0> y[N];
int<lower=0> n[N];
parameters {
real beta1;
real beta2;
model {
beta1 ~ normal(0,100);
beta2 ~ normal(0,100);
y ~ binomial_logit(n, beta1 + beta2 * x);

logistic_fit24 <- stan(model_code = logistic_example24,
                       data = list(N = dim(dados2)[1], n = dados2$total,
                                   x = dados2$logdose, y = dados2$resposta),
                       chain = 3, iter = 11000, warmup = 1000,
                       thin = 10, refresh = 0)

  xlab("Iterações") +
  theme_bw() + theme(axis.title = element_text(size = 28,color="black"),
                     axis.text = element_text(size = 24,color="black"),
                     strip.text.x = element_text(size = 22,color="black"))

Note that when using the stan_trace function, these atypical lines do not appear, however, my interest is in using the traceplot function due to aesthetics.


1 Like

Sorry for not getting to your question earlier. I tried to download the datafile, but it says it was moved to trash and that I can’t access it, so I cannot run the code. But I can reproduce the issue with a different fit.

It looks like the plot is combining multiple chains using a single geom_path (i.e. that the last iteration of chain 1 connects to first iteration of chain 2). This is because you force all the chains to be the same colour and ggplot may rely on colour to separate the chains. (this is possibly a minor bug in rstan). Notice that the problem goes away when you omit col = "black" part. If you want to modify the colours, you need to override the color scale (e.g. traceplot(fit$fit, ncol = 1,inc_warmup=T) + scale_color_brewer()) - see the docs for ggplot if you need help with the scales.

Best of luck with your model!