I'm trying to use Stan and R to fit a model that, uhh, models the observed realisations y_i = 16, 9, 10, 13, 19, 20, 18, 17, 35, 55, which are from a binomial distributed random variable, say, Y_i, with parameters m_i (number of trials) and p_i (success probability in each trial).
yi = c(16, 9, 10, 13, 19, 20, 18, 17, 35, 55)
For the purposes of this experiment, I'm going to assume that all of the m_i are fixed and given by m_i = 74, 99, 58, 70, 122, 77, 104, 129, 308, 119.
mi = c(74, 99, 58, 70, 122, 77, 104, 129, 308, 119)
I'm going to use Jeffrey's prior: \alpha=0.5 and \beta=0.5.
alpha = 0.5, beta = 0.5
I'm trying to
My attempt at 2. is this section of code:
real k;
real mx = 0;
real mn = 0;
if (p > mx)
mx = p;
if (mn > p) {
mn = p;
}
k = mx - mn;
My Stan code is as follows:
```{stan output.var="BinModBeta"}
data {
int <lower = 1> mi[10];
int <lower = 0> yi[10];
real <lower = 0> alpha;
real <lower = 0> beta;
}
parameters {
real <lower = 0, upper = 1> p[10];
}
transformed parameters {
real k;
real mx = 0;
real mn = 0;
if (p > mx)
mx = p;
if (mn > p) {
mn = p;
}
k = mx - mn;
}
model {
yi ~ binomial(mi, p);
p ~ beta(alpha, beta);
}
```
My R code is as follows:
```{r}
library(rstan)
```
```{r}
data.in <- list(mi = c(74, 99, 58, 70, 122, 77, 104, 129, 308, 119), yi = c(16, 9, 10, 13, 19, 20, 18, 17, 35, 55), alpha = 0.5, beta = 0.5)
model.fit1 <- sampling(BinModBeta, data=data.in)
```
```{r}
print(model.fit1, pars = c("p"), probs=c(0.1,0.5,0.9), digits = 5)
```
Now, I just started learning Stan, so I'm honestly not sure if this is correct at all. However, it seems like this code works for my first aim (at least, whatever I've coded seems to work ...). But my trouble starts when attempting to code my second aim.
When I attempt to compile the Stan code above, I get the following error:
Now, based on this error message, it seems like my issue is arising from the fact that p is a vector of 10 real values, rather than a single real. However, due to my inexperience with Stan, I'm unsure of how to get around this problem.


pas a real vector of length 10 in yourparametersblock. So this causes the error in theifstatement. - Maurits Everspas a real scalar. - Maurits Evers