如何在Psych ::主函数中设置“权重”参数?

发布于 2025-01-17 23:33:03 字数 4267 浏览 4 评论 0原文

我正在尝试使用 PCA 进行探索性分析来确定量表的因子结构。 我正在使用的软件包是:

library(GPArotation) # required for `principal` to work
library(psych)

功能是:

principal()

我想根据参与者的性别应用调整权重。

这是我的数据集的示例:

GPS_01 GPS_03 GPS_04 GPS_05 GPS_07 GPS_08 GPS_10 GPS_11 GPS_12 GPS_13 GPS_14 GPS_15 GPS_17 GPS_18 GPS_19 gender_pscore
1       1      1      2      2      4      1      3      2      1      1      3      1      2      2      4          0.62
2       1      1      1      1      2      1      1      1      1      1      3      2      3      2      1          2.78
3       1      1      1      1      1      1      1      1      1      1      2      1      2      2      1          0.62
4       1      1      2      2      1      1      1      1      1      1      3      1      1      4      1          0.62
5       4      4      4      4      5      5      4      5      4      4      5      2      5      5      4          0.62
6       1      1      1      1      1      1      1      1      1      1      2      2      3      2      2          0.62
7       1      1      1      1      1      1      2      1      1      1      3      2      4      3      2          0.62
8       1      3      1      1      1      1      3      1      2      1      4      1      4      3      2          0.62
9       3      3      3      5      3      1      4      2      3      1      2      1      5      2      3          0.62
10      1      2      1      1      2      2      1      2      1      2      4      2      2      3      2          0.62
11      1      4      1      1      3      4      1      2      3      1      2      2      3      2      3          0.62
12      1      1      1      1      5      2      1      5      1      3      5      4      5      4      5          0.62
13      1      2      1      1      1      4      1      4      1      3      5      1      4      2      5          0.62
14      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1          0.62
15      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1          0.62
16      1      1      1      1      1      1      2      2      1      1      3      1      1      1      4          0.62
17      2      2      1      2      2      2      4      4      1      4      3      1      2      3      4          0.62
18      1      1      2      2      1      1      1      1      2      1      2      1      2      2      1          0.62
19      1      2      1      1      3      3      1      3      1      1      4      1      3      3      4          0.62
20      1      1      1      2      1      1      2      1      1      1      3      1      2      1      1          2.78

或者是原始数据的更小的子集(如果更容易的话)

data<-structure(list(GPS_01 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_03 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_04 = c(2L, 1L, 1L, 2L, 4L, 1L), GPS_05 = c(2L, 
1L, 1L, 2L, 4L, 1L), GPS_07 = c(4L, 2L, 1L, 1L, 5L, 1L), GPS_08 = c(1L, 
1L, 1L, 1L, 5L, 1L), GPS_10 = c(3L, 1L, 1L, 1L, 4L, 1L), GPS_11 = c(2L, 
1L, 1L, 1L, 5L, 1L), GPS_12 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_13 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_14 = c(3L, 3L, 2L, 3L, 5L, 2L), GPS_15 = c(1L, 
2L, 1L, 1L, 2L, 2L), GPS_17 = c(2L, 3L, 2L, 1L, 5L, 3L), GPS_18 = c(2L, 
2L, 2L, 4L, 5L, 2L), GPS_19 = c(4L, 1L, 1L, 1L, 4L, 2L), gender_pscore = c(0.62, 
2.78, 0.62, 0.62, 0.62, 0.62)), row.names = c(NA, 6L), class = "data.frame")

这是我使用的代码:

pc <-  principal(data[,1:15], nfactors = 3, rotate ="oblimin",weights ="gender_pscore")

我总是遇到同样的问题:

Error in (function (L, Tmat = diag(ncol(L)), gam = 0, normalize = FALSE,  : 
  unused argument (weights = "gender_pscore")
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  'data' must be of a vector type, was 'NULL'
In addition: Warning message:
In data[,1:20], nfactors = 3, rotate = "oblimin",  :
  The requested transformaton failed, Promax was used instead as an oblique transformation

我对使用 R 很陌生,所以不确定如何解决这个问题。 当我删除 weights ="gender_pscore" 时,问题就消失了。但在这种情况下,我无法再根据参与者的性别对因子分析应用调整权重。

I am trying to run an exploratory analysis using PCA to determine the factorial structure of a scale.
The packages I am using are:

library(GPArotation) # required for `principal` to work
library(psych)

The function is:

principal()

I would like to apply an adjustment weight based on participants’ gender.

Here is a sample of my dataset:

GPS_01 GPS_03 GPS_04 GPS_05 GPS_07 GPS_08 GPS_10 GPS_11 GPS_12 GPS_13 GPS_14 GPS_15 GPS_17 GPS_18 GPS_19 gender_pscore
1       1      1      2      2      4      1      3      2      1      1      3      1      2      2      4          0.62
2       1      1      1      1      2      1      1      1      1      1      3      2      3      2      1          2.78
3       1      1      1      1      1      1      1      1      1      1      2      1      2      2      1          0.62
4       1      1      2      2      1      1      1      1      1      1      3      1      1      4      1          0.62
5       4      4      4      4      5      5      4      5      4      4      5      2      5      5      4          0.62
6       1      1      1      1      1      1      1      1      1      1      2      2      3      2      2          0.62
7       1      1      1      1      1      1      2      1      1      1      3      2      4      3      2          0.62
8       1      3      1      1      1      1      3      1      2      1      4      1      4      3      2          0.62
9       3      3      3      5      3      1      4      2      3      1      2      1      5      2      3          0.62
10      1      2      1      1      2      2      1      2      1      2      4      2      2      3      2          0.62
11      1      4      1      1      3      4      1      2      3      1      2      2      3      2      3          0.62
12      1      1      1      1      5      2      1      5      1      3      5      4      5      4      5          0.62
13      1      2      1      1      1      4      1      4      1      3      5      1      4      2      5          0.62
14      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1          0.62
15      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1          0.62
16      1      1      1      1      1      1      2      2      1      1      3      1      1      1      4          0.62
17      2      2      1      2      2      2      4      4      1      4      3      1      2      3      4          0.62
18      1      1      2      2      1      1      1      1      2      1      2      1      2      2      1          0.62
19      1      2      1      1      3      3      1      3      1      1      4      1      3      3      4          0.62
20      1      1      1      2      1      1      2      1      1      1      3      1      2      1      1          2.78

or an even smaller subset of your original data (if easier)

data<-structure(list(GPS_01 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_03 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_04 = c(2L, 1L, 1L, 2L, 4L, 1L), GPS_05 = c(2L, 
1L, 1L, 2L, 4L, 1L), GPS_07 = c(4L, 2L, 1L, 1L, 5L, 1L), GPS_08 = c(1L, 
1L, 1L, 1L, 5L, 1L), GPS_10 = c(3L, 1L, 1L, 1L, 4L, 1L), GPS_11 = c(2L, 
1L, 1L, 1L, 5L, 1L), GPS_12 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_13 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_14 = c(3L, 3L, 2L, 3L, 5L, 2L), GPS_15 = c(1L, 
2L, 1L, 1L, 2L, 2L), GPS_17 = c(2L, 3L, 2L, 1L, 5L, 3L), GPS_18 = c(2L, 
2L, 2L, 4L, 5L, 2L), GPS_19 = c(4L, 1L, 1L, 1L, 4L, 2L), gender_pscore = c(0.62, 
2.78, 0.62, 0.62, 0.62, 0.62)), row.names = c(NA, 6L), class = "data.frame")

Here the code I used:

pc <-  principal(data[,1:15], nfactors = 3, rotate ="oblimin",weights ="gender_pscore")

I always get the same issue:

Error in (function (L, Tmat = diag(ncol(L)), gam = 0, normalize = FALSE,  : 
  unused argument (weights = "gender_pscore")
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  'data' must be of a vector type, was 'NULL'
In addition: Warning message:
In data[,1:20], nfactors = 3, rotate = "oblimin",  :
  The requested transformaton failed, Promax was used instead as an oblique transformation

I am quite new to using R, so not sure how to solve this issue.
The problems disappear when I remove the weights ="gender_pscore". But in this case, I can no longer apply an adjustment weight to my factorial analyses based on participants’ gender.

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

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

发布评论

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

评论(1

長街聽風 2025-01-24 23:33:03

权重似乎是principal()函数的正确参数。
我认为问题来自您告诉principal()使用列“ gengender_pscore”的方式。

?principal我们可以读到权重是每个观察值的重量n. ob的向量。如果您使用stright =“ gengender_pscore”,则将lenght One的字符向量馈入stright strigt参数。

选择数据中的列应解决您的问题:

psych::principal(df[,1:15], 
                 nfactors = 3, 
                 rotate ="oblimin",
                 weight = df[,"gender_pscore"])

警告:这不是一般规则,它们是可与引用或无引用的列名称(例如:大多数整理函数)一起使用的函数!

上面代码的输出:

> psych::principal(df[,1:15], 
+                  nfactors = 3, 
+                  rotate ="oblimin",
+                  weight = df[,"gender_pscore"])
Principal Components Analysis
Call: psych::principal(r = df[, 1:15], nfactors = 3, rotate = "oblimin", 
    weight = df[, "gender_pscore"])
Standardized loadings (pattern matrix) based upon correlation matrix
         TC1   TC3   TC2   h2      u2 com
GPS_01  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_03  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_04  0.81  0.29 -0.17 0.99 0.00837 1.4
GPS_05  0.81  0.29 -0.17 0.99 0.00837 1.4
GPS_07  0.08  0.88  0.15 0.94 0.05795 1.1
GPS_08  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_10  0.28  0.81 -0.07 1.00 0.00011 1.3
GPS_11  0.63  0.43  0.11 0.99 0.00559 1.8
GPS_12  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_13  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_14  0.69  0.19  0.23 0.83 0.17091 1.4
GPS_15 -0.05 -0.20  1.00 0.95 0.05006 1.1
GPS_17  0.25  0.20  0.81 0.99 0.01386 1.3
GPS_18  1.17 -0.28 -0.23 0.97 0.03065 1.2
GPS_19 -0.01  0.99 -0.13 0.95 0.05381 1.0

                       TC1  TC3  TC2
SS loadings           8.23 3.99 2.31
Proportion Var        0.55 0.27 0.15
Cumulative Var        0.55 0.81 0.97
Proportion Explained  0.57 0.27 0.16
Cumulative Proportion 0.57 0.84 1.00

 With component correlations of 
     TC1  TC3  TC2
TC1 1.00 0.65 0.24
TC3 0.65 1.00 0.19
TC2 0.24 0.19 1.00

Mean item complexity =  1.3
Test of the hypothesis that 3 components are sufficient.

The root mean square of the residuals (RMSR) is  0.02 
 with the empirical chi square  0.52  with prob <  1 

Fit based upon off diagonal values = 1Warning messages:
1: In cor.smooth(r) : Matrix was not positive definite, smoothing was done
2: In psych::principal(df[, 1:15], nfactors = 3, rotate = "oblimin",  :
  The matrix is not positive semi-definite, scores found from Structure loadings

dput df

df <- structure(list(GPS_01 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_03 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_04 = c(2L, 1L, 1L, 2L, 4L, 1L), GPS_05 = c(2L, 
1L, 1L, 2L, 4L, 1L), GPS_07 = c(4L, 2L, 1L, 1L, 5L, 1L), GPS_08 = c(1L, 
1L, 1L, 1L, 5L, 1L), GPS_10 = c(3L, 1L, 1L, 1L, 4L, 1L), GPS_11 = c(2L, 
1L, 1L, 1L, 5L, 1L), GPS_12 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_13 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_14 = c(3L, 3L, 2L, 3L, 5L, 2L), GPS_15 = c(1L, 
2L, 1L, 1L, 2L, 2L), GPS_17 = c(2L, 3L, 2L, 1L, 5L, 3L), GPS_18 = c(2L, 
2L, 2L, 4L, 5L, 2L), GPS_19 = c(4L, 1L, 1L, 1L, 4L, 2L), gender_pscore = c(0.62, 
2.78, 0.62, 0.62, 0.62, 0.62)), row.names = c(NA, 6L), class = "data.frame")

weight seems to be the correct argument for the principal() function.
I think the problem comes from the way you told principal() to use the column "gender_pscore".

From ?principal we can read that weight is vector of length n.obs that contains weights for each observation. If you use weight = "gender_pscore", you feed a character vector of lenght one into the weight argument.

Selecting the column in your data should resolve your problem:

psych::principal(df[,1:15], 
                 nfactors = 3, 
                 rotate ="oblimin",
                 weight = df[,"gender_pscore"])

Warning: this is not a general rule, they are functions that works with quoted or unquoted column names (ex: most tidyverse functions)!

Output of the code above:

> psych::principal(df[,1:15], 
+                  nfactors = 3, 
+                  rotate ="oblimin",
+                  weight = df[,"gender_pscore"])
Principal Components Analysis
Call: psych::principal(r = df[, 1:15], nfactors = 3, rotate = "oblimin", 
    weight = df[, "gender_pscore"])
Standardized loadings (pattern matrix) based upon correlation matrix
         TC1   TC3   TC2   h2      u2 com
GPS_01  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_03  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_04  0.81  0.29 -0.17 0.99 0.00837 1.4
GPS_05  0.81  0.29 -0.17 0.99 0.00837 1.4
GPS_07  0.08  0.88  0.15 0.94 0.05795 1.1
GPS_08  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_10  0.28  0.81 -0.07 1.00 0.00011 1.3
GPS_11  0.63  0.43  0.11 0.99 0.00559 1.8
GPS_12  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_13  0.83  0.13  0.22 0.99 0.01288 1.2
GPS_14  0.69  0.19  0.23 0.83 0.17091 1.4
GPS_15 -0.05 -0.20  1.00 0.95 0.05006 1.1
GPS_17  0.25  0.20  0.81 0.99 0.01386 1.3
GPS_18  1.17 -0.28 -0.23 0.97 0.03065 1.2
GPS_19 -0.01  0.99 -0.13 0.95 0.05381 1.0

                       TC1  TC3  TC2
SS loadings           8.23 3.99 2.31
Proportion Var        0.55 0.27 0.15
Cumulative Var        0.55 0.81 0.97
Proportion Explained  0.57 0.27 0.16
Cumulative Proportion 0.57 0.84 1.00

 With component correlations of 
     TC1  TC3  TC2
TC1 1.00 0.65 0.24
TC3 0.65 1.00 0.19
TC2 0.24 0.19 1.00

Mean item complexity =  1.3
Test of the hypothesis that 3 components are sufficient.

The root mean square of the residuals (RMSR) is  0.02 
 with the empirical chi square  0.52  with prob <  1 

Fit based upon off diagonal values = 1Warning messages:
1: In cor.smooth(r) : Matrix was not positive definite, smoothing was done
2: In psych::principal(df[, 1:15], nfactors = 3, rotate = "oblimin",  :
  The matrix is not positive semi-definite, scores found from Structure loadings

dput of df:

df <- structure(list(GPS_01 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_03 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_04 = c(2L, 1L, 1L, 2L, 4L, 1L), GPS_05 = c(2L, 
1L, 1L, 2L, 4L, 1L), GPS_07 = c(4L, 2L, 1L, 1L, 5L, 1L), GPS_08 = c(1L, 
1L, 1L, 1L, 5L, 1L), GPS_10 = c(3L, 1L, 1L, 1L, 4L, 1L), GPS_11 = c(2L, 
1L, 1L, 1L, 5L, 1L), GPS_12 = c(1L, 1L, 1L, 1L, 4L, 1L), GPS_13 = c(1L, 
1L, 1L, 1L, 4L, 1L), GPS_14 = c(3L, 3L, 2L, 3L, 5L, 2L), GPS_15 = c(1L, 
2L, 1L, 1L, 2L, 2L), GPS_17 = c(2L, 3L, 2L, 1L, 5L, 3L), GPS_18 = c(2L, 
2L, 2L, 4L, 5L, 2L), GPS_19 = c(4L, 1L, 1L, 1L, 4L, 2L), gender_pscore = c(0.62, 
2.78, 0.62, 0.62, 0.62, 0.62)), row.names = c(NA, 6L), class = "data.frame")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文