| Title: | Interactive Analysis of Mills Ratios and Tail Thickness |
|---|---|
| Description: | Compute and compare Mills ratios for normal, t, and exponential distributions. Explore the t(30) paradox where heavier tails produce larger Mills ratios despite near-normal density. Includes an interactive Shiny dashboard for visualising tail behaviour and hazard functions. |
| Authors: | John Gavin [aut, cre] |
| Maintainer: | John Gavin <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.3 |
| Built: | 2026-06-07 07:09:08 UTC |
| Source: | https://github.com/JohnGavin/millsratio |
Generate Data for t(30) Paradox Analysis
analyze_t30_paradox(x_range = c(0, 5), n_points = 200)analyze_t30_paradox(x_range = c(0, 5), n_points = 200)
x_range |
Range of x values |
n_points |
Number of points |
Data frame comparing t(30) and normal distributions
paradox_data <- analyze_t30_paradox()paradox_data <- analyze_t30_paradox()
Analyze Tail Thickness at Specific Points
analyze_tail_thickness( x_points, distributions = c("normal", "t30", "exponential"), compute_ratios = FALSE )analyze_tail_thickness( x_points, distributions = c("normal", "t30", "exponential"), compute_ratios = FALSE )
x_points |
Numeric vector of x values to analyze |
distributions |
Character vector of distributions |
compute_ratios |
Logical; if TRUE, computes ratios relative to normal |
Data frame with Mills ratios and optional comparison metrics
# Analyze at key points tail_analysis <- analyze_tail_thickness( x_points = c(2, 3, 4, 5), distributions = c("normal", "t30", "exponential"), compute_ratios = TRUE )# Analyze at key points tail_analysis <- analyze_tail_thickness( x_points = c(2, 3, 4, 5), distributions = c("normal", "t30", "exponential"), compute_ratios = TRUE )
Compare Mills Ratio and Hazard Function
compare_mills_hazard(x, distribution = "normal", ...)compare_mills_hazard(x, distribution = "normal", ...)
x |
Numeric vector of values |
distribution |
Character string specifying distribution |
... |
Additional distribution parameters |
Data frame with x, mills_ratio, and hazard columns
# Compare for normal distribution compare_mills_hazard(seq(0, 5, by = 0.5), "normal") # Tidyverse visualization library(dplyr) library(ggplot2) compare_mills_hazard(seq(0.1, 5, by = 0.1), "normal") %>% tidyr::pivot_longer(c(mills_ratio, hazard), names_to = "function_type", values_to = "value") %>% ggplot(aes(x, value, color = function_type)) + geom_line() + scale_y_log10() + labs(title = "Mills Ratio vs Hazard Function", subtitle = "Note: h(x) = 1/m(x)")# Compare for normal distribution compare_mills_hazard(seq(0, 5, by = 0.5), "normal") # Tidyverse visualization library(dplyr) library(ggplot2) compare_mills_hazard(seq(0.1, 5, by = 0.1), "normal") %>% tidyr::pivot_longer(c(mills_ratio, hazard), names_to = "function_type", values_to = "value") %>% ggplot(aes(x, value, color = function_type)) + geom_line() + scale_y_log10() + labs(title = "Mills Ratio vs Hazard Function", subtitle = "Note: h(x) = 1/m(x)")
Compare Mills Ratios Across Distributions
compare_mills_ratios(x, distributions = c("normal", "t30", "exponential"))compare_mills_ratios(x, distributions = c("normal", "t30", "exponential"))
x |
Numeric vector of quantiles |
distributions |
Character vector of distribution names ("normal", "t3", "t10", "t30", "exponential") |
Data frame with Mills ratios for each distribution
x_vals <- c(1, 2, 3, 4) compare_mills_ratios(x_vals, c("normal", "t30", "exponential"))x_vals <- c(1, 2, 3, 4) compare_mills_ratios(x_vals, c("normal", "t30", "exponential"))
Get Dashboard About Information
dashboard_about()dashboard_about()
List with dashboard metadata
Find Crossover Points Between Distributions
find_crossover_point( dist1, dist2, df1 = NULL, df2 = NULL, x_range = c(0.01, 10), tolerance = 1e-06 )find_crossover_point( dist1, dist2, df1 = NULL, df2 = NULL, x_range = c(0.01, 10), tolerance = 1e-06 )
dist1 |
First distribution |
dist2 |
Second distribution |
df1 |
Degrees of freedom for first distribution (if t) |
df2 |
Degrees of freedom for second distribution (if t) |
x_range |
Search range for crossover |
tolerance |
Numerical tolerance for finding crossover |
Numeric value of x where Mills ratios are equal (NA if no crossover)
# Find where t(30) and normal Mills ratios cross crossover <- find_crossover_point("t", "normal", df1 = 30)# Find where t(30) and normal Mills ratios cross crossover <- find_crossover_point("t", "normal", df1 = 30)
Convert Mills Ratio to Hazard Function
hazard_from_mills(mills_ratio)hazard_from_mills(mills_ratio)
mills_ratio |
Numeric vector of Mills ratio values |
Numeric vector of hazard function values
# Normal distribution example x <- seq(0, 5, by = 0.5) m <- mills_ratio_normal(x) h <- hazard_from_mills(m) # Tidyverse example library(dplyr) data.frame(x = x) %>% mutate( mills = mills_ratio_normal(x), hazard = hazard_from_mills(mills) )# Normal distribution example x <- seq(0, 5, by = 0.5) m <- mills_ratio_normal(x) h <- hazard_from_mills(m) # Tidyverse example library(dplyr) data.frame(x = x) %>% mutate( mills = mills_ratio_normal(x), hazard = hazard_from_mills(mills) )
Calculate Hazard Function Directly
hazard_function(x, distribution = "normal", ...)hazard_function(x, distribution = "normal", ...)
x |
Numeric vector of values |
distribution |
Character string: "normal", "t", "exponential" |
... |
Additional parameters for the distribution |
Numeric vector of hazard function values
# Compare hazard functions across distributions x <- seq(0.1, 5, by = 0.1) h_normal <- hazard_function(x, "normal") h_t30 <- hazard_function(x, "t", df = 30) h_exp <- hazard_function(x, "exponential", rate = 1) # Tidyverse comparison library(dplyr) library(tidyr) data.frame(x = x) %>% mutate( Normal = hazard_function(x, "normal"), `t(30)` = hazard_function(x, "t", df = 30), Exponential = hazard_function(x, "exponential") ) %>% pivot_longer(-x, names_to = "distribution", values_to = "hazard")# Compare hazard functions across distributions x <- seq(0.1, 5, by = 0.1) h_normal <- hazard_function(x, "normal") h_t30 <- hazard_function(x, "t", df = 30) h_exp <- hazard_function(x, "exponential", rate = 1) # Tidyverse comparison library(dplyr) library(tidyr) data.frame(x = x) %>% mutate( Normal = hazard_function(x, "normal"), `t(30)` = hazard_function(x, "t", df = 30), Exponential = hazard_function(x, "exponential") ) %>% pivot_longer(-x, names_to = "distribution", values_to = "hazard")
Functions for calculating hazard functions and their relationship to Mills ratios. The hazard function h(x) = f(x)/(1-F(x)) is the reciprocal of the Mills ratio m(x).
The hazard function (also called failure rate or force of mortality) represents the instantaneous rate of occurrence of an event at time x, given survival to time x.
Mathematical relationship: h(x) = 1/m(x)
Analyze properties of the hazard function for different distributions.
hazard_properties(distribution = "normal", df = NULL)hazard_properties(distribution = "normal", df = NULL)
distribution |
Character string: "normal", "t", or "exponential" |
df |
Degrees of freedom for t-distribution |
List with hazard function properties
# Analyze normal distribution hazard hazard_properties("normal") # Compare properties across distributions library(purrr) list( normal = hazard_properties("normal"), t30 = hazard_properties("t", df = 30), exponential = hazard_properties("exponential") ) %>% map_df(~ as.data.frame(.x), .id = "distribution")# Analyze normal distribution hazard hazard_properties("normal") # Compare properties across distributions library(purrr) list( normal = hazard_properties("normal"), t30 = hazard_properties("t", df = 30), exponential = hazard_properties("exponential") ) %>% map_df(~ as.data.frame(.x), .id = "distribution")
Launches an interactive Shiny dashboard for exploring Mills ratios and tail thickness behavior across different distributions.
launch_dashboard(port = NULL, launch_browser = TRUE, host = "127.0.0.1")launch_dashboard(port = NULL, launch_browser = TRUE, host = "127.0.0.1")
port |
Port number for the Shiny app (default = NULL for auto-selection) |
launch_browser |
Logical; if TRUE, opens dashboard in browser |
host |
Host address (default = "127.0.0.1" for local access) |
Runs the Shiny application
## Not run: # Launch the dashboard launch_dashboard() # Launch on specific port launch_dashboard(port = 8080) ## End(Not run)## Not run: # Launch the dashboard launch_dashboard() # Launch on specific port launch_dashboard(port = 8080) ## End(Not run)
Convenience function to launch dashboard without attempting to open browser. Useful when browser settings are not configured.
launch_dashboard_no_browser(port = NULL, host = "127.0.0.1")launch_dashboard_no_browser(port = NULL, host = "127.0.0.1")
port |
Port number for the Shiny app (default = NULL for auto-selection) |
host |
Host address (default = "127.0.0.1" for local access) |
Runs the Shiny application
## Not run: # Launch without browser launch_dashboard_no_browser() # Then manually open the displayed URL in your browser ## End(Not run)## Not run: # Launch without browser launch_dashboard_no_browser() # Then manually open the displayed URL in your browser ## End(Not run)
Launch the simplified, content-focused version of the Mills ratio dashboard. This version emphasizes clarity and substance over visual effects.
launch_dashboard_simple( port = 4628, launch_browser = FALSE, host = "127.0.0.1" )launch_dashboard_simple( port = 4628, launch_browser = FALSE, host = "127.0.0.1" )
port |
Port number for the Shiny app (default = 4628) |
launch_browser |
Launch browser? (default = FALSE) |
host |
Host address (default = "127.0.0.1" for local access) |
Runs the simplified Shiny application
## Not run: # Launch simplified dashboard launch_dashboard_simple() ## End(Not run)## Not run: # Launch simplified dashboard launch_dashboard_simple() ## End(Not run)
Calculate Asymptotic Approximation of Mills Ratio
mills_asymptotic(x, distribution, df = NULL)mills_asymptotic(x, distribution, df = NULL)
x |
Numeric vector of quantiles |
distribution |
Character string: "normal", "t", or "exponential" |
df |
Degrees of freedom for t-distribution |
Numeric vector of asymptotic approximations
# Normal asymptotic approximation (1/x for large x) mills_asymptotic(10, "normal") # Student's t asymptotic approximation (x/df for large x) mills_asymptotic(10, "t", df = 30)# Normal asymptotic approximation (1/x for large x) mills_asymptotic(10, "normal") # Student's t asymptotic approximation (x/df for large x) mills_asymptotic(10, "t", df = 30)
Compute Ratio of Mills Ratios
mills_ratio_comparison(x, dist1, dist2, df1 = NULL, df2 = NULL)mills_ratio_comparison(x, dist1, dist2, df1 = NULL, df2 = NULL)
x |
Numeric vector of quantiles |
dist1 |
First distribution ("normal", "t", "exponential") |
dist2 |
Second distribution ("normal", "t", "exponential") |
df1 |
Degrees of freedom for first distribution (if t) |
df2 |
Degrees of freedom for second distribution (if t) |
Numeric vector of ratios m1(x)/m2(x)
# Ratio of t(30) to normal Mills ratios x_vals <- seq(1, 5, by = 0.5) ratio <- mills_ratio_comparison(x_vals, "t", "normal", df1 = 30)# Ratio of t(30) to normal Mills ratios x_vals <- seq(1, 5, by = 0.5) ratio <- mills_ratio_comparison(x_vals, "t", "normal", df1 = 30)
Compute Mills Ratio for Exponential Distribution
mills_ratio_exp(x, rate = 1, log = FALSE)mills_ratio_exp(x, rate = 1, log = FALSE)
x |
Numeric vector of quantiles |
rate |
Rate parameter (> 0) |
log |
Logical; if TRUE, returns log(Mills ratio) |
Numeric vector of Mills ratios
# Exponential with rate = 1 at x = 2 mills_ratio_exp(2, rate = 1) # Mills ratio is constant for exponential! mills_ratio_exp(c(1, 2, 3, 4), rate = 1)# Exponential with rate = 1 at x = 2 mills_ratio_exp(2, rate = 1) # Mills ratio is constant for exponential! mills_ratio_exp(c(1, 2, 3, 4), rate = 1)
Generic Mills Ratio Function
mills_ratio_generic(x, cdf_fun, pdf_fun, log = FALSE, ...)mills_ratio_generic(x, cdf_fun, pdf_fun, log = FALSE, ...)
x |
Numeric vector of quantiles |
cdf_fun |
CDF function (must accept lower.tail and log.p arguments) |
pdf_fun |
PDF function (must accept log argument) |
log |
Logical; if TRUE, returns log(Mills ratio) |
... |
Additional arguments passed to cdf_fun and pdf_fun |
Numeric vector of Mills ratios
# Using generic function for normal distribution mills_ratio_generic(2, cdf_fun = pnorm, pdf_fun = dnorm) # Using for custom distribution # mills_ratio_generic(x, cdf_fun = my_cdf, pdf_fun = my_pdf, param1 = value1)# Using generic function for normal distribution mills_ratio_generic(2, cdf_fun = pnorm, pdf_fun = dnorm) # Using for custom distribution # mills_ratio_generic(x, cdf_fun = my_cdf, pdf_fun = my_pdf, param1 = value1)
Functions to compute Mills ratios for different probability distributions. The Mills ratio m(x) is defined as the ratio of the complementary cumulative distribution function (CCDF) to the probability density function (PDF): m(x) = \[1 - F(x)\] / f(x)
mills_ratio_normal(x, mean = 0, sd = 1, log = FALSE)mills_ratio_normal(x, mean = 0, sd = 1, log = FALSE)
x |
Numeric vector of quantiles |
mean |
Mean of the distribution (default = 0) |
sd |
Standard deviation (default = 1) |
log |
Logical; if TRUE, returns log(Mills ratio) for numerical stability |
Numeric vector of Mills ratios
John Gavin [email protected] Compute Mills Ratio for Normal Distribution
# Standard normal Mills ratio at x = 2 mills_ratio_normal(2) # Multiple values mills_ratio_normal(c(1, 2, 3, 4)) # Log Mills ratio for extreme values mills_ratio_normal(10, log = TRUE)# Standard normal Mills ratio at x = 2 mills_ratio_normal(2) # Multiple values mills_ratio_normal(c(1, 2, 3, 4)) # Log Mills ratio for extreme values mills_ratio_normal(10, log = TRUE)
Compute Mills Ratio for Student's t Distribution
mills_ratio_t(x, df, log = FALSE)mills_ratio_t(x, df, log = FALSE)
x |
Numeric vector of quantiles |
df |
Degrees of freedom (> 0) |
log |
Logical; if TRUE, returns log(Mills ratio) |
Numeric vector of Mills ratios
# t-distribution with df = 30 at x = 2 mills_ratio_t(2, df = 30) # Compare different degrees of freedom x_vals <- seq(0, 5, by = 0.5) m_t3 <- mills_ratio_t(x_vals, df = 3) m_t30 <- mills_ratio_t(x_vals, df = 30)# t-distribution with df = 30 at x = 2 mills_ratio_t(2, df = 30) # Compare different degrees of freedom x_vals <- seq(0, 5, by = 0.5) m_t3 <- mills_ratio_t(x_vals, df = 3) m_t30 <- mills_ratio_t(x_vals, df = 30)
Monte Carlo Simulation for Mills Ratio Estimation
monte_carlo_mills( n_sim = 10000, x_val = 2, distribution = "normal", df = NULL, seed = NULL )monte_carlo_mills( n_sim = 10000, x_val = 2, distribution = "normal", df = NULL, seed = NULL )
n_sim |
Number of simulations |
x_val |
Value at which to estimate Mills ratio |
distribution |
Distribution to sample from |
df |
Degrees of freedom (for t-distribution) |
seed |
Random seed for reproducibility |
List with empirical Mills ratio estimate and confidence interval
# Empirically verify Mills ratio for normal at x=2 mc_result <- monte_carlo_mills(n_sim = 10000, x_val = 2, distribution = "normal")# Empirically verify Mills ratio for normal at x=2 mc_result <- monte_carlo_mills(n_sim = 10000, x_val = 2, distribution = "normal")
Plot Mills Ratio Comparison
plot_mills_comparison( x_range = c(0, 5), dist1, dist2, df1 = NULL, df2 = NULL, show_ratio = TRUE )plot_mills_comparison( x_range = c(0, 5), dist1, dist2, df1 = NULL, df2 = NULL, show_ratio = TRUE )
x_range |
Range of x values |
dist1 |
First distribution |
dist2 |
Second distribution |
df1 |
Degrees of freedom for first distribution |
df2 |
Degrees of freedom for second distribution |
show_ratio |
Logical; if TRUE, shows ratio plot |
ggplot2 object
plot_mills_comparison(c(0, 5), "t", "normal", df1 = 30)plot_mills_comparison(c(0, 5), "t", "normal", df1 = 30)
Functions to create static and interactive visualizations of Mills ratios, supporting both ggplot2 and plotly outputs.
plot_mills_curves( data, log_y = FALSE, log_x = FALSE, interactive = FALSE, title = "Mills Ratio Comparison", show_asymptotic = TRUE )plot_mills_curves( data, log_y = FALSE, log_x = FALSE, interactive = FALSE, title = "Mills Ratio Comparison", show_asymptotic = TRUE )
data |
Data frame from simulate_mills_curves() |
log_y |
Logical; if TRUE, uses log scale for y-axis |
log_x |
Logical; if TRUE, uses log scale for x-axis |
interactive |
Logical; if TRUE, returns plotly object |
title |
Plot title |
show_asymptotic |
Logical; if TRUE, shows asymptotic approximations |
ggplot2 or plotly object
John Gavin [email protected] Plot Mills Ratio Curves
# Generate and plot Mills ratio curves curves <- simulate_mills_curves(distributions = c("normal", "t30", "exponential")) plot_mills_curves(curves, log_y = TRUE)# Generate and plot Mills ratio curves curves <- simulate_mills_curves(distributions = c("normal", "t30", "exponential")) plot_mills_curves(curves, log_y = TRUE)
Plot Mills Ratio vs Hazard Function Side-by-Side
plot_mills_vs_hazard( x_range = c(0.1, 5), distributions = c("normal", "t30", "exponential"), n_points = 200, interactive = FALSE, log_y = TRUE )plot_mills_vs_hazard( x_range = c(0.1, 5), distributions = c("normal", "t30", "exponential"), n_points = 200, interactive = FALSE, log_y = TRUE )
x_range |
Numeric vector of length 2 giving the range of x values |
distributions |
Character vector of distribution names (e.g., c("normal", "t30", "exponential")) |
n_points |
Number of points to compute (default 200) |
interactive |
Logical; if TRUE, returns plotly object |
log_y |
Logical; if TRUE, uses log scale for y-axis |
ggplot2 or plotly object with faceted m(x) and h(x) panels
plot_mills_vs_hazard() plot_mills_vs_hazard(x_range = c(0.5, 8), distributions = c("normal", "t30"))plot_mills_vs_hazard() plot_mills_vs_hazard(x_range = c(0.5, 8), distributions = c("normal", "t30"))
Plot t(30) Paradox Visualization
plot_t30_paradox(data, focus = "mills", interactive = FALSE)plot_t30_paradox(data, focus = "mills", interactive = FALSE)
data |
Data frame from analyze_t30_paradox() |
focus |
Character: "mills", "pdf", "cdf", or "all" |
interactive |
Logical; if TRUE, returns plotly object |
ggplot2 or plotly object
paradox_data <- analyze_t30_paradox() plot_t30_paradox(paradox_data, focus = "mills")paradox_data <- analyze_t30_paradox() plot_t30_paradox(paradox_data, focus = "mills")
Plot Tail Thickness Heatmap
plot_tail_thickness_heatmap(x_points, distributions, interactive = FALSE)plot_tail_thickness_heatmap(x_points, distributions, interactive = FALSE)
x_points |
Vector of x values |
distributions |
Vector of distribution names |
interactive |
Logical; if TRUE, returns plotly object |
ggplot2 or plotly heatmap
plot_tail_thickness_heatmap( x_points = seq(1, 5, by = 0.5), distributions = c("normal", "t3", "t10", "t30", "exponential") )plot_tail_thickness_heatmap( x_points = seq(1, 5, by = 0.5), distributions = c("normal", "t3", "t10", "t30", "exponential") )
Functions to generate and compare Mills ratio curves across distributions, supporting the interactive dashboard and analysis vignettes.
simulate_mills_curves( x_range = c(0.5, 5), n_points = 100, distributions = c("normal", "t30", "exponential"), log_scale = FALSE, include_asymptotic = FALSE )simulate_mills_curves( x_range = c(0.5, 5), n_points = 100, distributions = c("normal", "t30", "exponential"), log_scale = FALSE, include_asymptotic = FALSE )
x_range |
Numeric vector of two elements: c(min, max) |
n_points |
Number of points to evaluate (default = 100) |
distributions |
Character vector of distributions to include |
log_scale |
Logical; if TRUE, uses log-spaced x values |
include_asymptotic |
Logical; if TRUE, includes asymptotic approximations |
Data frame in long format suitable for plotting
John Gavin [email protected] Generate Mills Ratio Curves
# Generate curves for comparison curves <- simulate_mills_curves( x_range = c(0.5, 5), distributions = c("normal", "t3", "t30", "exponential") )# Generate curves for comparison curves <- simulate_mills_curves( x_range = c(0.5, 5), distributions = c("normal", "t3", "t30", "exponential") )