Skip to content
DuckDB community extension

stats_duck

A statistical computing toolkit for DuckDB, with descriptive statistics, hypothesis tests, R-style distribution functions (d/p/q/r), linear and generalized linear models with robust and cluster-robust standard errors, linear hypotheses and estimated marginal means, grammar-of-graphics charts (VISUALIZE to Vega-Lite), and readers and writers for SAS, SPSS and Stata files, all in SQL.

01 / SETUP

Install and load

Run these commands in a Haybarn SQL session. The extension is available from the community repository.

Haybarn 2.0.0 · SQL
INSTALL stats_duck FROM community;
LOAD stats_duck FROM community;

This published WASM build cannot load in the current browser session. Use a native Haybarn session for now.

Using upstream DuckDB? Follow the maintainer’s installation instructions.

02 / QUERY

Usage example

R-style distribution functions (d/p/q/r for 15+ families):

SQL session and output
-- R-style distribution functions (d/p/q/r for 15+ families):
D SELECT pnorm(1.96) AS p, qt(0.975, 30) AS t_crit, dpois(3, 2.5) AS pmf;
┌────────────────────┬────────────────────┬─────────────────────┐
│         p          │       t_crit       │         pmf         │
│       double       │       double       │       double        │
├────────────────────┼────────────────────┼─────────────────────┤
│ 0.9750021048517796 │ 2.0422724563012373 │ 0.21376301724973648 │
└────────────────────┴────────────────────┴─────────────────────┘

-- OLS regression as an aggregate (one model per GROUP BY), with robust SEs.
-- vcov: 'const' | 'HC0'..'HC3' | 'CR0'/'CR1' (cluster-robust, + a cluster key).
D SELECT (c).term, (c).estimate, (c).std_error
  FROM (
    SELECT unnest((lm_fit(y, [x1, x2], 'HC1')).coefficients) AS c
    FROM my_data
  );

-- A generalized linear model as an aggregate: poisson with a log link.
-- Families: gaussian, binomial, poisson, gamma. Options (link, vcov,
-- intercept, weights, offset, cluster) go in a trailing STRUCT.
D SELECT (c).term, (c).estimate, (c).std_error
  FROM (
    SELECT unnest((glm_fit(events, [x1, x2], 'poisson', {'vcov': 'HC1'})).coefficients) AS c
    FROM my_data
  );

-- A clinical "Table 1" over mixed variable types, with between-group tests:
D SELECT * FROM table_one('patients', variables := ['age', 'sex', 'bmi'], by := ['arm']);

-- Grammar-of-graphics: turn a query into a Vega-Lite spec (+ the SQL to feed it):
D VISUALIZE wt AS x, mpg AS y FROM cars DRAW point;

Provided by KoliStat. Not yet verified on Haybarn 2.0.0.

Sample data & setup

About this extension

A statistical computing toolkit that brings common statistical work into DuckDB SQL. Every function is a streaming aggregate or a scalar function, so it runs inside GROUP BY and window frames over large tables, and the same build runs in DuckDB-WASM in the browser.

Full function reference: https://github.com/KoliStat/the-stats-duck.

Project overview from the community descriptor.

Documentation and resources

Project and build details
Repository
KoliStat/the-stats-duck
License
Apache-2.0
Maintainers
caerbannogwhite
Written in
C++
Build source
98aa4e305ddd
Build definition
Community descriptor