/home/marcle/src/R/microsimulation/src/person-r.cc
Go to the documentation of this file.
#include <microsimulation.h>
namespace {
using namespace std;
using namespace ssim;
enum gleason_t {nogleason,gleasonLt7,gleason7,gleasonGt7};
enum stage_t {Healthy,Localised,DxLocalised,LocallyAdvanced,DxLocallyAdvanced,
Metastatic,DxMetastatic,Death};
enum event_t {toDeath, toPCDeath, toLocalised, toDxLocalised,
toDxLocallyAdvanced,
toLocallyAdvanced, toMetastatic, toDxMetastatic};
class Person : public cProcess
{
public:
gleason_t gleason;
stage_t stage;
bool dx;
int id;
// static member(s)
static std::map<std::string, std::vector<double> > report;
static std::map<std::string, Rng *> rng;
static void resetPopulation ();
//
Person(int i = 0) : gleason(nogleason), stage(Healthy), dx(false), id(i) {};
void init();
virtual void handleMessage(const cMessage* msg);
virtual Time age() { return now(); }
};
void Person::resetPopulation() {
report.clear();
}
// initialise static member(s)
std::map<std::string, std::vector<double> > Person::report;
std::map<std::string, Rng *> Person::rng;
double dxHR(stage_t stage) {
// raise error if healthy?
return stage==Healthy ? -1 :
(stage==Localised ? 1.1308 :
(stage==LocallyAdvanced ? 0.5900 :1.3147));
}
double progressionHR(gleason_t gleason) {
return gleason==gleasonLt7 ? 1 :
(gleason==gleason7 ? 1.3874 : 1.4027 * 1.3874);
}
void Person::init() {
rng["NH"]->set();
if (R::runif(0.0,1.0)<0.2241)
scheduleAt(R::rweibull(exp(2.3525),64.0218),toLocalised);
scheduleAt(R::rexp(80.0),toDeath);
}
void Person::handleMessage(const cMessage* msg) {
double pDx;
report["id"].push_back(id);
report["startTime"].push_back(previousEventTime);
report["endTime"].push_back(now());
report["state"].push_back(stage);
report["event"].push_back(msg->kind);
rng["NH"]->set();
if (msg->kind == toDeath) {
Sim::stop_simulation();
}
else if (msg->kind == toPCDeath) {
Sim::stop_simulation();
}
else if (msg->kind == toLocalised) {
stage = Localised;
gleason = (R::runif(0.0,1.0)<0.6812) ? gleasonLt7 :
((R::runif(0.0,1.0)<0.5016) ? gleason7 : gleasonGt7);
Time dwellTime = now()+
rweibullHR(exp(1.0353),19.8617,progressionHR(gleason)*
dxHR(stage));
// now separate out for different transitions
pDx = 1.1308/(2.1308);
if (R::runif(0.0,1.0)<pDx) {
scheduleAt(dwellTime, toDxLocalised);
}
else {
scheduleAt(dwellTime,toLocallyAdvanced);
}
}
else if (msg->kind == toLocallyAdvanced) {
stage=LocallyAdvanced;
Time dwellTime = now()+
rweibullHR(exp(1.4404),16.3863,progressionHR(gleason)*
dxHR(stage));
// now separate out for different transitions
pDx = 0.5900/(1.0+0.5900);
if (R::runif(0.0,1.0)<pDx) {
scheduleAt(dwellTime, toDxLocallyAdvanced);
}
else {
scheduleAt(dwellTime,toMetastatic);
}
}
else if (msg->kind == toMetastatic) {
stage=Metastatic;
Time dwellTime = now()+
rweibullHR(exp(1.4404),1.4242,progressionHR(gleason)*
dxHR(stage));
// now separate out for different transitions
pDx = 1.3147/(1.0+1.3147);
if (R::runif(0.0,1.0)<pDx) {
scheduleAt(dwellTime, toDxMetastatic);
}
else {
scheduleAt(dwellTime,toPCDeath); // prior to diagnosis!
}
}
else if (msg->kind == toDxLocalised) {
dx=true;
// relative survival
}
else if (msg->kind == toDxLocallyAdvanced) {
dx=true;
// relative survival
}
else if (msg->kind == toDxMetastatic) {
dx=true;
// relative survival
};
}
extern "C" {
RcppExport SEXP callPersonSimulation(SEXP inseed, SEXP parms) {
Rcpp::List parmsl(parms);
Rcpp::IntegerVector inseed2(inseed);
int nin = Rcpp::as<int>(parmsl["n"]);
double seed[6];
for (int i=0; i<6; i++) {
seed[i]=inseed2[i];
}
//r_create_current_stream();
RngStream::SetPackageSeed(seed);
Person::resetPopulation();
Person::rng["NH"] = new Rng();
Person::rng["S"] = new Rng();
Person::rng["NH"]->set();
Person person;
for (int i = 0; i < nin; i++) {
//Person::rng.foreach(nextSubStream);
Person::rng["NH"]->nextSubstream();
Person::rng["S"]->nextSubstream();
person = Person(i);
Sim::create_process(&person);
Sim::run_simulation();
Sim::clear();
}
// tidy up -- what needs to be deleted?
delete Person::rng["NH"];
delete Person::rng["S"];
// Person::rng.clear();
// output arguments to R
return Rcpp::wrap(Person::report);
} // callPersonSimulation()
} // extern "C"
} // namespace person_r
namespace {
class VerySimple : public cProcess {
public:
void init() {
scheduleAt(10.0, "a message");
scheduleAt(11.0, "another message");
};
virtual void handleMessage(const cMessage* msg) {};
};
extern "C" {
RcppExport SEXP callSpeedTest() {
VerySimple simple;
for (int i = 0; i < 1000000; i++) {
simple = VerySimple();
Sim::create_process(&simple);
Sim::run_simulation();
Sim::clear();
}
return Rcpp::wrap(1);
} // callSpeedTest()
} // extern "C"
} // anonymous namespace
nogleason
@ nogleason
Definition: person-r-20121231.cc:37
ssim::cMessage
cMessage class for OMNET++ API compatibility. This provides a heavier message class than Sim::Event,...
Definition: microsimulation.h:211
Person::handleMessage
virtual void handleMessage(const cMessage *msg)
Definition: person-r-20121231.cc:107
gleason7
@ gleason7
Definition: person-r-20121231.cc:37
gleasonLt7
@ gleasonLt7
Definition: person-r-20121231.cc:37
ssim::cProcess
cProcess class for OMNET++ API compatibility.
Definition: microsimulation.h:314
Rcpp::wrap
SEXP wrap(const std::vector< std::pair< T1, T2 > > v)
callPersonSimulation
void callPersonSimulation(int *inseed, double *parms, int *nin, double *out, int *nout)
Definition: person-r-20121231.cc:194
stage_t
stage_t
enum of type of disease stage
Definition: person-r-20121231.cc:40
progressionHR
double progressionHR(gleason_t gleason)
Definition: person-r-20121231.cc:90
Localised
@ Localised
Definition: person-r-20121231.cc:40
Metastatic
@ Metastatic
Definition: person-r-20121231.cc:41
gleason_t
gleason_t
enum for type of Gleason score
Definition: person-r-20121231.cc:37
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:9
Death
@ Death
Definition: person-r-20121231.cc:41
Person::init
void init()
Definition: person-r-20121231.cc:98
LocallyAdvanced
@ LocallyAdvanced
Definition: person-r-20121231.cc:40
DxMetastatic
@ DxMetastatic
Definition: person-r-20121231.cc:41
DxLocallyAdvanced
@ DxLocallyAdvanced
Definition: person-r-20121231.cc:40
gleasonGt7
@ gleasonGt7
Definition: person-r-20121231.cc:37
dxHR
double dxHR(stage_t stage)
Definition: person-r-20121231.cc:80
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
DxLocalised
@ DxLocalised
Definition: person-r-20121231.cc:40
Person::resetPopulation
static void resetPopulation()
Definition: person-r-20121231.cc:65
ssim::rweibullHR
double rweibullHR(double shape, double scale, double hr)
Random Weibull distribution for a given shape, scale and hazard ratio.
Definition: microsimulation.cc:5
Healthy
@ Healthy
Definition: person-r-20121231.cc:40
illnessDeath::report
EventReport< short, short, double > report
Definition: illness-death.cpp:13
illnessDeath::event_t
event_t
Definition: illness-death.cpp:11
ssim::Rng
Definition: microsimulation.h:548
microsimulation.h
ssim::cMessage::kind
short kind
Definition: microsimulation.h:222