You are on page 1of 24

Confidence Intervals

in Stata
Liang Wu
Liang.Wu@uth.tmc.edu
Topics
• The 𝑡 distribution
• Confidence Intervals
• Normal distribution
• Binomial distribution
• Poisson distribution
Student’s 𝒕 distribution
• Let 𝑋 be an independent random sample from a normal
distribution with known mean and unknown variance
(𝑋1 , . . , 𝑋𝑛 ~𝑁(𝜇, 𝜎 2 )). Then,

𝑥ҧ − 𝜇
𝑡= ~𝑡𝑛−1
𝑆/ 𝑛
• where S is the standard deviation of the sample. This statistic
emerge as solution of the estimation problem when the
population variance is unknown; in 1908 by Gosset better
known by his pseudonym “Student” (See Rosner page 169).
Stata commands for the
Student’s 𝒕 distribution
• The two commands that we will use to calculate
the distribution probabilities are
• ttail
• invttail
Student’s t distribution
• display ttail(n-1,t)
• It will compute the reverse cumulative
(upper tail, P(T>t)) Student’s 𝑡 distribution,
where (n-1) is the number of degrees of
freedom and 𝑡 is the test statistic.
Example 1
• Assume that a random sample of 20 people is
observed and a test statistic is computed
(t=1.645).
• What is the probability that t is greater than 1.645?
P(T≥1.645) ?
• Data: n=20, t=1.645
• display ttail(20-1,1.645)
• Answer: Because it is a continuous distribution
Pr(T>t)=Pr(T ≥ t) therefore
P(T ≥ 1.645)=0.058
Example 2
• Assume that a random sample of 20 people is
observed and a test statistic is computed
(t=1.0).
• What is the probability that T is less than or
equal to 1.0?
• Answer:
Data: n=20, t=1.0
display 1-ttail(20-1,1.0)
Pr(T≤t) = 1 - Pr(T>t) =0.835
Student’s 𝑡 distribution

• display invttail(n-1,p)
• This command, returns the inverse reverse
cumulative (upper-tail) Student's 𝑡 distribution
where 𝑛 − 1 are the degrees of freedom and 𝑝 is
the probability.

• Note: If ttail(n-1,t) = p, then invttail(n-1,p) = t.


• Where n-1= d.f
Student’s t distribution
• display invttail(n,p)
• P(T>t)=.05 ttail(d,t)=.05
• tt

• In Stata
Confidence Intervals
calculation
• How can we determine with certainty that our results are
close to the true value?
• The statistics that we calculate can vary depending on the
sample that we obtain.
• In order to compute the Confidence Intervals (C.I.), we need
to know the probability distribution of an statistic that
involves the parameter of interest.
Confidence Intervals functions
• The two commands commands to calculate
confidence intervals in Stata are:

• ci (when using the information


direct from a dataset)
• cii (when we have information
of summary statistics)
Confidence Intervals
functions
CI for means, proportions, and
variances

Variable
name

Distributions
options
Confidence Intervals
Commands
. When a data set is available

Normal distribution

• ci means #variable, level(#confidence level)

Binomial distribution

. ci proportions #variable, exact level(#conf level)

Poisson distribution

. ci means #variable, poisson level(#conf level)


Normal Confidence Interval
• Stata Immediate command for variable distributed as
normal
• cii means #obs #mean #sd, level(#confidence level)

cii means 10 116.90 2.70, level(95)

Variable | Obs Mean Std. Err. [95% Conf. Interval]


-------------+---------------------------------------------------------------
| 10 116.9 .853815 114.9685 118.8315
Binomial Confidence Interval
Calculator
• No. observations
(sample size)
• No. successes
• % Confidence Interval:
90%, 95%, 99%

Stata Immediate command for variable distributed as binomial

cii proportions #obs #succ, exact level(#confidence level)


Binomial Confidence Interval
Example

. cii proportion 10 6, exact level(95)

-- Binomial Exact --
Variable | Obs Mean Std. Err. [95% Conf. Interval]
-------------+---------------------------------------------------------------
| 10 .6 .1549193 .2623781 .8784477
Example - binomial

• cii #obs #succ, binomial


In Stata:

cii proportion #obs #succ, exact level(#conf. level)

cii proportion 10000 400, exact level(95)

-- Binomial Exact --
Variable | Obs Proportion Std. Err. [95% Conf. Interval]
-------------+---------------------------------------------------------------
| 10,000 .04 .0019596 .0362438 .044027
Poisson Confidence Interval
Calculator
• Number of exposure
• Number events or
counts
• % Confidence
Interval: 90%, 95%,
99%

Immediate command for variable distributed as Poisson

cii means #exposure #events , poisson level(#confidence level)


Poisson Confidence Interval
Example

cii means 12000 12, poisson level(95)

-- Poisson Exact --
Variable | Exposure Mean Std. Err. [95% Conf. Interval]
-------------+---------------------------------------------------------------
| 12000 .001 .0002887 .0005167 .0017468
Confidence Intervals functions
• In addition to using commands in to calculate our confidence
intervals, we can also type in the formulas manually assuming
we know the point estimate of interest, std deviation of the
point estimate, and whether we are using the t or z
distribution.
• With known population variance
𝑋ത ± 𝑍1−𝛼/2 𝜎/ 𝑛

• Without knowing population variance


Using Stata to calculate CI
•Population normal distributed with σ (sd) known
•Problem: sample mean is 24.3, population standard deviation
is 6, sample size is 16 and X has a Normal distribution. Find the
90% CI for the population mean (E(X)=µ).
•Solution: Recall the formula:
• 𝑋ത ± 𝑍1−𝛼/2 𝜎/ 𝑛
•It means to calculate the CI at 1 − 𝛼 confidence level, we need to
determine the value of 𝑍1−𝛼/2 = invnormal(1 − 𝛼/2) and
substitute in the above formula. Therefore, to find the 90%CI we
𝛼 0.1
have 𝛼 = 0.10 and 1 − = 1 − = 0.95. We substitute the
2 2
values and we have
•For the upper limit we type in Stata:
• display 24.3 + invnormal(0.95)*6/sqrt(16)
•For the lower limit we type in Stata
• display 24.3 - invnormal(0.95)*6/sqrt(16)
Questions?

You might also like