<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/simulating-a-weibull-conditional-on-time-to-event-is-greater-than-a-given-time/"></div></div><div><a data-url="http://www.r-bloggers.com/simulating-a-weibull-conditional-on-time-to-event-is-greater-than-a-given-time/" data-counturl="http://www.r-bloggers.com/simulating-a-weibull-conditional-on-time-to-event-is-greater-than-a-given-time/" data-count="horizontal" data-text="Simulating a Weibull conditional on time-to-event is greater than a given time" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://realizationsinbiostatistics.blogspot.com/2016/05/simulating-weibull-conditional-on-time.html"> Realizations in Biostatistics</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div><a target="_blank" href="https://1.bp.blogspot.com/-fC_SJOSwmGA/Vz3Yxee9aVI/AAAAAAAAbYs/JRHVhZM9Bps0pmLomSTzrBgf7fNX-ToygCLcB/s1600/Rplot.png"><img height="121" width="200" src="https://1.bp.blogspot.com/-fC_SJOSwmGA/Vz3Yxee9aVI/AAAAAAAAbYs/JRHVhZM9Bps0pmLomSTzrBgf7fNX-ToygCLcB/s200/Rplot.png" border="0"></a></div>
<p>Recently, I had to simulate a time-to-event of subjects who have been on a study, are still ongoing at the time of a data cut, but who are still at risk of an event (e.g. progressive disease, cardiac event, death). This requires the simulation of a conditional Weibull. To do this, I created the following function:</p>
<p><span># simulate conditional Weibull conditional on survival > T —————</span><br><span><br></span><span># reliability function is exp{-(T+t/b)^a} / exp{-(T/b)^a} = 1-F(t)</span><br><span># n = number of points to return</span><br><span># shape = shape parm of weibull</span><br><span># scale = scale parm of weibull (default 1)</span><br><span># t is minimum (default is 0, which makes the function act like rweibull)</span><br><span>my_rweibull &lt;- function(n,shape,scale=1,t=0) {</span><br><span>  if (length(t)!=1 &amp;&amp; length(t)!=n) {</span><br><span>    stop(“length(t) is not 1 or n”)</span><br><span>  }</span><br><span>  return(shape*(-log(runif(n))+(t/shape)^scale)^(1/scale))</span><br><span>}</span>
<div></div>
<p><span><br></span><span><br></span>You use this function just like <span>rweibull</span>, with the exception that you pass in another vector <span>t</span> of minimum times or a scalar representing the minimum time of all simulated values. The idea is that the probability that the random variable will be at least T is given by <span>exp{-(T+t/b)^a} / exp{-(T/b)^a}</span><span>, so you can simulate this with a uniform random variate. I use the inversion method on the reliability function (just like using the inversion method on the distribution function, with the insight that if </span><span>U</span><span> is uniform(0,1), so is </span><span><span>1-</span><span>U</span></span><span>).</span><br><span><br></span><span>Truth be told, I ought to buckle down and learn how to do packages in R, but for now I’ll just pass on some code on my blog if I think it will be helpful (or if I need to find it while doing a Google search later).</span></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/simulating-a-weibull-conditional-on-time-to-event-is-greater-than-a-given-time/"></div></div><div><a data-url="http://www.r-bloggers.com/simulating-a-weibull-conditional-on-time-to-event-is-greater-than-a-given-time/" data-counturl="http://www.r-bloggers.com/simulating-a-weibull-conditional-on-time-to-event-is-greater-than-a-given-time/" data-count="vertical" data-text="Simulating a Weibull conditional on time-to-event is greater than a given time" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://realizationsinbiostatistics.blogspot.com/2016/05/simulating-weibull-conditional-on-time.html"> Realizations in Biostatistics</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/tnCZMH4RXj4"></a></div>