R中主成分的加载置信区间

时间:2015-06-25 08:23:25

标签: r pca confidence-interval

我使用以下代码对R中使用prcomp函数的前4列虹膜数据集进行主成分分析:

> prcomp(iris[1:4])
Standard deviations:
[1] 2.0562689 0.4926162 0.2796596 0.1543862

Rotation:
                     PC1         PC2         PC3        PC4
Sepal.Length  0.36138659 -0.65658877  0.58202985  0.3154872
Sepal.Width  -0.08452251 -0.73016143 -0.59791083 -0.3197231
Petal.Length  0.85667061  0.17337266 -0.07623608 -0.4798390
Petal.Width   0.35828920  0.07548102 -0.54583143  0.7536574

如何在R中获得这些值的置信区间?有没有可以做到的套餐?谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

你可以在这上面使用bootstrapping。只需使用bootstrapping包重新采样数据,并记录每次计算的主要组件。使用得到的经验分布来获得置信区间。

boot包让这很容易。

这是一个计算第一个PCA组件相对于Sepal的置信区间为95%的示例.Length:

library(boot)

getPrcStat <- function (samdf,vname,pcnum){
  prcs <- prcomp(samdf[1:4]) # returns matrix
  return(prcs$rotation[ vname,pcnum ])   # pick out the thing we need
}

bootEst <- function(df,d){
   sampledDf <- df[ d, ]  # resample dataframe 
   return(getPrcStat(sampledDf,"Sepal.Length",1))
}

bootOut <- boot(iris,bootEst,R=10000)
boot.ci(bootOut,type=c("basic"))

输出结果为:

  BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
  Based on 10000 bootstrap replicates

  CALL : 
  boot.ci(boot.out = bootOut, type = c("basic"))

  Intervals : 
  Level      Basic         
  95%   ( 0.3364,  1.1086 )  
  Calculations and Intervals on Original Scale

因此,使用通常的基本引导方法,我们得到95%置信区间在0.3364和1.1086之间。还有许多其他更先进的统计方法也可以使用,但你需要知道自己在做什么。