Search code examples
rplotlogistic-regressionbayesianstan

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?

Below are the codes.

require(rstan)
library(boa)
library(bayesplot)
library(rstanarm)
library(ggplot2)
library(dplyr)

setwd("C:\\Users\\Desktop")
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, 
               C_resposta=60-resposta)
dados <- dados %>% dplyr::mutate(Period = ifelse(periodo %in% c("24h","24h","24h","24h","24h",
                                                                "48h","48h","48h","48h","48h",
                                                                "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)

x11()
par(cex=1.5,cex.lab=1.3)
traceplot(logistic_fit24,inc_warmup=T,ncol=1,col="black")+
  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.

x11()
stan_trace(logistic_fit24)


Solution

  • By setting col="black" you have removed the information ggplot needs to keep the traces for each chain separate. Adding aes(group=chain) as below appears to work (although I would consider whether you really want to make the chains indistinguishable from each other: part of the point of showing a trace plot is to verify that the different chains have similar behaviour ...)

    traceplot(logistic_fit24,inc_warmup=TRUE,ncol=1,col="black")+
        aes(group=chain) 
    

    But I'll add that you can achieve the same graph (as far as I can tell) via:

    stan_trace(logistic_fit24, inc_warmup=TRUE, ncol=1) +
        scale_colour_manual(values=rep("black",3),guide=FALSE)