如何从cor.test()中提取p.value并进行估计? [英] How to extract the p.value and estimate from cor.test()?

查看:100
本文介绍了如何从cor.test()中提取p.value并进行估计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在for循环中对数据集执行cor.test,但是我不知道如何从测试中提取估计值和tau之类的信息.

在数据集中执行for循环之前,cor.test()函数返回如下:

cor.test(armpit $ Corynebacterium.1,armpit $ Staphylococcus.1,alternantive ="two-side",method ="kendall",exact = FALSE,continuity = TRUE)

返回结果

这是我执行for循环的代码.现在,我想从测试中提取估计值和tau.

(1:8中的i)的

  {对于(1:8中的j){如果(j!= i)cor.test(as.numeric(unlist(armpit [i])),as.numeric(unlist(armpit [j])),alterantive =双面",method ="kendall",exact = FALSE,continuity = TRUE)}} 

我已经检查了

中的类似问题

类似问题

然后我将代码更改为:

  estimates =数字(50)pvalues =数值(50)对于(1:8中的i){对于(1:8中的j){如果(j!= i)cor.test(as.numeric(unlist(armpit [i])),as.numeric(unlist(armpit [j])),alterantive =双面",method ="kendall",exact = FALSE,continuity = TRUE)估计值[i] = cor.test $ estimatepvalues [i] = cor.test $ p-value}} 

但是它返回:

cor.test $ estimate中的错误:"closure"类型的对象不可子集化

有人可以为我提供一些有关如何从for循环中的cor.test()函数提取估计值和tau值的帮助吗?预先感谢.

解决方案

好的,找到它了,我们应该早点看过它.如果,则 if(j!= i)语句必须用方括号括起来.对于您使用的特定格式,R无法正确解析它.我无法获取您的数据,所以我做了一些(它将针对随机列测试随机行).这有效:

  M<-matrix(rnorm(8 * 8),ncol = 8)#组成测试数据估计=数字(50)pvalues =数值(50)对于(1:8中的i){对于(1:8中的j){如果(j!= i){#需要此括号cor_test<-cor.test(M [i,],M [,j],Alternative ="two.side",method ="kendall",exact = FALSE,continuity = TRUE)估计值[i] = cor_test $ estimatepvalues [i] = cor_test $ p.value}#和这个括号}}估计值 

将所有结果存储在数据框中的备用版本.

  M<-matrix(rnorm(8 * 8),ncol = 8)#组成测试数据ans<-data.frame(i = rep(NA,64),j = rep(NA,64),估计= rep(NA,64),pvalue = rep(NA,64))小于1对于(1:8中的i){对于(1:8中的j){如果(j!= i){cor_test<-cor.test(M [i,],M [,j],alternative ="two.side",method ="kendall",exact = FALSE,continuity = TRUE)ans [cnt,1]<-ians [cnt,2]<-jans [cnt,3]<-cor_test $ estimateans [cnt,4]<-cor_test $ p.valuecnt<-cnt + 1}}}ans<-na.omit(ans) 

I perform cor.test for a dataset in a for loop, but I don't know how to extract the information like estimate and tau from my test.

Before performing for loop in the dataset, The cor.test() function returns as follows:

cor.test(armpit$Corynebacterium.1, armpit$Staphylococcus.1, alterantive="two-sided", method="kendall", exact=FALSE, continuity=TRUE)

return result

Here is my code for performing for loop. Now I want to extract estimate and tau from my test.

for (i in 1:8) {
  for (j in 1:8) {
    if (j != i)
    cor.test( as.numeric(unlist(armpit[i])),
        as.numeric(unlist(armpit[j])), alterantive="two-sided",
        method="kendall", exact=FALSE, continuity=TRUE)
   }
}

I have check the similar question from

similar question

Then I change my code as:

estimates = numeric(50)
pvalues = numeric(50)
for (i in 1:8) {
  for (j in 1:8) {
    if (j != i)
    cor.test( as.numeric(unlist(armpit[i])),
        as.numeric(unlist(armpit[j])), alterantive="two-sided",
         method="kendall", exact=FALSE, continuity=TRUE)
    estimates[i] = cor.test$estimate
    pvalues[i]= cor.test$p-value
   }
   }

But it returns:

Error in cor.test$estimate : object of type 'closure' is not subsettable

Could anyone offer me some help about how to extract estimate and tau value from cor.test() function in a for loop? Thanks in advance.

解决方案

Alright, found it, we should have seen it earlier. The if (j != i) statement needs to have brackets around everything that should be done if the statement is true. With the particular formatting you had, R was not parsing it correctly. I couldn't get your data, so I made some up (which will test random rows against random columns). This works:

M <- matrix(rnorm(8*8), ncol = 8) # made up test data
estimates = numeric(50)
pvalues = numeric(50)
for (i in 1:8) {
  for (j in 1:8) {
    if (j != i) { # need this bracket
        cor_test <-  cor.test(M[i,], M[,j],
             alternative="two.sided",
              method="kendall", exact=FALSE, continuity=TRUE)
        estimates[i] = cor_test$estimate
        pvalues[i]= cor_test$p.value
        } # and this bracket
    }
    }
estimates
pvalues

EDIT: alternative version to store all results in a data frame.

M <- matrix(rnorm(8*8), ncol = 8) # made up test data

ans <- data.frame(i = rep(NA, 64), j = rep(NA, 64), estimate = rep(NA, 64), pvalue = rep(NA, 64))
cnt <- 1
for (i in 1:8) {
  for (j in 1:8) {
    if (j != i) {
        cor_test <-  cor.test(M[i,], M[,j], alternative="two.sided", method="kendall", exact=FALSE, continuity=TRUE)
        ans[cnt,1] <- i
        ans[cnt,2] <- j
        ans[cnt,3] <- cor_test$estimate
        ans[cnt,4] <- cor_test$p.value
        cnt <- cnt + 1
        }
    }
    }

ans <- na.omit(ans)

这篇关于如何从cor.test()中提取p.value并进行估计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆