infer R Package

repository·main·Indexed 21 days ago

https://github.com/tidymodels/infer

An R package for tidyverse-friendly statistical inference using a unified, verb-based grammar. It provides a structured pipeline via specify(), hypothesize(), generate(), and calculate() to perform randomization-based hypothesis testing and bootstrapping. The package includes utilities for visualizing distributions with visualize(), shading p-values or confidence intervals, and extracting results using get_p_value() and get_confidence_interval().

Tokens
2.1K
Snippets
5
Records
8
Agent score
72%

What's inside infer

  1. How the infer grammar works

    main

    The infer package uses an expressive, verb-based grammar to perform statistical inference (both hypothesis testing and confidence interval construction) following tidyverse principles. Instead of calling specific test functions (like a t-test), you build a pipeline using four main verbs that abstract the underlying statistical process.

    Hypothesis Testing Workflow

    To test if an effect is real or due to chance, follow this pipeline:

    1. specify(): Define the variable or relationship of interest.
    2. hypothesize(): Declare the null hypothesis (e.g., that there is no difference).
    3. generate(): Create a null distribution by simulating data that reflects the null hypothesis (randomization) or by using bootstrapping.
    4. calculate(): Compute summary statistics from both the observed data and the generated null data to form the test statistic and the null distribution.

    Confidence Interval Workflow

    To construct confidence intervals via bootstrapping, use a similar pipeline but omit the hypothesize() step:

    1. specify()
    2. generate() (using bootstrap)
    3. calculate()

    Visualizing and Extracting Results

    Once the pipeline is complete, use these utilities to interpret the output:

    • For Hypothesis Testing:
      • visualize(): Plots the null distribution.
      • shade_p_value(): Shades the region of the distribution as extreme or more extreme than the observed statistic.
      • get_p_value(): Calculates the p-value by comparing the observed statistic to the null distribution.
    • For Confidence Intervals:
      • visualize(): Plots the bootstrap distribution.
      • shade_confidence_interval(): Situates the confidence interval region within the distribution.
      • get_confidence_interval(): Calculates the specific bounds of the interval.
  2. How the statistical inference workflow works in infer

    main

    The infer package uses a statistical grammar centered around four main verbs to perform randomization-based inference. This workflow follows a specific sequence:

    1. specify(): Define the variable or relationship (e.g., using a formula like y ~ x) that you are interested in.
    2. hypothesize(): Declare the null hypothesis (e.g., null = "independence").
    3. generate(): Create data that reflects the null hypothesis (e.g., using type = "permute" and specifying the number of reps).
    4. calculate(): Compute a distribution of statistics from the generated data to form the null distribution.

    This process can be visualized to compare the observed statistic against the generated null distribution.

    # Example of the full pipeline logic
    F_hat <- gss |> 
      specify(age ~ partyid) |
      calculate(stat = "F")
    
    null_dist <- gss |> 
       specify(age ~ partyid) |
       hypothesize(null = "independence") |
       generate(reps = 1000, type = "permute") |
       calculate(stat = "F")
  3. Install the infer R package

    main

    You can install the current stable version of infer from CRAN using install.packages(). To install the developmental stable version, use the pak package.

    CRAN (Stable)

    install.packages("infer")

    GitHub (Developmental)

    # install.packages("pak")
    pak::pak("tidymodels/infer")
    install.packages("infer")
  4. Use formula and non-formula interfaces in infer

    main

    The infer package provides flexibility in how you define variables. You can use the standard R formula interface (e.g., age ~ partyid) or a non-formula interface (e.g., response = age, explanatory = partyid).

    While both work for all implemented inference procedures, it is recommended to use the formula y ~ x notation if you plan to integrate with modeling functions like lm() or glm().

  5. Understand the four main verbs of the infer statistical grammar

    main

    The infer package uses an expressive statistical grammar designed to work with the tidyverse. The workflow is centered around four primary verbs:

    1. specify(): Define the variable or the relationship between variables of interest.
    2. hypothesize(): Declare the null hypothesis.
    3. generate(): Create data that reflects the null hypothesis (e.g., via permutation).
    4. calculate(): Compute a distribution of statistics from the generated data to form the null distribution.

    These verbs allow you to perform randomization-based inference in a structured, readable pipeline.

  6. Perform a full statistical inference pipeline

    main

    A typical inference workflow involves calculating an observed statistic, generating a null distribution, and then comparing the two to visualize or calculate a p-value. infer supports both formula interfaces (e.g., y ~ x) and non-formula interfaces (e.g., response = y, explanatory = x).

    Example: Testing if age is independent of partyid using an ANOVA (F-statistic) approach:

    1. Calculate observed statistic: Use specify() and calculate().
    2. Generate null distribution: Use specify(), hypothesize(), generate(), and calculate().
    3. Visualize: Use visualize() combined with shade_p_value().
    4. Calculate p-value: Use get_p_value().
    library(infer)
    data(gss)
    
    # 1. Calculate the observed statistic
    F_hat <- gss |> 
      specify(age ~ partyid) |> 
      calculate(stat = "F")
    
    # 2. Generate the null distribution
    null_dist <- gss |> 
       specify(age ~ partyid) |> 
       hypothesize(null = "independence") |> 
       generate(reps = 1000, type = "permute") |> 
       calculate(stat = "F")
    
    # 3. Visualize the observed statistic alongside the null distribution
    visualize(null_dist) +
      shade_p_value(obs_stat = F_hat, direction = "greater")
    
    # 4. Calculate the p-value
    null_dist |> 
      get_p_value(obs_stat = F_hat, direction = "greater")
  7. Perform an analysis of variance (ANOVA) using infer

    main

    To test if a variable (e.g., age) is independent of a categorical variable (e.g., partyid), follow these steps using the gss dataset:

    1. Calculate the observed statistic: Use specify() with a formula and calculate(stat = "F").
    2. Generate the null distribution: Use hypothesize(null = "independence") followed by generate(reps = 1000, type = "permute") and calculate(stat = "F").
    3. Visualize: Use visualize() combined with shade_p_value() to see the observed statistic relative to the null distribution.
    4. Calculate p-value: Use get_p_value() on the null distribution object.

    Note: You can use either formula notation (y ~ x) or non-formula notation (response = y, explanatory = x). Formula notation is recommended if you plan to use modeling functions like lm() or glm() later.

    # 1. Calculate observed statistic
    F_hat <- gss |> 
      specify(age ~ partyid) |
      calculate(stat = "F")
    
    # 2. Generate null distribution
    null_dist <- gss |> 
       specify(age ~ partyid) |
       hypothesize(null = "independence") |
       generate(reps = 1000, type = "permute") |
       calculate(stat = "F")
    
    # 3. Visualize
    visualize(null_dist) +
      shade_p_value(obs_stat = F_hat, direction = "greater")
    
    # 4. Get p-value
    null_dist |> 
      get_p_value(obs_stat = F_hat, direction = "greater")
  8. Core verbs in the infer package

    main

    The infer package provides four primary functions that form the basis of its statistical inference grammar. These functions are designed to be used in a pipeline to move from raw data to a statistical conclusion.

    specify() %>% 
      hypothesize() %>% 
      generate() %>% 
      calculate()