# Re: st: Sampling from a t-distribution using Stata

 From jpitblado@stata.com (Jeff Pitblado, StataCorp LP) To statalist@hsphsun2.harvard.edu Subject Re: st: Sampling from a t-distribution using Stata Date Mon, 04 Jun 2007 16:46:46 -0500

```Tiago <tiago.pereira@incor.usp.br> asks how to simulate data from Student's t
distribution:

> I would like to generate a new variable called "x", in which "x" has a
> t-distribution with n-3 degrees of freedom (where n is the number of new
> generated observations), a mean "m" and a standard deviation, "sd", that
> is:
>
> x ~ t-distribution[n-3 df] (m, sd)
>
> How do I obtain a set of 100 new observations assuming that m=0.4 and
> sd=0.05 using Stata?
>
> I will be very grateful for any help.

Here is a do-file that will simulate a dataset from Student's t distribution
with the requested degrees of freedom, mean, and standard deviation:

***** BEGIN:	sim-t.do
// set the random number seed for reproducing the simulated values
set seed 12345

// parameters for the data generating process
local n  100			// sample size
local m    0.4			// mean
local sd   0.05			// standard deviation

// assuming we are starting with an empty dataset, set the sample size
set obs `n'

gen x = `m' + `sd'*invttail(`n'-3, uniform())
***** END:	sim-t.do

We can use -invttail()- since the t distribution is continuous and symmetric

Note that we could use -invnormal()- instead since the degrees of freedom, 97,
are so much greater than 30 (where the differences between the two CDF's are
practically indistinguishable).

--Jeff