You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言psych包principal函数权重未生效问题求助

psych包principal函数权重参数失效的解决方法

我在R中使用psych包的principal()函数复现SPSS中的加权主成分分析时,发现添加weight参数后,分析结果和无权重分析完全一致,说明权重未被正确应用。

无权重分析代码及输出

pc0 <- principal(mtcars[,c(8:11)], cor = "cov", covar = TRUE)
print(pc0)

# 输出:
Call: principal(r = mtcars[, c(8:11)], covar = TRUE, cor = "cov")
Unstandardized loadings (pattern matrix) based upon covariance matrix
       PC1     h2    u2    H2    U2
vs   -0.29 0.0854 0.169 0.336 0.664
am    0.05 0.0028 0.246 0.011 0.989
gear  0.24 0.0559 0.488 0.103 0.897
carb  1.61 2.5983 0.011 0.996 0.004

                PC1
SS loadings    2.74
Proportion Var 0.75

 Standardized loadings (pattern matrix)
     V   PC1    h2    u2
vs   1 -0.58 0.336 0.664
am   2  0.11 0.011 0.989
gear 3  0.32 0.103 0.897
carb 4     1 0.996 0.004

                PC1
SS loadings    3.00
Proportion Var 0.75

Mean item complexity =  1  
Test of the hypothesis that 1 component is sufficient.  

The root mean square of the residuals (RMSR) is  0.13   
with the empirical chi-square  6.87  with prob <  0.032  

Fit based upon off-diagonal values = 0.95  

原无效带权重分析代码及输出

pc1 <- principal(mtcars[,c(8:11)], cor = "cov", weight = mtcars[,c("wt")], covar = TRUE)
print(pc1)

# 输出与pc0完全一致,此处省略重复内容

问题原因

principal()函数的weight参数仅在让函数自动计算加权相关/协方差矩阵时生效,当指定cor="cov"且covar=TRUE时,函数会直接基于原始数据计算普通协方差矩阵,忽略传入的weight参数。

解决步骤

要实现加权主成分分析,需先手动计算加权协方差矩阵,再传入principal()函数:

  1. 计算加权协方差矩阵
# 使用cov.wt()计算加权协方差,wt指定权重列
weighted_cov_mat <- cov.wt(mtcars[,8:11], wt = mtcars$wt, cor = FALSE)$cov
  1. 基于加权协方差矩阵执行主成分分析
pc_weighted <- principal(r = weighted_cov_mat, covar = TRUE)
print(pc_weighted)

此时得到的结果会与无权重分析有明显差异,权重已被正确应用。

内容的提问来源于stack exchange,提问作者user29972384

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:19:51