Skip to content

Instantly share code, notes, and snippets.

@smithdanielle
Last active February 27, 2017 12:20
Show Gist options
  • Save smithdanielle/7c45fe0fcf29b43fa1f0 to your computer and use it in GitHub Desktop.
Save smithdanielle/7c45fe0fcf29b43fa1f0 to your computer and use it in GitHub Desktop.
Outlier removal in R with MAD criterion: equations taken from [Leys et al (2013)](http://www.sciencedirect.com/science/article/pii/S0022103113000668).
require(plyr)
require(dplyr)
data.outliersRemoved<-ddply(data, .(withinIV1, withinIV2), function(d){
limits.outliers = median(d$DV) + 2.5*c(-1, 1)*mad(d$DV))
d$DV[which(((d$DV - limits.outliers[1])*(limits.outliers[2] - d$DV)) <= 0)]<-NA
return(d)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment