vignettes/B_estimation_diagnostics_example.Rmd
B_estimation_diagnostics_example.RmdThis vignette demonstrates the diagnostic and visualization tools for
assessing the quality of effect size (B) estimation in the GLOW
framework. These tools work with both glow_B_estimate
objects (from get_B()) and glow_B_model
objects (from train_B_model()). They help you:
First, run get_B() with return_full = TRUE
to get a complete result object:
library(GLOWr)
# Example: Estimate B for a set of variants
result <- get_B(
training_trait = "binary",
training_MAF = training_MAF,
training_BETA = training_BETA,
training_P = training_P,
training_N = training_N,
target_trait = "binary",
target_MAF = target_MAF,
target_case_prop = 0.1,
method = "both", # Run both methods for comparison
return_full = TRUE, # Get full result object
verbose = 2
)The quickest way to assess quality is with the dashboard plot:
# Display 2x2 dashboard of diagnostic plots
plot(result)This creates a 2x2 layout showing: - Top-left: Fitted model vs training data - Top-right: Q-Q plot of residuals - Bottom-left: Residuals vs fitted values - Bottom-right: Method comparison (if both methods used) or Cook’s distance
For a detailed diagnostic report:
# Generate comprehensive diagnostics
# get_B_diagnostics() accepts both glow_B_estimate and glow_B_model objects
diag <- get_B_diagnostics(result, show_plots = TRUE, verbose = 1)
# The function automatically displays:
# - Model fit statistics (R², Adjusted R²)
# - Residual analysis
# - Outlier detection results
# - Method comparison (if applicable)
# - Quality control flagstrain_B_model()
You can also run diagnostics on a standalone trained model (without predictions):
# Train a model
b_model <- train_B_model(
training_trait = "binary",
training_MAF = training_MAF,
training_BETA = training_BETA,
method = "beta"
)
# Plot model-level diagnostics (no target data needed)
plot(b_model)
plot(b_model, type = "fit")
plot(b_model, type = "residuals")
# Full diagnostic report from a trained model
diag <- get_B_diagnostics(b_model, show_plots = TRUE)When both methods are used, the comparison plot shows:
plot(result, type = "comparison")This creates a 2x2 layout with: 1. Scatterplot: Beta method vs P-value method 2. Bland-Altman plot: Difference vs average 3. Percent difference vs average 4. Distribution of differences
The diagnostic report provides several measures of model quality:
diag <- get_B_diagnostics(result, show_plots = FALSE, verbose = 0)
# R-squared: Proportion of variance explained
# Diagnostics use short names: $beta and $pvalue (not $beta_method)
diag$model_fit$beta$R2 # Higher is better (>0.5 preferred)
# Adjusted R-squared: Accounts for model complexity
diag$model_fit$beta$adj_R2 # Higher is better
# Model formula
diag$model_fit$beta$formula # Shows transformation usedInterpretation: - R² > 0.5: Good fit - R² 0.3-0.5: Moderate fit (may benefit from more data) - R² < 0.3: Poor fit (warning issued)
Check residual behavior:
# Residual statistics
diag$residual_stats$mean # Should be ~0
diag$residual_stats$sd # Spread of residuals
diag$residual_stats$normality_p # Shapiro-Wilk test p-value
# Non-normal residuals (p < 0.05) suggest:
# - Transformation may be inappropriate
# - Outliers present
# - Non-linear relationshipIdentify potentially influential observations:
# Number of outliers detected
diag$outliers$n_outliers
# Proportion of data
diag$outliers$proportion # Warning if >10%
# Which observations are outliers
diag$outliers$indices
# Cook's distance values
diag$outliers$cooks_dWhat to do about outliers: 1. Investigate whether they are data errors 2. Check if they represent real biological phenomena 3. Consider robustness of results with/without them 4. Document decisions about handling outliers
When both methods are used:
# Correlation between methods
diag$method_comparison$correlation # Should be >0.7
# Root mean squared error
diag$method_comparison$rmse
# Percent differences
diag$method_comparison$mean_percent_diff
diag$method_comparison$max_percent_diffInterpretation: - High correlation (>0.9): Methods agree well - Moderate correlation (0.7-0.9): Reasonable agreement - Low correlation (<0.7): Methods disagree (warning issued)
Note: Large percent differences can occur even with high correlation because methods operate on different scales. Focus on correlation.
The diagnostic report automatically identifies potential issues:
# Check for QC issues
if (length(diag$qc_flags) > 0) {
cat("Quality Control Issues:\n")
for (flag in diag$qc_flags) {
cat(" -", flag, "\n")
}
}Common flags: - “Low R²”: Model explains <30% of variance - “Non-normal residuals”: May indicate model misspecification - “High proportion of outliers”: >10% of data flagged - “Low correlation between methods”: Methods disagree
Compare different candidate models:
# Compare all candidate models
model_comp <- compare_B_models(
result,
which_method = "beta",
criterion = "R2",
plot = TRUE
)
# View results
print(model_comp)Note: Current implementation has limited functionality. It shows the selected model but cannot re-fit all 8 candidates without original training data. This is a known limitation documented in the code.
Here’s a complete workflow:
# 1. Load data
als_data <- readRDS("path/to/ALS_data.rds")
training_data <- als_data$data
# 2. Extract required fields
training_MAF <- training_data$MAF
training_BETA <- training_data$BETA
training_P <- training_data$P
training_N <- training_data$N
# Remove any invalid values
valid_idx <- complete.cases(data.frame(training_MAF, training_BETA, training_P, training_N)) &
training_MAF > 0 & training_MAF < 1 &
abs(training_BETA) > 1e-10
training_MAF <- training_MAF[valid_idx]
training_BETA <- training_BETA[valid_idx]
training_P <- training_P[valid_idx]
training_N <- training_N[valid_idx]
# 3. Define target variants
target_MAF <- seq(0.01, 0.4, by = 0.05)
target_case_prop <- 0.001 # ALS prevalence
# 4. Estimate B with both methods
result <- get_B(
training_trait = "binary",
training_MAF = training_MAF,
training_BETA = training_BETA,
training_P = training_P,
training_N = training_N,
target_trait = "binary",
target_MAF = target_MAF,
target_case_prop = target_case_prop,
method = "both",
return_full = TRUE,
verbose = 2
)
# 5. Visual diagnostics
plot(result)
# 6. Detailed diagnostics
diag <- get_B_diagnostics(result, show_plots = TRUE)
# 7. Check for issues
if (length(diag$qc_flags) > 0) {
cat("Issues detected:\n")
print(diag$qc_flags)
}
# 8. Use the estimates
B_estimates <- result$B # Primary estimates
# Use these in optimal weight calculation...If diagnostics reveal issues:
The diagnostic tools provide: - Visual assessment through multiple plot types - Quantitative metrics for model quality - Automatic QC flagging of potential issues - Method comparison when multiple approaches used
Use these tools to ensure your B estimates are reliable and appropriate for downstream GLOW analyses.
?get_B - All-in-one B estimation (train + predict)?train_B_model - Train B estimation model
separately?predict_B - Predict B from a trained model?plot.glow_B_estimate - Plot method for estimation
results?plot.glow_B_model - Plot method for trained
models?get_B_diagnostics - Diagnostic function (accepts both
classes)?compare_B_models - Model comparison documentation