statsmodels
ScienceStatistical analysis
What is statsmodels?
Statsmodels is a Python library that provides classes and functions for the estimation of many different statistical models, as well as for conducting statistical tests and exploring statistical data. It complements SciPy with a higher-level interface for regression, time-series analysis, nonparametric statistics, and more.
With PyRun, you can fit OLS regression models, run hypothesis tests, and explore statistical summaries directly in your browser. Statsmodels is bundled in the Pyodide distribution, so you can start analysing data immediately without any local Python setup.
Code Example
Fit and evaluate an ordinary least-squares model.
OLS Linear Regression
Try in Editorimport statsmodels.api as sm
import numpy as np
np.random.seed(42)
x = np.linspace(0, 10, 50)
y = 3 * x + 7 + np.random.normal(0, 2, size=50)
X = sm.add_constant(x)
model = sm.OLS(y, X).fit()
print("OLS Regression Results")
print("=" * 40)
print(f"R-squared : {model.rsquared:.4f}")
print(f"Intercept : {model.params[0]:.4f} (true: 7)")
print(f"Slope : {model.params[1]:.4f} (true: 3)")
print(f"P-values : {model.pvalues.round(4).tolist()}")
print(f"AIC : {model.aic:.2f}")Why run statsmodels in PyRun?
- ✦ Zero setup — no pip install, no virtual environment, no Python download
- ✦ Instant results — powered by WebAssembly, runs locally in your browser
- ✦ Share your code — generate a link and anyone can run it instantly
- ✦ Works offline — after first load, PyRun runs without internet