Sum vector by groups (month-year)

Hi all,

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:

  1. how to declare time.month in data block given that its class is character in R.
  2. 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.

Thanks!

Huiying

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.

20180910_stancode.stan (10.1 KB)

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.

head(mytime.month)
[1] “Jan 2003” “Jan 2003” “Jan 2003” “Jan 2003” “Jan 2003” “Jan 2003”

I think this is going to be easier if you do it in R afterwards. You could do something like

pred <- extract(fit, pars = "pred")[[1]]

which should be 4000xnumSteps. Then do

apply(pred, MARGIN = 1, FUN = function(p) {
  # do whatever to each realization of size numSteps
})

Oh no pred isn’t a parameter I’m estimating.

I’m still just solving the differential equations at the moment using fixed param algorithm

test <- stan(file=“20180910_stancode.stan”,data=stan_d,chains=1,iter=1,algorithm=“Fixed_param”,seed=123)

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;
    }
  }
}