Trimmed Mean in R

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 x such that:

LQ - 1.5 \times IQR < x < UQ + 1.5 \times IQR

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)
data_trimmed Outlier_LQ]

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

> mean(JohnsonJohnson)
[1] 4.799762
> trimmed_means(JohnsonJohnson)
[1] 4.523902

The trimmed mean above computing the mean on all observations -3.44<x<2.82


Trimmed Mean in R

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s