How to extract the p.value and estimate from cor.test()?

瘦欲@ 提交于 2019-12-23 21:16:45

问题


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.


回答1:


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)



回答2:


cor.test returns a list. You can create an object to capture this list:

cor_test <- cor.test( as.numeric(unlist(armpit[i])), as.numeric(unlist(armpit[j])), alterantive="two-sided", method="kendall", exact=FALSE, continuity=TRUE)

Then use cor_test afterwards with $ to access each element of the list:

estimates[i] = cor_test$estimate  
pvalues[i]= cor_test$p.value  # note the ., not the -

The original error is pretty arcane, so it's understandable you were confused about this. You wrote cor.test$estimate, which asks R to access the estimate component of the cor.test function, not the result of the test.

estimates = numeric(50)
pvalues = numeric(50)
for (i in 1:8) {
  for (j in 1:8) {
    if (j != i)
    cor_test <- 
      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
   }
 }


来源:https://stackoverflow.com/questions/34471573/how-to-extract-the-p-value-and-estimate-from-cor-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!