Brian Borchersの回答は非常に優れています。奇妙な外れ値を含むデータは、多くの場合、OLSで十分に分析されていません。写真、モンテカルロ、およびいくつかのR
コードを追加して、これをさらに拡張します。
非常に単純な回帰モデルを考えてみましょう:
Yi ϵi=β1xi+ϵi=⎧⎩⎨⎪⎪N(0,0.04)31−31w.p.w.p.w.p.0.9990.00050.0005
このモデルは、勾配係数1の設定に準拠しています。
添付のプロットは、このモデルの100個の観測からなるデータセットを示し、x変数は0から1まで実行されます。プロットされたデータセットでは、異常値(この場合は+31)になるエラーが1回描画されます。 。また、青のOLS回帰線と赤の最小絶対偏差回帰線もプロットされています。LLSではなくOLSが外れ値によってどのように歪められるかに注意してください。

モンテカルロを行うことでこれを確認できます。モンテカルロで、同じとして、上記の分布で10,000回、100観測のデータセットを生成します。これらの10,000の複製では、大多数で異常値は発生しません。しかし、いくつかは異常値を取得し、それは毎回LLSを台無しにしますが、LADを台無しにします。以下のコードはモンテカルロを実行します。勾配係数の結果は次のとおりです。ϵxϵR
Mean Std Dev Minimum Maximum
Slope by OLS 1.00 0.34 -1.76 3.89
Slope by LAD 1.00 0.09 0.66 1.36
OLSとLADはどちらも不偏推定量を生成します(勾配は両方とも、10,000の複製に対して平均1.00です)。OLSでは、標準偏差が0.34対0.09のはるかに高い推定量が生成されます。したがって、ここでは、OLSは不偏推定量の中で最も効率的ではありません。もちろん、まだ青ですが、LADは線形ではないため、矛盾はありません。OLSがMin列とMax列で発生する可能性のあるワイルドエラーに注意してください。それほどLADではありません。
以下は、グラフとモンテカルロの両方のRコードです。
# This program written in response to a Cross Validated question
# http://stats.stackexchange.com/questions/82864/when-would-least-squares-be-a-bad-idea
# The program runs a monte carlo to demonstrate that, in the presence of outliers,
# OLS may be a poor estimation method, even though it is BLUE.
library(quantreg)
library(plyr)
# Make a single 100 obs linear regression dataset with unusual error distribution
# Naturally, I played around with the seed to get a dataset which has one outlier
# data point.
set.seed(34543)
# First generate the unusual error term, a mixture of three components
e <- sqrt(0.04)*rnorm(100)
mixture <- runif(100)
e[mixture>0.9995] <- 31
e[mixture<0.0005] <- -31
summary(mixture)
summary(e)
# Regression model with beta=1
x <- 1:100 / 100
y <- x + e
# ols regression run on this dataset
reg1 <- lm(y~x)
summary(reg1)
# least absolute deviations run on this dataset
reg2 <- rq(y~x)
summary(reg2)
# plot, noticing how much the outlier effects ols and how little
# it effects lad
plot(y~x)
abline(reg1,col="blue",lwd=2)
abline(reg2,col="red",lwd=2)
# Let's do a little Monte Carlo, evaluating the estimator of the slope.
# 10,000 replications, each of a dataset with 100 observations
# To do this, I make a y vector and an x vector each one 1,000,000
# observations tall. The replications are groups of 100 in the data frame,
# so replication 1 is elements 1,2,...,100 in the data frame and replication
# 2 is 101,102,...,200. Etc.
set.seed(2345432)
e <- sqrt(0.04)*rnorm(1000000)
mixture <- runif(1000000)
e[mixture>0.9995] <- 31
e[mixture<0.0005] <- -31
var(e)
sum(e > 30)
sum(e < -30)
rm(mixture)
x <- rep(1:100 / 100, times=10000)
y <- x + e
replication <- trunc(0:999999 / 100) + 1
mc.df <- data.frame(y,x,replication)
ols.slopes <- ddply(mc.df,.(replication),
function(df) coef(lm(y~x,data=df))[2])
names(ols.slopes)[2] <- "estimate"
lad.slopes <- ddply(mc.df,.(replication),
function(df) coef(rq(y~x,data=df))[2])
names(lad.slopes)[2] <- "estimate"
summary(ols.slopes)
sd(ols.slopes$estimate)
summary(lad.slopes)
sd(lad.slopes$estimate)