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 on April 23, and its replacement, is already up and running.

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

Re: st: Replace

From   Maarten Buis <>
Subject   Re: st: Replace
Date   Fri, 2 Sep 2011 22:52:54 +0200

--- on Fri, Sep 2, 2011 at 10:29 PM, Daniel Marcelino wrote:
> I'm in doubt how I could run a loop to randomly introduce 100 missing
> values in the 'auto' dataset.

That is easy as the auto dataset contains only 74 observations ;)

Lets assume that you wanted exactly 20 missing observations on the variable mpg

sysuse auto, clear
gen s = runiform()
sort s
replace mpg = . in 1/20

Generally I like to specify a probability of getting a missing rather
than a fixed number of missings. Below I assume you want a 20% chance
of getting a missing.

sysuse auto, clear
replace mpg = . if runiform() < .2

Generalizations of this trick that will allow you let that probability
depend on other variables (e.g. to check how important the MAR, MCAR
and NMAR assumptions are) can be found in: M.L. Buis (2007b), "Stata
tip 48: Discrete uses for uniform()", The Stata Journal, 7(3), pp.
434-435. <>

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
*   For searches and help try:

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