Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cohort based model #221

Closed
seabbs opened this issue Aug 1, 2024 · 19 comments
Closed

Cohort based model #221

seabbs opened this issue Aug 1, 2024 · 19 comments
Labels
low For a future release research project A more academic contribution

Comments

@seabbs
Copy link
Contributor

seabbs commented Aug 1, 2024

It would be nice to offer a model that scales with unique delays and strata versus by individual delay data points.

One approach for this is estimating a correct double censored PMF (by integrating the primary event distribution and the delay distribution to get a CDF and then taking the differences to get the secondary event PMF) for each observation time and then weighting the log pmf for each delay by the number of observed for that delay + obs time combination.

A julia prototype for this method is here: CDCgov/Rt-without-renewal#386 (comment)

For the simple single strata case this is quite simple but as strata become more numerous (and arbitrary) it becomes more of an engineering challenge. I plan to do this in the simple case for stan in EpiNow2 (epiforecasts/EpiNow2#350) which should hopefully serve as a guide.

We then need to think what this looks like within a brms implementation. I think it might involve injecting transformed parameters. This could be made simpler if brms has a cohort based model which it may have.

For anything beyond daily censoring this approach becomes quite complicated and so would need some more thought.

@athowes athowes added low For a future release research project A more academic contribution labels Aug 8, 2024
@seabbs

This comment was marked as outdated.

@seabbs
Copy link
Contributor Author

seabbs commented Aug 21, 2024

@athowes
Copy link
Collaborator

athowes commented Nov 11, 2024

Steps here IMO:

  1. Generate simulated cohort data (and keep individual level version)
  2. Use the weights argument of brms to implement the direct model with cohort data
  3. Verify that the weighted version of the direct model is equivalent
  4. Input distributions from primarycensored into Stan (read this to figure out options here)
  5. Use that to define a version of the cohort model defined with primarycensored but outside of brms
  6. Translate the above model into brms and the infrastructure of epidist
  7. Use an alternation of epidist_formula() which puts the weights in (or epidist_weights() if it's more complicated)
  8. Complete an implementation of cohort_model in epidist
  9. Could try to implement individual level version of primarycensored distribution model
  10. Think about next steps on making this more computationally efficient for many strata

@athowes
Copy link
Collaborator

athowes commented Nov 11, 2024

Using | weights(...) seems to work fine (56ecf8f):

> summary(fit_direct)
 Family: lognormal 
  Links: mu = identity; sigma = identity 
Formula: delay_daily ~ 1 
   Data: sim_obs (Number of observations: 500) 
  Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
         total post-warmup draws = 4000

Regression Coefficients:
          Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept     1.77      0.02     1.73     1.82 1.00     3457     2386

Further Distributional Parameters:
      Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     0.49      0.02     0.46     0.52 1.00     3250     2531

Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
> summary(fit_direct_weighted)
 Family: lognormal 
  Links: mu = identity; sigma = identity 
Formula: delay | weights(n) ~ 1 
   Data: cohort_obs (Number of observations: 21) 
  Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
         total post-warmup draws = 4000

Regression Coefficients:
          Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept     1.77      0.02     1.73     1.82 1.00     3542     2778

Further Distributional Parameters:
      Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     0.49      0.02     0.46     0.52 1.00     3094     2518

Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).

@athowes
Copy link
Collaborator

athowes commented Nov 12, 2024

primarycensored has analytic solution for:

int check_for_analytical {
  if (dist_id == 2 && primary_id == 1) return 1; // Gamma delay with Uniform primary
  if (dist_id == 1 && primary_id == 1) return 1; // Lognormal delay with Uniform primary
  if (dist_id == 3 && primary_id == 1) return 1; // Weibull delay with Uniform primary
  return 0; // No analytical solution for other combinations
}

So this allows primarycensored_analytical_lcdf to dispatch out to other Stan functions. I think that still means that we would need to have imported all the functions into R in order to use the dispatch one. Perhaps some argument we should just be doing the dispatch ourselves rather than import all the functions.

@athowes
Copy link
Collaborator

athowes commented Nov 12, 2024

For the primary_censored_lognormal_uniform_lcdf the parameters are:

Name Description
d Delay time
q Lower bound of integration (max(d - pwindow, 0))
params Array of Lognormal distribution parameters [mu, sigma]
pwindow Primary event window

Notes here:

  • Could either calculate q in the Stan template or input it as data. I think in terms of keeping everything in the same place it should be in the transformed data block
  • For params I guess we just construct params from mu and sigma in the Stan template
  • pwindow -- expecting this to be e.g. c(1, ..., 1) most of the time for daily

@athowes
Copy link
Collaborator

athowes commented Nov 12, 2024

for (n in 1:N) {
    target += weights[n] * (primarycensored_lognormal_uniform_lcdf_lpdf(Y[n] | mu[n], sigma, pwindow));
}

Have this issue. Even if define param then brms wants to put its distributional parameters into the likelihood. I think best option here is just to redefine primarycensored_lognormal_uniform_lcdf with regex to get rid of params and replace with the distributional parameters.

Also not currently putting q into likelihood. Guess it must be with the "special names" brms has.

@athowes

This comment was marked as outdated.

@athowes
Copy link
Collaborator

athowes commented Nov 13, 2024

as_epidist_group.epidist_linelist

@athowes

This comment was marked as outdated.

@athowes
Copy link
Collaborator

athowes commented Nov 18, 2024

@seabbs @SamuelBrand1 tough ask but I don't suppose either of you have any guesses about what might be going wrong here to cause compilation issues?

The Stan code is:

}
  real primarycensored_wrapper_lpmf(data int d, real mu, real sigma, data real pwindow) {
  int dist_id = 1; // lognormal
  array[2] real params;
  params[1] = mu;
  params[2] = sigma;
  int d_upper = d + 1;
  int primary_id = 1; // Uniform
  array[0] real primary_params;
  return primarycensored_lpmf(d | dist_id, params, pwindow, d_upper, positive_infinity(), primary_id, primary_params);
}
}
data {
  int<lower=1> N;  // total number of observations
  array[N] int Y;  // response variable
  vector<lower=0>[N] weights;  // model weights
  // data for custom real vectors
  array[N] real vreal1;
  int prior_only;  // should the likelihood be ignored?
}
transformed data {
}
parameters {
  real Intercept;  // temporary intercept for centered predictors
  real sigma;  // dispersion parameter
}
transformed parameters {
  real lprior = 0;  // prior contributions to the log posterior
  lprior += student_t_lpdf(Intercept | 3, 11, 7.4);
  lprior += student_t_lpdf(sigma | 3, 0, 7.4);
}
model {
  // likelihood including constants
  if (!prior_only) {
    // initialize linear predictor term
    vector[N] mu = rep_vector(0.0, N);
    mu += Intercept;
    for (n in 1:N) {
      target += weights[n] * (primarycensored_wrapper_lpmf(Y[n] | mu[n], sigma, vreal1[n]));
    }
  }
  // priors including constants
  target += lprior;
}
generated quantities {
  // actual population-level intercept
  real b_Intercept = Intercept;
}

And the compilation error is:

Compiling Stan program...
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1799:47: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
std::vector{dist_id, primary_id, stan::math::size(params),
^~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1885:18: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_cdf<int, std::vector, int, std::vector, nullptr>' requested here
primarycensored_cdf(d, dist_id, params, pwindow,
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1969:21: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_lcdf<int, std::vector, int, std::vector, nullptr>' requested here
log_cdf_upper = primarycensored_lcdf(d_upper, dist_id, params, pwindow,
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:2236:12: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_lpmf<false, int, std::vector, int, std::vector, nullptr>' requested here
return primarycensored_lpmf(d, dist_id, params, pwindow,
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:2399:15: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_wrapper_lpmf<false, double, double, nullptr>' requested here
primarycensored_wrapper_lpmf(
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:27
23:12: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::model_6fa90462cf3e16ffa43f0361963d4387_model::log_prob_impl<false, false, Eigen::Matrix<double, -1, 1>, Eigen::Matrix<int, -1, 1>, nullptr, nullptr, nullptr>' requested here
return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
^

/Users/adamhowes/.cmdstan/cmdstan-2.35.0/stan/src/stan/model/model_base_crtp.hpp:93:50: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::model_6fa90462cf3e16ffa43f0361963d4387_model::log_prob<false, false, double>' requested here
return static_cast<const M*>(this)->template log_prob<false, false, double>(
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:2252:3: note: in instantiation of member function 'stan::model::model_base_crtp<model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::model_6fa90462cf3e16ffa43f0361963d4387_model>::log_prob' requested here
~model_6fa90462cf3e16ffa43f0361963d4387_model() {}
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1799:47: note: insert an explicit cast to silence this issue
std::vector{dist_id, primary_id, stan::math::size(params),
^~~~~~~~~~~~~~~~~~~~~~~~
static_cast( )
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1800:11: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
stan::math::size(primary_params)}, "assigning variable ids");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1800:11: note: insert an explicit cast to silence this issue
stan::math::size(primary_params)}, "assigning variable ids");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static_cast( )

/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1799:47: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
std::vector{dist_id, primary_id, stan::math::size(params),
^~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1885:18: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_cdf<int, std::vector<stan::math::var_value>, int, std::vector<stan::math::var_value>, nullptr>' requested here
primarycensored_cdf(d, dist_id, params, pwindow,
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1969:21: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_lcdf<int, std::vector<stan::math::var_value>, int, std::vector<stan::math::var_value>, nullptr>' requested here
log_cdf_upper = primarycensored_lcdf(d_upper, dist_id, params, pwindow,
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:2236:12: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_lpmf<false, int, std::vector<stan::math::var_value>, int, std::vector<stan::math::var_value>, nullptr>' requested here
return primarycensored_lpmf(d, dist_id, params, pwindow,
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:2474:15: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::primarycensored_wrapper_lpmf<false, stan::math::var_value, stan::math::var_value, nullptr>' requested here
primarycensored_wrapper_lpmf(
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:2723:12: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::model_6fa90462cf3e16ffa43f0361963d4387_model::log_prob_impl<false, false, Eigen::Matrix<stan::math::var_value, -1, 1>, Eigen::Matrix<int, -1, 1>, nullptr, nullptr, nullptr>' requested here
return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
^
/Users/adamhowes/.cmdstan/cmdstan-2.35.0/stan/src/stan/model/model_base_crtp.hpp:98:50: note: in instantiation of function template specialization 'model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::model_6fa90462cf3e16ffa43f0361963d4387_model::log_prob<false, false, stan::math::var_value>' requested here
return static_cast<const M*>(this)->template log_prob<false, false>(theta,
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:2252:3: note: in instantiation of member function 'stan::model::model_base_crtp<model_6fa90462cf3e16ffa43f0361963d4387_model_namespace::model_6fa90462cf3e16ffa43f0361963d4387_model>::log_prob' requested here
~model_6fa90462cf3e16ffa43f0361963d4387_model() {}
^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1799:47: note: insert an explicit cast to silence this issue
std::vector{dist_id, primary_id, stan::math::size(params),
^~~~~~~~~~~~~~~~~~~~~~~~
static_cast( )
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1800:11: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
stan::m
ath::size(primary_params)}, "assigning variable ids");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.hpp:1800:11: note: insert an explicit cast to silence this issue
stan::math::size(primary_params)}, "assigning variable ids");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static_cast( )

4 errors generated.

make: *** [/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmptN5cBH/model-111232eafcdd.o] Error 1

Error: An error occured during compilation! See the message above for more information.

@SamuelBrand1
Copy link

Looks like primary_params doesn't support size ?

@athowes
Copy link
Collaborator

athowes commented Nov 18, 2024

Not sure what that means. So I guess what we want is to input empty primary_params into Stan because it's the uniform model with default priors. This was the approach here:

https://github.com/epinowcast/primarycensored/blob/d65f69c4722508919142ae12df8ca61d1f1deb09/inst/stan/functions/primarycensored.stan#L78

which I mentioned in an issue (epinowcast/primarycensored#170) doesn't seem to work.

So I guess to debug I should try different ways to specify primary_params?

Perhaps having a zero length vector into Stan is just not going to work? Wonder if you have vignettes over at primarycensored making that work?

Edit: I can see you have:

  transformed data {
    array[0] real primary_params;
  }

@SamuelBrand1
Copy link

That might be the problem; worth checking

@seabbs
Copy link
Contributor Author

seabbs commented Nov 19, 2024

I think you can't create params inside your wrapper function as it looks like it wants it to be defined in the model block? Seems like a weird edge case but a place to start

@athowes
Copy link
Collaborator

athowes commented Nov 19, 2024

I think to use the custom family functionality of brms it has to be putting the distributional parameters into the likelihood. So I'm not sure how to go about moving creating param outside the call to primarycensored_lpmf and then geting param. Maybe it could be done by creating a dummy family which doesn't actually do anything and then having everything be custom Stan code injection

@athowes
Copy link
Collaborator

athowes commented Nov 19, 2024

Good news! My cmdstanr won't compile on the primarycensored vignette either:

Compiling Stan program...
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:736:47: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
        std::vector<int>{dist_id, primary_id, stan::math::size(params),
                                              ^~~~~~~~~~~~~~~~~~~~~~~~

/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:822:18: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_cdf<int, std::vector<double>, int, std::vector<double>, nullptr>' requested here
                 primarycensored_cdf(d, dist_id, params, pwindow,
                 ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:906:21: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lcdf<int, std::vector<double>, int, std::vector<double>, nullptr>' requested here
    log_cdf_upper = primarycensored_lcdf(d_upper, dist_id, params, pwindow,
                    ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:2322:13: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lpmf<false, int, std::vector<double>, int, std::vector<double>, nullptr>' requested here
            primarycensored_lpmf<false>(
            ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:2596:12: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lognormal_model::log_prob_impl<false, false, Eigen::Matrix<double, -1, 1>, Eigen::Matrix<int, -1, 1>, nullptr, nullptr, nullptr>' requested here
    return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
           ^
/Users/adamhowes/.cmdstan/cmdstan-2.35.0/stan/src/stan/model/model_base_crtp.hpp:93:50: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lognormal_model::log_prob<false, false, double>' requested here
    return static_cast<const M*>(this)->template log_prob<false, false, double>(
                                                 ^
/var/folders/4r/hkp4v9fn3wx044
_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:2188:3: note: in instantiation of member function 'stan::model::model_base_crtp<primarycensored_lognormal_model_namespace::primarycensored_lognormal_model>::log_prob' requested here
  ~primarycensored_lognormal_model() {}
  ^

/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:736:47: note: insert an explicit cast to silence this issue
        std::vector<int>{dist_id, primary_id, stan::math::size(params),
                                              ^~~~~~~~~~~~~~~~~~~~~~~~
                                              static_cast<int>(       )
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:737:11: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
          stan::math::size(primary_params)}, "assigning variable ids");
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:737:11: note: insert an explicit cast to silence this issue
          stan::math::size(primary_params)}, "assigning variable ids");
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          static_cast<int>(               )

/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:736:47: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
        std::vector<int>{dist_id, primary_id, stan::math::size(params),
                                              ^~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:822:18: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_cdf<int, std::vector<stan::math::var_value<double>>, int, std::vector<double>, nullptr>' requested here
                 primarycensored_cdf(d, dist_id, params, pwindow,
                 ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:906:21: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lcdf<int, std::vector<stan::math::var_value<double>>, int, std::vector<double>, nullptr>' requested here
    log_cdf_upper = primarycensored_lcdf(d_upper, dist_id, params, pwindow,
                    ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:2383:13: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lpmf<false, int, std::vector<stan::math::var_value<double>>, int, std::vector<double>, nullptr>' requested here
            primarycensored_lpmf<false>(
            ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:2596:12: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lognormal_model::log_prob_impl<false, false, Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<int, -1, 1>, nullptr, nullptr, nullptr>' requested here
    return log_prob_impl<propto__, jacobian__>(params_r, 
params_i, pstream);
           ^
/Users/adamhowes/.cmdstan/cmdstan-2.35.0/stan/src/stan/model/model_base_crtp.hpp:98:50: note: in instantiation of function template specialization 'primarycensored_lognormal_model_namespace::primarycensored_lognormal_model::log_prob<false, false, stan::math::var_value<double>>' requested here
    return static_cast<const M*>(this)->template log_prob<false, false>(theta,
                                                 ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:2188:3: note: in instantiation of member function 'stan::model::model_base_crtp<primarycensored_lognormal_model_namespace::primarycensored_lognormal_model>::log_prob' requested here
  ~primarycensored_lognormal_model() {}
  ^
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:736:47: note: insert an explicit cast to silence this issue
        std::vector<int>{dist_id, primary_id, stan::math::size(params),
                                              ^~~~~~~~~~~~~~~~~~~~~~~~
                                              static_cast<int>(       )
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:737:11: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'int' in initializer list [-Wc++11-narrowing]
          stan::math::size(primary_params)}, "assigning variable ids");
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.hpp:737:11: note: insert an explicit cast to silence this issue
          stan::math::size(primary_params)}, "assigning variable ids");
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          static_cast<int>(               )

4 errors generated.

make: *** [/var/folders/4r/hkp4v9fn3wx044_hk8qnsjxw0000gn/T/RtmpRKMYAu/model-1df328634dce.o] Error 1

Error: An error occured during compilation! See the message above for more information.

Suggests to me it's some kind of install / compiler issue I have (rather than an issue with the model).

I have tried:

  • Uninstall and reinstall cmdstanr

The result of:

> cmdstanr::cmdstan_make_local()
[1] "CXXFLAGS += -Wno-deprecated-declarations"

suggests my cmdstanr uses the default compiler. I could try setting it more specifically. Also making sure up to date.

Maybe something similar to acfr/snark#117

@athowes
Copy link
Collaborator

athowes commented Nov 19, 2024

I'm a bit confused about how to solve this. I'd be interested @seabbs or @SamuelBrand1 if you try running the code in https://github.com/epinowcast/epidist/pull/426/files#diff-7080e507dc8e86af57fa62d8ef779fa1a108c00ff88a09b526df205512881bca and it might work for you if you are able to run the vignette in primarycensored.

seabbs added a commit that referenced this issue Dec 3, 2024
* Add cohort model template

* Fix to previous commit (name as cohort_model)

* Generate simulated cohort data

* Add unweighted and weighted direct models

* Thinking about custom family for pcd function

* functions component of stanvars

* Add transformed parameters for cohort model

* Progress on implementing PCD model

* Set q as vector

* Get rid of params input

* This would work, apart from it's the CDF. For the PMF need to import many primarycensored functions..

* Almost working with "import all functions" strategy

* Wrap up on attempt

* Rename to marginal model

* Move towards single wrap function with others imported

* Just running into some C++ errors..

* This doesn't change anything

* Move to marginal model name and lint

* Create aggregate data inside model conversion function for now

* First draft on moving marginal_model into functions

* Run document

* Tests working up to valid Stan code

* Regex version of marginal model

* Use prep_marginal_obs

* Improve assert for marginal model

* Add pkgdown and document

* Clean up scratch implementation

* remove scratch file

* update data format, formula, and family

* update stan code

* basic working version

* add transformm data methods

* start exploring the ebola example

* add a helper to find meaningful relative_obs_times

* get the full ebola vignette working with new variable requirements

* improve return messages

* update approx vignette

* get ebole vignette passing by checking pp and related inputs

* add marginal model integration tests

* expand post processing tests

* add marginal model

* use the right transform data keyword

* add ... pass through to make constructors correct

* fix .summarise_n_by_formula test so error message is as expected

* drop not required .row_id

* check using ... properly

* make the progress messages prettier for reducing data complexit:

* check post process tests again

* add a test for the specific transform data method

* add some tests for the generic transform data method

* put transform data tests in the correct folder

* add a news update

* change vignette language to talk about marginal model

* update the FAQ to use the marginal variables

* call it transformed_data not trans_data

* change the error message to make it clear its a epidist limitation

* update stan docs

* Update NEWS.md

* Update NEWS.md

Co-authored-by: Adam Howes <[email protected]>

* Update NEWS.md

Co-authored-by: Adam Howes <[email protected]>

* Update inst/stan/latent_model/functions.stan

Co-authored-by: Adam Howes <[email protected]>

* Update setup.R

---------

Co-authored-by: Sam <[email protected]>
@seabbs
Copy link
Contributor Author

seabbs commented Dec 5, 2024

Done in #426

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
low For a future release research project A more academic contribution
Projects
None yet
Development

No branches or pull requests

3 participants