You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a possible bug that I encountered while running Tukey HSD test on a dataset. The Python ANOVA table found some significant effects (p-values < 0.005) which are same results as with R and SAS.
Yet, Python's Tukey HSD reports no significant comparisons of the factors levels while R find significant comparisons associated with the factors that were significant in ANOVA table.
import statsmodels.formula.api as smf
import pandas as pd
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.graphics.factorplots import interaction_plot
import statsmodels.stats.multicomp as multi
model = smf.ols('Cleaness ~ C(Stain) + C(DETERGENT)', data=melted_df).fit()
aov_table = anova_lm(model, typ=2)
Multiple comparisons : issues are found in the output of this code.
Can you add the R commands and output?
I don't know or remember how to do this in R.
Unfortunately we still don't have multiplicity corrected p-values in tukeyhsd output, but confidence intervals include zero and boundaries are not close to zero.
If I only use detergent as explanatory variable, then it is not statistically significant. So the detergent effect will be only conditional on Stain, i.e. confounded without it.
model = smf.ols('Cleaness ~ C(DETERGENT)', data=melted_df).fit()
aov_table = anova_lm(model, typ=2)
aov_table
sum_sq df F PR(>F)
C(DETERGENT) 110.916667 3.0 1.920635 0.204786
Residual 154.000000 8.0 NaN NaN
I don't know how good the distribution for the studentized range statistic that is used for the critical values and confidence intervals is in small samples.
(Most likely a permutation test would be better in this case, but we don't have them yet in master.)
Hello Josef-pkt,
Thanks for your reply and comments. Yes, I agree that permutation tests will be much appropriate... Please see the attached:
I expected the last two rows of the Tukey's HSD to match R by providing True as answers instead of False.
Thanks
LZ
I don't see a way how we could use tukeyhsd directly for the comparisons in a multiway model.
What I did in this gist notebook https://gist.github.com/josef-pkt/612e91be58f8c9cd2ab12c3a21c55426
is to build manually a contrast matrix for the all-pairwise comparison and use multipletests for the p-value correction. With the default method for p-value correction, hs, 2 pairwise comparisons are rejected, with fdr_bh (currently in the gist) 3 pairs are rejected. The A-C pair is around 0.05 threshold depending on the method for multiple testing correction.
There are several open issues to add this to statsmodels (e.g. #2687#3297), but it's easier to do in examples, and I haven't worked my way through the general case yet when all we have is the information from the formula.
Because the design is a complete block design, the two categorical variables are orthogonal in that cov_params has a zero (2e-15) block for the covariance between the two sets of coefficients. However, both have non-zero covariance with the constant which captures the reference levels.
However, the type 2 anova_lm result is different when we use the one-way model instead of the two-way model.
Something is "strange".
In cases like this we should be able to exploit the orthogonal structure, but that will not help in the general case. (I don't think reparameterization by using a different categorical encoding will help, because the pairwise comparison will have to be adjusted to compensate for it.)
I'm not completly sure whether this is correct but something like this should work.
Given the orthogonal structure we can take out the Stain effect and then run tukeyhsd on the residual
We reject 3 pairs as in fdr_bh, and the results differ from R
A-C has lower confidence interval close to zero
meandiff are the same as with the other methods, confidence interval and rejection depends on the assumption on the correlation and p-value correction. (using tukeyhsd assumes independent samples)
# Multile comparison : issues are here
mcTreatment2 = multi.MultiComparison(melted_df['Cleaness'] - melted_df.groupby(melted_df['Stain'])['Cleaness'].transform('mean'), melted_df['DETERGENT'])
results_Treatment2 = mcTreatment2.tukeyhsd()
results_Treatment2.summary()
group1 | group2 | meandiff | lower | upper | reject
-- | -- | -- | -- | -- | --
Detergent A | Detergent B | 2.0 | -2.0122 | 6.0122 | False
Detergent A | Detergent C | 4.6667 | 0.6544 | 8.6789 | True
Detergent A | Detergent D | -3.6667 | -7.6789 | 0.3456 | False
Detergent B | Detergent C | 2.6667 | -1.3456 | 6.6789 | False
Detergent B | Detergent D | -5.6667 | -9.6789 | -1.6544 | True
Detergent C | Detergent D | -8.3333 | -12.3456 | -4.3211 | True
Activity
josef-pkt commentedon Mar 11, 2018
Can you add the R commands and output?
I don't know or remember how to do this in R.
Unfortunately we still don't have multiplicity corrected p-values in tukeyhsd output, but confidence intervals include zero and boundaries are not close to zero.
If I only use detergent as explanatory variable, then it is not statistically significant. So the detergent effect will be only conditional on Stain, i.e. confounded without it.
josef-pkt commentedon Mar 11, 2018
maybe the sample size is just too small for this.
I don't know how good the distribution for the studentized range statistic that is used for the critical values and confidence intervals is in small samples.
(Most likely a permutation test would be better in this case, but we don't have them yet in master.)
dcasmr commentedon Mar 11, 2018
Hello Josef-pkt,
Thanks for your reply and comments. Yes, I agree that permutation tests will be much appropriate... Please see the attached:
I expected the last two rows of the Tukey's HSD to match R by providing True as answers instead of False.
Thanks
LZ
From R:
melted_df = read.csv("detergent.csv")
melted_df
model = lm('Cleaness ~ Stain + DETERGENT', data=melted_df)
anova(model)
TukeyHSD(aov(Cleaness ~ DETERGENT + Stain, data=melted_df))
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = Cleaness ~ DETERGENT + Stain, data = melted_df)
$DETERGENT
diff lwr upr p adj
Detergent B-Detergent A 2.000000 -3.0076411 7.0076411 0.5514395
Detergent C-Detergent A 4.666667 -0.3409745 9.6743078 0.0658092
Detergent D-Detergent A -3.666667 -8.6743078 1.3409745 0.1506830
Detergent C-Detergent B 2.666667 -2.3409745 7.6743078 0.3408012
Detergent D-Detergent B -5.666667 -10.6743078 -0.6590255 0.0299015
Detergent D-Detergent C -8.333333 -13.3409745 -3.3256922 0.0048171
josef-pkt commentedon Mar 11, 2018
Thanks, yes that's one way versus two way anova for tukeyhsd
we only have one way tukeyhsd
I'm not sure there is a workaround. (I try to think about it later today, busy with family.)
dcasmr commentedon Mar 11, 2018
Thank you !
josef-pkt commentedon Mar 13, 2018
I don't see a way how we could use tukeyhsd directly for the comparisons in a multiway model.
What I did in this gist notebook
https://gist.github.com/josef-pkt/612e91be58f8c9cd2ab12c3a21c55426
is to build manually a contrast matrix for the all-pairwise comparison and use
multipletestsfor the p-value correction. With the default method for p-value correction,hs, 2 pairwise comparisons are rejected, withfdr_bh(currently in the gist) 3 pairs are rejected. The A-C pair is around 0.05 threshold depending on the method for multiple testing correction.There are several open issues to add this to statsmodels (e.g. #2687 #3297), but it's easier to do in examples, and I haven't worked my way through the general case yet when all we have is the information from the formula.
josef-pkt commentedon Mar 13, 2018
Aside: what I don't understand right now
Because the design is a complete block design, the two categorical variables are orthogonal in that cov_params has a zero (2e-15) block for the covariance between the two sets of coefficients. However, both have non-zero covariance with the constant which captures the reference levels.
However, the type 2 anova_lm result is different when we use the one-way model instead of the two-way model.
Something is "strange".
In cases like this we should be able to exploit the orthogonal structure, but that will not help in the general case. (I don't think reparameterization by using a different categorical encoding will help, because the pairwise comparison will have to be adjusted to compensate for it.)
josef-pkt commentedon Mar 13, 2018
I'm not completly sure whether this is correct but something like this should work.
Given the orthogonal structure we can take out the
Staineffect and then run tukeyhsd on the residualWe reject 3 pairs as in fdr_bh, and the results differ from R
A-C has lower confidence interval close to zero
meandiff are the same as with the other methods, confidence interval and rejection depends on the assumption on the correlation and p-value correction. (using tukeyhsd assumes independent samples)
aside: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/TukeyHSD.html
"The intervals constructed in this way would only apply exactly to balanced designs where there are the same number of observations made at each level of the factor. This function incorporates an adjustment for sample size that produces sensible intervals for mildly unbalanced designs."
It doesn't say anything about multiway effects.
dcasmr commentedon Mar 15, 2018
Thanks for your excellent contribution to this post. I will keep an eye on it along with the other open cases in statsmodels.
[-]BUG: Incorrect Tukey HSD results [/-][+]FAQ/ENH: two-way Tukey HSD[/+]