pingouin icon indicating copy to clipboard operation
pingouin copied to clipboard

Adding planned contrasts

Open rnesti opened this issue 2 years ago • 2 comments

Hi @raphaelvallat,

Could you please add a possibility to test (planned) contrasts ?

Referring to Andy Field's Goggles.sav example from previously:

What I could imagine would be a function planned_contrasts() used like:

pg.planned_contrasts(data = data, dv = "Attractiveness", between = ['Alcohol'], contrasts = [-2,1,1] )

which would compare Placebo vs. Low dose & High dose.

Changing contrasts to [0,-1,1] would copare Low dosis vs. High dosis

Other contrasts would also be cool :)

Thank you very much!

Best regards!

rnesti avatar Dec 13 '21 21:12 rnesti

Hi @rnesti,

Thanks for this suggestion. I agree that this would be a useful addition. However, it will take a significant amount of time to implement and test, and it is therefore unlikely to be implemented in Pingouin in the near future. I know that there is a Contrast module in statsmodels, have you looked into it?

Thanks, Raphael

raphaelvallat avatar Dec 14 '21 01:12 raphaelvallat

Hi @raphaelvallat,

You're welcome!

Yes, I know the Contrast site of statsmodels. Sadly, It is also not possible to use the proper contrasts for planned contrasts. To get silimar results to JAMOVI you have to use the Sum contrast in the formula-based statsmodels:


import statsmodels.formula.api as smf

linear_model = smf.ols(" Y ~ C(X1, Sum) * C(X2, Sum)" , data = data) 

However, the problem is that you can't use additional contrasts, so the categoricals are "blocked" with the Sum contrast. A workaround would be to use the models t_test() function and pass it the contrasts like:


linear_model.t_test([2,-1,-1,0,0]) 

But this isn't working, because you are not able to access the reference levels. You could then use -1 to remove the Intercept but then the Intercept will be replaced with the mean of the first factor.

Best regards!

rnesti avatar Dec 14 '21 17:12 rnesti