在R中找到二进制变量之间的相关性时错误?

发布于 2025-01-27 16:58:38 字数 541 浏览 2 评论 0原文

我有一个变量(10)为二进制/因子变量的列表。例如,有特定副作用的人与没有的人。我想找到这些变量之间的相关性。一个变量是否与另一个变量相关/相关。我无法在R中使用COR函数,因为这仅处理连续/定量数据。因此,我使用的是四函数。

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)

但是,我遇到了一个错误:

Error in t(x) - mx : non-numeric argument to binary operator

我不确定这意味着什么?如何克服此错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2025-02-03 16:58:38

数据必须是数字(在这种情况下为二进制)。您可以看到,如果数据是数字起作用的,但是如果它们是因素,则不会:

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

在2022-05-10上由 reprex软件包(v2.0.1)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文