<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Health Care Economics on R Views</title>
    <link>https://rviews.rstudio.com/categories/health-care-economics/</link>
    <description>Recent content in Health Care Economics on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 08 Oct 2020 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/categories/health-care-economics/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Fake Survival Data for the Disease Progression Model</title>
      <link>https://rviews.rstudio.com/2020/10/08/fake-data-for-the-illness-death-model/</link>
      <pubDate>Thu, 08 Oct 2020 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2020/10/08/fake-data-for-the-illness-death-model/</guid>
      <description>
        


&lt;p&gt;In a &lt;a href=&#34;https://rviews.rstudio.com/2020/09/09/fake-data-with-r/&#34;&gt;previous post&lt;/a&gt;, I showed some examples of simulating fake data from a few packages that are useful for common simulation tasks and indicated that I would be following up with a look at simulating survival data. A tremendous amount of work in survival analysis has been done in R&lt;sup&gt;1&lt;/sup&gt; and it will take some time to explore what’s out there. In this first post, I am just going to jump into the ocean of ideas and see if I can fish out and interesting example.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://link.springer.com/article/10.2165/00019053-199813040-00003&#34;&gt;Markov models&lt;/a&gt; are commonly used in Health Care Economics to model the progression of a disease, and the efficacy and potential benefits of various treatments. One popular approach is to consider cohorts of patients who move through the three states of being &lt;em&gt;healthy&lt;/em&gt; (no disease progression), &lt;em&gt;diseased&lt;/em&gt; (some level of disease progression) and &lt;em&gt;dead&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The following figure illustrates the process. (I will explain the labeling on the arrows below).&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;/post/2020-10-02-fake-data-for-the-illness-death-model/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;These kinds of models are commonly called multi-state models in the survival literature. In the simplest case, disease progression might be modeled as a discrete time &lt;a href=&#34;https://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/Chapter11.pdf&#34;&gt;Markov chain&lt;/a&gt; where patients move from state-to-state according to a matrix of transition probabilities which govern how the process develops at discrete time intervals. However, for many studies, limiting transitions to discrete, uniform intervals is a little too simplistic. For example, in most cases, the exact time when a patient “progresses” from healthy to deceased is not observed. To account for this, modelers frequently consider &lt;a href=&#34;http://u.math.biu.ac.il/~amirgi/CTMCnotes.pdf&#34;&gt;Continuous Time Markov Chain&lt;/a&gt; which allow modeling the distribution of time spent in each state as well as the state-to-state transitions.&lt;/p&gt;
&lt;p&gt;One way to define a continuous time Markov chain is as a continuous time process that takes values in a discrete state space and obeys the Markov property where the transition to a future state depends only on the present and not on the past.&lt;/p&gt;
&lt;p&gt;A continuous-time stochastic process &lt;span class=&#34;math inline&#34;&gt;\(X_{t}, t \geq 0\)&lt;/span&gt; with discrete state space S is a continuous-time Markov chain if:
&lt;span class=&#34;math display&#34;&gt;\[P(X_{t+s}=j \:|\: X_{s}=i), X_u = x_u, 0 \leq u &amp;lt; s) = P(X_{t+s}=j \: | \: X_{s}=i)\]&lt;/span&gt; &lt;span class=&#34;math display&#34;&gt;\[ \forall s,t \geq 0 \:, i, j, x_{u} \in S, \: 0 \leq u &amp;lt; s \]&lt;/span&gt;
If the process does not depend on the the particular value of &lt;em&gt;s&lt;/em&gt; (the time when the process is in state &lt;em&gt;i&lt;/em&gt;) then it is said to be &lt;em&gt;time homogeneous&lt;/em&gt;. For a very readable account of how the definition above along with the assumption of time homogeneity ensure both the Markov property and that the time the process spends in the various states will be exponentially distributed, see Chapter 7 of &lt;a href=&#34;https://www.amazon.com/Introduction-Stochastic-Processes-Robert-Dobrow/dp/1118740653/ref=sr_1_1?dchild=1&amp;amp;keywords=stochastic+processes+in+r&amp;amp;qid=1601771046&amp;amp;s=books&amp;amp;sr=1-1&#34;&gt;Dobrow (2016)&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;One more bit of theory before we get to the example: unlike discrete time Markov chains, the development of a continuous time process is not driven by a transition matrix. Instead, state transition probabilities are generated by a matrix, &lt;em&gt;Q&lt;/em&gt;, that gives the instantaneous rates of going from one state to another. Transition probabilities for any time, &lt;em&gt;t&lt;/em&gt;, are then calculated from &lt;em&gt;Q&lt;/em&gt; using &lt;a href=&#34;https://cran.r-project.org/web/packages/expm/index.html&#34;&gt;matrix exponentiation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(t)=e^Q\]&lt;/span&gt;
The following is the &lt;em&gt;Q&lt;/em&gt; matrix for our three state disease progression model. Notice, that this is not a stochastic matrix: the rows sum to 0 not to 1. The basic idea is that the rate of flow into a state &lt;em&gt;i&lt;/em&gt; is equal to the flow out of &lt;em&gt;i&lt;/em&gt;. The final row is all zeroes in our &lt;em&gt;Q&lt;/em&gt; matrix because death is an &lt;em&gt;absorbing state&lt;/em&gt; and there are not transitions back to &lt;em&gt;healthy&lt;/em&gt; from &lt;em&gt;diseased&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[Q = \begin{pmatrix}
        \ -(q_{12} + q_{13}) &amp;amp; q_{12} &amp;amp; q_{13}) \\ 
        \ 0 &amp;amp; -q_{23} &amp;amp; q_{23} \\
        \  0 &amp;amp; 0 &amp;amp; 0          
     \end{pmatrix} \]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Armed with a little bit of theory, let’s see how continuous time Markov chains can be used both to simulate survival data and also to fit a model to the fake data.&lt;/p&gt;
&lt;div id=&#34;generating-simulated-survival-data&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Generating Simulated Survival Data&lt;/h3&gt;
&lt;p&gt;The following is essentially the example on page 12 of the pdf for the &lt;a href=&#34;https://CRAN.R-project.org/package=genSurv&#34;&gt;genSurv&lt;/a&gt; package&lt;sup&gt;2&lt;/sup&gt; listed in the CRAN Survival Task View. This shows how to use the &lt;code&gt;genTHMM()&lt;/code&gt; function to simulate data from a time homogeneous, continuous time Markov Chain. In the code below, the &lt;code&gt;model.cens&lt;/code&gt; parameter indicates that censoring is accomplished via a uniform distribution over the interval [0, &lt;code&gt;cens.par&lt;/code&gt;]. A covariate is generated by a uniform distribution over the interval [0, &lt;code&gt;covar&lt;/code&gt;] and enters the model through the equation:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[q_{i,j} = \lambda_{i,j} exp(\beta_{i,j} \cdot v)\]&lt;/span&gt;
where &lt;span class=&#34;math inline&#34;&gt;\(\lambda_{i,j}\)&lt;/span&gt; is the base rate, parameter &lt;code&gt;rate&lt;/code&gt; for the &lt;code&gt;genTHMM()&lt;/code&gt; function and &lt;span class=&#34;math inline&#34;&gt;\(\beta_{i,j}\)&lt;/span&gt; are the regression coefficients, &lt;code&gt;beta&lt;/code&gt; in the function. In the code below, we use the &lt;code&gt;covariate&lt;/code&gt; output to create a &lt;code&gt;sex&lt;/code&gt; covariate.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;set.seed(1234)
thmmdata &amp;lt;- genTHMM( n=100, model.cens=&amp;quot;uniform&amp;quot;, # censorship model
                     cens.par = 20, 
                     beta = c(0.01,0.08,0.05),
                     covar = 1, 
                     rate = c(0.1,0.05,0.08) )
                     
df &amp;lt;- thmmdata %&amp;gt;% mutate(sex = if_else(covariate &amp;lt;= .5,0,1 ))
df &amp;lt;- df %&amp;gt;% mutate_if(is.numeric, round, 3)
head(df,11)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    PTNUM   time state covariate sex
## 1      1  0.000     1     0.114   0
## 2      1  2.183     2     0.114   0
## 3      1  2.265     3     0.114   0
## 4      2  0.000     1     0.233   0
## 5      2  0.284     2     0.233   0
## 6      2  1.396     3     0.233   0
## 7      3  0.000     1     0.283   0
## 8      3  8.600     2     0.283   0
## 9      3 18.469     2     0.283   0
## 10     4  0.000     1     0.267   0
## 11     4  3.734     1     0.267   0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more on the theory underlying the &lt;code&gt;genSurv&lt;/code&gt; package have a look at the paper &lt;a href=&#34;https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2692556/&#34;&gt;Meira-Mechado et al. (2009)&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;fitting-the-survival-model&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Fitting the Survival Model&lt;/h3&gt;
&lt;p&gt;The code in this section fits a continuous time, Markov chain survival model to the data generated above using the &lt;a href=&#34;https://cran.r-project.org/package=msm&#34;&gt;&lt;code&gt;msm&lt;/code&gt;&lt;/a&gt; package&lt;sup&gt;3&lt;/sup&gt; and indicates how one might go about examining the output.&lt;/p&gt;
&lt;p&gt;First, let’s look at the transitions between states that occurred for the simulated patients.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;st &amp;lt;- statetable.msm(state, PTNUM,data = thmmdata)
st&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##     to
## from  1  2  3
##    1 28 50 22
##    2  0 25 25&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We see, for example, 50 progressed to the diseased state and 22 patient went directly from being &lt;em&gt;healthy&lt;/em&gt; to &lt;em&gt;dead&lt;/em&gt;. 25 patients who progressed to disease, subsequently died.&lt;/p&gt;
&lt;p&gt;Next, we set up the Q matrix of instantaneous transition rates described above,&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;Q &amp;lt;- matrix(c(0, 1, 1, 0, 0 , 1, 0, 0 , 0), nrow = 3, byrow = TRUE)
rownames(Q) &amp;lt;- c(&amp;quot;S1&amp;quot;, &amp;quot;S2&amp;quot;, &amp;quot;S3&amp;quot;)
colnames(Q)  &amp;lt;- c(&amp;quot;S1&amp;quot;, &amp;quot;S2&amp;quot;, &amp;quot;S3&amp;quot;)
Q&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    S1 S2 S3
## S1  0  1  1
## S2  0  0  1
## S3  0  0  0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;fit the model, and plot the survival curves for states &lt;em&gt;S1&lt;/em&gt; and &lt;em&gt;S2&lt;/em&gt; using the “old school” pre-built plot method.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;fit &amp;lt;- msm( state ~ time, subject=PTNUM, data = df, 
            qmatrix = Q, gen.inits = TRUE, covariates = ~ sex)
plot(fit)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2020-10-02-fake-data-for-the-illness-death-model/index_files/figure-html/unnamed-chunk-6-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The default print method for the mode fit shows the transition intensities with the hazard ratio of the covariate.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;fit&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## msm(formula = state ~ time, subject = PTNUM, data = df, qmatrix = Q,     gen.inits = TRUE, covariates = ~sex)
## 
## Maximum likelihood estimates
## Baselines are with covariates set to their means
## 
## Transition intensities with hazard ratios for each covariate
##         Baseline                    sex                   
## S1 - S1 -0.28724 (-0.37477,-0.2202)                       
## S1 - S2  0.24020 ( 0.17891, 0.3225) 0.8992 (0.49769,1.625)
## S1 - S3  0.04703 ( 0.02057, 0.1076) 0.2106 (0.04132,1.073)
## S2 - S2 -0.09756 (-0.14312,-0.0665)                       
## S2 - S3  0.09756 ( 0.06650, 0.1431) 0.6558 (0.30473,1.411)
## 
## -2 * log-likelihood:  413.3 
## [Note, to obtain old print format, use &amp;quot;printold.msm&amp;quot;]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can get the transition rates for sex = 0,&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;qmatrix.msm(fit, covariates = list(sex = 0))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    S1                          S2                         
## S1 -0.3583 (-0.51092,-0.25130)  0.2537 ( 0.16167, 0.39804)
## S2 0                           -0.1211 (-0.20856,-0.07037)
## S3 0                           0                          
##    S3                         
## S1  0.1046 ( 0.04986, 0.21962)
## S2  0.1211 ( 0.07037, 0.20856)
## S3 0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and for sex = 1.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;qmatrix.msm(fit, covariates = list(sex = 1))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    S1                            S2                           
## S1 -0.25013 (-0.357720,-0.17490)  0.22810 ( 0.155472, 0.33464)
## S2 0                             -0.07945 (-0.136411,-0.04627)
## S3 0                             0                            
##    S3                           
## S1  0.02204 ( 0.005169, 0.09396)
## S2  0.07945 ( 0.046269, 0.13641)
## S3 0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and &lt;code&gt;msm&lt;/code&gt; also allows us to calculate the transition function &lt;span class=&#34;math inline&#34;&gt;\(P(t)\)&lt;/span&gt; for arbitrary times.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;pmatrix.msm(fit, t= 13.3)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##         S1     S2     S3
## S1 0.02192 0.3182 0.6599
## S2 0.00000 0.2732 0.7268
## S3 0.00000 0.0000 1.0000&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we look at the mean sojourn times for patients in the &lt;em&gt;healthy&lt;/em&gt; and &lt;em&gt;diseased&lt;/em&gt; states. Normally, for a process that can transition in and out of states this means the average time spent in the state each time it is visited. For our model, patients, only go forward through the chain, there is no getting better, so the sojourn for S2 is essentially the average amount of time patients spent in the &lt;em&gt;diseased&lt;/em&gt; state.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sojourn.msm(fit)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    estimates     SE     L      U
## S1     3.481 0.4725 2.668  4.542
## S2    10.251 2.0045 6.987 15.038&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;a-few-remarks&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;A Few Remarks&lt;/h3&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;The work done in R on survival analysis, and partially embodied in the two hundred thirty-three packages listed in the CRAN &lt;a href=&#34;https://cran.r-project.org/web/views/Survival.html&#34;&gt;Survival Analysis Task View&lt;/a&gt;, constitutes a fundamental contribution to statistics. There is enough material here for a lifetime of study. Even confining oneself to a tour of the eleven packages listed in the simulation section would be a significant undertaking.&lt;/p&gt;
&lt;p&gt;&lt;sup&gt;2&lt;/sup&gt; &lt;code&gt;genSurv&lt;/code&gt; is a pretty bare bones package having just seven functions and little explanatory text. If it was not listed on the CRAN Task View, it would have been easy pass by. Nevertheless, I have only shown a small portion of what it can do.&lt;/p&gt;
&lt;p&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;code&gt;msm&lt;/code&gt; is an example of why R is a treasury of statistical knowledge. Not only does the package offer an impressive array of capabilities for analyzing multi-state Markov models in continuous time, the basic documentation, the package’s pdf, includes references to quite a few of the fundamental papers.&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2020/10/08/fake-data-for-the-illness-death-model/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
