/home/marcle/src/R/microsimulation/src/calibperson-r.cc
Go to the documentation of this file.
#include "microsimulation.h"
#include <Rcpp.h>
namespace {
using namespace ssim;
using namespace std;
enum stage_t {DiseaseFree,Precursor,PreClinical,Clinical,Death};
enum event_t {toPrecursor, toPreClinical, toClinical, toDeath, Count};
string stage_names[5] = {"DiseaseFree","Precursor","PreClinical","Clinical","Death"};
Rng * rng;
class CalibPerson : public cProcess
{
public:
stage_t stage;
bool diseasepot;
double Lam1,sigm1,p2,lam2,mu3,tau3;
double clinTime;
int id;
// static member(s)
static std::map<std::string, std::vector<double> > report;
static void resetPopulation ();
CalibPerson() {} // default constructor
CalibPerson(double *par, int i=0) {
Lam1=par[0];
sigm1=par[1];
p2=par[2];
lam2=par[3];
mu3=par[4];
tau3=par[5];
id=i;
stage=DiseaseFree;
};
void init();
virtual void handleMessage(const cMessage* msg);
virtual Time age() { return now(); }
};
void CalibPerson::resetPopulation() {
report.clear();
}
// initialise static member(s)
std::map<std::string, std::vector<double> > CalibPerson::report;
void CalibPerson::init() {
if (R::runif(0,1)<p2){
diseasepot = true;}
else {
diseasepot = false;
}
clinTime=1000;
stage=DiseaseFree;
double lam1 = exp(R::rnorm(Lam1,sigm1));
scheduleAt(R::rexp(lam1), toPrecursor);
double x = R::runif(0,1);
scheduleAt((65 - 15*log(-log(x))), toDeath); //Gumbel
for(int i=10;i<110;i=i+10){
scheduleAt(i,Count);
}
}
void CalibPerson::handleMessage(const cMessage* msg) {
double ctime[] = {20,40,60,80};
int cind;
if (msg->kind == toDeath) {
stage=Death;
clinTime=std::min(clinTime,now());
for(unsigned int i=0; i<4 ; i++){
if(i < report["TimeAtRisk"].size()){
report["TimeAtRisk"][i] += std::min(ctime[i],clinTime);
}
else {
report["TimeAtRisk"].push_back(std::min(ctime[i],clinTime));
}
if(clinTime < ctime[i]){
break;
}
}
Sim::stop_simulation();
}
else if (msg->kind == toPrecursor) {
stage = Precursor;
if (diseasepot){
simtime_t dwellTime = now()+ R::rexp(lam2);
scheduleAt(dwellTime, toPreClinical);
}
}
else if (msg->kind == toPreClinical) {
stage=PreClinical;
simtime_t dwellTime = now()+ exp(R::rnorm(mu3,tau3*mu3));
scheduleAt(dwellTime, toClinical);
}
else if (msg->kind == toClinical) {
stage=Clinical;
clinTime = now();
string stagestr = stage_names[stage];
}
else if (msg->kind == Count){
cind = min(9,int(now()/10 - 1));
string stagestr = stage_names[stage];
if(report.find(stagestr) == report.end()){ //key not found
report[stagestr].assign(10,0);
}
report[stagestr][cind]+=1;
}
}
extern "C" {
RcppExport SEXP callCalibrationSimulation(SEXP parms) {
Rcpp::List parmsl(parms);
int nin = Rcpp::as<int>(parmsl["n"]);
std::vector<double> par = Rcpp::as<std::vector<double> >(parmsl["runpar"]);
CalibPerson::resetPopulation();
rng = new Rng();
rng->set();
CalibPerson::report.insert(make_pair("TimeAtRisk", std::vector<double>()));
CalibPerson person;
for (int i = 0; i < nin; i++) {
person = CalibPerson(&par[0],0);
rng->nextSubstream();
Sim::create_process(&person);
Sim::run_simulation();
Sim::clear();
}
delete rng;
return Rcpp::wrap(CalibPerson::report);
}
}
}
ssim::cMessage
cMessage class for OMNET++ API compatibility. This provides a heavier message class than Sim::Event,...
Definition: microsimulation.h:211
callCalibrationSimulation
SEXP callCalibrationSimulation(SEXP)
ssim::cProcess
cProcess class for OMNET++ API compatibility.
Definition: microsimulation.h:314
Rcpp::wrap
SEXP wrap(const std::vector< std::pair< T1, T2 > > v)
ssim::simtime_t
Time simtime_t
simtime_t typedef for OMNET++ API compatibility
Definition: microsimulation.h:439
stage_t
stage_t
enum of type of disease stage
Definition: person-r-20121231.cc:40
ssim::Sim::create_process
static ProcessId create_process(Process *)
creates a new process
Definition: ssim.cc:108
ssim::Sim::stop_simulation
static void stop_simulation()
stops execution of the simulation
Definition: ssim.cc:251
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:9
Death
@ Death
Definition: person-r-20121231.cc:41
ssim::Sim::clear
static void clear()
clears out internal data structures
Definition: ssim.cc:115
ssim::Sim::run_simulation
static void run_simulation()
starts execution of the simulation
Definition: ssim.cc:148
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
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::Rng::set
void set()
Definition: microsimulation.cc:26
ssim::cMessage::kind
short kind
Definition: microsimulation.h:222
ssim::Rng::nextSubstream
void nextSubstream()
Definition: microsimulation.h:560