Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Forked from smithdanielle/outlierRemoval.R
Created February 27, 2017 12:20
Show Gist options
  • Save juanchiem/06e50233635f126b23e51cd3e71d2179 to your computer and use it in GitHub Desktop.
Save juanchiem/06e50233635f126b23e51cd3e71d2179 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