Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-Way Repeated Measures ANOVA + contrasts #226

Closed
dengemann opened this issue Dec 5, 2012 · 17 comments
Closed

2-Way Repeated Measures ANOVA + contrasts #226

dengemann opened this issue Dec 5, 2012 · 17 comments
Labels
Milestone

Comments

@dengemann
Copy link
Member

In fact, up to this day nothing equivalent to R's anova function seems to exist in python. As this reflects one of the most commonly used deigns in cognitive neuroscience, it would be nice to have this in mne-python or at least stats-models rather than using R, SPM etc. This is also somewhat related to #133. I think it would be nice to stick with a GLM framework and think about R-formula language as currently provided by python patsy to set contrasts, etc. Also I would like to have this general enough to cover ICA -, Sensor- , and source space.

Wdyt?

@agramfort
Copy link
Member

++++++1 :)

@dengemann
Copy link
Member Author

... won't be 0.5. however ;-)

@christianbrodbeck
Copy link
Member

I have a basic implementation of ANOVA capability for balanced and completely specified models at https://github.com/christianmbrodbeck/Eelbrain/blob/master/eelbrain/analyze/glm.py . Model specification is done with the factors I described in Issue #133, but that could be modified.

@christianbrodbeck
Copy link
Member

To be more specific:

  • for univariate dependent variables, I have implemented incremental f-tests for fixed effects models which can include scalar and categorial predictors example.
  • For balanced models I have also implemented random effects models example.
  • For the latter method I have added a class that can fit the model to several data points simultaneously, which can then be used for a cluster permutation test example. Here the ndvar object is useful because you can submit data with an arbitrary dimensions (time, sensor, source, ....) and get the result map of F- or p-values (or cluster extent) back with the same dimensions (minus the 'case' dimension).

@dengemann
Copy link
Member Author

Hi chris,

thanks! let's get back and see how we can interface / support integrate your efforts as the anova issue will transform into a WIP PR. Maybe we can setup that up together. From my side however not before 0.5

Best,
Denis

On 06.12.2012, at 00:50, Christian Brodbeck notifications@github.com wrote:

I have a basic implementation of ANOVA capability for balanced and completely specified models at https://github.com/christianmbrodbeck/Eelbrain/blob/master/eelbrain/analyze/glm.py . Model specification is done with the factors I described in Issue #133, but that could be modified.


Reply to this email directly or view it on GitHub.

@dengemann
Copy link
Member Author

Addressed by #580. Closing.

@TheChymera
Copy link

hey there, so does mne-python have a repeated measures anova? if so what's the name of the function - and are there any docs?

I'd also be willing to provide a more general package atom (ebuild) for my distribution (gentoo) if I can use your software.

@agramfort
Copy link
Member

hi,

look at this diff:

https://github.com/mne-tools/mne-python/pull/580/files

you'll see the example and the function of interest to you is : f_twoway_rm

the rendered example is at :

http://martinos.org/mne/auto_examples/stats/plot_cluster_stats_time_frequency_repeated_measures_anova.html

if some doc or function is missing don't hesitate to send us a pull request to help us making MNE-Python better.

Regarding gentoo packaging that would be great. Maybe @mluessi can give a hand too as a gentoo user.

Hope this helps,
Alex

@dengemann
Copy link
Member Author

Hi @TheChymera , now @agramfort was faster. Two things to add. First, this is optimized for mass-univariate use cases (estimate thousands of models at once), the interface is hence more Matlab-like, matrix oriented an allows for passing large matrices with thousands of observations per person. Functionality is tested against R and SPSS and of course you can also pass behavioral data with one observation per subject. Second currently we only have 2-way repeated measures ANOVA. Extending the code is relatively easy though, e.g. supporting more factors, non-repeated factors (e.g. groups) and covariates. However I did not have a real use case for this since simple 2-way ANOVA models are already quite complex in an MEG context. From my point of view adding groups support, a third factor and a covariate would make sense. From your post records across different projects (Psychopy, statsmodels) it seems you're looking for standard functionality / sth. for behavioural data. Why not R then? Gentoo packaging is of course a nice idea ;-) and of course if you're interested in extending our ANOVA support contributions are always welcome.

@TheChymera
Copy link

@dengemann Well, I want to keep all of my data post-processing and analysis in python instead of switching between programs. I tried Rpy but for some reason I have issues formatting my pandas dataframe for R...

Anyway, do you have a one-way repeated measure ANOVA? or cam I use f_twoway_rm for that as well? I have 5 conditions presented to 10 participants with 100 trials each - I'm only interested in the variance of the conditions, not of the participants etc.

@dengemann
Copy link
Member Author

@TheChymera

Here is some pseudo code / snippet thatdemonstrates how I use Rpy (for 'small' data / when it comes to standard analyses I always do this at some point --- I guess Python will never, at least not in the next 5-10 years catch up with all the general stats provided in R -- it's strength is in particular and advanced packages. Python makes tackling difficult / new problems fun, so many developers / scientists choose it to start a new project rather than reimplementing what's e.g. in R. People simply want to work on new stuff ;-). I can see that Rpy does not come with a self-evident API and you need to know both R and Python to take full advantage of it. But given my prediction it's worth the effort, and actually it works nicely.)

import pandas.rpy.common as com
import pandas as pd
base = com.importr('base')
stats = com.importr('stats')

# .... read data and stuff, a few lines omitted
aggfunc = np.mean  # how to aggregegate
agglist = ['subject', 'A', 'B', 'C']  # splitby 
fml = 'latency ~ A * B * C + Error(subject/ (A * B * C))'  #  formula string
df_ = df.groupby(agglist).agg(aggfunc).reset_index()  # final structure to be tested
dfr = com.convert_to_r_dataframe(df_, True)  # convert from pandas to R and make string columns factors
fml_ = Formula(fml)  #  make a formula    obect
print str(base.summary(stats.aov(fml_, dfr)))

As to the one-way in mne-python.... I don't know whether the implemented version accepts 1-factor only input, I never tried. You would need to setup a matrix with 5 columns (one for each level) and try to pass [5] as factor_levels argument.

@dengemann
Copy link
Member Author

Btw. you can also take a look at Roger Lew's pyvttbl package https://pypi.python.org/pypi/pyvttbl/0.4.1.0, I think this the most comprehensive collection of ANOVA models in Python. I drew from there and we actually had discussions at statsmodels to refactor his code and add it there. But no one including himself or myself had time for it.

@mluessi
Copy link
Contributor

mluessi commented Nov 7, 2013

@TheChymera having mne-python in the gentoo science overlay would be perfect. I will be happy to assist with making an ebuild :).

@TheChymera
Copy link

@mluessi I'm caught up quite a bit in my master thesis until the start of december, so since I won't be using the mne-python ANOVA function, I'll only get to the ebuild after that. There are a number of high-profile neuroscience packages still not merged into gentoo-sci - there's a list with them (as well as some info on neuro~gentoo) in this neurogentoo guide. If you have any provisorial ebuild for mne-python I can add it to the listing, maybe someone will notice it even before I get more time.

@dengemann
Copy link
Member Author

@TheChymera btw I took a look at running one-way repeated measures with our ANOVA. The contrast coding as is implemented right now prevents such a model, adding support for it should not be too difficult though. I just won't have time for it, unless an urgent use case emerges on my end which I don't see coming.

@mluessi
Copy link
Contributor

mluessi commented Nov 8, 2013

@TheChymera thanks for the link. I will look into making an mne-python ebuild, it should be terribly simple, unlike e.g. packing for Debian ;). Good luck with your thesis.

@chrisdembia
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants