################################################################################## # Load the flexmix package library(flexmix) # Load the CO_2 data data(CO2data,package="mixtools") # Fit a mixture of experts model with 50 random starting values. # The highest BIC value is stored as bicval #ÊThe best fitting model is stored as bestfit bicval <- Inf itermax <- 50 for (iter in 1:itermax) { fit <- flexmix(CO2~GNP,data=CO2data,k=2, concomitant = FLXPmultinom(~GNP)) if (bicval>BIC(fit)) { bicval <- BIC(fit) bestfit <- fit print(c(iter,bicval)) } } # Explore the fitted model summary(bestfit) parameters(bestfit) # Explore the gating network part of the model summary(bestfit,which="concomitant") parameters(bestfit,which="concomitant") ################################################################################## # Set seed of random number generator. set.seed(1) # Load the mixtools package library(mixtools) # Load the CO_2 data data(CO2data) # Fit a mixture of experts model with 50 random starting values. # The highest BIC value is stored as bicval # The best fitting model is stored as bestfit bicval <- -Inf itermax <- 50 for (iter in 1:itermax) { G <- 2 fit <- regmixEM(CO2data$CO2,CO2data$GNP,k=G) n <- nrow(CO2data) p <- nrow(fit$beta)*G+G+(G-1) fitbic <- 2*fit$loglik - log(n)*p if (bicvalBIC(fit1)) { bicval <- BIC(fit1) bestfit <- fit1 print(c(iter,bicval)) } } # Explore the fitted model. summary(bestfit) clusters(bestfit) parameters(bestfit) # Explore the gating network model. summary(bestfit,which="concomitant") parameters(bestfit,which="concomitant") ################################################################################## # Load the flexmix package library(flexmix) # Load the wine data library(MBCbook) data(wine27) # Select the physical and chemical measurements as the outcome # Select wine type and year as possible covariates wine <- as.matrix(wine27[,1:27]) type <- wine27[,"Type"] year <- wine27[,"Year"] # Fit a mixture of experts model with 50 random starting values. # The highest BIC value is stored as bicval. # The best fitting model is stored as bestfit. bicval <- Inf itermax <- 50 for (iter in 1:itermax) { fit <- flexmix(wine~1,model=FLXMCmvnorm(diagonal=TRUE),k=3,concomitant=FLXPmultinom(~type)) if (bicval>BIC(fit)) { bicval <- BIC(fit) bestfit <- fit print(c(iter,bicval)) } } # Explore the fitted model summary(bestfit) clusters(bestfit) parameters(bestfit) # Explore the gating network of the model summary(bestfit,which="concomitant") parameters(bestfit,which="concomitant") ################################################################################## ################################################################################## ##################################################################################