I perform a latent class analysis to a dataset of binary variables with

alz <- data.blca(Alzheimer)
sj3.em <- blca.em(alz, 3)

Now I want to label my observations of Alzheimer given the likelihood of each belonging to one particular group.

I extract the estimate of class membership for each unique datapoint

class_memb <- sj3.em$Z

and label each unique datapoint according to the highest probability

labels_df <- apply(class_memb, 1, function(x) sample(names(x), 1, prob=x))

I collapse each row of my original dataset Alzheimer into a string of 1 and 0

row_sequence <- apply(compl_cases_all, 1, 
                      function(x) as.factor(paste0(x, collapse='')))

and finally I replace the sequence of each observation with my max probability

grouping <- labels_df[row_sequence]

And yet the classprob and my labelling don't match. And then when I test it on a larger dataset the gap between class probabilities and proportion of labels is even wider. Am I doing something wrong?

# [1] 0.50162847 0.47880396 0.01956757
# grouping
#    Group 1    Group 2    Group 3 
#  0.54166667 0.43750000 0.02083333 

NOTE: Question crossposted here. Didn't receive much attention there and probably CrossValidated is more appropriate.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.