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().
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.
prepare_image()
prepare_audio()
prepare_video()
ollama_backend()
instrument()
template_items() (optional)
preview() (optional)
measure()
item_report()
inspect_cases() (optional)
as_wide()
measure() into one row per document and one column per question, each holding the probability of the target label (e.g., the probability of "yes"). For exclusive items, each category gets its own column instead.coding_sample()
coder_reference() (optional)
coding_sample() sheets into one, allowing a group of coders to serve as the single reference for validate_sample().validate_sample()
measure() against a completed coding_sample() sheet in terms of discrimination and ranking, with calibration on request. Given a named list of sheets, it also reports intercoder reliability and how far the model's responses are from the coders.threshold_sensitivity()
The package website carries the reference index and several articles.
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.
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")
Development of this package was assisted by Claude Code, an AI coding assistant by Anthropic, for code review, documentation drafting, and the validation components.