Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

r - 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: https://drive.google.com/file/d/1iOuGbjNI_caLWBIz4s7hZX5GlfhLrwr9/view?usp=sharing

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)

question from:https://stackoverflow.com/questions/65943693/removing-atypical-internal-lines-from-the-chain-convergence-graph-using-a-tracep

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...