Microsimulation API: /home/marcle/src/R/microsimulation/src/archive/person-r-20121231.cc Source File

Microsimulation API

person-r-20121231.cc
Go to the documentation of this file.
1
29 #include "event-r.h"
30 #include <Rcpp.h>
31
32 //double inf = 1.0 / 0.0;
33
34 using namespace std;
35
37 enum gleason_t {nogleason,gleasonLt7,gleason7,gleasonGt7};
38
40 enum stage_t {Healthy,Localised,DxLocalised,LocallyAdvanced,DxLocallyAdvanced,
41 Metastatic,DxMetastatic,Death};
42
44 class Person : public cProcess
45 {
46 public:
47 gleason_t gleason;
48 stage_t stage;
49 bool dx;
50 // static members (for statistics)
51 static int popSize,
52 nCancer,
53 nLocalisedCancer,
54 nLocallyAdvancedCancer,
55 nMetastaticCancer;
56 static Means personTime;
57 static void resetPopulation ();
58 //
59 Person() : dx(false), gleason(nogleason), stage(Healthy) {};
60 void init();
61 virtual void handleMessage(const cMessage* msg);
62 virtual Time age() { return now(); }
63 };
64
65 void Person::resetPopulation() {
66 personTime = Means();
67 popSize = nCancer = nLocalisedCancer = nLocallyAdvancedCancer = nMetastaticCancer = 0;
68 }
69
70 Means Person::personTime = Means();
71 int Person::popSize = 0;
72 int Person::nCancer = 0;
73 int Person::nLocalisedCancer = 0;
74 int Person::nLocallyAdvancedCancer = 0;
75 int Person::nMetastaticCancer = 0;
76
80 double dxHR(stage_t stage) {
81 // raise error if healthy?
82 return stage==Healthy ? -1 :
83 (stage==Localised ? 1.1308 :
84 (stage==LocallyAdvanced ? 0.5900 :1.3147));
85 }
86
90 double progressionHR(gleason_t gleason) {
91 return gleason==gleasonLt7 ? 1 :
92 (gleason==gleason7 ? 1.3874 : 1.4027 * 1.3874);
93 }
94
98 void Person::init() {
99 if (R::runif(0.0,1.0)<0.2241)
100 scheduleAt(R::rweibull(exp(2.3525),64.0218),"Localised");
101 scheduleAt(R::rexp(80.0),"Death");
102 }
103
107 void Person::handleMessage(const cMessage* msg) {
108
109 double dwellTime, pDx;
110
111 if (msg->name == "Death") {
112 personTime += msg->timestamp;
113 popSize += 1;
114 Sim::stop_simulation();
115 }
116
117 else if (msg->name == "PCDeath") {
118 // record that this was a PC death prior to diagnosis
119 personTime += msg->timestamp;
120 popSize += 1;
121 Sim::stop_simulation();
122 }
123
124 else if (msg->name == "Localised") {
125 stage = Localised;
126 gleason = (R::runif(0.0,1.0)<0.6812) ? gleasonLt7 :
127 ((R::runif(0.0,1.0)<0.5016) ? gleason7 : gleasonGt7);
128 Time dwellTime = now()+
129 rweibullHR(exp(1.0353),19.8617,progressionHR(gleason)*
130 dxHR(stage));
131 // now separate out for different transitions
132 pDx = 1.1308/(2.1308);
133 if (R::runif(0.0,1.0)<pDx) {
134 scheduleAt(dwellTime, "DxLocalised");
135 }
136 else {
137 scheduleAt(dwellTime,"LocallyAdvanced");
138 }
139 }
140
141 else if (msg->name == "LocallyAdvanced") {
142 stage=LocallyAdvanced;
143 nLocallyAdvancedCancer += 1;
144 Time dwellTime = now()+
145 rweibullHR(exp(1.4404),16.3863,progressionHR(gleason)*
146 dxHR(stage));
147 // now separate out for different transitions
148 pDx = 0.5900/(1.0+0.5900);
149 if (R::runif(0.0,1.0)<pDx) {
150 scheduleAt(dwellTime, "DxLocallyAdvanced");
151 }
152 else {
153 scheduleAt(dwellTime,"Metastatic");
154 }
155 }
156
157 else if (msg->name == "Metastatic") {
158 stage=Metastatic;
159 Time dwellTime = now()+
160 rweibullHR(exp(1.4404),1.4242,progressionHR(gleason)*
161 dxHR(stage));
162 // now separate out for different transitions
163 pDx = 1.3147/(1.0+1.3147);
164 if (R::runif(0.0,1.0)<pDx) {
165 scheduleAt(dwellTime, "DxMetastatic");
166 }
167 else {
168 scheduleAt(dwellTime,"PCDeath"); // prior to diagnosis!
169 }
170 }
171
172 else if (msg->name == "DxLocalised") {
173 dx=true;
174 nLocalisedCancer += 1;
175 // relative survival
176 }
177
178 else if (msg->name == "DxLocallyAdvanced") {
179 dx=true;
180 nLocallyAdvancedCancer += 1;
181 // relative survival
182 }
183
184 else if (msg->name == "DxMetastatic") {
185 dx=true;
186 nMetastaticCancer += 1;
187 // relative survival
188 };
189
190 };
191
192 extern "C" {
193
194 void callPersonSimulation(int* inseed, double* parms, int *nin, double *out, int *nout) {
195 // input parameters from R (TODO)
196 Person person;
197 unsigned long seed[6];
198 for (int i=0; i<6; i++) {
199 seed[i]=(unsigned long)inseed[i];
200 }
201 //GetRNGstate(); // for non-user-defined uniform random number generators
202 RngStream_SetPackageSeed(seed);
203 Rng * rng = new Rng();
204 rng->set();
205 Person::resetPopulation();
206 for (int i = 0; i < *nin; i++) {
207 rng->nextSubstream();
208 person = Person();
209 Sim::create_process(&person);
210 Sim::run_simulation();
211 Sim::clear();
212 }
213 // output arguments to R
214 out[0] = Person::personTime.mean();
215 out[1] = Person::personTime.sd();
216 // tidy up -- what needs to be deleted?
217 delete rng;
218 //PutRNGstate(); // for non-user-defined uniform random number generators
219 }
220
221
222 void testRng(int* inseed, double *out) {
223 unsigned long seed[6];
224 for (int i=0; i<6; i++) {
225 seed[i]=(unsigned long)inseed[i];
226 }
227 //GetRNGstate();
228 RngStream_SetPackageSeed(seed);
229 Rng * rng = new Rng();
230 rng->set();
231 // output arguments to R
232 out[0] = R::rnorm(0.0, 1.0);
233 out[1] = R::rnorm(0.0, 1.0);
234 // tidy up -- what needs to be deleted?
235 delete rng;
236 //PutRNGstate(); // for non-user-defined uniform random number generators
237 }
238
239 } // 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