# Update Data Frame to Remove Outliers

# load the libraries
library(mlbench)
# load the dataset
data(PimaIndiansDiabetes)
datos <- PimaIndiansDiabetes
# calculate stats for pregnant (number of times pregnant)
pregnant.mean <- mean(datos$pregnant)
pregnant.sd <- sd(datos$pregnant)
# max reasonable value is within 99.7% of the data (if Gaussian)
pregnant.max <- pregnant.mean + (3*pregnant.sd)
pregnant.min <- pregnant.mean - 3*pregnant.sd
# mark outlier pregnant values as N/A
datos.sinOutliers <- datos
datos.sinOutliers$pregnant[datos$pregnant>pregnant.max] <- NA
datos.sinOutliers$pregnant[datos$pregnant<pregnant.min] <- NA



# otro criterio interesante:
# valores por debajo de q1-1.5IQR son outliers
# valores por encima de q3+1.5IQR son outliers

pregnant.quantile <- quantile(datos$pregnant)
pregnant.IQR <- IQR(datos$pregnant)

pregnant.max <- pregnant.quantile[4] + 1.5*pregnant.IQR
pregnant.min <- pregnant.quantile[1] - 1.5*pregnant.IQR

# mark outlier pregnant values as N/A
datos.sinOutliers <- datos
datos.sinOutliers$pregnant[datos$pregnant>pregnant.max] <- NA
datos.sinOutliers$pregnant[datos$pregnant<pregnant.min] <- NA