运行 poLCA.entropy 时出现问题

问题描述 投票:0回答:1

我正在尝试使用 poLCA 包在 R 中进行 LCA 分析。我目前正在尝试获取数据的熵值,但我不断收到相同的消息: Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : 参数意味着不同的行数:2, 4, 3

我已经尝试从数据库中删除所有 NA,并创建一个新的数据库,而不会丢失案例。我正在将数据库从 xlsl 导出到 R 中。我也一直在尝试用方程运行数据,但它不起作用。我一直得到相同的输出。

#### Uploading database ####
LCA.women1=read.xlsx("LCA.women1.xlsx", sheet = 1)
str(LCA.women1)
LCA.women1=LCA.women1[,2:21]
### Running LCA ###
lcawomen1 <- poLCA(lcawperu, data=LCA.women1, maxiter=5000, tol=1e-10, na.rm=TRUE, probs.start=NULL, nclass=1, nrep=100, verbose=TRUE, calc.se=TRUE)
lcawomen2 <- poLCA(lcawperu, data=LCA.women1, maxiter=5000, tol=1e-10, na.rm=TRUE, probs.start=NULL, nclass=2, nrep=100, verbose=TRUE, calc.se=TRUE)
## Entropy measures ##
log(prod(sapply(lcawomen2$probs,ncol)))
p.hat <- lcawomen2$predcell$observed/lcawomen2$N
H.hat <- -sum(p.hat * log(p.hat))
H.hat
poLCA.entropy(lcawomen2)

### Entropy equations ###
entropy<-function (p) sum(-p*log(p))
error_prior <- entropy(lcawomen2$P)
error_post <- mean(apply(lcawomen2$posterior, 1, entropy))
R2_entropy <- (error_prior - error_post / error_prior)

### another method ###
results$R2_entropy
results[1,8]<-c("-")
error_prior<-entropy(lcawomen2$P) # class proportions model 2
error_post<-mean(apply(lcawomen2$posterior,1, entropy),na.rm = TRUE)
results[2,8]<-round(((error_prior-error_post) / error_prior),3)

这是运行 dput(head(LCAwomen2, 30)) 后我的数据结构。所有 NA 已更改为 3: 结构(列表(w1 = c(1, 2, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1), w2 = c(1, 1, 2, 1, 1, 2,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 = c(1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 3, 1, 1, 1, 2, 2, 1, 1), w14 = c(2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2), w15 = c(2, 2, 2, 2, 1, 2, 2, 1, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2), w16 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1), w17 = c(1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1), w18 = c(1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1), w19 = c(1, 1, 1, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1), w20 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, 30L), 类 = "data.frame")

r dataframe rows
1个回答
0
投票

你找到这个问题的答案了吗?我也遇到同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.