https://github.com/cran/rstpm2
Raw File
Tip revision: c12a9847539968aa375d4df8349a3a524e7c1bb5 authored by Mark Clements on 17 January 2019, 14:50:04 UTC
version 1.4.5
Tip revision: c12a984
timevar.html
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2018-08-03 Fri 10:35 -->
<meta  http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta  name="viewport" content="width=device-width, initial-scale=1" />
<title>Predictions</title>
<meta  name="generator" content="Org-mode" />
<meta  name="author" content="Mark Clements" />
<style type="text/css">
 <!--/*--><![CDATA[/*><!--*/
  .title  { text-align: center;
             margin-bottom: .2em; }
  .subtitle { text-align: center;
              font-size: medium;
              font-weight: bold;
              margin-top:0; }
  .todo   { font-family: monospace; color: red; }
  .done   { font-family: monospace; color: green; }
  .priority { font-family: monospace; color: orange; }
  .tag    { background-color: #eee; font-family: monospace;
            padding: 2px; font-size: 80%; font-weight: normal; }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  .org-right  { margin-left: auto; margin-right: 0px;  text-align: right; }
  .org-left   { margin-left: 0px;  margin-right: auto; text-align: left; }
  .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  .underline { text-decoration: underline; }
  #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  p.verse { margin-left: 3%; }
  pre {
    border: 1px solid #ccc;
    box-shadow: 3px 3px 3px #eee;
    padding: 8pt;
    font-family: monospace;
    overflow: auto;
    margin: 1.2em;
  }
  pre.src {
    position: relative;
    overflow: visible;
    padding-top: 1.2em;
  }
  pre.src:before {
    display: none;
    position: absolute;
    background-color: white;
    top: -10px;
    right: 10px;
    padding: 3px;
    border: 1px solid black;
  }
  pre.src:hover:before { display: inline;}
  pre.src-sh:before    { content: 'sh'; }
  pre.src-bash:before  { content: 'sh'; }
  pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  pre.src-R:before     { content: 'R'; }
  pre.src-perl:before  { content: 'Perl'; }
  pre.src-java:before  { content: 'Java'; }
  pre.src-sql:before   { content: 'SQL'; }

  table { border-collapse:collapse; }
  caption.t-above { caption-side: top; }
  caption.t-bottom { caption-side: bottom; }
  td, th { vertical-align:top;  }
  th.org-right  { text-align: center;  }
  th.org-left   { text-align: center;   }
  th.org-center { text-align: center; }
  td.org-right  { text-align: right;  }
  td.org-left   { text-align: left;   }
  td.org-center { text-align: center; }
  dt { font-weight: bold; }
  .footpara { display: inline; }
  .footdef  { margin-bottom: 1em; }
  .figure { padding: 1em; }
  .figure p { text-align: center; }
  .inlinetask {
    padding: 10px;
    border: 2px solid gray;
    margin: 10px;
    background: #ffffcc;
  }
  #org-div-home-and-up
   { text-align: right; font-size: 70%; white-space: nowrap; }
  textarea { overflow-x: auto; }
  .linenr { font-size: smaller }
  .code-highlighted { background-color: #ffff00; }
  .org-info-js_info-navigation { border-style: none; }
  #org-info-js_console-label
    { font-size: 10px; font-weight: bold; white-space: nowrap; }
  .org-info-js_search-highlight
    { background-color: #ffff00; color: #000000; font-weight: bold; }
  /*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart  The following is the entire license notice for the
JavaScript code in this tag.

Copyright (C) 2012-2013 Free Software Foundation, Inc.

The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.


@licend  The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
 function CodeHighlightOn(elem, id)
 {
   var target = document.getElementById(id);
   if(null != target) {
     elem.cacheClassElem = elem.className;
     elem.cacheClassTarget = target.className;
     target.className = "code-highlighted";
     elem.className   = "code-highlighted";
   }
 }
 function CodeHighlightOff(elem, id)
 {
   var target = document.getElementById(id);
   if(elem.cacheClassElem)
     elem.className = elem.cacheClassElem;
   if(elem.cacheClassTarget)
     target.className = elem.cacheClassTarget;
 }
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">Predictions</h1>

<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1"><span class="section-number-2">1</span> R implementation for <a href="https://pclambert.net/software/stpm2/stpm2_timevar/">https://pclambert.net/software/stpm2/stpm2_timevar/</a></h2>
<div class="outline-text-2" id="text-1">
<div class="org-src-container">

<pre class="src src-R"><span style="color: #008b8b;">library</span>(rstpm2)
<span style="color: #008b8b;">library</span>(readstata13)
suppressWarnings(rott2b <span style="color: #008b8b;">&lt;-</span> read.dta13(<span style="color: #8b2252;">"https://www.pclambert.net/data/rott2b.dta"</span>))
rott2b <span style="color: #008b8b;">&lt;-</span> transform(rott2b, time = pmin(rf,60)/12)
rott2b <span style="color: #008b8b;">&lt;-</span> transform(rott2b, event = (rfi==1) &amp; (time &lt; 60/12))
fit <span style="color: #008b8b;">&lt;-</span> stpm2(Surv(time,event) ~ hormon + nsx(age,df=3,centre=60,stata=<span style="color: #228b22;">TRUE</span>) + pr_1, 
             data=rott2b, df=4, stata=<span style="color: #228b22;">TRUE</span>)
eform(fit)
</pre>
</div>

<pre class="example">
Profiling...

                                                exp(beta)        2.5 %       97.5 %
(Intercept)                                  6.055607e-04 5.715701e-04 6.402896e-04
hormonyes                                    1.253597e+00 1.065865e+00 1.462097e+00
nsx(age, df = 3, centre = 60, stata = TRUE)1 6.550003e-01 5.594371e-01 7.702334e-01
nsx(age, df = 3, centre = 60, stata = TRUE)2 3.449221e-01 1.993938e-01 5.962187e-01
nsx(age, df = 3, centre = 60, stata = TRUE)3 9.626652e-01 7.189637e-01 1.275743e+00
pr_1                                         9.080228e-01 8.944134e-01 9.214805e-01
nsx(log(time), df = 4)1                      4.474345e+02 4.148646e+02 4.799754e+02
nsx(log(time), df = 4)2                      1.974220e+02 1.894423e+02 2.047062e+02
nsx(log(time), df = 4)3                      5.391834e+04 4.862060e+04 5.986957e+04
nsx(log(time), df = 4)4                      7.282589e+01 6.949526e+01 7.650186e+01
</pre>

<div class="org-src-container">

<pre class="src src-R">nd <span style="color: #008b8b;">&lt;-</span> data.frame(age=60, hormon=<span style="color: #8b2252;">"no"</span>, pr_1=0)
plot(fit, type=<span style="color: #8b2252;">"surv"</span>, newdata=nd, xlab=<span style="color: #8b2252;">"Years from surgery"</span>)
</pre>
</div>


<div class="figure">
<p><img src="surv_1.png" alt="surv_1.png" />
</p>
</div>

<div class="org-src-container">

<pre class="src src-R"><span style="color: #008b8b;">library</span>(ggplot2)
nd <span style="color: #008b8b;">&lt;-</span> data.frame(age=60, hormon=c(<span style="color: #8b2252;">"no"</span>,<span style="color: #8b2252;">"yes"</span>), pr_1=0)
haz <span style="color: #008b8b;">&lt;-</span> predict(fit, type=<span style="color: #8b2252;">"haz"</span>, newdata=nd, grid=<span style="color: #228b22;">TRUE</span>, se.fit=<span style="color: #228b22;">TRUE</span>, keep=<span style="color: #228b22;">TRUE</span>)
haz <span style="color: #008b8b;">&lt;-</span> cbind(haz,attr(haz,<span style="color: #8b2252;">"newdata"</span>))
ggplot(haz, aes(time,group=hormon)) + geom_line(aes(y=Estimate,colour=hormon)) + geom_ribbon(aes(ymin=lower,ymax=upper), alpha=0.2) + xlab(<span style="color: #8b2252;">"Time since surgery (years)"</span>) + ylab(<span style="color: #8b2252;">"Hazard"</span>)
</pre>
</div>


<div class="figure">
<p><img src="haz_1.png" alt="haz_1.png" />
</p>
</div>

<div class="org-src-container">

<pre class="src src-R">s_time1 <span style="color: #008b8b;">&lt;-</span> predict(fit, newdata=transform(rott2b, time=1), type=<span style="color: #8b2252;">"surv"</span>)
hist(s_time1,xlab=<span style="color: #8b2252;">"Time from surgery"</span>,main=<span style="color: #8b2252;">""</span>)
</pre>
</div>


<div class="figure">
<p><img src="histo_1.png" alt="histo_1.png" />
</p>
</div>

<div class="org-src-container">

<pre class="src src-R">s_time5 <span style="color: #008b8b;">&lt;-</span> predict(fit, newdata=transform(rott2b, time=5), type=<span style="color: #8b2252;">"surv"</span>)
hist(s_time5,xlab=<span style="color: #8b2252;">"Time from surgery"</span>,main=<span style="color: #8b2252;">""</span>)
</pre>
</div>


<div class="figure">
<p><img src="histo_2.png" alt="histo_2.png" />
</p>
</div>

<div class="org-src-container">

<pre class="src src-R"><span style="color: #008b8b;">library</span>(ggplot2)
nd <span style="color: #008b8b;">&lt;-</span> with(rott2b, data.frame(age=seq(min(age), max(age), length=301), hormon=<span style="color: #8b2252;">"no"</span>, pr_1=3.43, time=1))
s <span style="color: #008b8b;">&lt;-</span> cbind(nd,predict(fit, newdata=nd, type=<span style="color: #8b2252;">"surv"</span>, se.fit=<span style="color: #228b22;">TRUE</span>))
ggplot(s,aes(age)) + geom_line(aes(y=Estimate)) + geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.2) +
    xlab(<span style="color: #8b2252;">"Age (years)"</span>) + ylab(<span style="color: #8b2252;">"Survival"</span>)
</pre>
</div>


<div class="figure">
<p><img src="surv_1_age.png" alt="surv_1_age.png" />
</p>
</div>



<div class="org-src-container">

<pre class="src src-R">s <span style="color: #008b8b;">&lt;-</span> cbind(nd,predict(fit, newdata=transform(nd,time=5), type=<span style="color: #8b2252;">"surv"</span>, se.fit=<span style="color: #228b22;">TRUE</span>))
ggplot(s,aes(age)) + geom_line(aes(y=Estimate)) + geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.2) +
    xlab(<span style="color: #8b2252;">"Age (years)"</span>) + ylab(<span style="color: #8b2252;">"Survival"</span>)
</pre>
</div>


<div class="figure">
<p><img src="surv_5_age.png" alt="surv_5_age.png" />
</p>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Mark Clements</p>
<p class="date">Created: 2018-08-03 Fri 10:35</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>
back to top