Bookmark and Share

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

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

st: Trying to simulate sampling distribution of mean

From   krishanu karmakar <>
Subject   st: Trying to simulate sampling distribution of mean
Date   Tue, 29 Jan 2013 18:15:10 -0500

Dear Friends,

The following is my code

==== code start =====

program define ybar, rclass
	syntax [,]
	replace y1 = y2
	summarize y1
	return scalar m_y = r(mean)

local reps 5

	quietly use big.dta, clear	
	generate y2 = age
	sample 0.1

	gen y1=.
	simulate m_age=r(m_y), saving(meandata, replace) nodots reps(`reps'): ybar

==== code ends =====

What I am trying to do.
I have a dataset named "big.dta" with 100,000 observations. The only
variable in this dataset is "age".

I want to first draw a sample of size 100 from this dataset and
calculate the mean for the variable "age". I want to draw 5 such
samples and store the mean of "age" from each sample as the variable
"m_age" in a new dataset called "meandata". So this dataset will have
5 observations.

My code is running, but wrongly. I am getting stata to save the
"meandata", but all the five observations (mean of age from 5
different samples) are stored as equal in value. That means stata is
not drawing 5 different samples, but only one sample. Could anyone
help by showing which line my code should I change?

Thank you in advance for your time.

Krishanu Karmakar

Read it:
Specially Question 3.
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index