簡體   English   中英

如何從R的cor()相關分析中計算P值和標准誤差

[英]How to compute P-value and standard error from correlation analysis of R's cor()

我的數據包含每個條件(x和y)的54個樣本。 我通過以下方式計算了相關性:

> dat <- read.table("http://dpaste.com/1064360/plain/",header=TRUE)
> cor(dat$x,dat$y)
[1] 0.2870823

是否存在一種本地方法來生成上述R的cor()函數中的相關性SE和來自T檢驗的p值?

如本網站所述 (第14.6頁)

我認為您正在尋找的只是cor.test()函數,除了相關的標准誤差之外,它將返回您要查找的所有內容。 但是,正如您所看到的,該公式非常簡單,如果使用cor.test ,則需要計算所需的所有輸入。

使用示例中的數據(因此您可以將自己與第14.6頁的結果進行比較):

> cor.test(mydf$X, mydf$Y)

    Pearson's product-moment correlation

data:  mydf$X and mydf$Y
t = -5.0867, df = 10, p-value = 0.0004731
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.9568189 -0.5371871
sample estimates:
       cor 
-0.8492663 

如果您願意,還可以創建如下所示的函數以包含相關系數的標准誤差。

為方便起見,這里是等式:

在此輸入圖像描述

r =相關估計和n - 2 =自由度,這兩者在上面的輸出中都很容易獲得。 因此,一個簡單的功能可能是:

cor.test.plus <- function(x) {
  list(x, 
       Standard.Error = unname(sqrt((1 - x$estimate^2)/x$parameter)))
}

並使用如下:

cor.test.plus(cor.test(mydf$X, mydf$Y))

這里,“mydf”定義為:

mydf <- structure(list(Neighborhood = c("Fair Oaks", "Strandwood", "Walnut Acres",
  "Discov. Bay", "Belshaw", "Kennedy", "Cassell", "Miner", "Sedgewick", 
  "Sakamoto", "Toyon", "Lietz"), X = c(50L, 11L, 2L, 19L, 26L, 
  73L, 81L, 51L, 11L, 2L, 19L, 25L), Y = c(22.1, 35.9, 57.9, 22.2, 
  42.4, 5.8, 3.6, 21.4, 55.2, 33.3, 32.4, 38.4)), .Names = c("Neighborhood", 
  "X", "Y"), class = "data.frame", row.names = c(NA, -12L))

你不能簡單地從返回值中獲取測試統計數據嗎? 當然,測試統計是估計/ se,所以你可以從估計除以tstat來計算:

在上面的答案中使用mydf

r = cor.test(mydf$X, mydf$Y)
tstat = r$statistic
estimate = r$estimate
estimate; tstat

       cor 
-0.8492663 
        t 
-5.086732 

暫無
暫無

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

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