Title

myinterval — Confidence intervals for the mean (t-distribution)

Syntax

myinterval varlist [if] [in] [, level(#)]

Description

myinterval computes confidence intervals for the population mean of
each variable in varlist, using the Student's t distribution.
This is appropriate when the population variance is unknown and the sample
size is small, or when exact t-based inference is desired.

The confidence interval for a variable X is given by

X-bar +/- t(df, 1 - a/2) * SE

where X-bar is the sample mean, SE = s / sqrt(n)
is the standard error of the mean, and t(df, 1 - a/2)
is the critical value from the t distribution with df =
n - 1 degrees of freedom. a = 1 - level/100 is the
significance level.

Options

level(#) specifies the confidence level, as a percentage,
for the confidence intervals. The default is level(95), which
produces 95% confidence intervals. # must be strictly between 0
and 100.

Remarks

myinterval uses the t distribution rather than the normal
distribution. For large samples (n > 120), the t-based intervals
are virtually identical to normal-based intervals computed by Stata's
official ci command.

All observations with complete data on all variables in varlist
satisfying the optional if and in conditions are used.
At least two observations are required to compute the interval.

Comparison with official ci and innovations

myinterval differs from Stata's official ci command in several
important respects. These differences constitute the key innovations of
this program.

1. Default distribution: t versus normal.

Stata's ci means defaults to the normal distribution, producing
intervals of the form X-bar +/- z(1 - a/2) * SE.
The t-based interval is only available via the ttest option
or the undocumented level() suboption. In contrast,
myinterval always uses the t distribution. This is
statistically more conservative and exact for samples drawn from a
normal population with unknown variance. When the sample size is small
(n < 30), the normal approximation can substantially understate
the true confidence level; myinterval avoids this problem by
construction.

2. Multi-variable processing.

Stata's ci processes one variable at a time. If the user wishes
to obtain confidence intervals for five variables, ci must be
called five times. myinterval accepts a varlist and computes
intervals for all specified variables in a single call, displaying them
together in one table. This is more convenient for exploratory data
analysis and for reporting summary statistics across multiple measures.

3. Simultaneous return of results.

Because ci handles only one variable, only the last call's results
are accessible via return list. myinterval stores results
for all variables simultaneously using the
r(stat_varname) naming convention. This
enables programmatic post-processing — for example, constructing a custom
table of intervals across multiple variables, or passing results to a
graphing routine, all from a single invocation.

4. Simple and transparent.

myinterval is a single-purpose tool with readable source code.
The entire calculation is visible in a few lines, making it easy for
students and researchers to inspect, modify, and learn from. The
formula is printed in the Description section above, and every
intermediate quantity (mean, SE, df, critical value, bounds) is
displayed or stored.

5. Pedagogical value.

The program is designed with teaching in mind. By showing the degrees
of freedom and the standard error alongside the interval, and by
displaying the exact t-based formula in the help file,
myinterval helps students connect the abstract formula from the
textbook to the concrete numerical output. This transparency is absent
from the official ci command, which reports only the interval
endpoints and the sample mean.

Saved results

myinterval stores the following in r():

Scalars

r(N_varname) sample size for each variable

r(mean_varname) sample mean for each variable

r(se_varname) standard error of the mean for each variable

r(df_varname) degrees of freedom for each variable

r(lb_varname) lower bound of the confidence interval

r(ub_varname) upper bound of the confidence interval

r(level) confidence level used

r(vars) number of variables processed

Examples

. sysuse auto

. myinterval mpg

. myinterval mpg price, level(99)

. myinterval mpg price weight if foreign==1

. myinterval mpg price in 1/30

Authors

Wu Lianghai

School of Business, Anhui University of Technology (AHUT),

Ma'anshan, China

Email: agd2010@yeah.net

Wu Hanyan

School of Economics and Management,

Nanjing University of Aeronautics and Astronautics (NUAA), China

Email: 2325476320@qq.com

Chen Liwen

School of Business, Anhui University of Technology (AHUT),

Ma'anshan, China

Email: 2184844526@qq.com

Version history

2.0 19jun2026 Rewritten: corrected t-critical value,

added r-class returns, improved output, new help file.

1.0 16nov2015 Initial version by Wu Lianghai.

Also see

Help: ci, ttest, mean, ameans