Guide
phit
phit is a PHP extension, written in Rust, for statistical models. It fits linear, logistic, probit, Poisson and mixed-effects regression models, and evaluates probability distributions and descriptive statistics. Data go in as ordinary PHP arrays and results come back as associative arrays, with no model objects or services.
Models are written as R formulas: rent ~ area + district models rent from floor area and district. Factors, interactions and transformations such as log(area) are written in the formula, and phit builds the numeric design from named columns.
use function Phit\{fit_lm, predict};
$fit = fit_lm('rent ~ area + district', $flats);
$fit['named']; // ['(Intercept)' => 352.64, 'area' => 19.18, 'districtnorth' => 203.64, ...]
$fit['p_values']; // One per coefficient, in the same order
$fit['r_squared']; // 0.927
$p = predict($fit, ['area' => [55.0], 'district' => ['east']], interval: 'prediction');
$p['fit']; // [1407.55], with $p['lower'] and $p['upper']A first model runs this example with its data and explains each part of the result.
What phit provides
- Regression models:
fit_lm()andfit_glm()for linear, logistic, probit and Poisson regression, with weights and offsets. See Linear and generalized linear models. - Mixed models:
fit_lmer()for Gaussian models with random intercepts and slopes, andfit_glmer()for a binomial model with a random intercept. See Mixed models. - Prediction on new data with confidence and prediction intervals.
- Probability distributions: densities, CDFs, survival functions and quantiles for 12 families (13 parameterizations), and seeded random samples, including uniform, categorical and permutation sampling. See Probability distributions and Seeded sampling.
- Descriptive statistics: means, variances, quantiles, weighted statistics, kernel density estimates and the ECDF. See Descriptive statistics.
- Posterior draws: weighted moments and transforms for MCMC output. See Posterior draws.
- CSV input: a native reader used by phibble. See Reading CSV files.
Each model also has a function over numeric matrices, such as fit_ols() and fit_logistic(), for code that builds its own design matrix. See Matrix model functions.
Results
Results follow R’s conventions: coefficient names, treatment contrasts, the order of terms, deviance and AIC as glm() reports them, and lme4’s REML estimates. Rows with missing values are dropped with a warning, and every result reports which rows it used. Errors are PHP exceptions with stable numeric codes. The verification pages compare each model with R, lme4 and statsmodels, and record the tolerances.
Requirements
phit runs on PHP 8.3, 8.4 and 8.5 (non-thread-safe builds) on Linux and macOS. It installs with PIE, or from prebuilt Linux binaries. See Installation. The pure-PHP polyfill provides the same functions where the extension cannot be installed, at lower speed.
From phpstats/phit@76b22f0.