How to downweigh outlier in a sum?

I have a simple problem. Assume following dataset:

resids <- c(, 9, 8, 7, 12, 14, 8, 9, 15, 4, 9, 10, 200)
n <- length(resids)
p <- 2


Using this dataset I want to estimate:

Phi.P <- sum(resids^2)/(n-p)


We see that the variable 'resids' contain an outlier with value 200. This outlier will cause the estimate of Phi.P to be too high. So I used a robust estimator based on the median:

Phi.P <- median(resids^2)*(n/(n-p))


This works OK but not extremely good. Therefore I am looking for a way do downweight the outlier in sum(resids^2). Does anybody know how to do this?

• why are you dividing by n-p? are those resid, residuals of some regression function? In that case you might consider a robust regression. See rlm from the MASS package of R. Commented May 22, 2015 at 6:07
• A simple way to lower the influence of outliers in a mean is to eliminate values above/below certain percentiles. The trimmed mean is an example of this. Commented Nov 8, 2019 at 14:53
• Can't really answer this question without knowing why 200 is considered an "outlier". The code fragment suggests it is a residual, in which case it may not be an outlier but an indicator of a serious problem with a model, in which case you probably don't want to downweight it. Especially as it is apparently a squared residual. Commented Aug 10, 2023 at 14:39

One problem with the median is that it's not a smooth function, which can make optimizing things difficult. Several alternative robust estimates are available Tukey's bisquare and the huber loss function are both popular. You may also want to consider a trimmed mean.

From the syntax in your post it looks like you are using R. In R, the trimmed mean is implemented as mean(trim=). The other two I mentioned are implemented in the robust package in robust::psi.weight.

• Thank you for the response. The purpose is to downweight the outlier already in the sum., so weights should be used in the sum. Can this be obtained using psi.weight? Commented May 22, 2015 at 17:15
• robust::psi.weight(x) returns the weighted vector. The weights would be given by robust::psi.weight(x)/x Commented May 22, 2015 at 19:17
• And how can these weigst be used in the sum? Since the sum command does not have weight option. Commented May 23, 2015 at 8:24
• sum( yourvector * yourweights ) / sum(yourweights) Commented Feb 21, 2019 at 8:40

I don't understand exactly what do you want estimate. If you want approximative value of the variable 'resids' or value of sum(resids^2)/(n-p) and I don't know why you use this variable 'p'.

But if you want estimate the approximative value of 'resids', you can use the geometric mean, it works well even if your numerical vector ('resids') contains an outlier as we can see in this example below:

# Geometric mean function
fGeometric <- function(x) {
n <- length(x)
Moy <- prod(x)^(1/n)
return(Moy)
}

resids <- c(1,4,3,2,5,3,1,6, 4, 990)

fGeometric(resids)

[1] 4.934179

**********************************************************
# An other values of the numerical vector for example:

resids<-c(16,9,8,7,12,14,8,9,15,4,9,10,1257)

fGeometric(resids)

[1] 13.8061