Reputation: 437
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)
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
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.
Upvotes: 2
Views: 3427
Reputation: 6213
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)
Upvotes: 1
Reputation: 16089
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
}
}
Upvotes: 3