To create a R package using rstan, I first tried to reproduce this tutorial.
Here are the exact steps I followed:
- Open Rstudio and enter:
library("rstantools")
rstan_create_package(path = 'rstanlm')
This opened a new R project.
- Save the following .stan file to
inst/stan/lm.stan
:
// Save this file as inst/stan/lm.stan
data {
int<lower=1> N;
vector[N] x;
vector[N] y;
}
parameters {
real intercept;
real beta;
real<lower=0> sigma;
}
model {
// ... priors, etc.
y ~ normal(intercept + beta * x, sigma);
}
- Save the following .R file to
R/lm_stan.R
# Save this file as `R/lm_stan.R`
#' Bayesian linear regression with Stan
#'
#' @export
#' @param x Numeric vector of input values.
#' @param y Numeric vector of output values.
#' @param ... Arguments passed to `rstan::sampling` (e.g. iter, chains).
#' @return An object of class `stanfit` returned by `rstan::sampling`
#'
lm_stan <- function(x, y, ...) {
standata <- list(x = x, y = y, N = length(y))
out <- rstan::sampling(stanmodels$lm, data = standata, ...)
return(out)
}
- Run the following lines:
example(source) # defines the sourceDir() function
try(roxygen2::roxygenize(load_code = sourceDir), silent = TRUE)
roxygen2::roxygenize()
After running roxygen2::roxygenize()
, the following error appears:
C:\Users\antho\AppData\Local\Temp\ccMb4F6c.s: Fatal error: can't write 87 bytes to section .text of stanExports_lm.o: 'file too big'
as: stanExports_lm.o: too many sections (38247)
C:\Users\antho\AppData\Local\Temp\ccMb4F6c.s: Fatal error: stanExports_lm.o: file too big
make: *** [C:/PROGRA~1/R/R-42~1.2/etc/x64/Makeconf:258: stanExports_lm.o] Error 1
ERROR: compilation failed for package 'rstanlm'
─ removing 'C:/Users/antho/AppData/Local/Temp/RtmpIXnziS/devtools_install_2d6c6c884f2f/rstanlm'
Error in `(function (command = NULL, args = character(), error_on_status = TRUE, …`:
! System command 'Rcmd.exe' failed
---
Exit status: 1
stdout & stderr: <printed>
---
Type .Last.error to see the more details.
I know that this issue has been reported several times but it seemed to be related to larger/complex models. In my case, as I apply a simple linear model directly from the tutorial, I didn’t expect to face such issues.
I’m using R4.2.2 and rstan 2.26.21.