 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: How do I simulate survival data?

 From Antoine Terracol To thomas@tmbx.com, statalist@hsphsun2.harvard.edu Subject Re: st: How do I simulate survival data? Date Fri, 04 Oct 2013 18:43:57 +0200

```Hi Thomas,

would the following code suit your needs?

```
It draws from weibull distributions with time varying covariates, and uses the TVC to make the hazards differ from time t=18 onwards.
```
Antoine

--------------------------------------------------
clear
set obs 1000
gen id=_n
gen d=runiform()>0.5
g u=runiform()
expand 36

bysort id : gen t=_n
gen dt=d & t>=18

scalar alpha=.5
gen lambda=exp(-1+2*dt)
bysort id (t) : gen e=t[_n-1]
replace e=0 if t==1

```
bysort id (t) : gen double sum=sum(-lambda*t^alpha + lambda*e^alpha) + lambda*t^alpha
```gen time=( (ln(1-u)-sum)/(-lambda))^(1/alpha)
bysort id (t) : gen cont=time>t
bysort id (t) : gen sstop=sum(cont==0)
drop if sstop>1
bysort id (t) : gen fail=sstop

bysort id (t) : replace time=t if fail==0

bysort id (t) : keep if _n==_N
stset time, id(id) fail(fail)

sts graph ,by(d)
---------------------------------------------------------------

On 03/10/13 19:16, Thomas Speidel wrote:
```
```I'm trying to simluate survival data for illustrative purposes.  I've
never done this in Stata, so I'm not sure how I should proceed.
The aim is to produce a Kaplan-Meier graph of a ficticious time to event
process stratified by a binary variable. I would like the two lines to
overlap until half time, and then starts diverging from one another. I
also would like enough data to make the stair steps not too tall.
I know how to generate the random time.  But how do I generate the event
and the binary variable so that it meets my conditions?

Thanks.

```
```

--
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```