简体   繁体   English

PCA和Hotelling的T ^ 2用于R中的置信度

[英]PCA and Hotelling's T^2 for confidence intervall in R

I made a principal component analysis and took the 2 first principal components. 我进行了主成分分析并获得了2个第一主成分。 I made a chart of my points based on the score of the 2 PC. 我根据2台电脑的得分制作了我的积分图表。 I would like to add on this graph a 95% confidence region corresponding to the Hotelling's T^2 test in order to detect the points that are out of the ellipse (outliers) How is it possible in R? 我想在此图上添加一个对应于Hotelling T ^ 2测试的95%置信区域,以便检测椭圆外的点(异常值)R中的可能性如何? Do you have any example? 你有什么例子吗?

I would do something like this and detect the points out of the ellipse: 我会做这样的事情并检测椭圆中的点:

在此输入图像描述

We can plot the confidence ellipse for PCA with vegan or ggbiplot as below: 我们可以用veganggbiplot绘制PCA的置信椭圆,如下所示:

set.seed(1)
data <- matrix(rnorm(500), ncol=5) # some random data
data <- setNames(as.data.frame(rbind(data, matrix(runif(25, 5, 10), ncol=5))), LETTERS[1:5]) # add some outliers
class <- sample(c(0,3,6,8), 105, replace=TRUE) # 4 groups

library(vegan)
PC <- rda(data, scale=TRUE)
pca_scores <- scores(PC, choices=c(1,2))
plot(pca_scores$sites[,1], pca_scores$sites[,2],
     pch=class, col=class, xlim=c(-2,2), ylim=c(-2,2))
arrows(0,0,pca_scores$species[,1],pca_scores$species[,2],lwd=1,length=0.2)
ordiellipse(PC,class,conf=0.95)

在此输入图像描述

library(ggbiplot)
PC <- prcomp(data, scale = TRUE)
ggbiplot(PC, obs.scale = 1, var.scale = 1, groups = as.factor(class), ellipse = TRUE, 
                                                    ellipse.prob = 0.95)

在此输入图像描述

The pcaMethods package has a function simpleEllipse(x, y, alpha, len) that will do this. pcaMethods包有一个函数 simpleEllipse(x, y, alpha, len)来执行此操作。 Given two uncorrelated data vectors it will return an ellipse, where the axes are scaled based on the variance of each score, and the F statistic. 给定两个不相关的数据向量,它将返回一个椭圆,其中轴根据每个得分的方差和F统计量进行缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM