【发布时间】:2023-04-06 21:46:01
【问题描述】:
根据this Github issue(对于knitr v1.12标记为固定),应该可以在knitr中缓存Stan块,这样每次我们编织文件时都不需要重新编译Stan模型对象.
但是,使用 knitr v1.20(RStudio v1.1.463 中的 R v3.5.1),当第二次编织文件时(因此缓存已经建立),我得到一个错误:
Quitting from lines 9-18 (Testing_Stan_cache.Rmd)
Error in fun(environment()) : invalid first argument
Calls: <Anonymous> ... call_block -> <Anonymous> -> lazyLoad -> lazyLoadDBexec -> fun
Execution halted
示例 Rmarkdown 文件(错误中提到的第 9-18 行是 Stan 块):
---
title: "Testing Stan cache"
output: html_document
---
## Stan model
```{stan output.var="ex1", cache=TRUE}
data {
int<lower=0, upper=1> X[100];
}
parameters {
real<lower=0, upper=1> p;
}
model {
X ~ bernoulli(p);
}
```
## Run the model
```{r}
library(rstan)
fit <- sampling(ex1, data=list(X = rbinom(100, 1, 0.3)))
print(fit)
```
我也尝试在 Stan 块选项中使用 cache.lazy=FALSE,但得到不同的错误:
Quitting from lines 23-26 (Testing_Stan_cache.Rmd)
Error in sampling(ex1, data = list(X = rbinom(100, 1, 0.3))) :
object 'ex1' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> sampling
Execution halted
【问题讨论】: