首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >聚类分析,dimname(X) <- dn中出错:‘dimname’[2]的长度不等于数组范围

聚类分析,dimname(X) <- dn中出错:‘dimname’[2]的长度不等于数组范围
EN

Stack Overflow用户
提问于 2019-12-28 07:04:15
回答 1查看 271关注 0票数 3

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

我的数据框可从此处以rda文件的形式下载,并将其保存到您的工作目录中:https://drive.google.com/open?id=1eGJprHaXdoVhKNlGD5VcsoND7iyIoNwJ

代码语言:javascript
运行
复制
load(file = "QuestionData.rda")

当我尝试运行LCA时:

代码语言:javascript
运行
复制
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)

我得到以下错误:

代码语言:javascript
运行
复制
Error in dimnames(x) <- dn : 
length of 'dimnames' [2] not equal to array extent

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

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

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2019-12-28 09:55:36

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

代码语言:javascript
运行
复制
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

如果我没记错(不太熟悉您的数据),您可以使用相同的级别:

代码语言:javascript
运行
复制
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“,因为在示例数据中找不到它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59506814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档