I’m trying to estimate parameters for an SEIR model.
My simulation will produce output by day in the form of vector. However, because my observed data is by month, I will need to sum up the daily output by groups, which is month-year, e.g “Jun-2009”.
In R, its done using rowsum like in the following scripts:
pred <- rowSums(out[,c(“Ab”,“Ag”,“Ao”,“Ae”,“Ai”)]) # to sum up a few vectors
pred.month <- rowsum(pred,group=time.month,reorder=FALSE,na.rm=TRUE) # aggregate daily model output by month
I have two issues here:
how to declare time.month in data block given that its class is character in R.
Is there an equivalent way to write rowsum in rstan?
I’m not sure if attaching my Stan code will be more helpful. any help is much appreciated.
I think you are referring to data that you are going to condition on, in which case you can just do it in R and pass it to the data block. If it involves unknowns or otherwise you have to use the Stan language, then just use a for loop or two.
Attached is my Stan code. the last line in generated quantities block is what I’m trying to do now, before trying to estimate vc_threshold and vc_eff. “mytime_month” is currently a vector type with length n_obs=180, which doesn’t seem to be correct. “obs_ovitrap” is the data I’m trying to fit. I’ve managed to run the code to produce vector “pred” with length “numSteps”=5479, but pred_month is what I need to fit the model.
I still suspect this is going to be easier in R, but you could do something like
generated quantities {
vector[numSteps] pred = Ab + Ag + Ao + Ae + Ai;
vector[n_obs] pred_month = rep_vector(0, n_obs);
{
int pos = 1;
for (i in 1:n_obs) for (j in 1:days_in_month[i]) {
pred_month[i] += pred[pos];
pos += 1;
}
}
}