person-r-20121231.cc
Go to the documentation of this file.
#include "event-r.h"
#include <Rcpp.h>
//double inf = 1.0 / 0.0;
using namespace std;
enum gleason_t {nogleason,gleasonLt7,gleason7,gleasonGt7};
enum stage_t {Healthy,Localised,DxLocalised,LocallyAdvanced,DxLocallyAdvanced,
Metastatic,DxMetastatic,Death};
class Person : public cProcess
{
public:
gleason_t gleason;
stage_t stage;
bool dx;
// static members (for statistics)
static int popSize,
nCancer,
nLocalisedCancer,
nLocallyAdvancedCancer,
nMetastaticCancer;
static Means personTime;
static void resetPopulation ();
//
Person() : dx(false), gleason(nogleason), stage(Healthy) {};
void init();
virtual void handleMessage(const cMessage* msg);
virtual Time age() { return now(); }
};
void Person::resetPopulation() {
personTime = Means();
popSize = nCancer = nLocalisedCancer = nLocallyAdvancedCancer = nMetastaticCancer = 0;
}
Means Person::personTime = Means();
int Person::popSize = 0;
int Person::nCancer = 0;
int Person::nLocalisedCancer = 0;
int Person::nLocallyAdvancedCancer = 0;
int Person::nMetastaticCancer = 0;
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() {
if (R::runif(0.0,1.0)<0.2241)
scheduleAt(R::rweibull(exp(2.3525),64.0218),"Localised");
scheduleAt(R::rexp(80.0),"Death");
}
void Person::handleMessage(const cMessage* msg) {
double dwellTime, pDx;
if (msg->name == "Death") {
personTime += msg->timestamp;
popSize += 1;
Sim::stop_simulation();
}
else if (msg->name == "PCDeath") {
// record that this was a PC death prior to diagnosis
personTime += msg->timestamp;
popSize += 1;
Sim::stop_simulation();
}
else if (msg->name == "Localised") {
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, "DxLocalised");
}
else {
scheduleAt(dwellTime,"LocallyAdvanced");
}
}
else if (msg->name == "LocallyAdvanced") {
stage=LocallyAdvanced;
nLocallyAdvancedCancer += 1;
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, "DxLocallyAdvanced");
}
else {
scheduleAt(dwellTime,"Metastatic");
}
}
else if (msg->name == "Metastatic") {
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, "DxMetastatic");
}
else {
scheduleAt(dwellTime,"PCDeath"); // prior to diagnosis!
}
}
else if (msg->name == "DxLocalised") {
dx=true;
nLocalisedCancer += 1;
// relative survival
}
else if (msg->name == "DxLocallyAdvanced") {
dx=true;
nLocallyAdvancedCancer += 1;
// relative survival
}
else if (msg->name == "DxMetastatic") {
dx=true;
nMetastaticCancer += 1;
// relative survival
};
};
extern "C" {
void callPersonSimulation(int* inseed, double* parms, int *nin, double *out, int *nout) {
// input parameters from R (TODO)
Person person;
unsigned long seed[6];
for (int i=0; i<6; i++) {
seed[i]=(unsigned long)inseed[i];
}
//GetRNGstate(); // for non-user-defined uniform random number generators
RngStream_SetPackageSeed(seed);
Rng * rng = new Rng();
rng->set();
Person::resetPopulation();
for (int i = 0; i < *nin; i++) {
rng->nextSubstream();
person = Person();
Sim::create_process(&person);
Sim::run_simulation();
Sim::clear();
}
// output arguments to R
out[0] = Person::personTime.mean();
out[1] = Person::personTime.sd();
// tidy up -- what needs to be deleted?
delete rng;
//PutRNGstate(); // for non-user-defined uniform random number generators
}
void testRng(int* inseed, double *out) {
unsigned long seed[6];
for (int i=0; i<6; i++) {
seed[i]=(unsigned long)inseed[i];
}
//GetRNGstate();
RngStream_SetPackageSeed(seed);
Rng * rng = new Rng();
rng->set();
// output arguments to R
out[0] = R::rnorm(0.0, 1.0);
out[1] = R::rnorm(0.0, 1.0);
// tidy up -- what needs to be deleted?
delete rng;
//PutRNGstate(); // for non-user-defined uniform random number generators
}
} // extern "C"
nogleason
@ nogleason
Definition: person-r-20121231.cc:37
Person::nLocalisedCancer
static int nLocalisedCancer
number of localised cancers diagnosed
Definition: person-r-20121231.cc:53
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
callPersonSimulation
void callPersonSimulation(int *inseed, double *parms, int *nin, double *out, int *nout)
Definition: person-r-20121231.cc:194
Person::age
virtual Time age()
Definition: person-r-20121231.cc:62
Person::nCancer
static int nCancer
number of cancers
Definition: person-r-20121231.cc:52
stage_t
stage_t
enum of type of disease stage
Definition: person-r-20121231.cc:40
Person::dx
bool dx
Definition: person-r-20121231.cc:49
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
Person::Person
Person()
Definition: person-r-20121231.cc:59
Person::nLocallyAdvancedCancer
static int nLocallyAdvancedCancer
number of locally advanced cancers diagnosed
Definition: person-r-20121231.cc:54
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
Person::stage
stage_t stage
Definition: person-r-20121231.cc:48
DxMetastatic
@ DxMetastatic
Definition: person-r-20121231.cc:41
DxLocallyAdvanced
@ DxLocallyAdvanced
Definition: person-r-20121231.cc:40
Person::personTime
static Means personTime
Definition: person-r-20121231.cc:56
gleasonGt7
@ gleasonGt7
Definition: person-r-20121231.cc:37
Person::gleason
gleason_t gleason
Definition: person-r-20121231.cc:47
testRng
void testRng(int *inseed, double *out)
Definition: person-r-20121231.cc:222
dxHR
double dxHR(stage_t stage)
Definition: person-r-20121231.cc:80
Person::nMetastaticCancer
static int nMetastaticCancer
number of metastatic cancers diagnosed
Definition: person-r-20121231.cc:55
Person::popSize
static int popSize
size of the population
Definition: person-r-20121231.cc:51
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
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