Resource: | CRAN | Travis CI | Appveyor |
---|---|---|---|

Platforms: |
Multiple |
Linux & macOS |
Windows |

R CMD check | |||

Test coverage |

Functions for calculation and plotting of odds ratios of Generalized Additive (Mixed) Models and Generalized Linear (Mixed) Models with a binomial response variable (i.e. logistic regression models).

Install from CRAN:

Get the development version from Github:

`remotes::install_github("pat-s/oddsratio")`

Odds ratio calculation of predictors `gre`

& `gpa`

of a fitted model `fit_glm`

with increment steps of 380 and 5, respectively.

For factor variables (here: `rank`

with 4 levels), automatically all odds ratios corresponding to the base level (here: `rank1`

) are returned including their respective confident intervals. The default level is 95%. However, other levels can be specified with the param `CI`

. Data source: http://www.ats.ucla.edu/stat/r/dae/logit.htm

```
pacman::p_load(oddsratio, mgcv)
df <- data_glm
df$rank <- factor(df$rank)
fit_glm <- glm(admit ~ gre + gpa + rank, data = df, family = "binomial")
or_glm(data = df, model = fit_glm,
incr = list(gre = 380, gpa = 5, CI = 0.95))
```

For GAMs, the calculation of odds ratio is different. Due to its non-linear definition, odds ratios do only apply to specific value changes and are not constant throughout the whole value range of the predictor as for GLMs. Hence, odds ratios of GAMs can only be computed for one predictor at a time by holding all other predictors at a fixed value while changing the value of the specific predictor. Confident intervals are currently fixed to the 95% level for GAMs. Data source: `?mgcv::predict.gam()`

Here, the usage of `or_gam()`

is shown by calculating odds ratios of pred `x2`

for a 20% steps across the whole value range of the predictor.

```
set.seed(1234)
n <- 200
sig <- 2
df <- gamSim(1, n = n,scale = sig, verbose = FALSE)
df$x4 <- as.factor(c(rep("A", 50), rep("B", 50), rep("C", 50), rep("D", 50)))
fit_gam <- mgcv::gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = df)
or_gam(data = df, model = fit_gam, pred = "x2",
percentage = 20, slice = TRUE)
```

If you want to compute a single odds ratio for specific values, simply set param `slice = FALSE`

:

```
or_gam(data = df, model = fit_gam,
pred = "x2", values = c(0.099, 0.198))
```

Plotting of GAM smooths is also supported:

`plot_gam(fit_gam, pred = "x2", title = "Predictor 'x2'")`

Insert the calculated odds ratios into the smoothing function:

```
plot_object <- plot_gam(fit_gam, pred = "x2", title = "Predictor 'x2'")
or_object <- or_gam(data = df, model = fit_gam,
pred = "x2", values = c(0.099, 0.198))
plot <- insert_or(plot_object, or_object, or_yloc = 3,
values_xloc = 0.04, line_size = 0.5,
line_type = "dotdash", values_yloc = 0.5,
arrow_col = "red")
plot
```

Insert multiple odds ratios into one smooth:

```
or_object2 <- or_gam(data = df, model = fit_gam, pred = "x2",
values = c(0.4, 0.6))
insert_or(plot, 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 = T)
```