stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 265 forks source link

expose_stan_functions causes error: "$ operator is invalid for atomic vectors" #954

Open robert-dodier opened 3 years ago

robert-dodier commented 3 years ago

I'm working with experimental version 2.26 of RStan on Windows and I'm getting an error, "$ operator is invalid for atomic vectors", from expose_stan_functions.

I researched this a bit and did not find anything the same. Issue #899 mentions expose_stan_functions in passing but that appeared to be about other errors.

I get the error from an existing .stan file for which expose_stan_functions does not cause an error with RStan 2.26 on macOS. Instead of including the .stan file I'm working with, here is a shorter example which I nicked from another discussion. See: https://discourse.mc-stan.org/t/mixture-models/17721/9

functions {
  real repulsive_lpdf(vector mu, vector rho) {
    int K = num_elements(mu);
    matrix[K, K] S = diag_matrix(rep_vector(1, K));
    matrix[K, K] L;
    int c = 0;

    for (k1 in 1:(K - 1))
      for (k2 in (k1 + 1):K){
        c += 1;
        S[k1, k2] = log(1 - exp(- squared_distance(mu[k1], mu[k2]) / rho[c]));
        S[k2, k1] = S[k1, k2];
      }
    L = cholesky_decompose(S);

    return 2 * sum(log(diagonal(L)));
  }
}

data {
  int<lower=1> K;
  int<lower=1> N;
  real y[N];
}

parameters {
  ordered[K] mu;
  vector<lower=0>[choose(K, 2)] rho;
  real<lower=0> sigma[K];
  simplex[K] lambda;
}

model {
  // Prior model
  mu ~ normal(0, 5);
  sigma ~ std_normal();
  lambda ~ dirichlet(rep_vector(3, K));
  rho ~ gamma(1, 2);
  mu ~ repulsive(rho);

  // Observational model
  for (n in 1:N) {
    real comp_lpdf[K];
    for (k in 1:K) {
      comp_lpdf[k] = log(lambda[k]) + normal_lpdf(y[n] | mu[k], sigma[k]);
    }
    target += log_sum_exp(comp_lpdf);
  }
}

I put that into a file name example.stan and then I get, in my R session:

> expose_stan_functions ("example.stan")
Error: $ operator is invalid for atomic vectors

packageVersion ("RStan") reports 2.26.2.

R.version.string reports "R version 4.1.0 (2021-05-18)".

Operating system is MS Windows 10.

I installed RStan following these instructions. (I was trying the officially released version 2.21, however, it lacks the %/% operator which I want to use.) See: https://discourse.mc-stan.org/t/problems-installing-rstan-2-21-2-with-rtools-4/22492/5

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
robert-dodier commented 3 years ago

PS. This page (https://cran.r-project.org/web/checks/check_results_rstanarm.html) which appears to report build info has the same error (https://www.r-project.org/nosvn/R.check/r-devel-windows-x86_64-gcc10-UCRT/rstanarm-00check.html). I guess that page gets updated so the error might go away or something different reported; the one I'm looking at has the timestamp 2021-07-23 01:49:38 CEST.

tomhull commented 2 years ago

can confirm, I am also getting this issue.


rstan::stan_version()
[1] "2.26.1"

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

other attached packages:
[1] ggplot2_3.3.5      data.table_1.14.2  xts_0.12.1         zoo_1.8-9          dlm_1.1-5          rstan_2.26.6       StanHeaders_2.26.6

loaded via a namespace (and not attached):
 [1] RcppEigen_0.3.3.9.1 tidyselect_1.1.1    xfun_0.29           purrr_0.3.4         lattice_0.20-45     V8_4.0.0            colorspace_2.0-2    vctrs_0.3.8        
 [9] generics_0.1.1      htmltools_0.5.2     stats4_4.1.2        loo_2.4.1           yaml_2.2.1          utf8_1.2.2          rlang_0.4.12        pkgbuild_1.3.1     
[17] pillar_1.6.4        glue_1.6.0          withr_2.4.3         matrixStats_0.61.0  lifecycle_1.0.1     munsell_0.5.0       gtable_0.3.0        codetools_0.2-18   
[25] evaluate_0.14       inline_0.3.19       knitr_1.37          callr_3.7.0         fastmap_1.1.0       ps_1.6.0            curl_4.3.2          parallel_4.1.2     
[33] fansi_1.0.0         Rcpp_1.0.7          scales_1.1.1        RcppParallel_5.1.5  jsonlite_1.7.2      gridExtra_2.3       digest_0.6.29       processx_3.5.2     
[41] dplyr_1.0.7         grid_4.1.2          BH_1.78.0-0         cli_3.1.0           tools_4.1.2         magrittr_2.0.1      tibble_3.1.6        crayon_1.4.2       
[49] pkgconfig_2.0.3     ellipsis_0.3.2      Matrix_1.3-4        prettyunits_1.1.1   rmarkdown_2.11      R6_2.5.1            compiler_4.1.2