使用 traceplot 函数从链收敛图中删除非典型内部线
Removing atypical internal lines from the chain convergence graph using a traceplot function
我正在制作使用 traceplot 函数生成的链的收敛图。但是,请查看图表上出现了哪些不寻常的线条。您将如何删除它们?
下面是代码。
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"))
请注意,使用stan_trace函数时,这些非典型线条不会出现,但是,出于美观考虑,我的兴趣是使用tracplot函数。
x11()
stan_trace(logistic_fit24)
通过设置 col="black"
,您已经删除了 ggplot 需要的信息,以保持每个链的痕迹分开。如下添加 aes(group=chain)
似乎可行(尽管我会考虑您是否真的想让链条彼此无法区分:显示跟踪图的部分目的是验证不同的链条具有相似的行为.. .)
traceplot(logistic_fit24,inc_warmup=TRUE,ncol=1,col="black")+
aes(group=chain)
但我要补充一点,您可以通过以下方式获得相同的图表(据我所知):
stan_trace(logistic_fit24, inc_warmup=TRUE, ncol=1) +
scale_colour_manual(values=rep("black",3),guide=FALSE)
我正在制作使用 traceplot 函数生成的链的收敛图。但是,请查看图表上出现了哪些不寻常的线条。您将如何删除它们?
下面是代码。
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"))
请注意,使用stan_trace函数时,这些非典型线条不会出现,但是,出于美观考虑,我的兴趣是使用tracplot函数。
x11()
stan_trace(logistic_fit24)
通过设置 col="black"
,您已经删除了 ggplot 需要的信息,以保持每个链的痕迹分开。如下添加 aes(group=chain)
似乎可行(尽管我会考虑您是否真的想让链条彼此无法区分:显示跟踪图的部分目的是验证不同的链条具有相似的行为.. .)
traceplot(logistic_fit24,inc_warmup=TRUE,ncol=1,col="black")+
aes(group=chain)
但我要补充一点,您可以通过以下方式获得相同的图表(据我所知):
stan_trace(logistic_fit24, inc_warmup=TRUE, ncol=1) +
scale_colour_manual(values=rep("black",3),guide=FALSE)