Fit logistic and Poisson models
Model married women’s labor-force participation with logistic regression, and car insurance claim rates with Poisson regression, both with fit_generalized_linear().
The examples read the data by URL with phibble (composer require phpstats/phibble) and run with the phit extension or the plain-PHP polyfill (Extension or polyfill).
Logistic regression
The data are mroz: 753 married women in 1975, from the Panel Study of Income Dynamics. lfp is yes if the woman was in the labor force, k5 is her number of children under 6, age is in years, wc is yes if she attended college, and inc is the family’s income without her earnings, in $1,000.
use Phibble\DataFrame;use function Phit\{fit_generalized_linear, predict};$mroz = DataFrame::fromCsv( 'https://raw.githubusercontent.com/phpstats/datasets/main/csv/mroz.csv',);$logit = fit_generalized_linear( 'lfp ~ k5 + age + wc + inc', $mroz, family: 'binomial',);echo $logit, "\n";$t = $logit->tidy(confInt: true);echo DataFrame::fromColumns([ 'term' => $t['term'], 'odds_ratio' => array_map('exp', $t['estimate']), 'conf_low' => array_map('exp', $t['conf_low']), 'conf_high' => array_map('exp', $t['conf_high']),]), "\n\n";$women = [ 'k5' => [0, 1], 'age' => [35, 35], 'wc' => ['no', 'no'], 'inc' => [20, 20],];$p = predict($logit, $women, type: 'response');echo DataFrame::fromColumns([ 'k5' => $women['k5'], 'probability' => $p['fit'],]), "\n";Generalized linear model fit by maximum likelihood
Family: binomial (logit)
Formula: lfp ~ k5 + age + wc + inc
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.38634 0.52816 6.412 1.44e-10
k5 -1.42976 0.19123 -7.477 7.62e-14
age -0.05672 0.01128 -5.030 4.90e-07
wcyes 1.08181 0.19811 5.461 4.74e-08
inc -0.03141 0.00765 -4.105 4.04e-05
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 1029.75 on 752 degrees of freedom
Residual deviance: 924.31 on 748 degrees of freedom
AIC: 934.31
# A tibble: 5 × 4
term odds_ratio conf_low conf_high
<chr> <dbl> <dbl> <dbl>
1 (Intercept) 29.6 10.5 83.2
2 k5 0.239 0.165 0.348
3 age 0.945 0.924 0.966
4 wcyes 2.95 2.00 4.35
5 inc 0.969 0.955 0.984
# A tibble: 2 × 2
k5 probability
<int> <dbl>
1 0 0.684
2 1 0.341
lfpis a string column, so it becomes a factor. Its first level,no, is failure, and the model gives the log odds ofyes.exp()turns a log-odds coefficient into an odds ratio, with the other predictors held fixed.wcyescompares women who attended college with women who did not.- An odds ratio is not a ratio of probabilities.
type: 'response'returns predicted probabilities, which depend on every predictor’s value. - The coefficients describe associations in this sample, not the effect of having a child or attending college.
Poisson regression with an exposure offset
The data are insurance: car insurance claims in the third quarter of 1973 for 64 groups of policyholders. Claims is the number of claims, Holders the number of policyholders, and District the district of residence, where 4 is the major cities.
use Phibble\DataFrame;use function Phit\fit_generalized_linear;$insurance = DataFrame::fromCsv( 'https://raw.githubusercontent.com/phpstats/datasets/main/csv/insurance.csv',);$poisson = fit_generalized_linear( 'Claims ~ factor(District) + offset(log(Holders))', $insurance, family: 'poisson',);echo $poisson;Generalized linear model fit by maximum likelihood
Family: poisson (log)
Formula: Claims ~ factor(District) + offset(log(Holders))
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.03284 0.02691 -75.544 < 2e-16
factor(District)2 0.02237 0.04297 0.520 0.602729
factor(District)3 0.01325 0.05032 0.263 0.792316
factor(District)4 0.22184 0.06158 3.603 0.000315
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 236.26 on 63 degrees of freedom
Residual deviance: 223.53 on 60 degrees of freedom
AIC: 548.85
offset(log(Holders))puts the model on claims per policyholder, so groups of different sizes are comparable.- District 1 is the reference. The exponentiated intercept is its claim rate per policyholder, and each district coefficient’s exponent is that district’s rate ratio to district 1.
- The rate ratios are unadjusted for car group and age, which also differ between districts.
- The Poisson model assumes the variance equals the mean. Here the residual deviance is far above its degrees of freedom, so the variation exceeds that assumption and the standard errors are too small. Car group and age, left out of this model, account for part of it.
See also: Linear and generalized linear models for links, weights, result keys, and the differences from R’s glm()