使用ppc_stat()时的直方图值失真

问题描述 投票:1回答:1

我正在使用以下数据:

speed <- c(28, 26, 33, 24, 34, -44, 27, 16, 40, -2, 29, 22, 24, 21, 25, 30, 23, 29, 31, 19, 24, 20, 36, 32, 36, 28, 25, 21, 28, 29, 37, 25, 28, 26, 30, 32, 36, 26, 30, 22, 36, 23, 27, 27, 28, 27, 31, 27, 26, 33, 26, 32, 32, 24, 39, 28, 24, 25, 32, 25, 29, 27, 28, 29, 16, 23)

我当前的Stan代码如下:

```{stan output.var="NMM_PPD"}
data{
  int<lower=1> n;
  vector[n] y;
  real<lower=0> nu;
}

parameters{
  real y_mu;
  real y_sd;
}

model{
  y ~ student_t(nu, y_mu, y_sd);
  y_mu ~ normal(0, 1000);
  y_sd ~ cauchy(0, 5);
}

generated quantities{
  vector[n] y_rep;

  for(i in 1:n){
    y_rep[i] = student_t_rng(nu, y_mu, y_sd);
  }
}
```

并且我传入nu的值并使用以下代码从模型中抽取样本:

```{r}
data.in <- list(y=speed, n=length(speed), "nu" = 1)
model.fit <- sampling(NMM_PPD, data=data.in)
```

我们使用extract()函数从model.fit对象中提取复制的数据集:

```{r}
yrep <- extract(model.fit, pars = "y_rep")[[1]]
```

我现在绘制直方图:

```{r}
ppc_hist(speed, yrep[sample(NROW(yrep), 11), ])
```

enter image description here

```{r}
ppc_dens(speed, yrep[sample(NROW(yrep), 2), ])
```

enter image description here

```{r}
ppc_dens_overlay(speed, yrep[sample(NROW(yrep), 11), ])
```

enter image description here

```{r}
ppc_stat(speed, yrep)
```

enter image description here

```{r}
ppc_stat(speed, yrep, stat = "median")
```

enter image description here

```{r}
ppc_stat(speed, yrep, stat = "max")
```

enter image description here

```{r}
ppc_stat(speed, yrep, stat = "min")
```

enter image description here

请注意,由于存在较小和较大的值,因此某些直方图会失真。我想修复此失真,以便可以直观地看到直方图的值。

我在documentationppc_stat()中什么都看不到,无法解决此问题。

r plot statistics histogram stan
1个回答
0
投票

ppc_stat()返回ggplot对象,因此您可以像修改ggplot图一样对其进行修改。您可能要小心,只在比例上设置任意限制,尽管边缘上没有太多数据。您还可以使用伪日志转换。这表明两者都可以:

ppc_hist(speed, yrep[sample(NROW(yrep), 11), ]) + 
  scale_x_continuous(limits = c(-100,100), trans='pseudo_log')
ppc_stat(speed, yrep, stat = "min") + 
  scale_x_continuous(limits = c(-10000,100), trans='pseudo_log')
© www.soinside.com 2019 - 2024. All rights reserved.