### Configure numerical integration algorithms Source: https://context7.com/cran/mvtnorm/llms.txt Illustrates the configuration and usage of GenzBretz, Miwa, and TVPACK algorithms for calculating multivariate probabilities. These algorithms allow users to balance computational speed with precision requirements in normal and t-distributions. ```r # GenzBretz algorithm algo_gb <- GenzBretz(maxpts = 50000, abseps = 0.0001, releps = 0) prob_gb <- pmvnorm(lower = rep(-2, 5), upper = rep(2, 5), mean = rep(0, 5), corr = diag(5), algorithm = algo_gb) # Miwa algorithm algo_miwa <- Miwa(steps = 256, checkCorr = TRUE) prob_miwa <- pmvnorm(lower = rep(-1, 3), upper = rep(1, 3), mean = rep(0, 3), corr = diag(3), algorithm = algo_miwa) # TVPACK algorithm algo_tv <- TVPACK(abseps = 0.001) prob_tv <- pmvt(lower = rep(-1, 4), upper = rep(1, 4), df = 5, corr = diag(4), algorithm = algo_tv) ``` -------------------------------- ### Simulate multivariate normal samples Source: https://context7.com/cran/mvtnorm/llms.txt Demonstrates how to generate random samples from a defined multivariate normal object using the simulate function, followed by verification of sample dimensions and mean vector convergence. ```r simulated <- simulate(mvn, nsim = 100) dim(simulated) # 4 x 100 colMeans(simulated) # Should be close to mean_vec ``` -------------------------------- ### Analyze Marginal and Conditional Distributions Source: https://context7.com/cran/mvtnorm/llms.txt Shows how to create multivariate normal objects and compute marginal or conditional distributions. This is useful for slicing multidimensional distributions based on specific variable subsets or known values. ```R mvn <- mvnorm(mean = mean_vec, chol = chol_mat) mvn_marg <- margDist(mvn, which = c(1, 3)) mvn_cond <- condDist(mvn, which_given = 1, given = matrix(1.5, nrow = 1, ncol = 1)) ll <- logLik(mvn, obs = obs) ``` -------------------------------- ### Generate Multivariate Normal Random Samples Source: https://context7.com/cran/mvtnorm/llms.txt Demonstrates how to generate random samples from a multivariate normal distribution using the rmvnorm function. It supports various decomposition methods such as eigen, cholesky, and svd to handle different covariance matrix structures. ```r library(mvtnorm) # Generate 1000 samples from 3-dimensional MVN mean_vector <- c(1, 2, 3) cov_matrix <- matrix(c(1, 0.5, 0.3, 0.5, 1, 0.4, 0.3, 0.4, 1), nrow = 3) samples <- rmvnorm(n = 1000, mean = mean_vector, sigma = cov_matrix) # Using different decomposition methods samples_eigen <- rmvnorm(100, mean = c(0, 0), sigma = diag(2), method = "eigen") samples_chol <- rmvnorm(100, mean = c(0, 0), sigma = diag(2), method = "chol") samples_svd <- rmvnorm(100, mean = c(0, 0), sigma = diag(2), method = "svd") ``` -------------------------------- ### Compute Likelihood for Mixed and Censored Data Source: https://context7.com/cran/mvtnorm/llms.txt Demonstrates the use of ldpmvnorm for calculating likelihoods with mixed exact and censored data points. It requires defining observation matrices and lower/upper bounds for censored intervals, returning the log-likelihood values. ```R obs_mixed <- matrix(rnorm(2 * N), nrow = 2, ncol = N) lower_mixed <- matrix(-0.5, nrow = 1, ncol = N) upper_mixed <- matrix(0.5, nrow = 1, ncol = N) L_full <- ltMatrices(diag(3), diag = TRUE, byrow = TRUE) ll_mixed <- ldpmvnorm(obs = obs_mixed, lower = lower_mixed, upper = upper_mixed, mean = 0, chol = L_full, M = 500) print(ll_mixed) ``` -------------------------------- ### Convert Covariance and Correlation Parameterizations Source: https://context7.com/cran/mvtnorm/llms.txt Provides utility functions for converting between Cholesky factors, inverse Cholesky factors, covariance matrices, and correlation matrices. These transformations are essential for changing statistical model parameterizations. ```R cov_from_chol <- chol2cov(L) cor_from_chol <- chol2cor(L) prec_from_invchol <- invchol2pre(invL) L_std <- standardize(chol = L) ``` -------------------------------- ### Find Multivariate Normal Quantiles Source: https://context7.com/cran/mvtnorm/llms.txt Determines equicoordinate quantiles for multivariate normal distributions using qmvnorm. It supports both lower-tail and two-tailed quantile evaluations for various probability thresholds. ```r mean <- c(0, 0, 0) corr <- diag(3) # Find the quantile q such that P(|X_i| <= q for all i) = 0.95 quantile_result <- qmvnorm(p = 0.95, tail = "both.tails", mean = mean, corr = corr) # Lower tail quantile quantile_lower <- qmvnorm(p = 0.95, tail = "lower.tail", mean = c(0, 0), corr = matrix(c(1, 0.5, 0.5, 1), 2, 2)) ``` -------------------------------- ### Manipulate Lower Triangular Matrices Source: https://context7.com/cran/mvtnorm/llms.txt Illustrates the use of the ltMatrices class for representing multiple lower triangular matrices. Includes operations for matrix creation, array conversion, matrix multiplication, and solving linear systems. ```R L <- ltMatrices(matrix(values, nrow = 1, ncol = 3), diag = FALSE, byrow = TRUE, names = c("A", "B")) result <- Mult(L1, matrix(1:4, nrow = 2)) x <- solve(L1, matrix(c(1, 2), nrow = 2)) ``` -------------------------------- ### Generate Multivariate t Random Samples Source: https://context7.com/cran/mvtnorm/llms.txt Generate random samples from a multivariate t distribution using either shifted or Kshirsagar parameterizations. The df parameter controls tail thickness, with Inf representing a multivariate normal distribution. ```r n <- 500 df <- 5 delta <- c(1, 2) sigma <- matrix(c(1, 0.6, 0.6, 1), nrow = 2) samples_shifted <- rmvt(n = n, sigma = sigma, df = df, delta = delta, type = "shifted") samples_kshir <- rmvt(n = n, sigma = sigma, df = df, delta = delta, type = "Kshirsagar") ``` -------------------------------- ### Log-likelihood for Censored Multivariate Normal Data Source: https://context7.com/cran/mvtnorm/llms.txt Compute log-likelihoods for exact or interval-censored observations in multivariate Gaussian models. Utilizes Cholesky factorization and supports Monte Carlo methods for interval integration. ```r J <- 3 L <- ltMatrices(diag(J), diag = TRUE, byrow = TRUE) ll_exact <- ldmvnorm(obs = matrix(rnorm(J * 5), nrow = J), mean = rep(0, J), chol = L) ll_censored <- lpmvnorm(lower = matrix(-1, nrow = J, ncol = 5), upper = matrix(1, nrow = J, ncol = 5), mean = rep(0, J), chol = L, M = 1000) ``` -------------------------------- ### Compute Multivariate t Density Source: https://context7.com/cran/mvtnorm/llms.txt Calculate the probability density function (PDF) or log-density for multivariate t distributions. Supports vectorization for multiple observations and comparison with normal distributions by setting df=Inf. ```r x <- matrix(c(1, 2, 0, 1, 2, 3), nrow = 3, byrow = TRUE) densities_t <- dmvt(x, delta = c(1, 2), sigma = diag(2), df = 5, log = FALSE) log_densities_t <- dmvt(x, delta = c(1, 2), sigma = diag(2), df = 5, log = TRUE) ``` -------------------------------- ### Calculate Multivariate t and Normal Quantiles Source: https://context7.com/cran/mvtnorm/llms.txt Compute upper and equicoordinate quantiles for multivariate distributions. These functions support both standard and Kshirsagar parameterizations for bivariate and higher-dimensional distributions. ```r quantile_upper <- qmvnorm(p = 0.05, tail = "upper.tail", mean = c(0, 0), corr = matrix(c(1, 0.3, 0.3, 1), 2, 2)) print(quantile_upper$quantile) quantile_t <- qmvt(p = 0.95, tail = "both.tails", df = 4, delta = c(0, 0), corr = matrix(c(1, 0.3, 0.3, 1), nrow = 2)) print(quantile_t$quantile) ``` -------------------------------- ### Evaluate Multivariate Normal Density Source: https://context7.com/cran/mvtnorm/llms.txt Calculates the probability density function for multivariate normal distributions using dmvnorm. Users can compute standard densities or log-densities for improved numerical stability. ```r x <- matrix(c(1, 2, 3, 0, 1, 2, 2, 3, 4), nrow = 3, byrow = TRUE) mean_vec <- c(1, 2, 3) cov_mat <- diag(3) # Compute densities densities <- dmvnorm(x, mean = mean_vec, sigma = cov_mat) # Get log densities for numerical stability log_densities <- dmvnorm(x, mean = mean_vec, sigma = cov_mat, log = TRUE) ``` -------------------------------- ### Compute Multivariate Normal Cumulative Distribution Source: https://context7.com/cran/mvtnorm/llms.txt Uses pmvnorm to calculate probabilities over specific rectangular regions. It supports specifying either covariance or correlation matrices and allows fine-tuning of integration algorithms like GenzBretz. ```r mean <- c(0, 0) corr_matrix <- matrix(c(1, 0.5, 0.5, 1), nrow = 2) # Compute P(X1 <= 1, X2 <= 1) prob <- pmvnorm(lower = c(-Inf, -Inf), upper = c(1, 1), mean = mean, corr = corr_matrix) # Using GenzBretz algorithm with custom parameters prob_precise <- pmvnorm(lower = c(-1, -1), upper = c(1, 1), mean = c(0, 0), corr = corr_matrix, algorithm = GenzBretz(maxpts = 50000, abseps = 0.0001, releps = 0)) ``` -------------------------------- ### Perform Score Function Gradient Estimation Source: https://context7.com/cran/mvtnorm/llms.txt Calculates score functions (gradients) for likelihood-based inference using sldmvnorm and slpmvnorm. These functions compute derivatives with respect to mean, Cholesky factors, and interval bounds for normal and censored distributions. ```R scores <- sldmvnorm(obs = obs, mean = true_mean, chol = true_chol) scores_censored <- slpmvnorm(lower = lower, upper = upper, mean = 0, chol = true_chol, M = 500) print(scores$logLik) print(scores_censored$mean) ``` -------------------------------- ### Compute Multivariate t Cumulative Distribution Source: https://context7.com/cran/mvtnorm/llms.txt Calculate the cumulative distribution function (CDF) for multivariate t distributions to evaluate probabilities over hyper-rectangular regions. The result depends on the degrees of freedom and correlation matrix. ```r prob_t <- pmvt(lower = c(-1, -1), upper = c(1, 1), delta = c(0, 0), df = 5, corr = matrix(c(1, 0.5, 0.5, 1), nrow = 2), type = "shifted") print(prob_t) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.