Methekar, Pushpa (GE Transportation, Non-GE)
2015-Jan-05 12:20 UTC
[R] find and remove outlier
Hi, this is my function to find rstudent of model which will give me outlier But I wonder if I could find out the exact no. of outlier in data set. Like outlierTest() does from car package . rm.outliers = function(dataset,model){ dataset$rstudent = rstudent(model) for(i in 1:length(dataset$rstudent)){ if(dataset$rstudent[i]>=-3 & dataset$rstudent[i]<=3) {print(dataset$rstudent[i])} else { print("this is an outlier") library(car) print(outlierTest(model,n.max=1, order=TRUE) ) } i<-i+1 } } [[alternative HTML version deleted]]
Dear Pushpa Methekar, You can use which.max(), as in> library(car) > mod <- lm(prestige~ income + education, data=Duncan) > which.max(abs(rstudent(mod)))minister 6 In fact, this is exactly what outlierTest() does, as you can see by looking at car:::outlierTest.lm Beyond that, I'd encourage you to look more carefully at potential outliers, including at the distribution of the studentized residuals, rather than adopting a simple mechanical rule for removing outliers. I hope this helps, John ------------------------------------------------ John Fox, Professor McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/ On Mon, 5 Jan 2015 12:20:15 +0000 "Methekar, Pushpa (GE Transportation, Non-GE)" <pushpa.methekar at ge.com> wrote:> > > Hi, this is my function to find rstudent of model which will give me outlier > But I wonder if I could find out the exact no. of outlier in data set. > Like outlierTest() does from car package . > > > rm.outliers = function(dataset,model){ > dataset$rstudent = rstudent(model) > for(i in 1:length(dataset$rstudent)){ > if(dataset$rstudent[i]>=-3 & dataset$rstudent[i]<=3) > {print(dataset$rstudent[i])} > else { print("this is an outlier") > library(car) > print(outlierTest(model,n.max=1, order=TRUE) ) } > i<-i+1 > } > } > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.