Hello, I am trying to generate random survival times by inverting the function, S(t)= exp(b*F(t)), where b is constant and F(t) is some cumulative distribution function, let say that F(t) is cdf of normal distribution or any others distributions. as we know that S(t) has uniform distribution on (0,1) so we can write that U= exp(b*F(t)), where U is uniform (0,1). Now to generat the time t, we do log(U)= b* F(t) (log(U))/ b = F(t) and then t = F((log(U))/b)^(-1). My Question is how we can get the inverse function for the cumulative distribution F(t) in R?. Is there any package in R can help to invert some function especially if this function is pdf or cdf?. Thanks in advance. [[alternative HTML version deleted]]
On Sat, Sep 29, 2012 at 6:23 PM, Salma Wafi <salmawafi76 at yahoo.com> wrote:> Hello, > I am trying to generate random survival times by inverting the function, S(t)= exp(b*F(t)), where b is constant and F(t) is some cumulative distribution function, let say that F(t) is cdf of normal distribution or any others distributions. > > as we know that S(t) has uniform distribution on (0,1) so we can write that > U= exp(b*F(t)), where U is uniform (0,1). Now to generat the time t, we do > log(U)= b* F(t) > (log(U))/ b = F(t) and then t = F((log(U))/b)^(-1). > > My Question is how we can get the inverse function for the cumulative distribution F(t) in R?. Is there any package in R can help to invert some function especially if this function is pdf or cdf?.If F is a standard distribution, the q*** functions are what you are likely looking for. If F is an empirical fit, you'll have to roll your own but it's not hard. Take a look at the ?ecdf function for some inspiration. Michael> > Thanks in advance. > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Sep 29, 2012, at 10:23 AM, Salma Wafi wrote:> Hello, > I am trying to generate random survival times by inverting the function, S(t)= exp(b*F(t)),This looks a bit confused. S(t) = 1- exp( -integral( h(t)dt ) where h(t) is the instantaneous hazard. That integral is the cumulative hazard function.> where b is constant and F(t) is some cumulative distribution function, let say that F(t) is cdf of normal distribution or any others distributions. > > as we know that S(t) has uniform distribution on (0,1)We do? Seems dubious. That would seem especially unlikely if we assumed that S(t) =exp( b* CDF_normal(t))> so we can write that > U= exp(b*F(t)), where U is uniform (0,1). Now to generat the time t, we do > log(U)= b* F(t) > (log(U))/ b = F(t) and then t = F((log(U))/b)^(-1). > > My Question is how we can get the inverse function for the cumulative distribution F(t) in R?. Is there any package in R can help to invert some function especially if this function is pdf or cdf?.You may want to look at the Quantile2 function in package Hmisc. It is designed to generate simulated times for varying hazard ratios when offered survival probabilities. Harrell also has a Lognorm2, a Gompertz2, and Weibull2 functions. -- David Winsemius, MD Alameda, CA, USA
Dear All, Thank you very much for your kind and response. Actually, S(t) = exp(b* F(t)) represent one of survival models, where F(t) is known cumulative distribtion function. So, that is why I need to generate the survival times T based on this model. Then for generating time I have to invert S(t) as following, Since, S(t) has uniform distribution on (0,1) so we can write that U= exp(b*F(t)), where U is uniform (0,1). Now to generat the time t, we do log(U)= b* F(t) (log(U))/ b = F(t) and then t = F((log(U))/b)^(-1). Now in case F(t) is cdf of Lognormal distribution or Weibull distribution My Question is how we can get the inverse of the cumulative distribution F(t) in R?. Is there any package in R can help to invert some function especially if this function is pdf or cdf?. Thanks in advance. [[alternative HTML version deleted]]
On Sun, Sep 30, 2012 at 10:51 AM, Salma Wafi <salmawafi76 at yahoo.com> wrote:> Dear All, > Thank you very much for your kind and response. > > Actually, S(t) = exp(b* F(t)) represent one of survival models, where F(t) is known cumulative distribtion function. So, that is why I need to generate the survival times T based on this model. Then for generating time I have to invert S(t) as following, > > Since, S(t) has uniform distribution on (0,1) so we can write that > U= exp(b*F(t)), where U is uniform (0,1). Now to generat the time t, we do > log(U)= b* F(t) > (log(U))/ b = F(t) and then t = F((log(U))/b)^(-1). > > Now in case F(t) is cdf of Lognormal distribution or Weibull distribution > > My Question is how we can get the inverse of the cumulative distribution F(t) in R?. Is there any package in R can help to invert some function especially if this function is pdf or cdf?.I repeat myself: if you have fit F() to a log-normal or Weibull distribution, just use qlnorm() or qweibull() included in base R. If you still need to fit those distributions, use MASS::fitdistr. Cheers, Michael> > Thanks in advance. > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Sep 30, 2012, at 5:34 AM, R. Michael Weylandt wrote:> On Sun, Sep 30, 2012 at 10:51 AM, Salma Wafi <salmawafi76 at yahoo.com> wrote: >> Dear All, >> Thank you very much for your kind and response. >> >> Actually, S(t) = exp(b* F(t)) represent one of survival models, where F(t) is known cumulative distribtion function. So, that is why I need to generate the survival times T based on this model. Then for generating time I have to invert S(t) as following, >> >> Since, S(t) has uniform distribution on (0,1)No, it does NOT in general or in any of the scenarios you have painted have a uniform distribution. It does, of course, have a range of [0,1].>> so we can write that >> U= exp(b*F(t)), where U is uniform (0,1). Now to generat the time t, we do >> log(U)= b* F(t) >> (log(U))/ b = F(t) and then t = F((log(U))/b)^(-1). >> >> Now in case F(t) is cdf of Lognormal distribution or Weibull distribution >> >> My Question is how we can get the inverse of the cumulative distribution F(t) in R?. Is there any package in R can help to invert some function especially if this function is pdf or cdf?. > > I repeat myself: if you have fit F() to a log-normal or Weibull > distribution, just use qlnorm() or qweibull() included in base R. If > you still need to fit those distributions, use MASS::fitdistr.And I repeat myself, too. If you want to simulate one of a) an arbitrary survival vector, or b) a lognormal or c) a Weibull, then the Hmisc package has facilities to do each of those. -- David.> > Cheers, > > Michael > >>-- David Winsemius, MD Alameda, CA, USA