On 12/19/13, 4:58 PM, Linas Mockus wrote:
> rstan users:
>
> Is it possible implement the attached WinBUGS model on Rstan?
You could translate this one line-for-line into Stan.
Presumably x[i] == gamma won't come up, but if it did,
you'd wind up using both terms.
Conditionals are more efficient in Stan, and arguably easier
to understand:
for (i in 1:N) {
if (x[i] > gamma)
y[i] ~ normal(alpha1 * x[i] + beta1, sigma);
else
y[i] ~ normal(alpha2 * x[i] + beta2, sigma);
}
Note that I'm skipping the case where x[i] == gamma, because
presumably you didn't want both terms used in that case.
Note that Stan's normal() takes a standard deviation parameter,
not a precision parameter.
For most efficiency, you'd vectorize:
for (i in 1:N) {
if (x[i] > gamma)
mu[i] <- alpha1 * x[i] + beta1;
else
mu[i] <- alpha2 * x[i] + beta2;
}
y ~ normal(mu, sigma);
We've already translated all of the BUGS models if you
want more examples. And there's an appendix in the manual comparing
BUGS and Stan's modeling.
We'd recommend at least weakly informative priors instead
of the gamma and normals you're using. BUGS has changed their
examples away from those gammas. Stan supports improper priors,
so you can also just drop them as long as the posterior is proper.
- Bob
>
> model {
> for (i in 1 : N) {
> y[i]~dnorm(mu[i],tau)
> mu[i]<-(alpha1*x[i]+beta1)*step(x[i]-gamma)+(alpha2*x[i]+beta2)*(1-step(x[i]-gamma))
> }
> alpha1~dnorm(0,1.0E-6)
> alpha2~dnorm(0,1.0E-6)
> beta1~dnorm(0,1.0E-6)
> gamma~dunif(0,1)
> beta2<-alpha1*gamma+beta1-alpha2*gamma
> tau~dgamma(1.0E-3,1.0E-3)
> sigma<-1/sqrt(tau)
> }
>