## Generate Data
Total <- 30 #30 Individuals
J <- 2 # 2 variables
dist <- rep("multinomial",J) #both variables are multinomial
Rj <- rep(100,J) #100 repititions for each variable
#Nijr will always be 1 for multinomials and bernoulli's
Nijr <- array(1, dim = c(Total, J, max(Rj)))
K <- 4 # 4 sub-populations
alpha <- rep(.5, K) #hyperparameter for dirichlet distribution
Vj <- rep(5, J) #each multinomial has 5 options
theta <- array(0, dim = c(J, K, max(Vj)))
theta[1,,] <- gtools::rdirichlet(K, rep(.3, 5))
theta[2,,] <- gtools::rdirichlet(K, rep(.3, 5))
lambda <- gtools::rdirichlet(Total, rep(.6,K))
obs = array(0, dim = c(Total, J, max(Rj), max(Nijr)))
for(i in 1:Total)
{
for(j in 1:J)
{
for(r in 1:Rj[j])
{
for(n in Nijr[i,j,r])
{
# sub-population which governs the multinomial
sub.pop <- sample.int(K, size = 1, prob = lambda[i,])
#Note that observations must be from 0:(Vj-1)
obs[i,j,r,n] <- sample.int(Vj[j], size = 1,prob = theta[j,sub.pop,])-1 }
}
}
}
## Initialize a mixedMemModel object
test_model <- mixedMemModel(Total = Total, J = J,Rj = Rj, Nijr= Nijr,
K = K, Vj = Vj,dist = dist, obs = obs,
alpha = alpha, theta = theta+0)
## Fit the mixed membership model
out <-mmVarFit(test_model)Run the code above in your browser using DataLab