Multiple logistic regression

Stan file:

data {
int<lower=0> N; // number of data items
int<lower=0> K; // number of predictors
matrix[N, K] X; // predictor matrix
int<lower=0,upper=1> y[N];
vector[K] mu; // prior mean
vector<lower=0>[K] sig; // prior sd
}
parameters {
vector[K] beta; // coefficients for predictors
}
model {
beta ~ normal(mu,sig); // prior distribution of beta
y ~ bernoulli_logit(X * beta); // likelihood
}

Call:

library(rstan)
stan_data ← list(
N = dim(X)[1],
K = dim(X)[2],
X = X,
y = train_data$y,
mu = mu,
sig = sig
)
fit_rstan ← stan(
#file = “logistic_w_marginal_priors.stan”,
file = “logistic_w_marginal_priors.stan”,
data = stan_data,
cores = 4
)

I get the following error message:

Error in stanc(file = file, model_code = model_code, model_name = model_name, :
0

Syntax error in ‘string’, line 19, column 0 to column 1, parsing error:

Expected “generated quantities {” or end of file after end of model block.

In addition: Warning message:
In readLines(file, warn = TRUE) :
incomplete final line found on ‘logistic_w_marginal_priors.stan’

sessionInfo()

other attached packages:
[1] rstan_2.26.4 StanHeaders_2.26.4 mgcv_1.8-38 nlme_3.1-153 pROC_1.18.0

loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 plyr_1.8.6 compiler_4.1.2 pillar_1.6.4 prettyunits_1.1.1 tools_4.1.2 pkgbuild_1.2.0
[8] lattice_0.20-45 jsonlite_1.7.2 lifecycle_1.0.1 tibble_3.1.6 gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.12
[15] Matrix_1.3-4 rstudioapi_0.13 cli_3.1.0 parallel_4.1.2 curl_4.3.2 loo_2.4.1 gridExtra_2.3
[22] withr_2.4.2 dplyr_1.0.7 generics_0.1.1 vctrs_0.3.8 tidyselect_1.1.1 stats4_4.1.2 grid_4.1.2
[29] inline_0.3.19 glue_1.5.0 data.table_1.14.2 R6_2.5.1 processx_3.5.2 fansi_0.5.0 purrr_0.3.4
[36] ggplot2_3.3.5 callr_3.7.0 magrittr_2.0.1 splines_4.1.2 codetools_0.2-18 matrixStats_0.61.0 scales_1.1.1
[43] ps_1.6.0 ellipsis_0.3.2 colorspace_2.0-2 V8_3.6.0 utf8_1.2.2 RcppParallel_5.1.4 munsell_0.5.0
[50] crayon_1.4.2

Can any of you spot the issue with my Stan code? I have tried modifying it for quite some time without success.

Welcome to the Stan Discourse. I don’t see any issues, and the model compiled for me when using cmdstanr.

I believe this means that there is something unexpected after the final closing bracket. You might double-check your file to make sure there is nothing there. Another option is to copy your code into a new text file and save it, in case there is some encoding you’re not seeing. You might also consider copying the model as a string into R and passing it to the model_code argument, see RStan: the R interface to Stan • rstan.