[R] remove outlier

David Barron dnbarron at gmail.com
Fri Jan 2 18:01:36 CET 2015


Your problem is that you are subsetting the dataset on line 13 of the
function, but then on the following line you attempt to add predicted
values from the complete data used to fit the model. You'll have to subset
that as well (and the same would be true for the following line where you
attempt to add standardised residuals)

David

On 2 January 2015 at 12:58, Methekar, Pushpa (GE Transportation, Non-GE) <
pushpa.methekar at ge.com> wrote:

> Hi ,
> I am working on a function .
>
> rm.outliers = function(dataset,model){
>      dataset$predicted = predict(model)
>     dataset$stdres = rstudent(model)
>      m = 1
>      for(i in 1:length(dataset$stdres)){
>        dataset$outlier_counter[i] = if(dataset$stdres[i] >= 3 |
>                                              dataset$stdres[i] <= -3) {m}
>                                      else{0}
>       }
>    j = length(which(dataset$outlier_counter >= 1))
>     while(j>=1){
>        print(dataset[which(dataset$outlier_counter >= 1),])
>         dataset = dataset[which(dataset$outlier_counter == 0),]
>         dataset$predicted = predict(model)
>       dataset$stdres = rstudent(model)
>           m = m+1
>           for(k in 1:length(dataset$stdres)){
>               dataset$outlier_counter[k] = if(dataset$stdres[k] >= 3 |
>                                                     dataset$stdres[k] <=
> -3) {m} else{0}
>            }
>        j = length(which(dataset$outlier_counter >= 1))
>      }
>    return(dataset)
>   }
> When I pass
> rm.outliers(xsys,fitted.modely1.temp.l)
> fitted.modely1 .temp.l  is mylinear model.
> It shows me error like
>
> Error in `$<-.data.frame`(`*tmp*`, "predicted", value =
> c(0.306726561735386,  :
>
>   replacement has 731 rows, data has 717
>
> Called from: `$<-`(`*tmp*`, "predicted", value = c(0.306726561735386,
> 0.306726561)
>
>
>
> Help me out
>
>
> Xsys data has 331 rows and 18 column
>
>         [[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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list