I’m trying to fit an IRT model in rstan and, depending on the seed I set, I get the following error:
[1] "Error in sampler$call_sampler(args_list[[i]]) : " " c++ exception (unknown reason)"
error occurred during calling the sampler; sampling not done
Here is an example with fake data:
library(extraDistr)
library(MASS)
library(pscl)
library(ggplot2)
library(StanHeaders)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
rm(list = ls())
# Set seed
set.seed(4321)
# Generate fake data
# Set number of legislators and number of votes
J <- 12
K <- 100
# Legislators' party affiliation
party_true <- rep(c(-2, 0, 2), each = round(J / 3, digits = 0))
# Generate parameters for legislator ideal point distribution
gamma_0_true <- 0.5
gamma_1_true <- 2
mu_xi_true <- cbind(1, party_true) %*% c(gamma_0_true, gamma_1_true)
sigma_xi_true <- rhnorm(n = 1, sigma = 5)
# Generate legislator ideal points
xi_true <- mvrnorm(n = 1, mu = mu_xi_true, Sigma = sigma_xi_true * diag(J))
# Generate parameters for item-discrimination and item-difficulty distributions
mu_beta_true <- 0.5
mu_alpha_true <- 0
sigma_beta_true <- rhnorm(n = 1, sigma = 5)
sigma_alpha_true <- rhnorm(n = 1, sigma = 2)
beta_true <- rnorm(n = K, mean = mu_beta_true, sd = sigma_beta_true)
alpha_true <- rnorm(n = K, mean = mu_alpha_true, sd = sigma_alpha_true)
# Generate probability p that outcome y = 1
p <- sweep(outer(xi_true, beta_true), 2, alpha_true)
p <- 1 / (1 + exp(-p))
# Generate outcome variable y
y <- sapply(1:(J * K), function(x) rbinom(1, 1, p[x]))
y <- data.frame(matrix(y, nrow = J, ncol = K, byrow = FALSE))
names(y) <- paste("vote_", 1:K, sep = "")
fake_data <- list(votes = y,
legis.names = paste("legis_", 1:J, sep = ""),
vote.names = paste("vote_", 1:K, sep = ""),
legis.data = NULL,
vote.data = NULL)
fake_data <- rollcall(data = fake_data$votes,
yea = 1, nay = 0, missing = NA, notInLegis = 9,
legis.names = fake_data$legis.names,
vote.names = fake_data$vote.names,
legis.data = fake_data$legis.data,
vote.data = fake_data$vote.data)
fake_data <- dropRollCall(fake_data,
dropList = list(codes = "notInLegis", lop = 0))
# Run Stan on fake data
J <- nrow(fake_data$votes)
K <- ncol(fake_data$votes)
N <- J * K
y <- c(fake_data$votes)
j <- rep(1:J, times = K)
k <- rep(1:K, each = J)
stan_fake_data <- list(J = J, K = K, N = N,
j = j, k = k, y = y)
irt_model <- "
data {
int<lower=1> J; // number of legislators
int<lower=1> K; // number of votes
int<lower=1> N; // number of observations
int<lower=1,upper=J> j[N]; // legislator for observation n
int<lower=1,upper=K> k[N]; // vote for observation n
int<lower=0,upper=1> y[N]; // vote decision for observation n
}
parameters {
real mu_beta; // mean of item-discrimination parameters
real mu_alpha; // mean of item-difficulty parameters
real<lower=0> sigma_beta; // scale of item-discrimination parameters
real<lower=0> sigma_alpha; // scale of item-difficulty parameters
vector[J] xi; // legislator ideal points
vector[K] beta; // item-discrimination parameters
vector[K] alpha; // item-difficulty parameters
}
model {
xi ~ normal(0, 1);
beta ~ normal(mu_beta, sigma_beta);
alpha ~ normal(mu_alpha, sigma_alpha);
mu_beta ~ cauchy(0, 5);
mu_alpha ~ cauchy(0, 5);
sigma_beta ~ cauchy(0, 5);
sigma_alpha ~ cauchy(0, 5);
for (n in 1:N) {
y[n] ~ bernoulli_logit(beta[k[n]] * xi[j[n]] - alpha[k[n]]);
}
}
"
stan_fit <- stan(model_code = irt_model,
data = stan_fake_data,
chains = 1,
iter = 50)
When I replace set.seed(4321)
with set.seed(1)
, the error does not occur.
Operating System: macOS High Sierra 10.13.6
RStan Version: 2.17.3
Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))
:
# The following statements are required to use the clang4 binary
LDFLAGS= -L/usr/local/clang4/lib
# End clang4 inclusion statements
# The following statement changes the Fortran linking path
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0
# End Fortran linking path statement
# The following statement changes the Fortran linking path
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0
# End Fortran linking path statement
# The following statement changes the Fortran linking path
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0
# End Fortran linking path statement
CC=/usr/local/clang4/bin/clang
CXX=/usr/local/clang4/bin/clang++
CXX1X=/usr/local/clang4/bin/clang++
CXX98=/usr/local/clang4/bin/clang++
CXX11=/usr/local/clang4/bin/clang++
CXX14=/usr/local/clang4/bin/clang++
CXX17=/usr/local/clang4/bin/clang++
# The following statement changes the Fortran linking path
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0
# End Fortran linking path statement
CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-macro-redefined -Wno-unknown-pragmas
CC=clang
CXX=clang++ -arch x86_64 -ftemplate-depth-256
Output of devtools::session_info("rstan")
:
Session info -------------------------------------------------------------------------------------------------
setting value
version R version 3.5.1 (2018-07-02)
system x86_64, darwin15.6.0
ui RStudio (1.1.453)
language (EN)
collate en_US.UTF-8
tz Europe/Zurich
date 2018-07-18
Packages -----------------------------------------------------------------------------------------------------
package * version date source
assertthat 0.2.0 2017-04-11 CRAN (R 3.5.0)
BH 1.66.0-1 2018-02-13 CRAN (R 3.5.0)
cli 1.0.0 2017-11-05 CRAN (R 3.5.0)
colorspace 1.3-2 2016-12-14 CRAN (R 3.5.0)
crayon 1.3.4 2017-09-16 CRAN (R 3.5.0)
dichromat 2.0-0 2013-01-24 CRAN (R 3.5.0)
digest 0.6.15 2018-01-28 CRAN (R 3.5.0)
fansi 0.2.3 2018-05-06 CRAN (R 3.5.0)
ggplot2 * 3.0.0 2018-07-03 CRAN (R 3.5.0)
glue 1.2.0 2017-10-29 CRAN (R 3.5.0)
graphics * 3.5.1 2018-07-05 local
grDevices * 3.5.1 2018-07-05 local
grid 3.5.1 2018-07-05 local
gridExtra 2.3 2017-09-09 CRAN (R 3.5.0)
gtable 0.2.0 2016-02-26 CRAN (R 3.5.0)
inline 0.3.15 2018-05-18 CRAN (R 3.5.0)
labeling 0.3 2014-08-23 CRAN (R 3.5.0)
lattice 0.20-35 2017-03-25 CRAN (R 3.5.1)
lazyeval 0.2.1 2017-10-29 CRAN (R 3.5.0)
magrittr 1.5 2014-11-22 CRAN (R 3.5.0)
MASS * 7.3-50 2018-04-30 CRAN (R 3.5.0)
Matrix 1.2-14 2018-04-13 CRAN (R 3.5.1)
methods * 3.5.1 2018-07-05 local
mgcv 1.8-24 2018-06-23 CRAN (R 3.5.1)
munsell 0.5.0 2018-06-12 CRAN (R 3.5.0)
nlme 3.1-137 2018-04-07 CRAN (R 3.5.1)
pillar 1.3.0 2018-07-14 CRAN (R 3.5.0)
plyr 1.8.4 2016-06-08 CRAN (R 3.5.0)
R6 2.2.2 2017-06-17 CRAN (R 3.5.0)
RColorBrewer 1.1-2 2014-12-07 CRAN (R 3.5.0)
Rcpp 0.12.17 2018-05-18 CRAN (R 3.5.1)
RcppEigen 0.3.3.4.0 2018-02-07 CRAN (R 3.5.0)
reshape2 1.4.3 2017-12-11 CRAN (R 3.5.0)
rlang 0.2.1 2018-05-30 CRAN (R 3.5.0)
rstan * 2.17.3 2018-01-20 CRAN (R 3.5.0)
scales 0.5.0 2017-08-24 CRAN (R 3.5.0)
StanHeaders * 2.17.2 2018-01-20 CRAN (R 3.5.0)
stats * 3.5.1 2018-07-05 local
stats4 3.5.1 2018-07-05 local
stringi 1.2.3 2018-06-12 CRAN (R 3.5.0)
stringr 1.3.1 2018-05-10 CRAN (R 3.5.0)
tibble 1.4.2 2018-01-22 CRAN (R 3.5.0)
tools 3.5.1 2018-07-05 local
utf8 1.1.4 2018-05-24 CRAN (R 3.5.0)
utils * 3.5.1 2018-07-05 local
viridisLite 0.3.0 2018-02-01 CRAN (R 3.5.0)
withr 2.1.2 2018-03-15 CRAN (R 3.5.0)
Does anyone know how I can solve this? Thanks!