Error with my RSTAN Code

Hi everyone.

I created a stan file in Rstudio and every time I run it or read it I have a long error and I do not know how to fix my code.

the code on the stan file is as follows: (you can tell it is a 2_PL IRT model)

data {
int<lower=0>n_student;
int<lower=0>n_item;
int<lower=0,upper=1>Y[n_student,n_item];
}
parameters {
real theta[n_student]; // theta
real<lower=0>alpha[n_item]; //a
real beta[n_item]; //b
real mu_beta; // mean of b
real<lower=0>sigma_beta; //sd of b
}

model {
theta ~ normal(0,1);
alpha ~ lognormal(0,1);
beta ~ normal(mu_beta,sigma_beta);
mu_beta ~ normal(0,5);
sigma_beta ~ cauchy(0,5);

for (i in 1:n_student){
for (j in 1:n_item){Y[i,j] ~ bernoulli_logit(alpha[j]*(theta[i]-beta[j]));
}}
}

After I saved the stan file as “2_PL” I use the following code to run my data and the Rstan code:

data1 ← as.matrix(data[1:1000,1:88])
data1

# DATA as a list
res<-data1
I=dim(res)[1]
J=dim(res)[2]
data_DSM=list(n_student =I,n_item=J,Y=res)
data_DSM

library(rstan)
rstan_options(auto_write = TRUE)

IRT<- stan(file = “2_PL.stan”, data = data_DSM, iter = 50, chains = 3)

Then when I run the code I end up with the following error :

make would use
if test “zfile4e2c755e4ee5.o” != “z”; then
if test -e “file4e2c755e4ee5-win.def”; then
echo “C:/RR/rtools40//mingw64/bin/“g++ -shared -s -static-libgcc -o file4e2c755e4ee5.dll file4e2c755e4ee5-win.def file4e2c755e4ee5.o “C:/RR/R-4.0.3/library/rstan/lib/x64/libStanServices.a” -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/RR/R-40~1.3/bin/x64" -lR ;
“C:/RR/rtools40//mingw64/bin/“g++ -shared -s -static-libgcc -o file4e2c755e4ee5.dll file4e2c755e4ee5-win.def file4e2c755e4ee5.o “C:/RR/R-4.0.3/library/rstan/lib/x64/libStanServices.a” -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/RR/R-40~1.3/bin/x64" -lR ;
else
echo EXPORTS > tmp.def;
“C:/RR/rtools40//mingw64/bin/“nm file4e2c755e4ee5.o | sed -n ‘s/^.* [BCDRT] / /p’ | sed -e ‘/[.]refptr[.]/d’ -e ‘/[.]weak[.]/d’ | sed 's/[^ ][^ ]*/”&”/g’ >> tmp.def;
echo “C:/RR/rtools40//mingw64/bin/“g++ -shared -s -static-libgcc -o file4e2c755e4ee5.dll tmp.def file4e2c755e4ee5.o “C:/RR/R-4.0.3/library/rstan/lib/x64/libStanServices.a” -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/RR/R-40~1.3/bin/x64" -lR ;
“C:/RR/rtools40//mingw64/bin/“g++ -shared -s -static-libgcc -o file4e2c755e4ee5.dll tmp.def file4e2c755e4ee5.o “C:/RR/R-4.0.3/library/rstan/lib/x64/libStanServices.a” -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/StanHeaders/libs/x64” -lStanHeaders -L"C:/Users/malqa/OneDrive/Documents/R/win-library/4.0/RcppParallel/lib/x64” -ltbb -L"C:/RR/R-40~1.3/bin/x64" -lR ;
rm -f tmp.def;
fi
fi

Error in compileCode(f, code, language = language, verbose = verbose) :
C:/RR/rtools40/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/8.3.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: C:/RR/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of boost::math::detail::bessel_j0<long double>(long double)::P1'; file4e2c755e4ee5.o:file4e2c755e4ee5.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/RR/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/RR/R-4.0.3/library/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC]+0x0): multiple definition of boost::math::detail::bessel_j0(long double)::QC’; file4e2c755e4ee5.o:file4e2c755e4ee5.cpp:(.data$ZZN5boost4math6detai
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
'C:/RR/rtools40/usr/mingw/bin/g++’ not found
Error in sink(type = “output”) : invalid connection

does anyone know what is the problem in my code? thank you so much your time.

You just need to restart R and then reinstall rstan and Stanheaders from source:

# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))

install.packages(c("StanHeaders","rstan"),type="source")

thanks buddy, it worked (:

1 Like