I found myself having to compute the trimmed mean today in R, and couldn’t immediately find a function that would accomplish this end. What do I mean (ha!) by the trimmed mean?
The trimmed mean removes outliers. We define outliers as any value such that:
Here’s our function:
#function to compute the trimmed mean
Outlier_UQ = quantile(data_to_trim, .75)+1.5*IQR(data_to_trim)
Outlier_LQ = quantile(data_to_trim, .25)-1.5*IQR(data_to_trim)
Simple as that. You can use this in something like ddply to compare with the mean.
Using the JohnsonJohnson dataset on quarterly earnings (dollars) per Johnson & Johnson share 1960–80, we get
The trimmed mean above computing the mean on all observations