Comparing the Mean and Median When One Value Is Extreme
In the previous TechTip (Calculating and Graphing the Mean), we calculated the mean price of four cars. This time, we add a fifth car and compare the mean with the median, the middle price when the cars are ordered from least to most expensive.
Load the Car Prices
Load Stata’s built-in auto dataset, keeping the four models from the previous example and adding a fifth, the Cadillac Seville:
sysuse auto, clear
keep if inlist(make, "AMC Concord", "AMC Pacer", "AMC Spirit", ///
"Buick Century", "Cad. Seville")
sort price
Find the Median for Four Cars
Adding detail to summarize gives the median for the original four cars:
summarize price if make != "Cad. Seville", detail
Price
Percentiles Smallest
1% 3799 3799
5% 3799 4099
10% 3799 4749 Obs 4
25% 3949 4816 Sum of wgt. 4
50% 4424 Mean 4365.75
Largest Std. dev. 497.315
75% 4782.5 3799
90% 4816 4099 Variance 247322.3
95% 4816 4749 Skewness -.1672793
99% 4816 4816 Kurtosis 1.241801
With four cars, the middle falls between the second and third prices. These belong to the AMC Concord ($4,099) and the AMC Pacer ($4,749). Their average is $4,424. The median is close to the mean of $4,365.75.
Compare the Mean and Median for Five Cars
Run the same command for all five cars:
summarize price, detail
Price
Percentiles Smallest
1% 3799 3799
5% 3799 4099
10% 3799 4749 Obs 5
25% 4099 4816 Sum of wgt. 5
50% 4749 Mean 6673.8
Largest Std. dev. 5178.896
75% 4816 4099
90% 15906 4749 Variance 2.68e+07
95% 15906 4816 Skewness 1.474021
99% 15906 15906 Kurtosis 3.215793
The Cadillac costs $15,906, bringing the mean up to $6,673.80. The other four cars all cost $4,816 or less.
Adding the Cadillac moves the middle position to the third car, the AMC Pacer, at $4,749.
Graph the Prices and Mark the Mean and Median
After summarize, detail, Stata stores the mean in r(mean) and the median in r(p50). Use both values for the reference lines and axis labels:
sort price
generate byte position = _n
format price %9.0fc
quietly summarize price, detail
local mean_price = r(mean)
local median_price = r(p50)
local mean_label = strtrim(string(`mean_price', "%12.2fc"))
local median_label = strtrim(string(`median_price', "%12.0fc"))
twoway ///
(bar price position, barwidth(0.65) color(eltblue)) ///
(function y = mean_price', range(0.5 5.5) ///
lcolor(red) lpattern(dash) lwidth(medthick)) ///
(function y = median_price', range(0.5 5.5) ///
lcolor(blue) lpattern(solid) lwidth(medthick)) ///
(scatter price position, msymbol(none) mlabel(price) ///
mlabformat(%9.0fc) mlabposition(12) mlabcolor(black)), ///
xlabel(1 "AMC Spirit" 2 "AMC Concord" 3 "AMC Pacer" ///
4 "Buick Century" 5 "Cad. Seville", noticks) ///
ylabel(0(4000)16000 mean_price' "mean_label'" ///
median_price' "median_label'", format(%9.0fc) angle(horizontal)) ///
yscale(range(0 17500)) ///
xtitle("") ytitle("Price (US dollars)") ///
title("Mean and median prices of five cars") ///
legend(order(2 "Mean" 3 "Median") rows(1) position(6))

If the Cadillac’s price doubled, the mean would rise again. The median would stay at $4,749 because the AMC Pacer would still be the middle car.