I could use a little help. I’m trying to build a nested normal distribution model - no priors, just likelihood - as simple as possible for learning purposes.
I created some data below, and I first built a model without nesting and that works fine. For the second model, I’m just want to get a mean for each of 3 groups (so I should see 3 parameter values for grpMu around 10, 11 and 12). I think I don’t get the syntax / approach here. I’ve tried a number of approaches and keep getting various errors (mostly around indexing).
Could you please just show me the correct way to do that? Code below – thanks in advance. Code below:
library(tidyverse)
library(rstan)
mu <- 10
sd <- 1
n <- 100
distDF <- data.frame(x = 1, y = rnorm(100, mu, sd))
distDF <- rbind(distDF, data.frame(x = 2, y = rnorm(100, mu+1, sd)))
distDF <- rbind(distDF, data.frame(x = 3, y = rnorm(100, mu+2, sd)))
ggplot(distDF, aes(y, colour = as.factor(x))) + geom_density(bw = 1)
stanMod <- ’
data {
int N;
vector[N] y;
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
y ~ normal(mu, sigma);
}
’
fit <- stan(model_code = stanMod, data = list(y= distDF$y, N = nrow(distDF)))
summary(fit)
stanMod2 <-
’
data {
int N; // number of observations
vector[N] y; // observations
vector[N] x; // group values
int J; // number of groups
int Grp[J]; // group indicators
}
parameters {
vector[J] grpMu;
}
model {
real mu;
real sigma;
mu ~ normal(11,1);
grpMu ~ normal(mu[Grp], sigma[Grp]);
y ~ normal(grpMu, sigma);
}
’
fit <- stan(model_code = stanMod2, data = list(N = nrow(distDF),
y = distDF$y,
J = length(unique(distDF$x)),
x = as.numeric(distDF$x),
Grp = as.integer(unique(distDF$x))
))
summary(fit)