---
title: "Print crude and adjusted"
author: "Max Gordon"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Print crude and adjusted}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# How to output crude and adjusted models

## Background

It is a common practice in epidemiological papers to present regression estimates in both adjusted and unadjusted format. The unadjusted format is just that particular variable without any of the covariates and is often referred to as the **crude** estimate. The advantage with the two is that you provide the reader with a better understanding of how much the variables affect each other and also a sense of how much confounding is present in the model.

## The `printCrudeAndAdjusted` function

The `printCrudeAndAdjusted` was designed for this purpose. It provides a table with the `coef` and `confint` estimates for the full model, then reruns for each variable through an `update` call on the model providing `outcome ~ variable` as input. Below is a basic example:

```{r, message=FALSE}
library(datasets)
data(mtcars)
mtcars$am <- factor(mtcars$am, labels = c("Automatic", "Manual"))
fit <- lm(mpg ~ cyl + disp + hp + am, data = mtcars)
library(Greg)
printCrudeAndAdjustedModel(fit)
```

As the variable names often aren't that pretty you can use the `rowname.fn` in order to get the row names desired. Here's a simple substitution example together with some limits to the digits, and a reference group:

```{r}
printCrudeAndAdjustedModel(fit,
                           digits = 1,
                           add_references = TRUE,
                           rowname.fn = function(n){
  if (n == "disp")
    return("Displacement (cu.in.)")
  if (n == "hp")
    return("Gross horsepower")
  if (n == "cyl")
    return("No. cylinders")
  if (n == "am")
    return("Transmission")
  return(n)
})
```

You can achieve the same effect with the `label` function in the `Hmisc` package:

```{r, message=FALSE}
library(Hmisc)
label(mtcars$disp) <- "Displacement (cu.in)"
label(mtcars$cyl) <- "No. cylinders"
label(mtcars$hp) <- "Gross horsepower"
label(mtcars$am) <- "Transmission"

printCrudeAndAdjustedModel(fit,
                           digits = 1,
                           add_references = TRUE)
```

If we want to style the table we can use `htmlTable::addHtmlTableStyle`

```{r styling_with_addHtmlTableStyle}
library(htmlTable)

printCrudeAndAdjustedModel(fit,
                           digits = 1,
                           add_references = TRUE) |>
  # We can also style the output as shown here
  addHtmlTableStyle(css.rgroup = "")
```

The style can be added to all the tables by setting a theme option and activating the style for all the table elements.

```{r theming}
setHtmlTableTheme(css.rgroup = "")
```

## Binding columns/rows

The returned variable from `printCrudeAndAdjustedModel` works just like any matrix, i.e. you can bind it both on rows and on columns:

```{r}
fit_mpg <- lm(mpg ~ cyl + disp + hp + am, data = mtcars)
fit_weight <- lm(wt ~ cyl + disp + hp + am, data = mtcars)
p_mpg <- printCrudeAndAdjustedModel(fit_mpg, digits = 1, add_references = TRUE)
p_weight <- printCrudeAndAdjustedModel(fit_weight, digits = 1, add_references = TRUE)
rbind("Miles per gallon" = p_mpg,
      "Weight (1000 lbs)" = p_weight)

cbind("Miles per gallon" = p_mpg,
      "Weight (1000 lbs)" = p_weight)

```

## Selecting column/rows using `[`

It is also possible to select individual rows/columns if so desired using the `[` operator:

```{r}
p_mpg[,1:2]

p_mpg[1:2,]
```

## More advanced models

The function is prepared for use with splines and strata but is currently _not_ tested for mixed models. As the spline estimates are best interpreted in a graph they are omitted from the output.

```{r}
library("survival")

set.seed(10)
n <- 500
ds <- data.frame(
  ftime = rexp(n),
  fstatus = sample(0:1, size = n, replace = TRUE),
  y = rnorm(n = n),
  x1 = factor(sample(LETTERS[1:4], size = n, replace = TRUE)),
  x2 = rnorm(n, mean = 3, 2),
  x3 = rnorm(n, mean = 3, 2),
  x4 = factor(sample(letters[1:3], size = n, replace = TRUE)),
  stringsAsFactors = FALSE)

library(survival)
library(splines)
fit <- coxph(Surv(ds$ftime, ds$fstatus == 1) ~ x1 + ns(x2, 4) + x3 + strata(x4), data = ds)

printCrudeAndAdjustedModel(fit, add_references = TRUE)
```

```{r}
# Note that the crude is with the strata
a <- getCrudeAndAdjustedModelData(fit)
a["x3", "Crude"] == exp(coef(coxph(Surv(ds$ftime, ds$fstatus == 1) ~ x3 + strata(x4), data = ds)))
```
