here is some toy data. My outcome variable has a lower bound which is outcome >= -x where x is a predictor.
This seems to be a truncated data situation so I looked on page 99 of the stan manual it goes over adding a constraint on the outcome: http://www.uvm.edu/~bbeckage/Teaching/DataAnalysis/Manuals/stan-reference-2.8.0.pdf
The examples define the contraint as a single number “real U”. In my case the boundary is not a single number U but a vector of numbers so I declared “real U[N]”. When I run the code I get this error:
ERROR - Lower bounds in truncated distributions must be univariate.
It seems I’m very close to a solution here if a vector bound was available. Is there a way to incorporate a lower bound that is a vector? Thank you.
Here is the toy code:
x = rep(seq(-10,10,1),each=5)
y = rep(0,length(x) )
for(i in 1:length(x)){
y[i] = sample( seq(-x[i],-x[i]+ ifelse(x[i]<0 ,
sample(seq(1,10,.1),1),
sample(seq(5,40,.1),1) ),1)
,1)
}
weights = sample( seq(1,1000,1) ,length(x))
groups = rep( letters[1:5], times =length(x)/5 )
library(rstan)
head(dat)
# create a numeric vector to indicate the categorical groups
dat$GROUP_ID = match( dat$group, levels( dat$group ) )
dat$BOUND = -dat$x
library(rstan)
standat <- list(
N = nrow(dat),
y = dat$y,
x = dat$x,
GROUP_ID = dat$GROUP_ID,
nGROUPS = 5,
L = dat$BOUND,
)
stanmodelcode = '
data {
int<lower=1> N;
int nGROUPS;
real y[N];
real x[N];
int<lower=1, upper=nGROUPS> GROUP_ID[N];
real L[N];
}
transformed data{
}
parameters {
real intercept;
vector[nGROUPS] intercept_x2;
real beta;
real<lower=0> sigma;
real<lower=0> sigma_2;
}
transformed parameters { // none needed
}
model {
real mu;
// priors
intercept~ normal(0,10);
intercept_x2 ~ normal(0,sigma_2);
beta ~ normal(0,10);
sigma ~ normal(0,10);
sigma_2 ~ normal(0,10);
// likelihood
for(i in 1:N){
mu = intercept + intercept_x2[ GROUP_ID[i] ] + beta*x[i];
y[i] ~ normal(mu, sigma) T[L,];
}
}
'
fit22 = stan(model_code=stanmodelcode, data=standat, iter=2000, warmup=500, chains = 1)
fit22