Alex
Alex

Reputation: 2077

Ordered probit in stan

I'm just learning stan and have a few questions. I am trying to do an ordered probit model in stan. I have a couple of questions. First, the model below throws an error message Stan model does not contain samples. What does that mean and how do I go about fixing it?

Secondly, how do I tell stan the constraints I want to identify the model? As of now, it's unidentified location-wise. I'd like to tell stan to set one of the taus to be a specific value (0, for example) but am uncertain how to do that.

data{
  int<lower=1> N; // number of obs
  int<lower=3> J; // number of categories
  int<lower=2> K; // num of predictors
  int y[N]; // outcome var 
  matrix[N, K] x; // predictor vars 
}
parameters{
  ordered[J-1] tau; // thresholds
  vector[K] beta; // beta coefficients 
}
model{
  vector[J] theta;
  vector[N] xB;
  beta ~ normal(0, 100);
  xB <- x*beta;
  for(n in 1:N){
    theta[1] <- 1 - Phi(xB[n] - tau[1]);
    for(j in 2:J-1)
      theta[j] <- Phi(xB[n]-tau[j-1]) - Phi(xB[n]-tau[j]);
    theta[J] <- Phi(xB[n] - tau[J-1]);
    y[n] ~ categorical(theta);
  }
}

EDIT

Here's the R code I called:

stan_data <- list(N = dim(insurance)[1], # 1000
                  K = dim(insurance)[2], #5
                  J = length(table(insurance$spend)), #3
                  y = insurance$spend, # vector of length N where each element is 0, 1, or 2
                  x = my_xmatrix) # matrix of dim 1000, 5

mcmc_oprobit <- stan(file="stan/oprobit.stan",
                     data = stan_data)

Upvotes: 3

Views: 1459

Answers (1)

Ben Goodrich
Ben Goodrich

Reputation: 4980

If I call, N <- 1000 J <- 3L K <- 2L y <- sample(0:2, N, replace = TRUE) x <- matrix(rnorm(2 * N), N , 2) mcmc_oprobit <- stan(file="oprobit.stan") then I ultimately get Informational Message: The current Metropolis proposal is about to be rejected because of the following issue: Exception thrown at line 22: stan::math::categorical_log: Number of categories is 0, but must be between (1, 3) If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine, but if this warning occurs often then your model may be either severely ill-conditioned or misspecified. In which case, the problem can be overcome by recoding your outcome variable to be 1, 2, or 3 rather than 0, 1, or 2. But that raises the question of why you are not seeing that Informational Message? What platform, GUI, and version number of RStan are you using?

Upvotes: 2

Related Questions