### Initialize and Select FlexMix Models Source: https://context7.com/cran/flexmix/llms.txt Use initFlexmix to perform multiple random initializations and compare models across different component counts. This helps identify the optimal model based on information criteria like AIC, BIC, and ICL. ```r library(flexmix) data("Nclus", package = "flexmix") # Run multiple initializations for k=4 set.seed(511) model_init <- initFlexmix(Nclus ~ 1, k = 4, model = FLXMCmvnorm(diagonal = FALSE), nrep = 5) # 5 random starts print(model_init) # Compare models with k = 2 to 5 components model_search <- initFlexmix(Nclus ~ 1, k = 2:5, model = FLXMCmvnorm(diagonal = FALSE), control = list(minprior = 0), nrep = 3) print(model_search) # Plot information criteria across models plot(model_search) # Get AIC, BIC, ICL values AIC(model_search) BIC(model_search) ICL(model_search) # Select model by different criteria model_bic <- getModel(model_search, which = "BIC") # Best BIC model_icl <- getModel(model_search, which = "ICL") # Best ICL model_k3 <- getModel(model_search, which = "3") # Exactly 3 components model_smallest <- getModel(model_search, which = 1) # Smallest k # Keep only unique number of fitted clusters (some may have dropped components) unique_models <- unique(model_search) print(unique_models) ``` -------------------------------- ### Fit Latent Class and Poisson Mixture Models Source: https://context7.com/cran/flexmix/llms.txt Demonstrates fitting a latent class model for binary data and a Poisson mixture model for count data. ```r set.seed(123) n <- 200 k <- 3 p <- 5 probs <- matrix(runif(k * p), nrow = k) # Item probabilities per class class <- sample(1:k, n, replace = TRUE) binary_data <- matrix(0, n, p) for (i in 1:n) { binary_data[i, ] <- rbinom(p, 1, probs[class[i], ]) } colnames(binary_data) <- paste0("item", 1:p) # Fit latent class model lca_model <- flexmix(binary_data ~ 1, k = 3, model = FLXMCmvbinary()) print(lca_model) # Parameters are item response probabilities per class parameters(lca_model) # Compare estimated vs true classifications table(clusters(lca_model), class) ``` ```r # Poisson clustering for count data set.seed(456) poisson_data <- cbind( rpois(100, lambda = 2), rpois(100, lambda = 5), rpois(100, lambda = 3) ) poisson_data <- rbind(poisson_data, cbind( rpois(100, lambda = 10), rpois(100, lambda = 1), rpois(100, lambda = 8) )) pois_model <- flexmix(poisson_data ~ 1, k = 2, model = FLXMCmvpois()) parameters(pois_model) # Estimated Poisson rates per component ``` -------------------------------- ### Fit Mixture of Regressions Source: https://context7.com/cran/flexmix/llms.txt Demonstrates fitting a mixture of regression models using flexmix and extracting parameters for specific components or multivariate models. ```r model <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, model = FLXMRglm(family = "gaussian")) parameters(model) parameters(model, component = 1) parameters(model, component = 2) multi_model <- flexmix(yn ~ x, data = NPreg, k = 2, model = list(FLXMRglm(yn ~ . + I(x^2)), FLXMRglm(yp ~ ., family = "poisson"))) parameters(multi_model, component = 1, model = 1) parameters(multi_model, component = 1, model = 2) NPreg$w <- rnorm(nrow(NPreg)) model_concom <- flexmix(yn ~ x, data = NPreg, k = 2, concomitant = FLXPmultinom(~ w)) parameters(model_concom, which = "concomitant") ``` -------------------------------- ### Model Selection with Information Criteria Source: https://context7.com/cran/flexmix/llms.txt Compares mixture models using AIC, BIC, ICL, and EIC to determine the optimal number of components. ```r library(flexmix) data("NPreg", package = "flexmix") models <- initFlexmix(yn ~ x + I(x^2), data = NPreg, k = 1:5, nrep = 3) aic_values <- AIC(models) bic_values <- BIC(models) icl_values <- ICL(models) eic_values <- EIC(models) comparison <- data.frame( k = 1:5, AIC = aic_values, BIC = bic_values, ICL = icl_values, EIC = eic_values ) print(comparison) plot(models, what = c("AIC", "BIC", "ICL")) best_icl <- getModel(models, which = "ICL") summary(best_icl) ``` -------------------------------- ### Latent Class Analysis Drivers in flexmix Source: https://context7.com/cran/flexmix/llms.txt Implement model-based clustering for binary data (Latent Class Analysis) and Poisson distributed data using drivers like `FLXMCmvbinary` and `FLXMCmvpois`. These assume conditional independence given component membership. ```R library(flexmix) # Binary data clustering (Latent Class Analysis) ``` -------------------------------- ### Bootstrap and Likelihood Ratio Tests Source: https://context7.com/cran/flexmix/llms.txt Performs parametric or empirical bootstrap for uncertainty estimation and likelihood ratio tests for comparing model components. ```r library(flexmix) data("NPreg", package = "flexmix") model <- initFlexmix(yn ~ x + I(x^2) | id2, data = NPreg, k = 2) set.seed(123) boot_result <- boot(model, R = 50, sim = "parametric", verbose = 1) print(boot_result) boot_emp <- boot(model, R = 50, sim = "empirical", verbose = 1) boot_params <- parameters(boot_result) dim(boot_params) boot_clusters <- clusters(boot_result, newdata = NPreg) lr_result <- LR_test(model, R = 100, alternative = "greater", verbose = 1) print(lr_result) lr_less <- LR_test(model, R = 100, alternative = "less", verbose = 1) ``` -------------------------------- ### Configure EM Algorithm with FLXcontrol Source: https://context7.com/cran/flexmix/llms.txt Adjusts EM algorithm hyperparameters such as iteration limits, convergence tolerance, and classification methods (weighted, hard, or random). ```r library(flexmix) data("NPreg", package = "flexmix") # View default control settings new("FLXcontrol") # iter.max: 200 # minprior: 0.05 # tolerance: 1e-06 # verbose: 0 # classify: "auto" # nrep: 1 # Create custom control via list coercion my_control <- list( iter.max = 500, # More iterations tolerance = 1e-8, # Stricter convergence minprior = 0.01, # Allow smaller components verbose = 10, # Report every 10 iterations classify = "weighted" # Soft assignment (default) ) model <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, control = my_control) ``` ```r # CEM (Classification EM) - faster convergence, local modes model_cem <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, control = list(classify = "hard")) # SEM (Stochastic EM) - avoids local modes, more exploration model_sem <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, control = list(classify = "random")) ``` -------------------------------- ### Refit Models for Statistical Inference Source: https://context7.com/cran/flexmix/llms.txt Refit a model to obtain variance-covariance estimates for standard errors and significance testing. Supported methods include optimization-based and M-step approaches. ```r library(flexmix) data("NPreg", package = "flexmix") # Fit initial model model <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2) # Refit using optim to get variance-covariance matrix model_refit <- refit(model, method = "optim") # Get significance tests for coefficients summary(model_refit) # Output: Component 1 and 2 coefficient tables with Estimate, Std. Error, z value, Pr(>|z|) # Typically one component shows all coefficients significant (quadratic relationship) # Other component shows only linear term significant # Refit using M-step method (faster, approximate) model_mstep <- refit(model, method = "mstep") summary(model_mstep) # Plot coefficient estimates with confidence intervals plot(model_refit) # Access specific model results summary(model_refit, model = 1, which = "model") ``` -------------------------------- ### Extract Posterior Probabilities and Assignments Source: https://context7.com/cran/flexmix/llms.txt Use posterior and clusters to retrieve component membership probabilities and hard assignments. These methods support both training data and new observations. ```r library(flexmix) data("NPreg", package = "flexmix") # Fit a mixture model model <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2) # Get posterior probabilities for all observations post_probs <- posterior(model) head(post_probs) # [,1] [,2] # [1,] 0.9998234 0.0001766 # [2,] 0.9995123 0.0004877 # ... # Get hard cluster assignments cluster_ids <- clusters(model) table(cluster_ids) # Verify clusters match maximum posterior all(cluster_ids == max.col(post_probs)) # TRUE # Get unscaled component likelihoods likelihoods <- posterior(model, unscaled = TRUE) head(likelihoods) # Predict for new data new_data <- data.frame(x = seq(-1, 1, length = 10)) new_post <- posterior(model, newdata = new_data) new_clusters <- clusters(model, newdata = new_data) ``` -------------------------------- ### Generate Random Samples from Fitted flexmix Model Source: https://context7.com/cran/flexmix/llms.txt Use `rflexmix()` to generate random samples from a fitted mixture model. This is useful for model checking and simulation studies. Ensure the model is fitted before calling this function. The output includes generated response values, component membership, and grouping factors if applicable. ```R library(flexmix) data("NPreg", package = "flexmix") # Fit a mixture model model <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2) # Generate samples from the fitted model set.seed(123) samples <- rflexmix(model) # Result contains: # - y: generated response values # - class: component membership for each sample # - group: grouping factor (if present in model) str(samples) # List of 3 # $ y : num [1:200] ... # $ class: int [1:200] 1 1 2 1 2 ... # $ group: NULL # Compare generated vs original data par(mfrow = c(1, 2)) plot(NPreg$x, NPreg$yn, main = "Original Data") plot(NPreg$x, samples$y, col = samples$class, main = "Simulated Data") # Generate for new covariate values new_x <- data.frame(x = seq(-2, 2, length = 100)) new_samples <- rflexmix(model, newdata = new_x) plot(new_x$x, new_samples$y, col = new_samples$class, main = "Predictions at New X Values") ``` -------------------------------- ### Reorder flexmix Components with relabel() Source: https://context7.com/cran/flexmix/llms.txt Use `relabel()` to reorder mixture components based on parameter values or a specified permutation. This is crucial for comparing models across different runs due to arbitrary component labeling. Components can be ordered by size, a specific coefficient, prior probabilities, or a manual permutation. ```R library(flexmix) # Generate data with known structure set.seed(123) beta <- matrix(1:16, ncol = 4) # 4 components, 4 parameters each print(beta) df <- ExLinear(beta, n = 100, sd = 0.5) model <- flexmix(y ~ ., data = df, k = 4) # Parameters may be in different order than true beta due to label switching round(parameters(model)) # Relabel by component size (default) model_size <- relabel(model) # Relabel by a specific coefficient model_relabel <- relabel(model, by = "model", which = "x1") round(parameters(model_relabel)) # Now components are ordered by their x1 coefficient # Relabel by prior probabilities model_prior <- relabel(model, by = "prior") # Manual relabeling with explicit permutation model_perm <- relabel(model, by = c(3L, 1L, 4L, 2L)) # Compare with true parameters after relabeling true_params <- rbind(beta, 0.5) # Add sigma summary(abs(as.vector(true_params - parameters(model_relabel)))) ``` -------------------------------- ### Perform Multivariate Gaussian Clustering with FLXMCmvnorm Source: https://context7.com/cran/flexmix/llms.txt FLXMCmvnorm is used for model-based clustering of continuous multivariate data, supporting both diagonal and full covariance matrices. ```r library(flexmix) library(MASS) data("Nclus", package = "flexmix") # Plot the data eqscplot(Nclus) # Cluster with diagonal covariance matrix (faster, more constrained) model_diag <- flexmix(Nclus ~ 1, k = 4, model = FLXMCmvnorm(diagonal = TRUE)) print(model_diag) # Cluster with full covariance matrix (more flexible) model_full <- flexmix(Nclus ~ 1, k = 4, model = FLXMCmvnorm(diagonal = FALSE)) print(model_full) # Extract parameters (mean vector and covariance matrix) parameters(model_full, component = 1) # Output: center (means) and cov (covariance matrix elements) # Get posterior probabilities for specific observations sample_obs <- sample(1:nrow(Nclus), 10) posteriors <- posterior(model_full)[sample_obs, ] print(posteriors) # Hard cluster assignments should match max posterior max.col(posteriors) clusters(model_full)[sample_obs] # Visualize clustering with confidence ellipses plotEll(model_full, Nclus) ``` -------------------------------- ### Perform Penalized Mixture Regression with FLXMRglmnet Source: https://context7.com/cran/flexmix/llms.txt Uses adaptive lasso or elastic net penalization for variable selection in mixture models. Requires consistent cross-validation folds across iterations. ```r library(flexmix) # Generate example data with sparse coefficients set.seed(12) p <- 10 # 10 predictors beta <- matrix(0, nrow = p + 1, ncol = 2) # 2 components beta[1, ] <- c(-1, 1) # Intercepts beta[5, 1] <- 1 # x4 active in component 1 beta[10, 2] <- 1 # x9 active in component 2 nobs <- 200 X <- matrix(rnorm(nobs * p), nobs, p) mu <- cbind(1, X) %*% beta z <- sample(1:2, nobs, replace = TRUE) y <- mu[cbind(1:nobs, z)] + rnorm(nobs) data <- data.frame(y = y, X) # Important: Use consistent cross-validation folds across EM iterations foldid <- sample(rep(1:10, length = nobs)) # Fit penalized mixture model model <- flexmix(y ~ ., data = data, k = 2, cluster = z, # Good initialization helps model = FLXMRglmnet(adaptive = TRUE, foldid = foldid), control = list(iter.max = 20)) # Extract sparse coefficients params <- parameters(model) print(params) ``` ```r # Non-adaptive lasso model_lasso <- flexmix(y ~ ., data = data, k = 2, cluster = z, model = FLXMRglmnet(adaptive = FALSE, foldid = foldid), control = list(iter.max = 20)) ``` -------------------------------- ### Simultaneous Inference for flexmix Models Source: https://context7.com/cran/flexmix/llms.txt Perform simultaneous inference using `flxglht()` to test hypotheses about coefficients, such as whether they are zero or equal across components. This function leverages the `multcomp` package for adjusted p-values. Consider refitting the model for more accurate inference. ```R library(flexmix) data("NPreg", package = "flexmix") # Fit mixture regression model <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, control = list(verbose = 5, iter.max = 100)) # Test if coefficients are significantly different from zero zero_test <- flxglht(model, "zero") print(zero_test) summary(zero_test) # Output: Simultaneous tests for all coefficients in all components # Adjusted p-values account for multiple comparisons # Test if coefficients are equal across components (Tukey contrasts) tukey_test <- flxglht(model, "tukey") print(tukey_test) summary(tukey_test) # Output: Pairwise comparisons between components # Tests whether coefficient differences are significant # E.g., "Comp2 - Comp1" for each coefficient # Can also use refitted model for more accurate inference model_refit <- refit(model, method = "optim") zero_refit <- flxglht(model_refit, "zero") summary(zero_refit) ``` -------------------------------- ### Fit Zero-Inflated Generalized Linear Models with flexmix Source: https://context7.com/cran/flexmix/llms.txt Fit mixture models with zero-inflated Poisson or binomial components using `FLXMRziglm()`. This is suitable for count data with excess zeros, where one component models zero-inflation and others model positive counts. The first component typically represents zero-inflation. ```R library(flexmix) data("dmft", package = "flexmix") # Zero-inflated Poisson mixture model <- FLXMRziglm(family = "poisson") fitted <- flexmix(End ~ log(Begin + 0.5) + Gender + Ethnic + Treatment, model = model, k = 2, data = dmft, control = list(minprior = 0.01)) # First component is the zero-inflation component # Second component is the Poisson regression # Get parameter estimates with significance tests result <- refit(fitted) summary(result) # Component 1: Zero-inflation (intercept fixed at -Inf, other coefs at 0) # Component 2: Poisson regression with estimated coefficients # Examine the prior probabilities prior(fitted) # Predict expected counts for new observations predictions <- predict(fitted, newdata = dmft) ``` -------------------------------- ### Extract Model Parameters Source: https://context7.com/cran/flexmix/llms.txt Retrieve estimated parameters from mixture components, including coefficients and variance parameters. ```r library(flexmix) data("NPreg", package = "flexmix") ``` -------------------------------- ### Fit and Analyze Mixture Models with flexmix Source: https://context7.com/cran/flexmix/llms.txt The flexmix function is the primary interface for fitting finite mixture models. It supports formula specifications, grouping factors, and various diagnostic methods. ```r library(flexmix) data("NPreg", package = "flexmix") # Basic mixture of two linear regression models model1 <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, control = list(verbose = 5, iter.max = 100)) # View model summary print(model1) # Output: 'flexmix' object with 2 components showing convergence info summary(model1) # Output: Shows prior probabilities, cluster sizes, and separation metrics # Extract cluster assignments cluster_assignments <- clusters(model1) table(cluster_assignments, NPreg$class) # 1 2 # 1 95 5 # 2 5 95 # Get fitted values from each component fitted_values <- fitted(model1) head(fitted_values) # Plot the mixture model results plot(model1) ``` -------------------------------- ### Fit Mixtures of Generalized Linear Models with FLXMRglm Source: https://context7.com/cran/flexmix/llms.txt FLXMRglm supports various families including Gaussian, binomial, Poisson, and Gamma. It allows for multivariate responses and grouped observations. ```r library(flexmix) data("NPreg", package = "flexmix") # Mixture of Gaussian linear models (default) gaussian_mix <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, model = FLXMRglm(family = "gaussian")) # Mixture of Poisson regression models poisson_mix <- flexmix(yp ~ x, data = NPreg, k = 2, model = FLXMRglm(family = "poisson")) # Extract parameters - Gaussian includes coefficients and sigma parameters(gaussian_mix, component = 1) # Output: coef.(Intercept), coef.x, coef.I(x^2), sigma # Extract parameters - Poisson shows only coefficients parameters(poisson_mix, component = 1) # Output: coef.(Intercept), coef.x # Multivariate response: Gaussian + Poisson multi_mix <- flexmix(yn ~ x, data = NPreg, k = 2, model = list(FLXMRglm(yn ~ . + I(x^2)), FLXMRglm(yp ~ ., family = "poisson"))) # Binomial example with grouped observations set.seed(1234) binomial_mix <- initFlexmix(cbind(yb, 1 - yb) ~ x | id2, data = NPreg, k = 2, model = FLXMRglm(family = "binomial"), nrep = 5) table(NPreg$class, clusters(binomial_mix)) ``` -------------------------------- ### Model Concomitant Variables Source: https://context7.com/cran/flexmix/llms.txt Apply FLXPmultinom to allow component membership probabilities to vary based on observed covariates. This approach models heterogeneity in cluster assignment. ```r library(flexmix) data("NPreg", package = "flexmix") # Constant priors (default) model_const <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, concomitant = FLXPconstant()) # Prior probabilities depend on covariate 'w' # Simulated covariate for demonstration NPreg$w <- rnorm(nrow(NPreg)) model_concom <- flexmix(yn ~ x + I(x^2), data = NPreg, k = 2, concomitant = FLXPmultinom(~ w)) # Compare models summary(model_const) summary(model_concom) # Extract concomitant model parameters parameters(model_concom, which = "concomitant") # Prior probabilities now vary by observation prior_probs <- prior(model_concom) head(prior_probs) # Each row shows component probabilities based on that observation's w value ``` -------------------------------- ### Compute Kullback-Leibler Divergence Source: https://context7.com/cran/flexmix/llms.txt Calculates KL divergence between mixture components to assess similarity and potential for merging. ```r library(flexmix) data("Nclus", package = "flexmix") model <- flexmix(Nclus ~ 1, k = 4, model = FLXMCmvnorm(diagonal = FALSE)) kl_matrix <- KLdiv(model) print(kl_matrix) kl_discrete <- KLdiv(model, method = "discrete") x <- seq(-3, 3, length = 200) densities <- cbind(uniform = dunif(x), normal = dnorm(x), t_dist = dt(x, df = 10)) matplot(x, densities, type = "l") KLdiv(densities) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.