Hi any and all Stan aficionados – I am having some trouble getting the following Poisson change point Stan model to run, the pairs plot seems to suggest that the slope and intercept variables within a change point are perfectly collinear. I am quite a novice for these issues so I was hoping I could defer to someone with more expertise in how to tackle this problem.
Stan code and simulated code to recreate my issue can be found below
data {
// Define variables in data
// Number of observations (an integer)
int<lower=0> N;
real x[N];
// Count outcome
int<lower=0> y[N];
}
parameters {
real a1;
real a2;
real b1;
real b2;
real<lower=0> fixedkp;
}
transformed parameters {
//
real lp[N];
for (j in 1:N) {
if (x[j] < fixedkp)
lp[j] <- a1 + b1*x[j];
else
lp[j] <- a2 + b2*x[j];
}
}
model {
// Prior part of Bayesian inference
// Flat prior for mu (no need to specify if non-informative)
a1 ~ normal(0,3);
a2 ~ normal(0,3);
b1 ~ normal(0,3);
b2 ~ normal(0,3);
fixedkp ~ uniform(1,30);
// Likelihood part of Bayesian inference
y ~ poisson_log(lp);
}
n1 <- 1000
n2 <- 1000
a1 <- 4
b1 <- -.2
a2 <- .5
b2 <- .0044
x1 <- sample(1:11, n1, replace=TRUE)
mu <- exp(a1 + b1 * x1 + rnorm(n1,.2,.3))
y1 <- rpois(n=n1, lambda=mu)
x2 <- sample(12:21, n2, replace=TRUE)
mu <- exp(a2 + b2 * x2 + rnorm(n2, .2,.3))
y2 <- rpois(n=n2, lambda=mu)
## Now make the data frame
out.dat = data.frame(y = c(y1, y2), x = c(x1, x2))
stan.data = list(
N = nrow(out.dat),
x = out.dat$x,
y = out.dat$y
)
plot(out.dat$x, out.dat$y)
stanmonitor <- c("a1","a2","b1","b2","fixedkp")
mod <- stan(file="/home/arosen/Documents/pinesRep/scripts/stan_models/singleBasePoisCP.stan",
data = stan.data, cores=4,
pars = stanmonitor,
iter=1000, warmup = 500, control = list(adapt_delta = 0.9, max_treedepth = 15))