简体   繁体   English

R:如何从cor.test函数中提取置信区间

[英]R: How to extract confidence interval from cor.test function

Im trying to extract 95 percent confidence interval from the result of pearson correlation. 我试图从皮尔森相关结果中提取95 percent confidence interval

My output looks like this: 我的输出如下:

Pearson's product-moment correlation

data:  newX[, i] and newY
t = 2.1253, df = 6810, p-value = 0.0336
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.001998576 0.049462864
sample estimates:
       cor 
0.02574523 

I get it with the following code 我用以下代码得到它

t <- apply(FNR[, -1], 2, cor.test, FNR$HDL, method="pearson")

I would appreciate any help. 我将不胜感激任何帮助。 Thanks. 谢谢。

cor.test returns a list with various elements, including the confidence interval. cor.test返回包含各种元素的列表,包括置信区间。 You can see the structure of the object returned by cor.test as follows (using the built-in mtcars data frame for illustration): 你可以看到cor.test返回的对象的结构如下(使用内置的mtcars数据框进行说明):

ct = cor.test(mtcars$mpg, mtcars$wt, method="pearson")

str(ct)
 List of 9 $ statistic : Named num -9.56 ..- attr(*, "names")= chr "t" $ parameter : Named int 30 ..- attr(*, "names")= chr "df" $ p.value : num 1.29e-10 $ estimate : Named num -0.868 ..- attr(*, "names")= chr "cor" $ null.value : Named num 0 ..- attr(*, "names")= chr "correlation" $ alternative: chr "two.sided" $ method : chr "Pearson's product-moment correlation" $ data.name : chr "mtcars$mpg and mtcars$wt" $ conf.int : atomic [1:2] -0.934 -0.744 ..- attr(*, "conf.level")= num 0.95 - attr(*, "class")= chr "htest" 

Now extract the confidence interval: 现在提取置信区间:

ct$conf.int[1:2]   

[1] -0.9338264 -0.7440872 [1] -0.9338264 -0.7440872

Let's look at the ?cor.test page and then modify the last example so it resembles your code: 让我们看一下?cor.test页面,然后修改最后一个例子,使它类似于你的代码:

t <- apply(USJudgeRatings[, -1], 2, cor.test, USJudgeRatings$CONT, method="pearson")

This is the first sublist of the returned value: 这是返回值的第一个子列表:

> str(t[1])
List of 1
 $ INTG:List of 9
  ..$ statistic  : Named num -0.861
  .. ..- attr(*, "names")= chr "t"
  ..$ parameter  : Named int 41
  .. ..- attr(*, "names")= chr "df"
  ..$ p.value    : num 0.395
  ..$ estimate   : Named num -0.133
  .. ..- attr(*, "names")= chr "cor"
  ..$ null.value : Named num 0
  .. ..- attr(*, "names")= chr "correlation"
  ..$ alternative: chr "two.sided"
  ..$ method     : chr "Pearson's product-moment correlation"
  ..$ data.name  : chr "newX[, i] and USJudgeRatings$CONT"
  ..$ conf.int   : atomic [1:2] -0.417 0.174
  .. ..- attr(*, "conf.level")= num 0.95
  ..- attr(*, "class")= chr "htest"

To get all the conf.int nodes from that list with 11 items, use sapply with the "[[" function and given the character-valued name, "conf.int": 为了让所有conf.int从该列表中有11个项目的节点,使用sapply"[["功能和给定的字符值的名字,“conf.int”:

> sapply(t, "[[", "conf.int")
           INTG       DMNR       DILG       CFMG       DECI       PREP
[1,] -0.4168591 -0.4339992 -0.2890276 -0.1704402 -0.2195110 -0.2898732
[2,]  0.1741182  0.1537524  0.3115762  0.4199860  0.3770813  0.3107427
           FAMI       ORAL       WRIT       PHYS       RTEN
[1,] -0.3234896 -0.3112193 -0.3396845 -0.2501717 -0.3306462
[2,]  0.2768389  0.2893898  0.2599541  0.3489073  0.2694228

The sapply function returns a column-oriented matrix result (at least with the default of simplify=TRUE) when given a set of arguments that return equal length values as is the case here. 当给定一组返回相等长度值的参数时, sapply函数返回一个面向列的矩阵结果(至少默认值为simplify = TRUE),就像这里的情况一样。

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

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