Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: RE: generate lognormal RV less than 20000 observations.


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: generate lognormal RV less than 20000 observations.
Date   Sun, 11 Apr 2010 17:53:10 +0100

Sun Samn made two points. 

First, "Sorry, I just updated my rndlgn; the old one does require 20000
or more." 

There is some misunderstanding by Sun here. 

For a long time, the -rnd- package by Joseph Hilbe and W. Linde-Zwirble
was the most developed way to get random deviates in Stata. -rnd-
includes -rndlgn-. 

Typing 

. findit rndlgn 

reveals the facts. 

Search of official help files, FAQs, Examples, SJs, and STBs

STB-41  sg44.1  . . . . . . . . . . . . Correction to random number
generators
        (help rnd if installed) . . . . . . . .  J. Hilbe and W.
Linde-Zwirble
        1/98    p.23; STB Reprints Vol 7, p.166
        faster version plus minor changes

STB-28  sg44  . . . . . . . . . . . . . . . . . . . . Random number
generators
        (help rnd if installed) . . . . . . . .  J. Hilbe and W.
Linde-Zwirble
        11/95   pp.20--21; STB Reprints Vol 5, pp.118--121
        programs to implement random number generators that allows user
        to generate synthetic data from a variety of distributions

Web resources from Stata and other users

(contacting http://www.stata.com)

sg44_1 from http://www.stata.com/stb/stb41
    STB-41 sg44_1.  Correction to random number generators. / STB insert
by /
    Joseph Hilbe, Arizona State University; / Walter Linde-Zwirble,
Health
    Outcomes Technologies. / Support:  atjmh@asuvm.inre.asu.edu / After
    installation, see help rnd.

sg44 from http://www.stata.com/stb/stb28
    STB-28 sg44.  Random number generators. / STB insert by / Joseph
Hilbe, /
    Walter Linde-Zwirble. / Support:  atjmh@asuvm.inre.asu.edu / After
    installation, see help rnd.

rnd from http://www.stata.com/users/jhilbe
    rnd.  Random data generators. / Program by Joseph Hilbe, Arizona
State
    Univ. <jhilbe@aol.com> / Statalist distribution, 29 January 1999. /
These
    programs generate random numbers for a variety of important /
    distributions.  This is an update of my STB-28 insert. / See help
rnd.

rnd from http://fmwww.bc.edu/RePEc/bocode/r
    'RND': modules for random number generation / The random number
generators
    in this package are updated versions / of those published in STB-28
and
    STB-44. They allow for / noncentrally distributed random numbers,
    distributed F, t, or / Chi-squared. The other RNGs include some
which are

-rndlgn- was published in STB-28 and updated in STB-41. There is another
copy on the StataCorp website. The most up-to-date public version is on
SSC. 

As I said, there has never been any such requirement of -rndlgn- to have
20000 or more observations. I don't know where such a bizarre idea comes
from. The help gives various examples of using -set obs- before you call
the -rnd- commands. Independently of that, the -rnd- programs will
increase the dataset size if the number of observations specified is
greater than the number of observations in memory. But the treatment of
dataset size has not changed at all in the public lifetime of -rndlgn-,
so there is no difference between older and newer versions of -rndlgn-
in this respect. 

The most notable change between STB-28 and SSC versions is a correction
of a typo in the comment: the third argument is the standard deviation,
not the variance. 

. type http://www.stata.com/stb/stb28/sg44/rndlgn.ado
*!version 1.0.0 1993 Joseph Hilbe, Walter Linde-Zwirble         (sg44:
STB-28)
* Lognormal distribution random number generator [via normal
distribution]
* Example: rndlgn 1000 0 .5  [set obs 1000; 0 = mean; .5 = variance]

program define rndlgn
        version 3.1
        cap drop xlgn
        qui     {
                local cases `1'
                set obs `cases'
                mac shift
                local mn `1'
                mac shift
                local var `1'
                mac shift
                tempvar ran1
                noi di in gr "( Generating " _c
                gen `ran1' = exp(`mn'+`var' * invnorm(uniform()))
                gen xlgn = `ran1'
                noi di in gr "." _c
                noi di in gr " )"
                noi di in bl "Variable " in ye "xlgn " in bl "created."
       }
end

. ssc type rndlgn.ado
*!version 1.1 1999 Joseph Hilbe
* version 1.0.0 1993 Joseph Hilbe                          (sg44:
STB-28)
* Lognormal distribution random number generator [via normal
distribution]
* Example: rndlgn 1000 0 .5  [set obs 1000; 0 = mean; .5 = std.
deviation]

program define rndlgn
        version 4.0
        set type double
        cap drop xlgn
        qui     {
                local cases `1'
                set obs `cases'
                mac shift
                local mn `1'
                mac shift
                local var `1'
                mac shift
                tempvar ran1
                noi di in gr "( Generating " _c
                gen `ran1' = exp(`mn'+`var' * invnorm(uniform()))
                gen xlgn = `ran1'
                noi di in gr "." _c
                noi di in gr " )"
                noi di in bl "Variable " in ye "xlgn " in bl "created."
                lab var xlgn "Lognormal random variable"
                set type float
       }
end

Second,  " For my case, I prefer to use rndlgn instead of exp(normal)
because I wanna [meaning:want to] specify heteroskedasticity."

There is no real difference here. 

In Stata 10.1 up, you can call -rnormal()- with specified mean and
standard deviation; either or both could be given via variable names, or
indeed more complicated expressions. This is quite as flexible as,
indeed slightly more flexible than, using -rndlgn-, as the latter
reliably supports only single numbers or variable names as arguments. 

Nick 
n.j.cox@durham.ac.uk 

sun samn

Sorry, I just updated my rndlgn; the old one does require 20000 or more.

For my case, I prefer to use rndlgn instead of exp(normal) because I
wanna specify heteroskedasticity.

n.j.cox@durham.ac.uk

> This sparked a thread with Martin Weiss. I have four comments by way
of summarizing and going beyond that exchange.
>
> 1. Please do specify where user-written software you refer to comes
from.
> (Most postings from Sun Samn ignore advice in the FAQ in at least one
way.)
>
> 2. It is quite untrue that -rndlgn- requires 20,000 observations.
>
> 3. In this case, the user-specified software is not, and never has
been, needed. A one-line call with reference to -exp(rnormal())- gets
you random draws from a lognormal quite directly. (Before -rnormal()-
was introduced, other functions could be used, and indeed were used
internally within -rndlgn-. I imagine that for their own reasons Joe
Hilbe and friend wanted something uniform in syntax with the other
commands that they wrote a while back.)
>
> 4. -rndlgn- is a command, not a function. -exp()- and -rnormal()- are
functions.

sun samn

>    I know the function ' rndlgn' can generate lognormal RVs, but it
requires the numbers of observation to be at 20000. Now, I want a list
of only 500. What should I do then?

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index