13 enum hpv_t {LR_HPV,HPV_16,HPV_18,Other_HR_HPV};
15 enum state_t {Normal, HPV, CIN1, CIN23, LocalCancer, RegionalCancer, DistantCancer,
Death};
17 enum event_t {toHPV, toCIN1, toNormal, toCIN23, toNoCIN, toLocalCancer, toRegionalCancer,
33 typedef std::tuple<hpv_t,state_t,state_t> H_key;
34 typedef map<H_key,NumericInterpolate> H_t;
35 typedef vector<HPV_infection *> infections_t;
42 Rng * rngNh, * rngOther, * rngScreen, * rngTreatment;
45 NumericVector parameter;
46 LogicalVector bparameter;
52 rngScreen =
new Rng();
53 rngTreatment =
new Rng();
86 class cMessageByHPV :
public cMessage {
92 inline bool cMessageHPVPred(
const ssim::Event* e,
const hpv_t hpv) {
93 const cMessageByHPV * msg =
dynamic_cast<const cMessageByHPV *
>(e);
94 return (msg != 0 && msg->hpv == hpv);
97 inline bool cMessageAnyHPVPred(
const ssim::Event* e) {
98 return (
dynamic_cast<const cMessageByHPV *
>(e) != 0);
101 class cMessageUtilityChange :
public cMessage {
103 cMessageUtilityChange(
double change) :
cMessage(toUtilityChange), change(change) { }
107 class cMessageUtility :
public cMessage {
109 cMessageUtility(
double utility) :
cMessage(toUtility), utility(utility) { }
115 T bounds(T x, T a, T b) {
116 return (x<a)?a:((x>b)?b:x);
119 T max(T left, T right) {
120 return (left<right) ? right : left;
123 class HPV_infection :
public cProcess
128 bool ever_infected, immunity;
133 HPV_infection(hpv_t hpv = LR_HPV,
double p_immunity = 1.0) :
134 hpv(hpv), p_immunity(p_immunity) {}
137 virtual void handleMessage(
const cMessage* msg);
138 void removeEvents() {
139 cancel([hpv](
const cMessage * msg) {
return this->hpv == hpv; });
149 double cohort, utility;
152 infections_t infections;
153 Person(SimInput * in = NULL, SimOutput * out = NULL,
const int id = 0,
const double cohort = 1950) :
154 in(in), out(out), id(id), cohort(cohort), utility(1.0) { }
156 virtual void handleMessage(
const cMessage* msg);
157 void link(HPV_infection * infection) {
158 infection->out = out;
160 infections.push_back(infection);
161 infection->person=
this;
164 void removeHPVevents() {
167 Sim::ignore_event(cMessageAnyHPVPred);
182 return in->H[H_key(hpv,from,to)].invert(-log(R::runif(0.0,1.0)),
now());
185 void HPV_infection::init() {
188 ever_infected =
false;
192 scheduleAt(event_time(Normal,HPV),
new cMessageByHPV(toHPV,hpv));
195 void HPV_infection::handleMessage(
const cMessage* msg) {
199 if (person->id < in->parameter[
"nLifeHistories"]) {
200 out->lifeHistories.record(
"id",person->id);
201 out->lifeHistories.record(
"time",
now());
202 out->lifeHistories.record(
"hpv",hpv);
203 out->lifeHistories.record(
"event",msg->
kind);
211 scheduleAt(event_time(Normal,HPV),
212 new cMessageByHPV(toHPV,hpv));
216 ever_infected =
true;
218 scheduleAt(event_time(HPV,Normal),
219 new cMessageByHPV(toNormal,hpv));
220 scheduleAt(event_time(HPV,CIN1),
221 new cMessageByHPV(toCIN1,hpv));
222 scheduleAt(event_time(HPV,CIN23),
223 new cMessageByHPV(toCIN23,hpv));
228 scheduleAt(event_time(CIN1,Normal),
229 new cMessageByHPV(toNormal,hpv));
231 scheduleAt(event_time(CIN1,HPV),
232 new cMessageByHPV(toHPV,hpv));
233 scheduleAt(event_time(CIN1,CIN23),
234 new cMessageByHPV(toCIN23,hpv));
239 scheduleAt(event_time(CIN1,Normal),
240 new cMessageByHPV(toNormal,hpv));
242 scheduleAt(event_time(CIN1,HPV),
243 new cMessageByHPV(toHPV,hpv));
244 scheduleAt(event_time(CIN1,CIN23),
245 new cMessageByHPV(toCIN23,hpv));
249 person->removeHPVevents();
250 person->scheduleAt(
now(),
new cMessage(toLocalCancer));
288 double aoc = in->rmu0.rand(R::runif(0.0,1.0));
307 if (id < in->parameter[
"nLifeHistories"]) {
308 out->outParameters.record(
"id",
double(
id));
309 out->outParameters.record(
"aoc",aoc);
310 out->outParameters.record(
"cohort",cohort);
327 if (id < in->parameter[
"nLifeHistories"]) {
328 out->lifeHistories.record(
"id",
id);
329 out->lifeHistories.record(
"time",
now());
330 out->lifeHistories.record(
"hpv",-1.0);
331 out->lifeHistories.record(
"event",msg->
kind);
340 if (id < in->parameter[
"nLifeHistories"]) {
341 out->outParameters.record(
"dx",dx);
342 out->outParameters.record(
"age_d",
now());
343 out->outParameters.record(
"cancer_death",1.0);
345 Sim::stop_simulation();
349 if (id < in->parameter[
"nLifeHistories"]) {
350 out->outParameters.record(
"dx",dx);
351 out->outParameters.record(
"age_d",
now());
352 out->outParameters.record(
"cancer_death",0.0);
354 Sim::stop_simulation();
362 case toRegionalCancer:
363 state = RegionalCancer;
367 case toDistantCancer:
368 state = DistantCancer;
394 REprintf(
"No valid kind of event: %i\n",msg->
kind);
402 RcppExport SEXP callCervical(SEXP parmsIn) {
406 HPV_infection lr_hpv, hpv_16, hpv_18, other_hr_hpv;
414 List tables = parms[
"tables"];
415 in.parameter = parms[
"parameter"];
416 in.bparameter = parms[
"bparameter"];
417 List otherParameters = parms[
"otherParameters"];
418 in.debug = as<bool>(in.bparameter[
"debug"]);
419 NumericVector mu0 = as<NumericVector>(otherParameters[
"mu0"]);
426 DataFrame df_H = as<DataFrame>(tables[
"H"]);
428 IntegerVector hpv = df_H[
"hpv"],
429 from_state = df_H[
"from_state"],
430 to_state = df_H[
"to_state"];
431 NumericVector age = df_H[
"age"],
432 survival = df_H[
"survival"];
433 typedef pair<double,double> dpair;
435 for (
int i=0; i<age.size(); ++i) {
436 in.H[H_key((hpv_t) hpv[i], (
state_t) from_state[i], (
state_t) to_state[i])].push_back(dpair(age[i],-log(survival[i])));
439 for (H_t::iterator
it = in.H.begin();
it != in.H.end();
it++)
440 it->second.prepare();
444 NumericVector cohort = as<NumericVector>(parms[
"cohort"]);
448 std::iota(ages0, ages0+106, 0.0);
449 in.rmu0 =
Rpexp(&mu0[0], ages0, 106);
450 vector<double> ages(101);
451 std::iota(ages.begin(), ages.end(), 0.0);
452 ages.push_back(1.0e+6);
457 out.outParameters.clear();
458 out.lifeHistories.clear();
466 for (
int i = 0; i < in.parameter[
"n"]; ++i) {
468 int id = i+as<int>(in.parameter[
"firstID"]);
469 lr_hpv = HPV_infection(LR_HPV);
470 hpv_16 = HPV_infection(HPV_16);
471 hpv_18 = HPV_infection(HPV_18);
472 other_hr_hpv = HPV_infection(Other_HR_HPV);
474 person = Person(&in,&out,
id,cohort[i]);
476 person.link(&lr_hpv);
477 person.link(&hpv_16);
478 person.link(&hpv_18);
479 person.link(&other_hr_hpv);
481 Sim::create_process(&person);
482 Sim::create_process(&lr_hpv);
483 Sim::create_process(&hpv_16);
484 Sim::create_process(&hpv_18);
485 Sim::create_process(&other_hr_hpv);
487 Sim::run_simulation();
490 in.rngNh->nextSubstream();
491 in.rngOther->nextSubstream();
492 in.rngScreen->nextSubstream();
493 in.rngTreatment->nextSubstream();
494 if (i % 10000 == 0) Rcpp::checkUserInterrupt();
502 _(
"lifeHistories") = out.lifeHistories.wrap(),
503 _(
"parameters") = out.outParameters.wrap()