R psych tetrachoric - 二分变量
R psych tetrachoric - dichotomic variables
我有一个与人格问卷项目相对应的二分变量数据框。这是第一行。
head(mixclinic)
# A tibble: 6 x 15
CMS_1 CMS_2 CMS_3 CMS_4 CMS_5 CMS_6 CMS_7 CMS_8 CMS_9 CMS_10 CMS_11
<fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct>
1 1 1 0 1 0 1 0 0 0 0 0
2 1 1 0 1 0 0 0 1 0 0 0
3 1 1 0 1 0 1 0 0 0 0 0
4 0 1 0 1 0 1 0 1 0 0 0
5 0 1 0 1 0 1 0 0 0 0 0
6 1 1 0 1 1 1 0 0 0 0 0
我想执行四项相关以找到解释最大部分变异性的因素。在搜索基于 R 的资源时,我发现了 'psych' 包,它具有 tetrachoric 函数。我阅读了文档,但仍然无法执行分析。似乎缺少教程来提供帮助。任何人都可以帮助或参考有用的资源吗?谢谢
当输入数据框作为参数时,函数可能无法很好地处理因子(可能如果您将它们全部切换为数字)。但是,它需要一个矩阵作为参数,所以这适用于我创建的数据集。将来,包括一个可重现的例子总是有帮助的。希望这可以帮助!
编辑:澄清一下。我认为问题在于您的数据集由因素组成。当变量是因子时,该函数似乎不起作用。如果变量是数字或输入的数据是矩阵,它将起作用。因此,无论您选择将数据框列转换为数字,或将数据框转换为矩阵,都将起作用(即,我代码中的 df_matrix <- data.matrix(df)
行将数据框转换为矩阵)。如果您有任何问题,请告诉我。
> # Creating your dataset
>
> library(tidyverse)
> library(psych)
>
> df <- data.frame(CMS_1 = sample(2, replace = T, size = 10)-1,
+ CMS_2 = sample(2, replace = T, size = 10)-1,
+ CMS_3 = sample(2, replace = T, size = 10)-1,
+ CMS_4 = sample(2, replace = T, size = 10)-1,
+ CMS_5 = sample(2, replace = T, size = 10)-1,
+ CMS_6 = sample(2, replace = T, size = 10)-1,
+ CMS_7 = sample(2, replace = T, size = 10)-1,
+ CMS_8 = sample(2, replace = T, size = 10)-1)
>
> df <- df %>% mutate_if(is.numeric, as.factor)
> str(df)
'data.frame': 10 obs. of 8 variables:
$ CMS_1: Factor w/ 2 levels "0","1": 1 2 1 2 2 2 1 2 2 2
$ CMS_2: Factor w/ 2 levels "0","1": 1 2 2 1 2 2 1 2 1 2
$ CMS_3: Factor w/ 2 levels "0","1": 1 1 2 2 1 1 1 1 1 1
$ CMS_4: Factor w/ 2 levels "0","1": 2 2 1 2 1 1 2 1 1 2
$ CMS_5: Factor w/ 2 levels "0","1": 2 1 1 2 2 2 1 2 1 2
$ CMS_6: Factor w/ 2 levels "0","1": 2 2 1 1 2 2 2 2 1 2
$ CMS_7: Factor w/ 2 levels "0","1": 2 1 2 1 1 2 1 1 1 2
$ CMS_8: Factor w/ 2 levels "0","1": 1 2 2 1 1 2 1 1 1 1
>
> # Covnerting your data.frame to a matrix
> df_matrix <- data.matrix(df)
>
>
> tetrachoric(df_matrix)
For i = 6 j = 3 A cell entry of 0 was replaced with correct = 0.5. Check your data!
For i = 8 j = 2 A cell entry of 0 was replaced with correct = 0.5. Check your data!
Call: tetrachoric(x = df_matrix)
tetrachoric correlation
CMS_1 CMS_2 CMS_3 CMS_4 CMS_5 CMS_6 CMS_7 CMS_8
CMS_1 1.00
CMS_2 0.47 1.00
CMS_3 -0.31 -0.21 1.00
CMS_4 -0.37 -0.54 -0.02 1.00
CMS_5 0.43 0.27 -0.22 0.02 1.00
CMS_6 0.14 0.45 -0.74 0.29 0.44 1.00
CMS_7 -0.44 0.34 0.22 -0.02 0.29 0.20 1.00
CMS_8 -0.13 0.58 0.33 -0.33 -0.44 -0.10 0.46 1.00
with tau of
CMS_1 CMS_2 CMS_3 CMS_4 CMS_5 CMS_6 CMS_7 CMS_8
-0.52 -0.25 0.84 0.00 -0.25 -0.52 0.25 0.52
Warning message:
In cor.smooth(mat) : Matrix was not positive definite, smoothing was done
我有一个与人格问卷项目相对应的二分变量数据框。这是第一行。
head(mixclinic)
# A tibble: 6 x 15
CMS_1 CMS_2 CMS_3 CMS_4 CMS_5 CMS_6 CMS_7 CMS_8 CMS_9 CMS_10 CMS_11
<fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct>
1 1 1 0 1 0 1 0 0 0 0 0
2 1 1 0 1 0 0 0 1 0 0 0
3 1 1 0 1 0 1 0 0 0 0 0
4 0 1 0 1 0 1 0 1 0 0 0
5 0 1 0 1 0 1 0 0 0 0 0
6 1 1 0 1 1 1 0 0 0 0 0
我想执行四项相关以找到解释最大部分变异性的因素。在搜索基于 R 的资源时,我发现了 'psych' 包,它具有 tetrachoric 函数。我阅读了文档,但仍然无法执行分析。似乎缺少教程来提供帮助。任何人都可以帮助或参考有用的资源吗?谢谢
当输入数据框作为参数时,函数可能无法很好地处理因子(可能如果您将它们全部切换为数字)。但是,它需要一个矩阵作为参数,所以这适用于我创建的数据集。将来,包括一个可重现的例子总是有帮助的。希望这可以帮助!
编辑:澄清一下。我认为问题在于您的数据集由因素组成。当变量是因子时,该函数似乎不起作用。如果变量是数字或输入的数据是矩阵,它将起作用。因此,无论您选择将数据框列转换为数字,或将数据框转换为矩阵,都将起作用(即,我代码中的 df_matrix <- data.matrix(df)
行将数据框转换为矩阵)。如果您有任何问题,请告诉我。
> # Creating your dataset
>
> library(tidyverse)
> library(psych)
>
> df <- data.frame(CMS_1 = sample(2, replace = T, size = 10)-1,
+ CMS_2 = sample(2, replace = T, size = 10)-1,
+ CMS_3 = sample(2, replace = T, size = 10)-1,
+ CMS_4 = sample(2, replace = T, size = 10)-1,
+ CMS_5 = sample(2, replace = T, size = 10)-1,
+ CMS_6 = sample(2, replace = T, size = 10)-1,
+ CMS_7 = sample(2, replace = T, size = 10)-1,
+ CMS_8 = sample(2, replace = T, size = 10)-1)
>
> df <- df %>% mutate_if(is.numeric, as.factor)
> str(df)
'data.frame': 10 obs. of 8 variables:
$ CMS_1: Factor w/ 2 levels "0","1": 1 2 1 2 2 2 1 2 2 2
$ CMS_2: Factor w/ 2 levels "0","1": 1 2 2 1 2 2 1 2 1 2
$ CMS_3: Factor w/ 2 levels "0","1": 1 1 2 2 1 1 1 1 1 1
$ CMS_4: Factor w/ 2 levels "0","1": 2 2 1 2 1 1 2 1 1 2
$ CMS_5: Factor w/ 2 levels "0","1": 2 1 1 2 2 2 1 2 1 2
$ CMS_6: Factor w/ 2 levels "0","1": 2 2 1 1 2 2 2 2 1 2
$ CMS_7: Factor w/ 2 levels "0","1": 2 1 2 1 1 2 1 1 1 2
$ CMS_8: Factor w/ 2 levels "0","1": 1 2 2 1 1 2 1 1 1 1
>
> # Covnerting your data.frame to a matrix
> df_matrix <- data.matrix(df)
>
>
> tetrachoric(df_matrix)
For i = 6 j = 3 A cell entry of 0 was replaced with correct = 0.5. Check your data!
For i = 8 j = 2 A cell entry of 0 was replaced with correct = 0.5. Check your data!
Call: tetrachoric(x = df_matrix)
tetrachoric correlation
CMS_1 CMS_2 CMS_3 CMS_4 CMS_5 CMS_6 CMS_7 CMS_8
CMS_1 1.00
CMS_2 0.47 1.00
CMS_3 -0.31 -0.21 1.00
CMS_4 -0.37 -0.54 -0.02 1.00
CMS_5 0.43 0.27 -0.22 0.02 1.00
CMS_6 0.14 0.45 -0.74 0.29 0.44 1.00
CMS_7 -0.44 0.34 0.22 -0.02 0.29 0.20 1.00
CMS_8 -0.13 0.58 0.33 -0.33 -0.44 -0.10 0.46 1.00
with tau of
CMS_1 CMS_2 CMS_3 CMS_4 CMS_5 CMS_6 CMS_7 CMS_8
-0.52 -0.25 0.84 0.00 -0.25 -0.52 0.25 0.52
Warning message:
In cor.smooth(mat) : Matrix was not positive definite, smoothing was done