Repository navigation
Description
We have several open issues for this. e.g. #852, #1668, #2362
The attempt that is currently most integrated with patsy is in an extension of #2687 (comment) extension merged in #4365 t_test_pairwise
There is also preliminary code all over the place, but I never managed to figure out a general structure and API
two possibilities using patsy formula factor information and specifying "base contrasts or encoding" explicitly
patsy
patsy attaches the contrast matrix, cm below, used to build the encoding, #2687 We don't currently use that information.
AFAICS:
- dot product of cm and params provides base parameterization and corresponding cov_params
- cov_params will be singular, and I guess we need to check the rank (?) for estimable contrasts/restrictions
- based on restriction value (
diff) and reparameterized cov_params (where we need only submatrix of cov_params), we can work on selected effects/constraints
arrays and explicit formulas
what do we need?
description of current encoding, e.g. is there a reference level, what is the label for the reference level
labels, column/params indices
target contrast/restriction
mnemonic all pairs, ....
current: ???
- sandbox.regression.penalized contains 3 function like
coef_restriction_meandifffor structured penalization or stochastic constraints. - sandbox.stats.multicomp contains 3 functions like
contrast_all_one(which is one of my early attempts) - sandbox.stats.contrast_tools is a more general attempt based on multicomp
- stats.contrats has pre-statsmodel
contrastfromcolsto check full rank that I never figured out AFAIR - tools.tools has pre-statsmodels
isestimablewhich I also never figured out - special case multivariate with MANOVA tests (2-D params, col and row hypothesis), no multiple testing AFAIR
related: contrasts/constructed exog for prediction, LSMEANS, margins, ... #2167 and hypothesis tests, inference on linear restrictions of predicted means (like treatment effects)
TODO/Usage:
partially based on what I started in #2687
- get term and factor info from design_info
- "normalize", i.e. get a base parameterization for "means", i.e. coefficients
- can be subset cov_params, so we are independent of other params in model?
- construct target restriction/contrast matrix
- t_test
- multiple testing p-value correction, corrected confidence intervals (not available yet, e.g. SUMM/FAQ-D: simultaneous confidence intervals #2955)
- reporting, summary and plots
update in #4365 I combined step 2 "normalize" and step 4 target restriction matrix into a combined restriction matrix that applies to the actual parameterization in the model and can directly be used with t_test and similar.
implementation detail: Where do we put these things?
Aside:
issue ??? suggest using prediction instead of coefficients. This would work for linear prediction (e.g. in GLM) but nonlinear prediction of response would have different effect. Predicted values are likely less close to normal distributed than coefficients, predicted values depend on values of other exog as in margins or predict_at.
prediction and LSMEANS runs into the same problem as here in that we need to construct the auxiliary exog (i.e. artificial sample reflecting the factor levels)
Activity
josef-pkt commentedon Mar 19, 2018
annoying implementation detail:
We need a sign convention.
When we have an ordered set of levels, e.g. A, B, C, do we compute A - B or B - A in the pair comparison?
This looks currently inconsistent across older versions in sandbox.
The labels assigned to the pairs should be consistent with this in literal interpretation,
e.g. label is
A-B, but values are B - Athat is, dash should not interpreted as minus.
TukeyHSD displays with two columns group1 group2, and AFAICS (in what I'm doing now) does not create combined labels.
aside: AFAICS TukeyHSD does not create a results dataframe, no
summary_frame.summary()returns a SimpleTablejosef-pkt commentedon Mar 19, 2018
storing computation for using design_info design matrix for base coefficients.
this works manually, but we don't have a standalone t_test function to do this automatically (maybe #3570 or others that I cannot find)
I added a FakeModel in #3570 (comment) that works for this example
josef-pkt commentedon May 20, 2019
parking two list comprehensions, I don't know if they are useful somewhere
analogue of triu, all pairwise comparisons
simple product of two lists
josef-pkt commentedon Jan 12, 2020
another case
testing: all levels are equal to mean
Even if we have k independent samples, the contrast is a linear combination of all variances. However, the model wald test, like
t_testautomatically compute the cov of an array of linear combinations. We can reuse this for some contrasts even if the intention is for standalone k-sample tests.The contrast/restriction should be something like
np.eye(k) - np.ones((k, k)) / k(patsy has names for various encodings that correspond to different contrasts, maybe is Helmert)
If we want to reuse the models for standalone hypothesis test, then we control the structure of
exogand we don't have to reverse engineer from patsy'sdesign_info.However, we can also just use basic linalg to get cov directly assuming we have
cov_params = diag(var_means). Then cov_restriction is justC @ cov_params @ C'Related: Can we handle trend test for ordered categorical exog, or equality null with trend alternative using the models/results? AFAIR, we are still missing standard (standalone) trend tests.
For independent samples, this would have a specific structure of one-sided hypothesis.
josef-pkt commentedon Jan 24, 2025
contrasts in hypothesis/restrictions on parameters versus contrast encoding in design matrix
(I think I never looked at the details.)
context: contrast helper functions for constraints for hypothesis in sandbox multicomp #9489
When are they the same?
When do they differ?
for example:
In oneway factor, many should be the same
Is that true also for sum to zero coding in unbalanced one-way?
(sum to zero should be similar to one versus average in hypothesis test, or one versus average of others.)
my guess for oneway:
Contrasts with only two nonzero values like (-1, 1) are the same,
Contrasts that involve sum/averages over several levels are only the same in the balanced case (unless contrasts are data dependent)
Can we directly used the contrasts provided by patsy/formulaic as restriction matrices in hypothesis tests?
Everything gets more messy and adds a separate issue for interaction in two- or multi-way factors.
Simpler case:
Two main effects and no interaction effect in unbalanced case: the two main effects are not orthogonal