首页 > 解决方案 > 集群分析,dimnames(x) <- dn 中的错误:“dimnames”[2] 的长度不等于数组范围

问题描述

我正在尝试使用 poLCA 包进行潜在聚类分析。

我的数据框可以在这里作为 rda 文件下载并保存到您的工作目录中: https ://drive.google.com/open?id=1eGJprHaXdoVhKNlGD5VcsoND7iyIoNwJ

load(file = "QuestionData.rda")

当我尝试运行 LCA 时:

library("MPsychoR")
library("poLCA")
formula <- cbind(Question1, Question2, Question3, Question4) ~ 1
OneClass <- poLCA(formula, data = Output, nclass = 1, nrep = 3)
TwoClass <- poLCA(formula, data = Output, nclass = 2, nrep = 3)

我收到以下错误:

Error in dimnames(x) <- dn : 
length of 'dimnames' [2] not equal to array extent

通过阅读并查看遇到此错误的其他人,这似乎是因为他们的数据框中的列数和他们输入到函数中的列名数之间存在差异......但我有 4我的数据框中的列和 4 添加到 poLCA 函数中。谁能帮我理解为什么我会遇到这个错误?

如果您不想下载我的数据,我的数据框的一般结构如下:

Question1 <- c('Sufficient', 'Problematic', 'Problematic', 'Sufficient', 
'Excellent')
Question2 <- c('Insufficient', 'Insufficient', 'Insufficient', 'Sufficient', 
'Sufficient')
Question3 <- c('Sufficient', 'Sufficient', 'Insufficient', 'Sufficient', 
'Sufficient')
Question4 <- c('Problematic', 'Insufficient', 'Problematic', 'Problematic', 
'Excellent')
Question5 <- c('Insufficient', 'Sufficient', 'Sufficient', 'Exceptional', 
'Exceptional')

DF <- data.frame(Participants, Question1, Question2, Question3, Question4, 
Question5)

DF$Question1 <- factor(DF$Question1, levels = c("Problematic", 
"Insufficient", "Sufficient", "Excellent"), ordered=TRUE)
DF$Question2 <- factor(DF$Question2, levels = c("Problematic", 
"Insufficient", "Sufficient", "Excellent"), ordered=TRUE)
DF$Question3 <- factor(DF$Question3, levels = c("Problematic", 
"Insufficient", "Sufficient", "Excellent"), ordered=TRUE)
DF$Question4 <- factor(DF$Question4, levels = c("Problematic", 
"Insufficient", "Sufficient", "Excellent"), ordered=TRUE)
DF$Question5 <- factor(DF$Question5, levels = c("Problematic", 
"Insufficient", "Sufficient", "Excellent", "Exceptional"), ordered=TRUE)

标签: rarrays

解决方案


每个响应变量都有不同的级别:

summary(Output)
        Question1          Question2          Question3           Question4  
 Problematic :150   Problematic : 57   Problematic :181   Problematic  :456  
 Insufficient:211   Insufficient:157   Insufficient:320   Insufficient :130  
 Sufficient  :238   Sufficient  :692   Sufficient  :405   Sufficient   : 48  
 Excellent   :307   Excellent   :  0                      Excellent    :272  
                                                          ExcellentPlus:  0

如果我没有错(对您的数据不太熟悉),您可以让它们具有相同的级别:

NewOutput = Output
for(i in 1:ncol(NewOutput)){
    NewOutput[,i] = factor(as.character(Output[,i]),order=TRUE,
    levels=c("Problematic","Insufficient","Sufficient","Excellent"))
}
poLCA(cbind(Question1,Question2,Question3,Question4)~1,data=NewOutput,nclass=1)

我从您的因素中省略了“ExcellentPlus”,因为在您的示例数据中找不到它。


推荐阅读