Author: Emily C. Zabor

# Tutorial: Obtain an overall p-value for a factor variable

#### Emily C. Zabor

#### Last updated: January 18, 2024

# Context

After using `eh_test_subtype()` to obtain a model fit, if factor variables are involved in the analysis it will be of interest to obtain overall p-values testing for differences across subtypes across all levels of the factor variable.

The `posthoc_factor_test()` function allows for post-hoc testing of a factor variable.

# Example

    # Load needed packages
    library(riskclustr)
    library(dplyr)

    # create a new example dataset that contains a factor variable
    factor_data <-
      subtype_data %>%
      mutate(
        x4 = cut(
          x1,
          breaks = c(-3.4, -0.4, 0.3, 1.1, 3.8),
          include.lowest = T,
          labels = c("1st quart",
                     "2nd quart",
                     "3rd quart",
                     "4th quart")
          )
        )

    # Fit the model using x4 in place of x1
    mod1 <- eh_test_subtype(
      label = "subtype",
      M = 4,
      factors = list("x4", "x2", "x3"),
      data = factor_data,
      digits = 2
    )

After we have the model fit, we can obtain the p-value testing all levels of `x4` simulaneously.

    mypval <- posthoc_factor_test(
      fit = mod1,
      factor = "x4",
      nlevels = 4
      )

The function returns both a formatted and unformatted p-value. The formatted p-value can be accessed as `pval`:

    mypval$pval
    #>      [,1]
    #> [1,] "<.001"

The unformatted p-value can be accessed as `pval_raw`:

    mypval$pval_raw
    #>      [,1]
    #> [1,]    0