I am trying to implement a bernoulli mixture and was wondering how to vectorize the calculations correctly without looping.
I have tried various versions of apply but can't get the desired output (dim = c(5,4,2). Should my component parameters be in a list instead of a matrix?
set.seed(123)
#Data
X <- matrix(sample(c(0,1), 20, replace = TRUE, prob = c(.6, .4)),
nrow = 5, ncol = 4)
#Params
parameters <- matrix(runif(8), nrow = 2, ncol = 4)
#Would like to vectorize this
dbinom(X, 1, parameters[1,], log = TRUE)
dbinom(X, 1, parameters[2,], log = TRUE)
We loop through the rows of
parameters
withapply
and apply thedbinom
Or using
pmap