### Install qgcomp R Package Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Installs the qgcomp package from CRAN or the development version from GitHub. Includes loading the package and example data. ```r # Install from CRAN install.packages("qgcomp") # Install development version from GitHub # install.packages("devtools") devtools::install_github("alexpkeil1/qgcomp") # Load the package library("qgcomp") # Load example data included with package data("metals", package = "qgcomp") head(metals) # y arsenic barium cadmium calcium chromium copper ... # 1 0.38232 0.0234 0.0451 -0.8561 1.2381 0.3241 -0.1239 ... # 2 -0.18234 -0.1842 0.2941 -0.9412 0.8124 -0.5123 0.3842 ... ``` -------------------------------- ### Install QGcomp Package Versions Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md Demonstrates how to install different versions of the qgcomp R package, including the developer version from GitHub and the stable version from CRAN. Requires the 'devtools' package for GitHub installations. ```R install.packages("devtools") devtools::install_github("alexpkeil1/qgcomp") # master version (usually reliable) devtools::install_github("alexpkeil1/qgcomp@dev") # dev version (may not be working) # or install version from CRAN install.packages("qgcomp") ``` -------------------------------- ### Bootstrapping for Population Average Risk Ratio with qgcomp.boot Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md This example shows how to use `qgcomp.boot` for bootstrapping to estimate the population average risk ratio via g-computation. It's suitable for binary outcomes and provides bootstrap confidence intervals for the mixture log(RR). The function requires a formula, exposure names, data, family, number of bootstrap iterations (B), and optionally a seed and a flag for calculating risk ratios (rr). ```r results4 = qgcomp.boot(disease_state~., expnms=Xnm, data = metals[,c(Xnm, 'disease_state')], family=binomial(), q=4, B=1000, seed=125, rr=TRUE) print(results4) # checking whether model fit seems appropriate p4 = plot(results4, suppressprint=TRUE) ggplot2::ggsave("res4.png", plot=p4, dpi=72, width=600/72, height=350/72, units="in") ``` -------------------------------- ### qgcomp Main Wrapper Function for Continuous and Binary Outcomes Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Demonstrates the main qgcomp wrapper function, which automatically selects the appropriate estimation method (noboot, boot, cox) based on the model family. It handles continuous (gaussian) and binary (binomial) outcomes, showing example usage and output interpretation. ```r library("qgcomp") data("metals", package = "qgcomp") # Define exposure mixture components Xnm <- c('arsenic', 'barium', 'cadmium', 'calcium', 'chromium', 'copper', 'iron', 'lead', 'magnesium', 'manganese', 'mercury', 'selenium', 'silver', 'sodium', 'zinc') # Continuous outcome - automatically uses qgcomp.glm.noboot (fast) results <- qgcomp(y ~ ., expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4) print(results) # Scaled effect size (positive direction, sum of positive coefficients = 0.39) # calcium iron barium silver arsenic mercury sodium chromium # 0.72216 0.06187 0.05947 0.03508 0.03447 0.02451 0.02162 0.02057 # # Mixture slope parameters (Delta method CI): # Estimate Std. Error Lower CI Upper CI t value Pr(>|t|) # (Intercept) -0.35667 0.107878 -0.56811 -0.14523 -3.3062 0.0010238 # psi1 0.26639 0.071025 0.12719 0.40560 3.7507 0.0002001 # Binary outcome - automatically uses bootstrap for risk ratio results_binary <- qgcomp(disease_state ~ ., expnms = Xnm, data = metals[, c(Xnm, 'disease_state')], family = binomial(), q = 4, B = 200, seed = 125) print(results_binary) # Mixture log(RR) (bootstrap CI): # Estimate Std. Error Lower CI Upper CI Z value Pr(>|z|) # (Intercept) -0.56237 0.23773 -1.02832 -0.09642 -2.3655 0.0180 # psi1 -0.16373 0.17239 -0.50161 0.17416 -0.9497 0.3423 ``` -------------------------------- ### Survival Outcomes without Bootstrapping with qgcomp.cox.noboot Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md This example demonstrates fitting a marginal structural Cox model for survival outcomes without bootstrapping using `qgcomp.cox.noboot`. It estimates scaled effect sizes and mixture log(hazard ratio) using the Delta method for confidence intervals. The input requires a survival formula, exposure names, and data containing time and event status. ```r results6 = qgcomp.cox.noboot(Surv(disease_time, disease_state)~., expnms=Xnm, metals[,c(Xnm, 'disease_time', 'disease_state')]) print(results6) ``` -------------------------------- ### Get Model Confidence Bands with qgcomp Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Calculates and prints the confidence bands for a model using the `modelbound.boot` function from the qgcomp package. This function is typically used for bootstrapping model results to estimate uncertainty. ```r mcb <- modelbound.boot(qc_linear, alpha = 0.05) print(mcb) ``` -------------------------------- ### Get Model Bounds and Pointwise Comparisons with qgcomp.glm.ee Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Provides functions to extract model bounds and perform pointwise comparisons for models fitted using the estimating equations method (`qgcomp.glm.ee`). `modelbound.ee` retrieves overall bounds, while `pointwisebound.noboot` allows for comparisons against a specified reference quantile. ```r modelbound.ee(qc_ee) pointwisebound.noboot(qc_ee, pointwiseref = 2) # Compare to 2nd quantile ``` -------------------------------- ### Get Survival Curve Data using qgcomp.survcurve.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Calculates survival curve data from a bootstrapped Cox proportional hazards model. It provides average survival curves from both a marginal structural model (MSM) and a conditional model, as well as survival by quantile for both models. Requires a pre-fit bootstrapped Cox model object. ```r surv_curves <- qgcomp.survcurve.boot(qc_cox_boot) # surv_curves$mdfpop - Average survival curve from MSM # surv_curves$cdfpop - Population average from conditional model # surv_curves$mdfq - Survival by quantile (MSM) # surv_curves$cdfq - Survival by quantile (conditional) ``` -------------------------------- ### Interactions and Non-linear Terms with qgcomp.boot Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md This snippet illustrates how to incorporate interactions and non-linear terms into the model using `qgcomp.boot`. The `degree` argument allows for specifying polynomial terms, and interactions can be defined directly in the formula. It outputs bootstrap confidence intervals for mixture slope parameters and includes plotting functionality. ```r results5 = qgcomp(y~. + .^2 + arsenic*cadmium, expnms=Xnm, metals[,c(Xnm, 'y')], family=gaussian(), q=4, B=10, seed=125, degree=2) print(results5) p5 = plot(results5, suppressprint=TRUE) ggplot2::ggsave("res5.png", plot=p5, dpi=72, width=600/72, height=350/72, units="in") ``` -------------------------------- ### Parallel Processing for Bootstrapping in qgcomp.glm.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Demonstrates how to enable parallel processing for the `qgcomp.glm.boot` function to speed up bootstrap calculations. This is achieved by setting `parallel = TRUE` and `parplan = TRUE`. Requires formula, exposure names, data, family, q, B, and seed. ```r qc_parallel <- qgcomp.glm.boot( f = y ~ . + .^2, expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4, B = 500, parallel = TRUE, parplan = TRUE, seed = 125 ) ``` -------------------------------- ### Compare qgcomp Models and Extract Diagnostics Source: https://context7.com/alexpkeil1/qgcomp/llms.txt This section details how to compare different qgcomp model fits using AIC and BIC, and how to extract various diagnostic and model information, including coefficients, variance-covariance matrices, confidence intervals, and specific qgcomp output like psi, weights, and breakpoints. Dependencies include the qgcomp package and its data. ```r library("qgcomp") data("metals", package = "qgcomp") Xnm <- c('arsenic', 'barium', 'cadmium', 'calcium', 'chromium', 'copper', 'iron', 'lead', 'magnesium', 'manganese', 'mercury', 'selenium', 'silver', 'sodium', 'zinc') # Fit models with different specifications qc_linear <- qgcomp.glm.boot(y ~ ., expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4, B = 100, seed = 125) qc_nonlin <- qgcomp.glm.boot(y ~ . + .^2, expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4, degree = 2, B = 100, seed = 125) # Compare using AIC/BIC on underlying fit AIC(qc_linear$fit) AIC(qc_nonlin$fit) BIC(qc_linear$fit) BIC(qc_nonlin$fit) # Extract model information coef(qc_linear) # Coefficients vcov(qc_linear) # Variance-covariance matrix confint(qc_linear) # Confidence intervals logLik(qc_linear$fit) # Log-likelihood # Access detailed results qc_linear$psi # Mixture effect estimate qc_linear$ci # Confidence interval qc_linear$pos.weights # Positive direction weights qc_linear$neg.weights # Negative direction weights qc_linear$pos.size # Sum of positive coefficients qc_linear$neg.size # Sum of negative coefficients qc_linear$expnms # Exposure names qc_linear$q # Number of quantiles qc_linear$breaks # Quantile breakpoints # Get pointwise bounds pwb <- pointwisebound.boot(qc_linear, pointwiseref = 1, alpha = 0.05) print(pwb) ``` -------------------------------- ### Fit and Plot Dose-Response Curves with qgcomp Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Demonstrates fitting dose-response curves for continuous and binary outcomes using qgcomp.glm.boot. It also shows how to customize plots, save them as ggplot objects, and save them to files. Dependencies include the qgcomp package. ```r library(qgcomp) data(metals, package = "qgcomp") Xnm <- c('arsenic', 'barium', 'cadmium', 'calcium', 'chromium', 'copper', 'iron', 'lead', 'magnesium', 'manganese', 'mercury', 'selenium', 'silver', 'sodium', 'zinc') # Boot method - dose-response curve qc_boot <- qgcomp.glm.boot(y ~ ., expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4, B = 200, seed = 125) # Full plot with all features plot(qc_boot, pointwisebars = TRUE, # Show pointwise comparison error bars modelfitline = TRUE, # Show MSM regression line modelband = TRUE, # Show model confidence band flexfit = TRUE, # Show smoothed conditional fit pointwiseref = 2) # Reference quantile for comparisons # Suppress printing and save as ggplot object for customization p <- plot(qc_boot, suppressprint = TRUE) p + ggtitle("Mixture Effect on Outcome") + theme_minimal() + coord_cartesian(ylim = c(-1, 1)) # Save plot ggsave("qgcomp_results.png", plot = p, width = 8, height = 6, dpi = 300) # Binary outcome with log scale qc_binary <- qgcomp.glm.boot(disease_state ~ ., expnms = Xnm, data = metals[, c(Xnm, 'disease_state')], family = binomial(), q = 4, B = 200, rr = TRUE, seed = 125) plot(qc_binary) # Y-axis shows Pr(Y=1) on log scale ``` -------------------------------- ### Binary Outcome Analysis with qgcomp.noboot Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md Conducts quantile g-computation for a binary outcome using qgcomp.noboot. It specifies the outcome variable, exposure names, data, and family as binomial. The 'q' argument controls the number of quantiles. Output includes scaled effect sizes and mixture log(OR). ```R library("qgcomp") data("metals", package="qgcomp") Xnm <- c( 'arsenic','barium','cadmium','calcium','chromium','copper', 'iron','lead','magnesium','manganese','mercury','selenium','silver', 'sodium','zinc' ) # binary outcome results2 = qgcomp.noboot(disease_state~., expnms=Xnm, data = metals[,c(Xnm, 'disease_state')], family=binomial(), q=4) print(results2) p2 = plot(results2, suppressprint=TRUE) ggplot2::ggsave("res2.png", plot=p2, dpi=72, width=600/72, height=350/72, units="in") ``` -------------------------------- ### Survival Outcomes with Bootstrapping with qgcomp.cox.boot Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md This snippet shows how to perform bootstrapping for survival outcomes using `qgcomp.cox.boot` to estimate the hazard ratio. It fits a marginal structural Cox model and provides bootstrap confidence intervals. The function requires a survival formula, exposure names, data, number of bootstrap iterations (B), and Monte Carlo sample size (MCsize) for variance estimation. ```r results7 = qgcomp.cox.boot(Surv(disease_time, disease_state)~., expnms=Xnm, metals[,c(Xnm, 'disease_time', 'disease_state')], B=10, MCsize=5000) p7 = plot(results7, suppressprint=TRUE) ggplot2::ggsave("res7.png", plot=p7, dpi=72, width=600/72, height=350/72, units="in") ``` -------------------------------- ### Adjusting for Covariates with qgcomp.noboot Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md This snippet demonstrates how to use the `qgcomp.noboot` function to fit a regression model while adjusting for covariates. It calculates scaled effect sizes and mixture slope parameters using the Delta method for confidence intervals. The input is a formula, exposure names, data, and family distribution. ```r results3 = qgcomp.noboot(y ~ mage35 + arsenic + barium + cadmium + calcium + chloride + chromium + copper + iron + lead + magnesium + manganese + mercury + selenium + silver + sodium + zinc, expnms=Xnm, metals, family=gaussian(), q=4) print(results3) # coefficient for confounder results3$fit$coefficients['mage35'] ``` -------------------------------- ### Plotting Dose-Response Curve with qgcomp.glm.boot Results Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Generates a dose-response curve with confidence intervals based on the results from a `qgcomp.glm.boot` model. This visualization helps interpret the estimated marginal effects of the exposure mixture. ```r plot(qc_rr) # Dose-response curve with CI ``` -------------------------------- ### Continuous Outcome Analysis with qgcomp.noboot Source: https://github.com/alexpkeil1/qgcomp/blob/main/README.md Performs quantile g-computation for a continuous outcome using the qgcomp.noboot function. It takes a formula, data, and specifies the family as Gaussian. The output includes scaled effect sizes and mixture slope parameters. ```R library("qgcomp") data("metals", package="qgcomp") Xnm <- c( 'arsenic','barium','cadmium','calcium','chromium','copper', 'iron','lead','magnesium','manganese','mercury','selenium','silver', 'sodium','zinc' ) # continuous outcome results = qgcomp.noboot(y~.,dat=metals[,c(Xnm, 'y')], family=gaussian()) print(results) p = plot(results, suppressprint=TRUE) ggplot2::ggsave("res1.png", plot=p, dpi=72, width=600/72, height=350/72, units="in") ``` -------------------------------- ### qgcomp.glm.noboot for Fast Linear Model Mixture Effects Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Implements the fast delta method variance estimation for linear and additive models. This method is efficient but limited to conditional effects and linear models. It provides access to underlying model components and demonstrates adjusting for confounders. ```r library("qgcomp") data("metals", package = "qgcomp") Xnm <- c('arsenic', 'barium', 'cadmium', 'calcium', 'chromium', 'copper', 'iron', 'lead', 'magnesium', 'manganese', 'mercury', 'selenium', 'silver', 'sodium', 'zinc') # Linear model for continuous outcome qc_linear <- qgcomp.glm.noboot( f = y ~ ., data = metals[, c(Xnm, 'y')], expnms = Xnm, family = gaussian(), q = 4 ) # View results with weights print(qc_linear) # Scaled effect size (positive direction) # Scaled effect size (negative direction) # Mixture slope parameters (Delta method CI) # Access model components qc_linear$psi # Overall mixture effect qc_linear$var.psi # Variance of mixture effect qc_linear$pos.weights # Positive direction weights qc_linear$neg.weights # Negative direction weights qc_linear$fit # Underlying GLM fit # Adjusting for confounders qc_adjusted <- qgcomp.glm.noboot( f = y ~ mage35 + arsenic + barium + cadmium + calcium + chromium + copper + iron + lead + magnesium + manganese + mercury + selenium + silver + sodium + zinc, expnms = Xnm, data = metals, family = gaussian(), q = 4 ) print(qc_adjusted) ``` -------------------------------- ### Fit Survival Model (Marginal Hazard Ratio) using qgcomp.cox.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits a weighted quantile sum (WQS) Cox proportional hazards model for time-to-event outcomes using the bootstrap method to estimate the marginal hazard ratio. This function supports complex exposure-response relationships and provides bootstrap confidence intervals. Requires survival formula, exposure names, data, q, B, MCsize, and seed. ```r qc_cox_boot <- qgcomp.cox.boot( f = Surv(disease_time, disease_state) ~ ., expnms = Xnm, data = metals[, c(Xnm, 'disease_time', 'disease_state')], q = 4, B = 200, MCsize = 10000, # Monte Carlo sample size for g-computation seed = 125 ) print(qc_cox_boot) ``` -------------------------------- ### Plotting Weights from qgcomp.cox.noboot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Generates a plot visualizing the weights assigned to each exposure in the mixture from a `qgcomp.cox.noboot` model. This helps understand the relative contribution of each exposure to the overall mixture effect. ```r plot(qc_cox) ``` -------------------------------- ### Plotting Survival Curves with qgcomp.cox.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Generates plots of marginal structural model (MSM) and conditional survival curves based on the results from a `qgcomp.cox.boot` model. This visualization aids in understanding the impact of the exposure mixture on survival. ```r plot(qc_cox_boot) # Shows MSM and conditional survival curves ``` -------------------------------- ### Visualize Model Results with plot.qgcompfit Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Generates publication-quality plots to visualize the results of qgcomp model fits. For non-bootstrapped methods (like qgcomp.glm.noboot), it produces a butterfly plot showing the estimated exposure weights. For bootstrapped or effect estimation methods, it can display dose-response curves. Requires a fitted qgcomp object. ```r library("qgcomp") library("ggplot2") data("metals", package = "qgcomp") Xnm <- c('arsenic', 'barium', 'cadmium', 'calcium', 'chromium', 'copper', 'iron', 'lead', 'magnesium', 'manganese', 'mercury', 'selenium', 'silver', 'sodium', 'zinc') # Noboot method - butterfly plot of weights qc_noboot <- qgcomp.glm.noboot(y ~ ., expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4) plot(qc_noboot) # Butterfly plot showing pos/neg weights ``` -------------------------------- ### Fit Survival Model (Conditional Hazard Ratio) using qgcomp.cox.noboot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits a weighted quantile sum (WQS) Cox proportional hazards model for time-to-event outcomes using the no-bootstrap method. This function estimates the conditional hazard ratio for a mixture of exposures. Requires a survival formula, exposure names, data, and q. ```r qc_cox <- qgcomp.cox.noboot( f = Surv(disease_time, disease_state) ~ ., expnms = Xnm, data = metals[, c(Xnm, 'disease_time', 'disease_state')], q = 4 ) print(qc_cox) ``` -------------------------------- ### Make Predictions from Fitted qgcomp Models Source: https://context7.com/alexpkeil1/qgcomp/llms.txt This snippet shows how to use the predict function with qgcomp objects to generate predictions on new data. It demonstrates both non-bootstrapped and bootstrapped models, including predictions from conditional and MSM models. Dependencies include the qgcomp package and its data. ```r library("qgcomp") data("metals", package = "qgcomp") Xnm <- c('arsenic', 'barium', 'cadmium', 'calcium', 'chromium', 'copper', 'iron', 'lead', 'magnesium', 'manganese', 'mercury', 'selenium', 'silver', 'sodium', 'zinc') # Fit model on training data train_data <- metals[1:400, ] test_data <- metals[401:nrow(metals), ] qc_fit <- qgcomp.glm.noboot(y ~ ., expnms = Xnm, data = train_data[, c(Xnm, 'y')], family = gaussian(), q = 4) # Predict on new data (automatically quantizes using original breaks) predictions <- predict(qc_fit, expnms = Xnm, newdata = test_data, type = "response") # Compare predictions to actual values cor(predictions, test_data$y) plot(test_data$y, predictions, xlab = "Actual", ylab = "Predicted", main = "Prediction Performance") abline(0, 1, col = "red") # For boot models, predict from conditional model qc_boot <- qgcomp.glm.boot(y ~ ., expnms = Xnm, data = train_data[, c(Xnm, 'y')], family = gaussian(), q = 4, B = 100, seed = 125) # Predictions from conditional model pred_cond <- predict(qc_boot, expnms = Xnm, newdata = test_data) # Predictions from MSM (for hypothetical joint exposure levels) msm_data <- data.frame(psi = seq(0, 3, by = 0.5)) pred_msm <- msm.predict(qc_boot, newdata = msm_data) cbind(msm_data, predicted = pred_msm) ``` -------------------------------- ### Plotting Results from qgcomp.glm.ee Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Generates a plot based on the results of a `qgcomp.glm.ee` model, likely visualizing the estimated effects or bounds. ```r plot(qc_ee_nonlin) ``` -------------------------------- ### Fit Zero-Inflated Models with qgcomp.zi.noboot and qgcomp.zi.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits zero-inflated count models (Poisson or negative binomial) using either a fast non-bootstrapped method or a bootstrap method for non-linear effects. The formula syntax allows specifying variables for both the count and zero-inflation components. Outputs include scaled effect sizes for count and zero probabilities, and mixture log(OR/RR) with confidence intervals. ```r library("qgcomp") set.seed(50) # Simulate zero-inflated data n <- 500 dat_zi <- data.frame( y = rbinom(n, 1, 0.5) * rpois(n, 1.2), # Zero-inflated Poisson x1 = runif(n), x2 = runif(n), z = runif(n) ) # Zero-inflated Poisson - mixture in both count and zero parts # Formula syntax: outcome ~ count_vars | zero_vars qc_zi <- qgcomp.zi.noboot( f = y ~ z + x1 + x2 | x1 + x2, expnms = c('x1', 'x2'), data = dat_zi, q = 4, dist = "poisson" ) print(qc_zi) # Prob(Y ~ count): # Scaled effect size (positive/negative direction) # Prob(Y ~ zero/count): # Scaled effect size (positive/negative direction) # Mixture log(OR/RR) (Delta method CI) # Zero-inflated negative binomial qc_zinb <- qgcomp.zi.noboot( f = y ~ z + x1 + x2 | z + x1 + x2, expnms = c('x1', 'x2'), data = dat_zi, q = 4, dist = "negbin" ) print(qc_zinb) # Bootstrap version for non-linear effects qc_zi_boot <- qgcomp.zi.boot( f = y ~ x1 + x2 | x1 + x2, expnms = c('x1', 'x2'), data = dat_zi, q = 4, dist = "poisson", B = 100, MCsize = 5000, parallel = TRUE, parplan = TRUE ) print(qc_zi_boot) plot(qc_zi_boot) ``` -------------------------------- ### Fit Logistic Model for Binary Outcome (Marginal Risk Ratio) using qgcomp.glm.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits a weighted quantile sum (WQS) regression model for a binary outcome using the bootstrap method to estimate the marginal risk ratio. This function supports non-linear effects and provides bootstrap confidence intervals. Key arguments include the formula, exposure names, data, family, number of bootstrap iterations (B), and a seed for reproducibility. ```r qc_rr <- qgcomp.glm.boot( f = disease_state ~ ., expnms = Xnm, data = metals[, c(Xnm, 'disease_state')], family = binomial(), q = 4, B = 200, # Number of bootstrap iterations rr = TRUE, # Estimate risk ratio instead of odds ratio seed = 125 ) print(qc_rr) ``` -------------------------------- ### Fit Logistic Model for Binary Outcome (Conditional Odds Ratio) using qgcomp.glm.noboot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits a weighted quantile sum (WQS) regression model for a binary outcome using the no-bootstrap method. This function estimates the conditional odds ratio for a mixture of exposures. It requires the formula, exposure names, data, and family (binomial). ```r qc_logistic <- qgcomp.glm.noboot( f = disease_state ~ ., expnms = Xnm, data = metals[, c(Xnm, 'disease_state')], family = binomial(), q = 4 ) print(qc_logistic) ``` -------------------------------- ### Fit Continuous Model using Estimating Equations (qgcomp.glm.ee) Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits a weighted quantile sum (WQS) regression model for a continuous outcome using the estimating equations (M-estimation) method. This approach provides robust inference and is faster than bootstrapping, supporting clustered or longitudinal data. Requires formula, exposure names, data, and family (e.g., gaussian). ```r qc_ee <- qgcomp.glm.ee( f = y ~ mage35 + arsenic + barium + cadmium + calcium + chromium + copper + iron + lead + magnesium + manganese + mercury + selenium + silver + sodium + zinc, expnms = Xnm, data = metals, family = gaussian(), q = 4 ) print(qc_ee) ``` -------------------------------- ### Fit Multinomial Models with qgcomp.multinomial.noboot and qgcomp.multinomial.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Handles categorical outcomes with more than two levels using multinomial logistic regression. The non-bootstrapped version is fast, while the bootstrapped version handles non-linear effects. The function outputs weights and mixture slope parameters with confidence intervals. It also supports global and homogeneity tests for the mixture effects. ```r library("qgcomp") data("metals", package = "qgcomp") # Create categorical outcome from continuous metals$ycat <- factor( quantize(metals, "y", q = 4)$data$y, levels = c("0", "1", "2", "3"), labels = c("low", "med_low", "med_high", "high") ) mixture <- c("arsenic", "lead", "cadmium") # Multinomial model - fast method qc_multi <- qgcomp.multinomial.noboot( f = ycat ~ arsenic + lead + cadmium, expnms = mixture, data = metals, q = 4 ) # Summary with hypothesis tests summary(qc_multi, tests = c("global", "homogeneity")) # Reference outcome levels: low med_low med_high high # Weights # Mixture slope parameters (Standard CI) # Test if mixture effect is null across all outcome levels joint_test(qc_multi) # H_0: psi_med_low = 0 & psi_med_high = 0 & psi_high = 0 # Chi^2 (df=3) = X.XX, p = X.XXX # Test if mixture effect is homogeneous across outcome levels homogeneity_test(qc_multi) # H_0: psi_med_low = psi_med_high = psi_high # Chi^2 (df=2) = X.XX, p = X.XXX # Bootstrap version for non-linear effects qc_multi_boot <- qgcomp.multinomial.boot( f = ycat ~ arsenic + lead + cadmium, expnms = mixture, data = metals, q = 4, B = 100, MCsize = 1000 ) summary(qc_multi_boot) ``` -------------------------------- ### Create Quantile-Based Exposure Variables with quantize Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Transforms continuous exposure variables into quantile-based categorical variables. This is often a prerequisite for using qgcomp functions. It can create variables based on a specified number of quantiles (defaulting to quartiles) or custom break points. The function returns the modified data frame and the calculated quantile breakpoints. ```r library("qgcomp") # Create sample data set.seed(123) dat <- data.frame( y = rnorm(100), x1 = runif(100), x2 = runif(100), z = runif(100) ) # Quantize exposures into quartiles (default q=4) qdata <- quantize( data = dat, expnms = c("x1", "x2"), q = 4 ) # Quantized data head(qdata$data) # y x1 x2 z # 1 0.12345 2 1 0.4521354 # 2 -0.54321 0 3 0.7823451 # Quantile breakpoints qdata$breaks # [[1]] # x1 breaks # [1] -1e+64 0.25 0.50 0.75 1e+64 # [[2]] # x2 breaks # [1] -1e+64 0.25 0.50 0.75 1e+64 table(qdata$data$x1) # Should be roughly equal across 0,1,2,3 # 0 1 2 3 # 25 25 25 25 # Custom breakpoints (e.g., theoretical quartiles) qdata_custom <- quantize( data = dat, expnms = c("x1", "x2"), breaks = list( c(-1e64, 0.25, 0.5, 0.75, 1e64), c(-1e64, 0.25, 0.5, 0.75, 1e64) ) ) # Use quantized data in downstream analysis fit <- lm(y ~ x1 + x2 + z, data = qdata$data) summary(fit) ``` -------------------------------- ### Fit Non-linear Model with Interaction Terms using qgcomp.glm.boot Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits a weighted quantile sum (WQS) regression model for a continuous outcome, allowing for non-linear effects and interaction terms. The `degree` argument specifies the order of polynomial terms to include. Bootstrap variance estimation is used. Requires formula, exposure names, data, family (e.g., gaussian), q, B, degree, and seed. ```r qc_nonlinear <- qgcomp.glm.boot( f = y ~ . + .^2, # Include all pairwise interactions expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4, B = 200, degree = 2, # Allow quadratic MSM seed = 125 ) print(qc_nonlinear) ``` -------------------------------- ### Fit Non-linear Continuous Model with Estimating Equations (qgcomp.glm.ee) Source: https://context7.com/alexpkeil1/qgcomp/llms.txt Fits a weighted quantile sum (WQS) regression model for a continuous outcome with non-linear effects using the estimating equations method. The `degree` argument allows for polynomial terms. Requires formula, exposure names, data, family, q, and degree. ```r qc_ee_nonlin <- qgcomp.glm.ee( f = y ~ . + .^2, expnms = Xnm, data = metals[, c(Xnm, 'y')], family = gaussian(), q = 4, degree = 2 ) print(qc_ee_nonlin) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.