Let's keep working with out table from the previous exercise, the three columns on Stroop data in a table called my_data.
Computing basic descriptives in MATLAB is easy, and with the power of tables and logical indexing it is very easy to isolate groups, conditions, and people quickly. For example, to get the mean reaction times for each of the two groups we could simply use the following commands:
meanRTsconditionOne = mean(my_data.RT(my_data.Condition == 1));
meanRTsconditionTwo = mean(my_data.RT(my_data.Condition == 2));
These two commands give us the mean (or average) of my_data.RT when my_data.Condition == 1. We could also pull out this data simply by using:
conditionOneRTs = my_data.RT(my_data.Condition == 1);
conditionTwoRTs = my_data.RT(my_data.Condition == 2);
Do you see the power of logical indexing?
If you wanted a subset of the first 10 participants this would simply be:
subsetOneRTs = my_data.RT(my_data.Subject > 0 & my_data.Subject < 11 & my_data.Condition == 1)
Of course, MATLAB has a multitude of other statistical commands. Try using median, std, and var to calculate the median, standard deviation, and the variance of the data for each group. This would be the same as the first bit of code on this page but with these new commands.
You might think this seems like a lot of work to get some information that you can get from SPSS or EXCEL. However, you can write scripts to automate this. Consider this script that automates making 95% confidence intervals.
clc;
clear all;
close all;
my_data = readtable('my_data.txt');
my_data.Properties.VariableNames = {'Subject' 'Condition' 'RT'};
people_in_conditionOne = sum(my_data.Condition(my_data.Condition == 1));
people_in_conditionTwo = sum(my_data.Condition(my_data.Condition == 2));
dfOne = people_in_conditionOne - 1;
dfTwo = people_in_conditionTwo - 1;
conditionOneRTs = my_data.RT(my_data.Condition == 1);
conditionTwoRTs = my_data.RT(my_data.Condition == 2);
stdOne = std(conditionOneRTs);
stdTwo = std(conditionTwoRTs);
ciRange1 = abs(tinv(0.025,dfOne)*stdOne/sqrt(people_in_conditionOne));
ciRange2 = abs(tinv(0.025,dfTwo)*stdTwo/sqrt(people_in_conditionTwo));
CIsOne(1) = mean(conditionOneRTs) - ciRange1;
CIsOne(2) = mean(conditionOneRTs) + ciRange1;
CIsTwo(1) = mean(conditionTwoRTs) - ciRange2;
CIsTwo(2) = mean(conditionTwoRTs) + ciRange2;
Now lets go through this bit by bit. We do the usual setup and load the data into a table.
Next, we work out how many people are in Condition One and how many people are in Condition Two. We then compute the degrees of freedom (n-1) for teach condition.
Next, we compute the standard deviations for each condition.
The next command is busy, but is essence recall the formula - the CI range is the critical t-value (here computed using tinv(0.025,dfOne) ) * the standard deviation of the data / the square root of the sample size. Make sure you can see all of that. We take the abs or absolute value of that to make sure it is a positive value.
Last of all we get the range of the CIs my subtracting and adding the CI range to the mean of each condition.
While this seems like quite a bit as I said, one you have written this script, assuming your data is in a standard format you can run this as many times as you want. In the next exercise we will automate this further by using a function. You can go there now.
Computing basic descriptives in MATLAB is easy, and with the power of tables and logical indexing it is very easy to isolate groups, conditions, and people quickly. For example, to get the mean reaction times for each of the two groups we could simply use the following commands:
meanRTsconditionOne = mean(my_data.RT(my_data.Condition == 1));
meanRTsconditionTwo = mean(my_data.RT(my_data.Condition == 2));
These two commands give us the mean (or average) of my_data.RT when my_data.Condition == 1. We could also pull out this data simply by using:
conditionOneRTs = my_data.RT(my_data.Condition == 1);
conditionTwoRTs = my_data.RT(my_data.Condition == 2);
Do you see the power of logical indexing?
If you wanted a subset of the first 10 participants this would simply be:
subsetOneRTs = my_data.RT(my_data.Subject > 0 & my_data.Subject < 11 & my_data.Condition == 1)
Of course, MATLAB has a multitude of other statistical commands. Try using median, std, and var to calculate the median, standard deviation, and the variance of the data for each group. This would be the same as the first bit of code on this page but with these new commands.
You might think this seems like a lot of work to get some information that you can get from SPSS or EXCEL. However, you can write scripts to automate this. Consider this script that automates making 95% confidence intervals.
clc;
clear all;
close all;
my_data = readtable('my_data.txt');
my_data.Properties.VariableNames = {'Subject' 'Condition' 'RT'};
people_in_conditionOne = sum(my_data.Condition(my_data.Condition == 1));
people_in_conditionTwo = sum(my_data.Condition(my_data.Condition == 2));
dfOne = people_in_conditionOne - 1;
dfTwo = people_in_conditionTwo - 1;
conditionOneRTs = my_data.RT(my_data.Condition == 1);
conditionTwoRTs = my_data.RT(my_data.Condition == 2);
stdOne = std(conditionOneRTs);
stdTwo = std(conditionTwoRTs);
ciRange1 = abs(tinv(0.025,dfOne)*stdOne/sqrt(people_in_conditionOne));
ciRange2 = abs(tinv(0.025,dfTwo)*stdTwo/sqrt(people_in_conditionTwo));
CIsOne(1) = mean(conditionOneRTs) - ciRange1;
CIsOne(2) = mean(conditionOneRTs) + ciRange1;
CIsTwo(1) = mean(conditionTwoRTs) - ciRange2;
CIsTwo(2) = mean(conditionTwoRTs) + ciRange2;
Now lets go through this bit by bit. We do the usual setup and load the data into a table.
Next, we work out how many people are in Condition One and how many people are in Condition Two. We then compute the degrees of freedom (n-1) for teach condition.
Next, we compute the standard deviations for each condition.
The next command is busy, but is essence recall the formula - the CI range is the critical t-value (here computed using tinv(0.025,dfOne) ) * the standard deviation of the data / the square root of the sample size. Make sure you can see all of that. We take the abs or absolute value of that to make sure it is a positive value.
Last of all we get the range of the CIs my subtracting and adding the CI range to the mean of each condition.
While this seems like quite a bit as I said, one you have written this script, assuming your data is in a standard format you can run this as many times as you want. In the next exercise we will automate this further by using a function. You can go there now.