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

调用psych::polychoric()报错:需提供矩阵类x或x与y参数

解决psych::polychoric()运行时的cor(x, use = "pairwise")错误

问题描述

运行psych::polychoric()时反复触发以下错误:

Error in cor(x, use = "pairwise") : supply both 'x' and 'y' or a matrix-like 'x'

输入的foe_scores明明是tibble/dataframe类型,却被判定为非矩阵类,尝试用rbind()转矩阵还出现行名称相关错误。

问题原因

tibble虽属于dataframe子类,但psych::polychoric()内部对输入类型的识别更严格——它只兼容标准矩阵或普通dataframe(非tibble)。你的foe_scores是tbl_df类型,这就是报错的核心原因。

解决方案

直接将tibble转为普通矩阵或dataframe即可,无需用rbind(),推荐两种简单方法:

方法1:转为普通dataframe

在调用polychoric()前,用as.data.frame()转换:

foe_poly <- psych::polychoric(as.data.frame(foe_scores), max.cat = 11)

方法2:转为矩阵

用as.matrix()直接转换(注意:仅适用于全数值型数据,你的数据集符合要求):

foe_poly <- psych::polychoric(as.matrix(foe_scores), max.cat = 11)

验证代码

用你提供的数据集测试,修改后的代码可正常运行:

# 加载数据集
foe_scores <- structure(list(Q7.2_1 = c(8, 6, 6, 9, 8, 10, 10, 7, 5, 8, 8, 
9, 0, 5, 9, 8, 9, 9, 8, 8, 5, 6, 6, 10, 7, 7, 9, 7), Q7.2_2 = c(5, 
8, 9, 9, 8, 9, 10, 8, 4, 10, 9, 10, 8, 5, 9, 9, 10, 8, 9, 9, 
8, 7, 10, 9, 7, 9, 10, 7), Q7.2_3 = c(7, 6, 4, 6, 5, 10, 8, 4, 
5, 1, 5, 9, 3, 5, 6, 5, 5, 9, 6, 5, 5, 7, 4, 4, 3, 6, 7, 5), 
    Q7.2_4 = c(8, 8, 7, 6, 5, 10, 8, 9, 6, 10, 8, 5, 5, 8, 9, 
    5, 6, 8, 10, 5, 5, 9, 10, 5, 5, 5, 9, 5), Q7.2_5 = c(6, 9, 
    4, 5, 6, 9, 8, 4, 5, 9, 0, 5, 10, 7, 5, 5, 5, 0, 5, 10, 5, 
    6, 5, 6, 10, 5, 7, 5), Q7.2_6 = c(8, 9, 3, 6, 8, 8, 5, 5, 
    5, 2, 3, 10, 0, 1, 10, 5, 5, 7, 5, 5, 5, 6, 8, 6, 7, 5, 6, 
    5), Q7.2_7 = c(7, 5, 9, 6, 3, 10, 5, 3, 5, 8, 6, 6, 10, 10, 
    7, 5, 7, 6, 5, 5, 5, 5, 6, 7, 5, 5, 5, 5), Q7.2_8 = c(7, 
    8, 9, 5, 7, 8, 6, 9, 5, 9, 3, 8, 5, 6, 9, 6, 5, 8, 8, 10, 
    5, 6, 8, 9, 5, 5, 7, 5), Q7.2_9 = c(9, 9, 4, 7, 9, 9, 8, 
    8, 6, 9, 10, 8, 5, 5, 6, 5, 7, 9, 7, 5, 1, 6, 9, 6, 3, 9, 
    7, 3), Q7.2_10 = c(7, 7, 3, 7, 1, 10, 10, 7, 8, 6, 3, 10, 
    4, 8, 10, 7, 6, 7, 4, 10, 10, 6, 9, 6, 6, 10, 10, 3), Q7.2_11 = c(7, 
    10, 10, 10, 8, 6, 10, 9, 7, 9, 9, 10, 10, 10, 10, 7, 10, 
    9, 9, 5, 9, 7, 10, 10, 9, 9, 10, 9), Q7.2_12 = c(6, 8, 8, 7, 10, 7, 10, 7, 6, 7, 6, 8, 10, 7, 10, 7, 5, 8, 9, 5, 5, 
    6, 8, 9, 5, 8, 9, 5), Q7.2_13 = c(3, 5, 9, 7, 10, 6, 10, 
    4, 5, 1, 9, 7, 10, 9, 10, 7, 8, 8, 6, 10, 5, 6, 10, 9, 4, 
    6, 9, 5), Q7.2_14 = c(5, 10, 7, 7, 10, 10, 10, 8, 7, 8, 9, 
    10, 8, 10, 8, 9, 9, 8, 7, 8, 5, 6, 7, 6, 4, 6, 9, 7), Q7.2_15 = c(2, 
    5, 7, 9, 2, 9, 5, 9, 9, 7, 3, 4, 7, 9, 5, 7, 7, 7, 7, 5, 
    5, 10, 9, 10, 4, 4, 5, 5), Q7.2_16 = c(3, 7, 10, 9, 1, 10, 
    5, 5, 6, 10, 5, 10, 5, 10, 5, 5, 9, 10, 10, 5, 10, 8, 10, 
    8, 8, 8, 10, 9), Q7.2_17 = c(7, 5, 6, 5, 1, 8, 8, 5, 5, 10, 
    6, 10, 1, 5, 5, 6, 8, 8, 5, 3, 5, 4, 5, 6, 5, 7, 8, 5), Q7.2_18 = c(5, 
    5, 9, 6, 9, 7, 8, 5, 6, 10, 8, 5, 10, 10, 7, 5, 7, 6, 5, 
    7, 5, 10, 7, 7, 7, 7, 8, 5), Q7.2_19 = c(3, 6, 10, 5, 8, 
    7, 5, 5, 5, 6, 3, 7, 10, 10, 5, 5, 6, 9, 5, 8, 0, 5, 5, 5, 
    8, 5, 7, 3), Q7.2_20 = c(7, 5, 0, 3, 2, 7, 5, 5, 5, 1, 1, 
    9, 1, 5, 10, 5, 5, 7, 5, 1, 8, 5, 8, 8, 5, 9, 7, 3), Q7.2_21 = c(8, 
    4, 6, 5, 2, 8, 4, 4, 6, 2, 3, 7, 6, 7, 5, 5, 5, 8, 6, 5, 
    0, 5, 5, 5, 2, 3, 5, 1), Q7.2_22 = c(8, 3, 5, 5, 0, 8, 8, 
    5, 6, 1, 2, 3, 7, 5, 5, 4, 6, 9, 6, 7, 5, 7, 6, 4, 7, 4, 
    4, 5), Q7.2_23 = c(2, 10, 7, 5, 7, 3, 5, 5, 7, 1, 10, 7,  
    10, 5, 8, 5, 3, 8, 5, 4, 5, 8, 8, 8, 3, 5, 6, 5), Q7.2_24 = c(7, 
    10, 7, 5, 2, 2, 5, 5, 7, 1, 6, 9, 10, 5, 7, 5, 3, 8, 5, 4, 
    0, 4, 8, 8, 1, 5, 8, 5)), row.names = c(NA, -28L), class = c("tbl_df", "tbl", "data.frame"))

# 转换为普通dataframe并运行分析
foe_poly <- psych::polychoric(as.data.frame(foe_scores), max.cat = 11)
foe_cor <- foe_poly$rho
knitr::kable(foe_cor, digits = 2)

补充说明

  • rbind()是行合并工具,并非数据类型转换函数,之前用它尝试转矩阵必然出错。
  • 若需保留tibble特性,可先完成数据清洗,再转换为polychoric()兼容的类型。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 17:46:01