I'm trying to understand flexmix, and specifically what I am doing wrong trying to fit the simplest conceivable binomial mixture model (mixture of two intercept-only models).
set.seed(42)
data=data.frame(class=rbinom(1000,size=1,prob=0.3)) %>% # 30% in class 1, 70% in class 0
mutate(yes=map_dbl(class,~ifelse(.x,rbinom(1,1,prob=0.8),rbinom(1,1,prob=0.4)))) # class 1 = 80%, class 2 = 40%
# Algebraic
(mean(data$yes==1)-0.4)/(0.8-0.4)
# = 0.295 this is what the model should recover, right?
library(flexmix)
mo1=FLXMRglm(offset=rep(log(.4/.6), nrow(data)),family="binomial")
mo2=FLXMRglm(offset=rep(log(.8/.2), nrow(data)),family="binomial")
flexfit <- flexmix(cbind(yes,1-yes) ~ -1, data=data, k=2, model=list(mo1, mo2))
flexfit
summary(flexfit)
This code is returning all data points as belonging to the first class. Am I setting up the model incorrectly? Or is there a more fundamental misunderstanding that I have about the way mixture models work?