fathom: Qualitative Coding and Measurement with Local Language Models

Dictionary Definition

The fathom R package is a toolkit for qualitative coding and measurement of a corpus of text, images, audio, or video with local large language models (LLMs), where every answer comes back as a probability rather than a word.

With the function measure(), R users can ask an Ollama-hosted LLM a constrained set of labeled questions about a corpus document and get back the model's next-token probability distribution for those constrained labels. The questions themselves are built and bundled with the function instrument(). The function item_report() provides a clear summary of how each question behaved across the entire corpus, while the function inspect_cases() displays documents that the model answered with a specific label. The function as_wide() prepares the output for any downstream analysis by pivoting the results so that each row is one document and each question becomes a column. To audit the model's answers, the function coding_sample() samples a random set of document-question pairs for human validation, which is then executed through the function validate_sample().

Motivation

Getting an LLM's response about a question provides no insight as to how confident it was in determining that answer. This is further obfuscated if a prompt does not provide a clear and explicit instruction to answer with a set of constrained response labels. Constraining a labeled set of responses and extracting their log probabilities allows a user to use the responses as a classification tool or continuous measurement item. Additionally, being able to read a model's probability distribution allows a user to assess how much probability mass was placed on each of the labels, revealing the presence of a compound question, a missing category, or a bad prompt.

The fathom workflow

0. Prepare the corpus (non-text corpora)

prepare_image()

prepare_audio()

prepare_video()

1. Prepare a local model

ollama_backend()

2. Write the coding instrument

instrument()

template_items() (optional)

preview() (optional)

3. Extract response probabilities

measure()

4. Summarize the results

item_report()

inspect_cases() (optional)

as_wide()

5. Validate response probabilities

coding_sample()

coder_reference() (optional)

validate_sample()

6. Get measurement diagnostics (optional)

threshold_sensitivity()

The package website carries the reference index and several articles.

Requirements

A running Ollama server at version 0.12.11 or later, and a compatible Ollama instruction-following model.

ffmpeg for measuring audio and video.

whisperx or whispermlx (Apple Silicon optimized) for measuring audio, with a HuggingFace User Access Token and access to the Community-1 model to use speaker diarization.

See the Quick start page for information on how to set each of these up.

Installation

fathom is distributed through R-Universe.

install.packages(
  "fathom",
  repos = c("https://alcocer-jj.r-universe.dev", "https://cloud.r-project.org")
)

The development version installs from GitHub.

# install.packages("pak")
pak::pak("alcocer-jj/fathom")

Acknowledgments

Development of this package was assisted by Claude Code, an AI coding assistant by Anthropic, for code review, documentation drafting, and the validation components.