2016-01-19 143 views
1

我正在从Michael Faraway的线性模型(其中R(第11章,第160页))开始PCA部分的工作。使用QQ Plot结果删除现有的新异常值

PCA分析对异常值敏感,Mahalanobis距离有助于我们识别它们。

作者通过绘制马哈拉诺比斯距离和卡方分布的分位数来检查异常值。

if require(faraway)==F install.packages("faraway"); require(faraway) 
data(fat, package='faraway') 
cfat <- fat[,9:18] 

n <- nrow(cfat); p <- ncol(cfat) 
plot(qchisq(1:n/(n+1),p), sort(md), xlab=expression(paste(chi^2, 
                  "quantiles")), 
ylab = "Sorted Mahalanobis distances") 
abline(0,1) 

我找准穴位:

identify(qchisq(1:n/(n+1),p), sort(md)) 

看来,异常值是行242:252。我删除这些离群值,重新创建QQ图:

cfat.mod <- cfat[-c(242:252),] #remove outliers 
robfat <- cov.rob(cfat.mod) 
md <- mahalanobis(cfat.mod, center=robfat$center, cov=robfat$cov) 
n <- nrow(cfat.mod); p <- ncol(cfat.mod) 
plot(qchisq(1:n/(n+1),p), sort(md), xlab=expression(paste(chi^2, 
                  "quantiles")), 
    ylab = "Sorted Mahalanobis distances") 
abline(0,1) 

identify(qchisq(1:n/(n+1),p), sort(md)) 

唉,现在看来,一个新的点的集合(行234:241),是现在异常。每次我删除更多的异常值时,都会发生这种情况。

期待理解我做错了什么。

+0

我们需要'md'解决。 –

+1

你可能没有做错任何事情。这是错误指定模型的常见问题。 –

+0

同意@Alex,您将根据排除旧异常值的“新”数据集重新计算异常值。 –

回答

2

要正确识别点,请确保标签对应于数据中点的位置。函数ordersortindex.return=TRUE将给出排序后的索引。以下是一个例子,任意删除大于阈值的点数为md

## Your data 
data(fat, package='faraway') 
cfat <- fat[, 9:18] 
n <- nrow(cfat) 
p <- ncol(cfat) 
md <- sort(mahalanobis(cfat, colMeans(cfat), cov(cfat)), index.return=TRUE) 
xs <- qchisq(1:n/(n+1), p) 
plot(xs, md$x, xlab=expression(paste(chi^2, 'quantiles'))) 

## Use indices in data as labels for interactive identify 
identify(xs, md$x, labels=md$ix) 

## remove those with md>25, for example 
inds <- md$x > 25 
cfat.mod <- cfat[-md$ix[inds], ] 
nn <- nrow(cfat.mod) 
md1 <- mahalanobis(cfat.mod, colMeans(cfat.mod), cov(cfat.mod)) 

## Plot the new data 
par(mfrow=c(1, 2)) 
plot(qchisq(1:nn/(nn+1), p), sort(md1), xlab='chisq quantiles', ylab='') 
abline(0, 1, col='red') 
car::qqPlot(md1, distribution='chisq', df=p, line='robust', main='With car::qqPlot') 

​​