Local LLM Support

Running statlingo with local, offline Large Language Models (LLMs) is an excellent way to avoid internet dependencies, external API costs, and API key requirements. This is particularly useful in classroom settings, private enterprise environments, or when working with sensitive data.

WarningImportant Notice: Local Model Capabilities & Hallucinations

While local/offline LLMs are highly convenient, smaller parameter models (such as 1B to 3B parameters like llama3.2:1b) are prone to severe statistical hallucinations. They frequently:

  • Confuse predictor and response variables.
  • Invert the direction of effects (e.g., interpreting positive coefficients as negative).
  • Misdefine statistics (e.g. confusing F-statistics with t-statistics) or misinterpret Adjusted \(R^2\) behaviors.

For reliable statistical explanations, we highly recommend using at least an 8B+ parameter model (such as gemma4) locally, or using production cloud APIs (such as Gemini 3.5 Flash/Pro, Claude Sonnet 5, etc.).

This guide shows how to run statlingo offline using locally hosted models via Ollama or Llama.cpp.


2. Using Llama.cpp (via OpenAI-Compatible Server)

Llama.cpp allows highly optimized local LLM inference. You can use it with statlingo by running its built-in HTTP server (llama-server), which exposes an API compatible with OpenAI’s endpoint.

Step A: Start the Llama.cpp Server

Run the local server on your machine, specifying your downloaded GGUF model file and a port (e.g. 8080):

./llama-server -m models/llama-3.2-1b-instruct.Q4_K_M.gguf -c 2048 --port 8080

Step B: Run with the R Package

Use ellmer::chat_openai_compatible() to point to your local Llama.cpp server port:

library(statlingo)
library(ellmer)

model <- lm(dist ~ speed, data = cars)

# Connect to the local llama-server
client <- chat_openai_compatible(
  model = "local-model",
  base_url = "http://localhost:8080/v1",
  api_key = "not-needed"
)

explanation <- explain(model, client = client, audience = "student")
print(explanation)

Step C: Run with the Python Package

Use chatlas.ChatOpenAICompletions to point to your local Llama.cpp server port:

import statsmodels.api as sm
from chatlas import ChatOpenAICompletions
from statlingo import explain

# Fit a model
duncan_data = sm.datasets.get_rdataset("Duncan", "carData").data
y = duncan_data["prestige"]
X = sm.add_constant(duncan_data[["income"]])
model = sm.OLS(y, X).fit()

# Connect to the local llama-server
client = ChatOpenAICompletions(
    model="local-model",
    base_url="http://localhost:8080/v1",
    api_key="not-needed"
)

explanation = explain(model_object=model, client=client, audience="student")
print(explanation["text"])