Title: | Odds Ratio Calculation for GAM(M)s & GLM(M)s |
---|---|
Description: | Simplified odds ratio calculation of GAM(M)s & GLM(M)s. Provides structured output (data frame) of all predictors and their corresponding odds ratios and confident intervals for further analyses. It helps to avoid false references of predictors and increments by specifying these parameters in a list instead of using 'exp(coef(model))' (standard approach of odds ratio calculation for GLMs) which just returns a plain numeric output. For GAM(M)s, odds ratio calculation is highly simplified with this package since it takes care of the multiple 'predict()' calls of the chosen predictor while holding other predictors constant. Also, this package allows odds ratio calculation of percentage steps across the whole predictor distribution range for GAM(M)s. In both cases, confident intervals are returned additionally. Calculated odds ratio of GAM(M)s can be inserted into the smooth function plot. |
Authors: | Patrick Schratz [aut, cre] |
Maintainer: | Patrick Schratz <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2.0.1.9000 |
Built: | 2025-03-12 03:46:40 UTC |
Source: | https://github.com/pat-s/oddsratio |
This function inserts calculated odds ratios of GAM(M)s into a plot of a GAM(M) smoothing function.
insert_or( plot_object = NULL, or_object = NULL, line_col = "red", line_size = 1.2, line_type = "solid", line_alpha = 1, text_alpha = 1, text_size = 4, text_col = "black", rect_alpha = 0.5, rect_col = NULL, rect = FALSE, arrow = TRUE, values = TRUE, values_yloc = 0, values_xloc = NULL, or_yloc = 0, arrow_length = NULL, arrow_yloc = NULL, arrow_col = NULL, arrow_xloc_r = NULL, arrow_xloc_l = NULL )
insert_or( plot_object = NULL, or_object = NULL, line_col = "red", line_size = 1.2, line_type = "solid", line_alpha = 1, text_alpha = 1, text_size = 4, text_col = "black", rect_alpha = 0.5, rect_col = NULL, rect = FALSE, arrow = TRUE, values = TRUE, values_yloc = 0, values_xloc = NULL, or_yloc = 0, arrow_length = NULL, arrow_yloc = NULL, arrow_col = NULL, arrow_xloc_r = NULL, arrow_xloc_l = NULL )
plot_object |
A |
or_object |
A data.frame as returned from |
line_col , line_alpha , line_type , line_size
|
Aesthetics of vertical lines. |
text_col , text_alpha , text_size
|
Aesthetics of inserted values. |
rect_col , rect_alpha
|
Aesthetics of shaded rectangle. |
rect |
Whether to print a shaded rectangle between the vertical lines. |
arrow |
Whether to print arrows above the inserted values. |
values |
Whether to print predictor value information nearby the inserted vertical lines. |
values_xloc |
x-axis location/shift of values relative to their vertical line. Default to 2\% of x-axis range. |
or_yloc , values_yloc
|
Specifies y-location of inserted odds ratio values. Relative to plotted y-axis range. A positive (negative) value will place the the text higher (lower). |
arrow_xloc_r , arrow_xloc_l , arrow_yloc , arrow_length , arrow_col
|
Axis placement options of inserted arrows. Relative to respective axis ranges. |
The idea behind this function is to add calculated odds ratios of fitted GAM
models (or_gam()
) into a plot showing the smooth function (plot_gam) of
the chosen predictor for which the odds ratio was calculated for. Multiple
insertions can be made by iterative calling the function (see examples).
Right now the function only accepts inputs from or_gam()
objects with
slice = FALSE
. If you want to insert multiple odds ratio values, call the
function multiple times.
library(oddsratio) library(mgcv) fit_gam <- gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data_gam) # fit model # create input objects (plot + odds ratios) plot_object <- plot_gam(fit_gam, pred = "x2", title = "Predictor 'x2'") or_object1 <- or_gam( data = data_gam, model = fit_gam, pred = "x2", values = c(0.099, 0.198) ) # insert first odds ratios to plot plot_object <- insert_or(plot_object, or_object1, or_yloc = 3, values_xloc = 0.04, line_size = 0.5, line_type = "dotdash", text_size = 6, values_yloc = 0.5, arrow_col = "red" ) # calculate second odds ratio or_object2 <- or_gam( data = data_gam, model = fit_gam, pred = "x2", values = c(0.4, 0.6) ) # add or_object2 into plot insert_or(plot_object, or_object2, or_yloc = 2.1, values_yloc = 2, line_col = "green4", text_col = "black", rect_col = "green4", rect_alpha = 0.2, line_alpha = 1, line_type = "dashed", arrow_xloc_r = 0.01, arrow_xloc_l = -0.01, arrow_length = 0.01, rect = TRUE )
library(oddsratio) library(mgcv) fit_gam <- gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data_gam) # fit model # create input objects (plot + odds ratios) plot_object <- plot_gam(fit_gam, pred = "x2", title = "Predictor 'x2'") or_object1 <- or_gam( data = data_gam, model = fit_gam, pred = "x2", values = c(0.099, 0.198) ) # insert first odds ratios to plot plot_object <- insert_or(plot_object, or_object1, or_yloc = 3, values_xloc = 0.04, line_size = 0.5, line_type = "dotdash", text_size = 6, values_yloc = 0.5, arrow_col = "red" ) # calculate second odds ratio or_object2 <- or_gam( data = data_gam, model = fit_gam, pred = "x2", values = c(0.4, 0.6) ) # add or_object2 into plot insert_or(plot_object, or_object2, or_yloc = 2.1, values_yloc = 2, line_col = "green4", text_col = "black", rect_col = "green4", rect_alpha = 0.2, line_alpha = 1, line_type = "dashed", arrow_xloc_r = 0.01, arrow_xloc_l = -0.01, arrow_length = 0.01, rect = TRUE )
This function calculates odds ratio(s) for specific increment
steps of GAM(M) models. Odds ratios can also be calculated for continuous
(percentage) increment steps across the whole predictor distribution using
slice = TRUE
.
or_gam( data = NULL, model = NULL, pred = NULL, values = NULL, percentage = NULL, slice = FALSE, ci = NULL )
or_gam( data = NULL, model = NULL, pred = NULL, values = NULL, percentage = NULL, slice = FALSE, ci = NULL )
data |
The data used for model fitting. |
model |
A fitted GAM(M). |
pred |
Predictor name for which to calculate the odds ratio. |
values |
Numeric vector of length two. Predictor values to estimate odds
ratio from. Function is written to use the first provided value as the
"lower" one, i.e. calculating the odds ratio 'from value1 to value2'. Only
used if |
percentage |
Percentage number to split the predictor distribution into.
A value of 10 would split the predictor distribution by 10\
Only needed if |
slice |
Whether to calculate odds ratios for fixed increment steps over
the whole predictor distribution. See |
ci |
Currently fixed to 95\ Currently supported functions: mgcv::gam, mgcv::gamm,
gam::gam. For mgcv::gamm, the |
A data.frame with (up to) eight columns. perc1
and perc2
are
only returned if slice = TRUE
:
predictor |
Predictor name |
value1 |
First value of odds ratio calculation |
value2 |
Second value of odds ratio calculation |
perc1 |
Percentage value of |
perc2 |
Percentage value of |
oddsratio |
Calculated odds ratio(s) |
ci_low |
Lower |
ci_high |
Higher |
or_glm()
plot_gam()
insert_or()
library(oddsratio) library(mgcv) fit_gam <- gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data_gam) # fit model # Calculate OR for specific increment step of continuous variable or_gam( data = data_gam, model = fit_gam, pred = "x2", values = c(0.099, 0.198) ) ## Calculate OR for change of indicator variable or_gam( data = data_gam, model = fit_gam, pred = "x4", values = c("B", "D") ) ## Calculate ORs for percentage increments of predictor distribution ## (here: 20%) or_gam( data = data_gam, model = fit_gam, pred = "x2", percentage = 20, slice = TRUE )
library(oddsratio) library(mgcv) fit_gam <- gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data_gam) # fit model # Calculate OR for specific increment step of continuous variable or_gam( data = data_gam, model = fit_gam, pred = "x2", values = c(0.099, 0.198) ) ## Calculate OR for change of indicator variable or_gam( data = data_gam, model = fit_gam, pred = "x4", values = c("B", "D") ) ## Calculate ORs for percentage increments of predictor distribution ## (here: 20%) or_gam( data = data_gam, model = fit_gam, pred = "x2", percentage = 20, slice = TRUE )
This function calculates odds ratio(s) for specific increment steps of GLMs.
or_glm(data, model, incr, ci = 0.95)
or_glm(data, model, incr, ci = 0.95)
data |
The data used for model fitting. |
model |
A fitted GLM(M). |
incr |
Increment values of each predictor given in a named list. |
ci |
Which confidence interval to calculate. Must be between 0 and 1. Default to 0.95 |
ci_low
and ci_high
are only calculated for GLM models because
MASS::glmmPQL()
does not return confident intervals due to its penalizing
behavior.
Currently supported functions: stats::glm,MASS::glmmPQL
A data frame with five columns:
predictor |
Predictor name(s) |
oddsratio |
Calculated odds ratio(s) |
ci_low |
Lower confident interval of odds ratio |
ci_high |
Higher confident interval of odds ratio |
increment |
Increment of the predictor(s) |
## Example with glm() library(oddsratio) # load data (source: http://www.ats.ucla.edu/stat/r/dae/logit.htm) and # fit model fit_glm <- glm(admit ~ gre + gpa + rank, data = data_glm, family = "binomial" ) # fit model # Calculate OR for specific increment step of continuous variable or_glm(data = data_glm, model = fit_glm, incr = list(gre = 380, gpa = 5)) # Calculate OR and change the confidence interval level or_glm( data = data_glm, model = fit_glm, incr = list(gre = 380, gpa = 5), ci = .70 ) ## Example with MASS:glmmPQL() # load data library(MASS) data(bacteria) fit_glmmPQL <- glmmPQL(y ~ trt + week, random = ~ 1 | ID, family = binomial, data = bacteria, verbose = FALSE ) # Apply function or_glm(data = bacteria, model = fit_glmmPQL, incr = list(week = 5))
## Example with glm() library(oddsratio) # load data (source: http://www.ats.ucla.edu/stat/r/dae/logit.htm) and # fit model fit_glm <- glm(admit ~ gre + gpa + rank, data = data_glm, family = "binomial" ) # fit model # Calculate OR for specific increment step of continuous variable or_glm(data = data_glm, model = fit_glm, incr = list(gre = 380, gpa = 5)) # Calculate OR and change the confidence interval level or_glm( data = data_glm, model = fit_glm, incr = list(gre = 380, gpa = 5), ci = .70 ) ## Example with MASS:glmmPQL() # load data library(MASS) data(bacteria) fit_glmmPQL <- glmmPQL(y ~ trt + week, random = ~ 1 | ID, family = binomial, data = bacteria, verbose = FALSE ) # Apply function or_glm(data = bacteria, model = fit_glmmPQL, incr = list(week = 5))
Plots the smoothing function of GAM(M) predictors via ggplot2
plot_gam( model = NULL, pred = NULL, col_line = "blue", ci_line_col = "black", ci_line_type = "dashed", ci_fill = "grey", ci_alpha = 0.4, ci_line_size = 0.8, sm_fun_size = 1.1, title = NULL, xlab = NULL, ylab = NULL, limits_y = NULL, breaks_y = NULL )
plot_gam( model = NULL, pred = NULL, col_line = "blue", ci_line_col = "black", ci_line_type = "dashed", ci_fill = "grey", ci_alpha = 0.4, ci_line_size = 0.8, sm_fun_size = 1.1, title = NULL, xlab = NULL, ylab = NULL, limits_y = NULL, breaks_y = NULL )
model |
A fitted model of class |
pred |
Predictor name. |
col_line |
Smoothing function line color. |
ci_line_col |
Confident interval line color. |
ci_line_type |
Linetype of confidence interval. |
ci_fill |
Fill color of area between smoothing function and its confidence interval lines. |
ci_alpha |
Opacity value of confidence interval. |
ci_line_size , sm_fun_size
|
Line sizes. |
title |
Plot title. |
xlab |
x-axis title. |
ylab |
y-axis title. |
limits_y |
y-axis limits. |
breaks_y |
y-axis breaks. Values are handed over to a |
library(oddsratio) library(mgcv) fit_gam <- mgcv::gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data_gam ) plot_gam(fit_gam, pred = "x2", title = "Predictor 'x2'")
library(oddsratio) library(mgcv) fit_gam <- mgcv::gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data_gam ) plot_gam(fit_gam, pred = "x2", title = "Predictor 'x2'")