In Neuroscience there is a strong tradition to use Matlab for most of our data analyses. The reasons for this trend can be mainly grouped in historical and practical ones. Indeed, some of the most commonly used toolboxes for analysing fMRI or EEG/MEG data were initially written in Matlab, which allegedly has a rich graphical interface, allowing users to double-click on variables and inspect or even change their content. Whether this feature is advantageous or toxically dangerous for reproducibility of any matlab-based findings, is of course open to discussion.
Although matlab was very popular back in the days when some of the most common Neuroscience toolboxes, like for example SPM, were first introduced, today there are several alternatives to it, like Python or R, most of which are free for anyone to use, open source, and do not require buying any licence. Who wouldn’t want to minimize close encouters with this window here?
Alternative packages to Matlab, apart from being free, also come with a large community around them, who constantly develops new functions, has faced most of the bugs that a new user may encounter, and openly discusses how to solve them on the web or social media. Thanks to such communities, projects like R or Python now come with powerful toolboxes, for advanced statistical analyses, that one cannot easily run in Matlab anymore, like for example specialized toolboxes for deep learning, machine learning, or statistical modeling. It was thanks to such tools that I initially decided to take the big leap and, after 9 years of non-stop Matlab use, cautiously dive into the worlds of Python and more recently, R.
Installing R is relatively easy, and if one is hooked to graphical environments there are some decent solutions that closely match Matlab’s interface, like R Studio. Downloading and installing most of the R packages that one will eventually need is just a
install.packages(“package_name”) command away, but what requires some effort is getting used to new programming commands and handling data in an R-compatible manner. R, like Python, works amazingly well with CSV files, or, thanks to the R.matlab project, directly with Neuroscientists’ favourites, ‘.mat’ files.
Although I would strongly discourage anyone who wants to build a project in R from the scratch to store their data in ‘.mat’ files, the R.matlab project is an awesome initiative, as it can get someone started in R with minimal effort. For example, one can easily import their already pre-processed electrophysiological M/EEG data in R and model them, or plot results.
Of course, before doing that, data need to be formatted accordingly: a very volatile, albeit memory-consuming way of handling data in R are the so-called Data Frames. One can think of them as generic matrices, that group different types of variables. Each row of a data frame contains all the variables related to a given data point. In the case of EEG data for example, where we usually measure time-courses of voltage potentials on several electrodes on the scalp, some of the variables related to a measurement, could be ‘Time’, ‘Channel’, ‘Condition’, and of course, the measurement itself, ‘Voltage’:
And so on, and so forth.
This structure is slightly different from the logic of Matlab, where data are usually organized in vectors and matrices. For the above-mentioned example, we would typically have one matrix of ‘Voltage’ values per experimental condition, of Channels x Time, containing the time-course of average ERP data for each EEG channel. Additionally, there would be one vector for Time and another vector for Channel labels, or, if using a software like Fieldtrip, all of these variables would be grouped in one structure for each experimental condition.
R can support matrices and vectors too, but, if we want to see R as a versatile plotting tool as well, then data frames is the way to go, as they provide a wide range of possibilities for easily plotting, grouping or splitting data.
In order to give a gist of how data frames work for M/EEG data, and do some first steps in R in a relatively safe, top-down way, I have uploaded some code to transform generic EEG data, which many of us usually pre-process in Matlab, in an R compatible format. For this, I have used some sample MEG data that are provided by the Fieldtrip community and can be downloaded here and here. The full data explanation can be found on the Fieldtrip wiki. You can find some Matlab code here for the initial data formatting, and then, some R code here, to produce average Event-Related Field plots, for some exemplar (randomly chosen) MEG channels:
The initial data import can be easily adjusted to include any other matrix or vector-based M/EEG data format in Matlab. More functions, statistics and plotting options in R are about to come!