Old Stan code doesn’t work in 2.21.2 on Apple M1 Max
R code:
library(readxl) # lets R read Excel files
library("loo")
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
y = scan("tayash1.txt") # reads in text file as a vector. Should work with any vector
y = exp(y)
n = length(y)
x_all = as.matrix(read_excel("slope_all.xlsx")) # design matrix with n rows and a few columns
u = ncol(x)
k = 2+u
c(n,k)
df = list(n=n,u=u,y=y,x=x)
#now run stan
set.seed(5)
fit_12 <- stan(file = 'baylasso_unshrunk.stan', data=df, verbose = FALSE, chains = 1, iter = 20000, control = list(adapt_delta = 0.8, max_treedepth = 15))
RStan file baylasso_unshrunk.stan:
data {
int n; // number of obs
int u; // number of variables
vector[n] y;
matrix[n,u] x;
}
parameters { // all except v will get uniform prior, which is default
real<lower=-5, upper=5> logcn;
real<lower=-6, upper = 6> logs; //log of s, related to lambda, not too high
real<lower=-6, upper = 6> logsig; //log of sig
vector[u] v; //parameters to fit
}
transformed parameters {
real s; // shrinkage parameter
real sig; //sigma
real cn;
vector[n] mu; //means
s = exp(logs);
sig = exp(logsig);
cn = exp(logcn);
mu = cn+x*v;
}
model { // gives priors for those not assumed uniform. Choose this one for lasso.
// for (i in 1:u) v[i] ~ double_exponential(0, s); // more weight to close to 0
y ~ normal(mu,sig);
}
Here is head(x)
D5-D8 R2 R3 R5 R6 R7 R8 R9 R10 C2 C3 C5 C6 C7 C8 C9
[1,] 0 3 2 0 0 0 0 0 0 3 2 0 0 0 0 0
[2,] 0 7 6 4 3 2 1 0 0 2 1 0 0 0 0 0
[3,] 1 1 0 0 0 0 0 0 0 3 2 0 0 0 0 0
[4,] 0 6 5 3 2 1 0 0 0 2 1 0 0 0 0 0
[5,] 1 3 2 0 0 0 0 0 0 1 0 0 0 0 0 0
[6,] 0 6 5 3 2 1 0 0 0 3 2 0 0 0 0 0
And head(y)
1562400 1443370 1183289 1131398 1108250 1063269
Using R version 4.1.2 (2021-11-01) – “Bird Hippie” on Mac OS Monterey 12.1
1 Like
This error is caused by some interaction between Rcpp
and the M1 Macs that we haven’t identified yet. Can you try your model with the preview of the next rstan version to see if that changes anything:
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")))
If not, then try using the cmdstanR
package as that may avoid the source of the error: Getting started with CmdStanR • cmdstanr
It’s looking to be a problem with the control argument. If I take that out, it works. But I don’t see anything wrong with it.
Can you try the rstan preview that I posted above? It’s resolved some issues with the control argument that we’ve seen in other situations
Ok. Pardon this question, but are the lines you sent to be run in Terminal, or in R console?
Just in the R console:
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")))
Doing it but for some reason could only do binary:
Restarting R session…
install.packages(“StanHeaders”, repos = c(“Repository for distributing (some) stan-dev R packages | r-packages”, getOption(“repos”)))
Warning in install.packages :
unable to access index for repository https://mc-stan.org/r-packages/bin/macosx/big-sur-arm64/contrib/4.1:
cannot open URL ‘https://mc-stan.org/r-packages/bin/macosx/big-sur-arm64/contrib/4.1/PACKAGES’
There is a binary version available but the source version is later:
binary source needs_compilation
StanHeaders 2.21.0-7 2.26.6 TRUE
Do you want to install from sources the package which needs compilation? (Yes/no/cancel) Yes
Can you try manually requesting the source install:
remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")),
type="source")
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")),
type="source")
1 Like
I answered Yes when it asked if I want to install binary versions. Installed 2.26.6. My code now works!
2 Likes
Thank you for your prompt help. Also glad to know it wasn’t just me.