/home/marcle/src/R/microsimulation/src/illness-death.cpp
Go to the documentation of this file.
#include <microsimulation.h>
#include <numeric>
namespace illnessDeath {
using namespace std;
using namespace ssim;
enum state_t {Healthy,Cancer};
enum event_t {toOtherDeath, toCancer, toCancerDeath};
EventReport<short,short,double> report;
double cure, zsd; // parameters - could be static class variables
double b_weibull(double mean, double a, double rr = 1.0) {
return mean/R::gammafn(1.0+1.0/a)*pow(rr,-1.0/a);
}
class SimplePerson : public cProcess
{
public:
state_t state;
int id;
double z;
SimplePerson(const int i = 0) : id(i) {};
void init();
virtual void handleMessage(const cMessage* msg);
};
void SimplePerson::init() {
state = Healthy;
z = exp(R::rnorm(0.0,zsd));
scheduleAt(R::rweibull(4.0,b_weibull(80.0,4.0)), toOtherDeath);
if (R::runif(0.0,1.0)>cure)
scheduleAt(R::rweibull(3.0,b_weibull(80.0,3.0,z)), toCancer);
}
void SimplePerson::handleMessage(const cMessage* msg) {
report.add(state, msg->kind, previousEventTime, now());
switch(msg->kind) {
case toOtherDeath:
case toCancerDeath:
// reporting already completed: stop the simulation
Sim::stop_process();
break;
case toCancer:
state = Cancer;
RemoveKind(toOtherDeath);
if (R::runif(0.0,1.0) < 0.5) // cure fraction
scheduleAt(now() + R::rweibull(1.0,10.0), toCancerDeath);
break;
default:
REprintf("No valid kind of event\n");
break;
} // switch
} // handleMessage()
RcppExport SEXP callIllnessDeath(SEXP parms) {
SimplePerson person;
Rcpp::RNGScope scope;
Rcpp::List parmsl(parms);
int n = Rcpp::as<int>(parmsl["n"]);
cure = Rcpp::as<double>(parmsl["cure"]);
zsd = Rcpp::as<double>(parmsl["zsd"]);
vector<double> ages(101);
std::iota(ages.begin(), ages.end(), 0.0);
ages.push_back(1.0e+6);
report.clear();
report.setPartition(ages);
for (int i = 0; i < n; i++) {
person = SimplePerson(i);
Sim::create_process(&person);
Sim::run_simulation();
Sim::clear();
}
return report.wrap();
}
} // 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