Microsimulation API

/home/marcle/src/R/microsimulation/src/illness-death.cpp
Go to the documentation of this file.
1 #include <microsimulation.h>
2 #include <numeric>
3
4 namespace illnessDeath {
5
6 using namespace std;
7 using namespace ssim;
8
9 enum state_t {Healthy,Cancer};
10
11 enum event_t {toOtherDeath, toCancer, toCancerDeath};
12
13 EventReport<short,short,double> report;
14 double cure, zsd; // parameters - could be static class variables
15
16
17 double b_weibull(double mean, double a, double rr = 1.0) {
18 return mean/R::gammafn(1.0+1.0/a)*pow(rr,-1.0/a);
19 }
20
21 class SimplePerson : public cProcess
22 {
23 public:
24 state_t state;
25 int id;
26 double z;
27 SimplePerson(const int i = 0) : id(i) {};
28 void init();
29 virtual void handleMessage(const cMessage* msg);
30 };
31
35 void SimplePerson::init() {
36 state = Healthy;
37 z = exp(R::rnorm(0.0,zsd));
38 scheduleAt(R::rweibull(4.0,b_weibull(80.0,4.0)), toOtherDeath);
39 if (R::runif(0.0,1.0)>cure)
40 scheduleAt(R::rweibull(3.0,b_weibull(80.0,3.0,z)), toCancer);
41 }
42
46 void SimplePerson::handleMessage(const cMessage* msg) {
47
48 report.add(state, msg->kind, previousEventTime, now());
49
50 switch(msg->kind) {
51
52 case toOtherDeath:
53 case toCancerDeath:
54 // reporting already completed: stop the simulation
55 Sim::stop_process();
56 break;
57
58 case toCancer:
59 state = Cancer;
60 RemoveKind(toOtherDeath);
61 if (R::runif(0.0,1.0) < 0.5) // cure fraction
62 scheduleAt(now() + R::rweibull(1.0,10.0), toCancerDeath);
63 break;
64
65 default:
66 REprintf("No valid kind of event\n");
67 break;
68
69 } // switch
70
71 } // handleMessage()
72
73 RcppExport SEXP callIllnessDeath(SEXP parms) {
74 SimplePerson person;
75 Rcpp::RNGScope scope;
76 Rcpp::List parmsl(parms);
77 int n = Rcpp::as<int>(parmsl["n"]);
78 cure = Rcpp::as<double>(parmsl["cure"]);
79 zsd = Rcpp::as<double>(parmsl["zsd"]);
80
81 vector<double> ages(101);
82 std::iota(ages.begin(), ages.end(), 0.0);
83 ages.push_back(1.0e+6);
84 report.clear();
85 report.setPartition(ages);
86
87 for (int i = 0; i < n; i++) {
88 person = SimplePerson(i);
89 Sim::create_process(&person);
90 Sim::run_simulation();
91 Sim::clear();
92 }
93 return report.wrap();
94 }
95
96 } // namespace illnessDeath
illnessDeath::cure
double cure
Definition: illness-death.cpp:14
ssim::cMessage
cMessage class for OMNET++ API compatibility. This provides a heavier message class than Sim::Event,...
Definition: microsimulation.h:211
illnessDeath::SimplePerson::z
double z
Definition: illness-death.cpp:26
illnessDeath::Cancer
@ Cancer
Definition: illness-death.cpp:9
ssim::cProcess
cProcess class for OMNET++ API compatibility.
Definition: microsimulation.h:314
ssim::Sim::create_process
static ProcessId create_process(Process *)
creates a new process
Definition: ssim.cc:108
illnessDeath::SimplePerson
Definition: illness-death.cpp:21
illnessDeath::toOtherDeath
@ toOtherDeath
Definition: illness-death.cpp:11
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:9
illnessDeath::state_t
state_t
Definition: illness-death.cpp:9
illnessDeath::SimplePerson::init
void init()
Definition: illness-death.cpp:35
ssim::RemoveKind
void RemoveKind(short kind)
RemoveKind is a function to remove messages with the given kind from the queue.
Definition: microsimulation.h:256
illnessDeath::SimplePerson::state
state_t state
Definition: illness-death.cpp:24
illnessDeath::SimplePerson::handleMessage
virtual void handleMessage(const cMessage *msg)
Definition: illness-death.cpp:46
illnessDeath
Definition: illness-death.cpp:4
illnessDeath::zsd
double zsd
Definition: illness-death.cpp:14
illnessDeath::SimplePerson::id
int id
Definition: illness-death.cpp:25
illnessDeath::b_weibull
double b_weibull(double mean, double a, double rr=1.0)
Definition: illness-death.cpp:17
ssim::Sim::clear
static void clear()
clears out internal data structures
Definition: ssim.cc:115
illnessDeath::SimplePerson::SimplePerson
SimplePerson(const int i=0)
Definition: illness-death.cpp:27
illnessDeath::Healthy
@ Healthy
Definition: illness-death.cpp:9
ssim::Sim::run_simulation
static void run_simulation()
starts execution of the simulation
Definition: ssim.cc:148
illnessDeath::toCancerDeath
@ toCancerDeath
Definition: illness-death.cpp:11
illnessDeath::toCancer
@ toCancer
Definition: illness-death.cpp:11
ssim::Sim::stop_process
static void stop_process()
stops the execution of the current process
Definition: ssim.cc:241
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
ssim::EventReport
EventReport class for collecting statistics on person-time, prevalence and numbers of events.
Definition: microsimulation.h:666
illnessDeath::callIllnessDeath
RcppExport SEXP callIllnessDeath(SEXP parms)
Definition: illness-death.cpp:73
illnessDeath::report
EventReport< short, short, double > report
Definition: illness-death.cpp:13
illnessDeath::event_t
event_t
Definition: illness-death.cpp:11
microsimulation.h
ssim::cMessage::kind
short kind
Definition: microsimulation.h:222