Just as it is easy to plot, it is easy to do inferential statistics in MATLAB. However, I want to emphasize that this tutorial is not designed to teach statistics - it is simply designed to show people who already who know statistics how to run tests in MATLAB.
Let's start a new script and go back to our reaction time data. Recall that this data is simulated data of the Stroop task, wherein Condition 1 is congruent words and Condition 2 is incongruent words. Based on the literature, we would expect the mean reaction time for Condition 1 to be shorter than the mean reaction time for Condition 2.
clc;
clear all;
close all;
my_data = readtable('my_data.txt');
my_data.Properties.VariableNames = {'Subject' 'Condition' 'RT'};
Let's create two new variables isolating the actual data for a paired samples t-test, each persons mean score for each condition:
con1Data = my_data.RT(my_data.Condition == 1);
con2Data = my_data.RT(my_data.Condition == 2);
Paired or Dependent Samples t-test
Now, the t-test is easy. Given the data, this test requires a Paired or Dependent Samples t-test:
[H p CI STATS] = ttest(con1Data,con2Data)
I have deliberately left the semi-colon off the end so you can see the results in the Command Window. This call returns H, the hypothesis (0 = null true, 1 = alternative true). It of course returns a p value. It returns the CI of the mean difference. It also returns STATS which holds the t score, the degrees of freedom, and standard error of the mean.
Single Sample t-test
Let's say that for some reason, we now want to compare the condition 1 data to a known population mean of 300. This would require a single sample t-test since we do not know the population standard deviation.
[H p CI STATS] = ttest(con1Data,300)
Again, the same variables are returned and you should see this test is not significant.
Independent Samples t-test
Let's pretend that con1Data is the incongruent data from one group on a Stroop Task and con2Data is the incongruent data from another group on the Stroop Task. This would now required an independent samples t-test as a dependent samples t-test is inappropriate.
[H p CI STATS] = ttest2(con1Data,con2Data)
You will note you get the same output as before, but now we are using ttest2, which is the MATLAB function for an independent samples t-test.
As I have said, doing inferential statistics in MATLAB is easy.
Things To Do
1. HERE is some data that is more suited for an ANOVA. It reflects mean reaction time scores for three groups (young, middle, old) on a simple reaction time task. See if you can figure out how to use the anova1 function to analyze this data. It is in the same format as your previous RT data so the commands at the top of this tutorial will help you load it but to be statistically correct you should rename Condition as Group.
2. Plot the results of the ANOVA above with error bars. Use a plot style of your choice but customize it to look good.
3. Write a script that does everything covered in the statistics tutorials - load a file, generate descriptive statistics, generate plots, and do a statistical test. This is what we do in my lab.
4. Advanced: Write a script that checks for outliers in your data. This one is not as hard as it sounds - for example, you could use a for loop to check each entry against a known value...
Let's start a new script and go back to our reaction time data. Recall that this data is simulated data of the Stroop task, wherein Condition 1 is congruent words and Condition 2 is incongruent words. Based on the literature, we would expect the mean reaction time for Condition 1 to be shorter than the mean reaction time for Condition 2.
clc;
clear all;
close all;
my_data = readtable('my_data.txt');
my_data.Properties.VariableNames = {'Subject' 'Condition' 'RT'};
Let's create two new variables isolating the actual data for a paired samples t-test, each persons mean score for each condition:
con1Data = my_data.RT(my_data.Condition == 1);
con2Data = my_data.RT(my_data.Condition == 2);
Paired or Dependent Samples t-test
Now, the t-test is easy. Given the data, this test requires a Paired or Dependent Samples t-test:
[H p CI STATS] = ttest(con1Data,con2Data)
I have deliberately left the semi-colon off the end so you can see the results in the Command Window. This call returns H, the hypothesis (0 = null true, 1 = alternative true). It of course returns a p value. It returns the CI of the mean difference. It also returns STATS which holds the t score, the degrees of freedom, and standard error of the mean.
Single Sample t-test
Let's say that for some reason, we now want to compare the condition 1 data to a known population mean of 300. This would require a single sample t-test since we do not know the population standard deviation.
[H p CI STATS] = ttest(con1Data,300)
Again, the same variables are returned and you should see this test is not significant.
Independent Samples t-test
Let's pretend that con1Data is the incongruent data from one group on a Stroop Task and con2Data is the incongruent data from another group on the Stroop Task. This would now required an independent samples t-test as a dependent samples t-test is inappropriate.
[H p CI STATS] = ttest2(con1Data,con2Data)
You will note you get the same output as before, but now we are using ttest2, which is the MATLAB function for an independent samples t-test.
As I have said, doing inferential statistics in MATLAB is easy.
Things To Do
1. HERE is some data that is more suited for an ANOVA. It reflects mean reaction time scores for three groups (young, middle, old) on a simple reaction time task. See if you can figure out how to use the anova1 function to analyze this data. It is in the same format as your previous RT data so the commands at the top of this tutorial will help you load it but to be statistically correct you should rename Condition as Group.
2. Plot the results of the ANOVA above with error bars. Use a plot style of your choice but customize it to look good.
3. Write a script that does everything covered in the statistics tutorials - load a file, generate descriptive statistics, generate plots, and do a statistical test. This is what we do in my lab.
4. Advanced: Write a script that checks for outliers in your data. This one is not as hard as it sounds - for example, you could use a for loop to check each entry against a known value...