Overveiw

This is a collection of custom R functions designed to automate common analyses and improve workflow efficiency.


Installation and Usage

Load any function directly into your R session using:

source("<function-URL>")

Replace <function-URL> with the corresponding raw GitHub link provided for each function.


Functions

Report Statistical Regression Model Output

Extracts and reports statistical regression model outputs with publication-quality formatting. The function is compatible with linear, generalized linear, and mixed-effects models, and generates templates for output interpretation. For documentation, see link.

# Fit regression model
model <- glm(formula = outcome ~ predictor1 + predictor2,
             data    = df,
             family  = <gaussian/binomial/poisson... etc.>)

# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/main/report_mod_out_knitr.R")

# Run function
report_mod_out_knitr(model,
                     exp           = TRUE,
                     params        = names(coef(model)),
                     n_digits      = 3,
                     caption_input = "Association between outcome, predictor1 and predictor2",
                     knitr_output  = TRUE)

Compute Absolute Fitted Values with Confidence Intervals

Computes absolute fitted values from regression models for specific parameter combinations. For documentation, see link.

# Fit regression model
mod <- glm(formula = y ~ a + bx,
           data    = df,
           family  = <gaussian/binomial/poisson... etc.>)

# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/regression/report_mod_out_knitr.R")

# Run function
# e.g., report fitted values at point x = 4
calc_abs_effect_val(model    = mod,
                    parms    = (e.g., "(Intercept) + a + b * 4"),
                    exp      = FALSE,
                    n_digits = 3)

Epi Conversions

Convert from Probability to Rate

\[r = \frac{-\ln (1 - p)}{t}\]
# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/epi/prob_to_rate.R")
prob_to_rate(time = 1,
             prob = 0.7) # returns 1.203973

Convert from Rate to Probability

\[p = 1 - exp ^ {(r \times t)}\]
# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/epi/rate_to_prob.R")
rate_to_prob(time = 1,
             rate = 1.203973) # returns 0.7

Convert Transition Probabilities between Different Time Units

\[tp_b = 1 - (1 - tp_a) ^ {b / a}\]
# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/epi/conv_trans_prob.R")
conv_trans_prob(tp    = 0.3,
                ratio = 1 / 12) # This converts an annual 30% TP to a monthly TP of 2%

Miscellaneous Functions

Logical Operator for Vectors

This is the logical complement to %in%. Returns TRUE for elements not present in a vector. Elements could be single values or vectors.

# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/misc/notin.R")
c("a", "b") %!in% c("b", "c", "d")  # returns: TRUE, FALSE

Report Data Completeness

This function generates a comprehensive missingness report for data frames, including counts and percentages by variable.

# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/misc/report_missing.R")
report_missing(your_dataframe)

Convert between Annual and Hourly Salary Rates

# Call function
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/misc/calc_salary.R")
calc_salary(val  = 75,       # salary value
            rate = "hourly", # value above is hourly rate
            days = 5,        # days worked
            fte  = 1)        # full-time equivalent, 100% 
# Returns: "Annual salary = 156,000.0"

ggplot2 Visualization Themes

Chris Adoph’s Theme (Modified)

# Call theme
source("https://raw.githubusercontent.com/Mohamed-Albirair/my-R-functions/refs/heads/main/R/viz_ggplot/theme_caviz.R")
# add `theme_caviz` to your `ggplot` script

Link to Chris Adolph’s UW page

Link to theme .zip compressed file download