Ok, I managed to solve the problem. The print
statement was causing RStudio to crash precisely because the Stan code/model was incorrect, which was obviously leading to something undesirable happening in the background. As to why the incorrect model was causing RStudio crash specifically when we attempted to print its output, I have no idea. This question is in the domain of the developers.
My code is as follows:
```{stan output.var="non_pooled"}
data {
int<lower=0> n;
vector<lower=0>[n] t;
int<lower=0> groups;
matrix<lower=0>[7, 5] y;
}
parameters {
matrix<lower=0>[3, groups] beta;
real<lower=0> sigma;
}
transformed parameters {
matrix[7, groups] mu;
for (j in 1:groups) {
for (i in 1:7) {
mu[i, j] = beta[1, j] * inv(1 + exp(-(t[i] - beta[2, j]) / beta[3, j]));
}
}
}
model {
for (j in 1:groups) {
for (i in 1:7) {
y[i, j] ~ normal(mu[i, j], sigma);
}
beta[1, j] ~ normal(170, 5);
beta[2, j] ~ normal(85, 20);
beta[3, j] ~ normal(500, 500);
}
sigma ~ student_t(2, 0, 1);
}
```
```{r}
data.in <- list(t = Orange$age, n = length(Orange$circumference), y = matrix(Orange$circumference, 7, 5), group = as.integer(Orange$Tree), groups <- length(unique(Orange$Tree)))
model.fit2 <- sampling(non_pooled, data=data.in)
```
```{r}
print(model.fit2, digits=4, pars=c("mu", "beta", "sigma"))
```
Explanation
We have groups
= 5 groups and n
= 35 observations within those groups.
I created the 7 \times 5 matrix y
, where the columns denote the groups and the rows denote the observations within those groups. Therefore, we have 7 \times 5 = 35 total observations, as required.
Since this is an unpooled model, we need to generate parameters \beta_1^j, \beta_2^j, \beta_3^j for each group j = 1, \dots, 5. Therefore, beta
is a 3 \times 5 matrix.
This is a part that I’m still unsure of:
mu
is a 7 \times 5 matrix, where the columns denote the group and the rows denote the observations. This is because each group has 7 observations, and so there are 7 observations within each group. However, I’m still concerned about this part, since I can’t make sense of having 7 different means mu
for each group. In an unpooled model, isn’t each group supposed to have their own mean, not 7, as we have here?
EDIT: I just noticed that I had accidentally also copy and pasted an old version of the model along with the new version. I have now deleted the old version. My apologies for any confusion.