簡體   English   中英

相關系數的自舉p值(重采樣方法)

[英]Bootstrap p-value for correlation coefficient (resampling methods)

我有這么大的數據集(N = 300.000),通過功效分析,得出的結論是,如果存在相關性,我僅需要250次觀察即可找到相關性。

因此,我想使用自舉選擇1000個大小為n = 250的樣本,以查找這1000個樣本中p值的范圍。 我對Bootstrap方法並不熟悉,但是在這里我舉了一個示例,介紹了我對boot軟件包的了解。 我用虹膜數據集進行了說明。

我想要的輸出是一個直方圖,顯示了1000個獲得的p值的頻率分布以及可能的p值的95%置信區間。

誰能幫忙我的腳本?

#activate iris datset
library(boot)
library(datasets)

#create function to retrieve p-value
boot.fn <- function(data, sample) {
           x <- iris$Petal.Length[i]
           y <- iris$Sepal.Length[i]
           boot.p <- cor.test(iris$Petal.Length[i],
                              iris$Sepal.Length[i])$p.value
           }

#create 1000 samples with bootstrap function
bootstr <- boot(iris, boot.fn, 1000)

功能boot將無法提供所需的行為。 但是,自己實現它非常簡單:

首先一些數據:

x1 <- rnorm(1e5)
y1 <- x1 + rnorm(1e5, 0.5)

cor.test(x1, y1)
#output
    Pearson's product-moment correlation

data:  x1 and y1
t = 315.97, df = 99998, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.7037121 0.7099151
sample estimates:
      cor 
0.7068272 

采樣250個索引1000次:

#set.seed(1)
z1 <- replicate(1000, sample(1:length(x1), 250, replace = T))

如果不需要更換,只需移除該零件

現在遍歷各列,使用索引對x1y1進行子集計算,統計並使用未列出的列表繪制直方圖。

hist(unlist(apply(z1, 2, function(x){
  cor.test(x1[x], y1[x])$p.value
})), xlab = "p value", main = "Uh)

在此處輸入圖片說明

也許更有用的是:

hist(unlist(apply(z1, 2, function(x){
  cor.test(x1[x], y1[x])$estimate
})), xlab = "cor", main ="Uh")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM