Graphing a Normal Distribution
For a normal distribution, approximately 68% of observations fall within one standard deviation of the mean and 95% within two. How closely do the 74 car lengths in Stata’s auto dataset follow that pattern?
Fit the Normal Distribution
Use the sample mean and standard deviation of length to set the centre and spread of the fitted curve:
sysuse auto, clear
summarize length
Variable | Obs Mean Std. dev. Min Max
-------------+---------------------------------------------------------
length | 74 187.9324 22.26634 142 233
scalar mean_length = r(mean)
scalar sd_length = r(sd)
Graph the Fitted Curve
Use a density-scaled histogram to compare the observed car lengths with a normal curve fitted from the sample mean and standard deviation. The green band covers the central 68% of the fitted distribution. The green and blue regions together cover approximately 95%:
preserve
local centre = mean_length
local lo1 = mean_length - sd_length
local hi1 = mean_length + sd_length
local lo2 = mean_length - 2*sd_length
local hi2 = mean_length + 2*sd_length
local mean_label = strtrim(string(mean_length, "%6.1f"))
quietly set obs 401
generate double z = (_n - 201) / 50
generate double x = mean_length + z * sd_length
generate double curve = normalden(x, mean_length, sd_length)
generate byte baseline = 0
twoway ///
(histogram length if !missing(length), density width(5) ///
fcolor(gs14) lcolor(gs10)) ///
(rarea baseline curve x if inrange(z, -2, 2), sort ///
color(eltblue) fintensity(100) lwidth(none)) ///
(rarea baseline curve x if inrange(z, -1, 1), sort ///
color(eltgreen) fintensity(100) lwidth(none)) ///
(histogram length if !missing(length), density width(5) ///
fcolor(none) lcolor(gs9) lwidth(vthin)) ///
(line curve x, sort lcolor(navy) lwidth(medthick)), ///
xlabel(`lo2' "-2 SD" `lo1' "-1 SD" `centre' "`mean_label' in" ///
`hi1' "+1 SD" `hi2' "+2 SD") ///
xtitle("Car length") ytitle("Density") ///
title("Distribution of car lengths") ///
legend(order(1 "Observed lengths" 5 "Fitted normal curve" ///
3 "Within 1 SD (68%)" 2 "Within 2 SD (95%)") ///
cols(2) position(6))
restore

Compare the Observed Percentages
Count the cars within one and two sample standard deviations of the mean length, then divide each count by the number of cars with a recorded length to find the dataset’s actual percentage within each number of standard deviations:
quietly count if !missing(length)
local total = r(N)
quietly count if inrange(length, mean_length - sd_length, mean_length + sd_length)
local within1 = r(N)
quietly count if inrange(length, mean_length - 2*sd_length, mean_length + 2*sd_length)
local within2 = r(N)
display "Within 1 SD: `within1' / `total' x 100 = " ///
%4.1f (100 * `within1' / `total') "%"
Within 1 SD: 46 / 74 x 100 = 62.2%
display "Within 2 SD: `within2' / `total' x 100 = " ///
%4.1f (100 * `within2' / `total') "%"
Within 2 SD: 72 / 74 x 100 = 97.3%
Compared with the fitted normal curve, a smaller share of the cars falls within one standard deviation (62.2% versus 68%), while a larger share falls within two (97.3% versus 95%).