I am trying to conduct an LCA analysis in R using the poLCA package. I am currently trying to get the entropy values for my data, but I keep getting the same message: Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 2, 4, 3
I have already tried deleting all the NAs from my database and created a new database without the missing cases. I am exporting my database from xlsl into R. I have also been trying to run the data with equations, but it doesn't work. I keep getting the same output.
#### 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$NH.hat <- -sum(p.hat * log(p.hat))H.hatpoLCA.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_entropyresults[1,8]<-c("-")error_prior<-entropy(lcawomen2$P) # class proportions model 2error_post<-mean(apply(lcawomen2$posterior,1, entropy),na.rm = TRUE)results[2,8]<-round(((error_prior-error_post) / error_prior),3)
This is the structure of my data after running dput(head(LCAwomen2, 30)). All NAs have been changed into 3:structure(list(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, 1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2), w3 = c(2, 1, 1, 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