Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

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


From   [email protected] (Jeff Pitblado, StataCorp LP)
To   [email protected]
Subject   Re: st: Sampling from a t-distribution using Stata
Date   Mon, 04 Jun 2007 16:46:46 -0500

Tiago <[email protected]> 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
about 0.

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
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index