Reputation: 59
I have a list of variables (10) that are binary/factor variables. E.g. people who got a particular side effect vs people who did not. I want to find the correlation between these variables. Whether one variable is related/correlated to another. I can't use the cor function in R since this only deals with continuous/quantitative data. So I am using the tetrachoric function.
correlation_data <- Toxicity %>% dplyr::select('variable1', 'variable2', 'variable3', 'variable4', etc....) %>% na.omit()
correlation_data [] <- lapply(correlation_data, factor)
install.packages("psych")
library(psych)
tetrachoric(correlation_data)
However, I get an error saying:
Error in t(x) - mx : non-numeric argument to binary operator
I am not sure what this means? How do I overcome this error?
Upvotes: 0
Views: 373
Reputation: 21992
The data have to be numeric (and binary in this case). You can see that if the data are numeric it works, but if they are factors it does not:
library(dplyr)
library(psych)
bins <- matrix(rbinom(250, 1, .25), ncol=10)
bins <- as.data.frame(bins)
tetrachoric(bins)
#> Warning in cor.smooth(mat): Matrix was not positive definite, smoothing was done
#> Call: tetrachoric(x = bins)
#> tetrachoric correlation
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> V1 1.00
#> V2 0.47 1.00
#> V3 -0.18 0.02 1.00
#> V4 -0.17 0.03 0.65 1.00
#> V5 0.11 -0.10 -0.26 -0.26 1.00
#> V6 0.38 0.45 -0.06 -0.36 -0.18 1.00
#> V7 -0.06 -0.28 -0.06 -0.08 -0.16 0.28 1.00
#> V8 -0.33 -0.19 0.02 0.03 -0.10 0.14 0.41 1.00
#> V9 -0.25 -0.65 -0.26 -0.22 0.32 -0.47 0.51 -0.10 1.00
#> V10 -0.26 -0.10 0.11 0.11 0.00 0.23 0.24 0.33 -0.01 1.00
#>
#> with tau of
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 0.99 0.71 0.99 0.99 0.84 0.58 0.58 0.71 0.25 0.84
bins <- bins %>%
mutate(across(everything(), as.factor))
tetrachoric(bins)
#> Error in t(x) - mx: non-numeric argument to binary operator
Created on 2022-05-10 by the reprex package (v2.0.1)
Upvotes: 0