Week 2 - PART II: Computer Script For Analysis of Cognitive Experiment On Intelligence
Week 2 - PART II: Computer Script For Analysis of Cognitive Experiment On Intelligence
Files_for_Programming_Practicum_CogNeuro2020_week2.zip
>> cd ~martijn/Desktop/Files_for_Programming_Practicum_CogNeuro2020_week2
You are now in the folder with all the needed files for this week
This week we will further train your freshly learned MATLAB skills by analyzing the data of a
cognitive neuroscience study.
CASE: W
e will examine the data of a new promising therapy that our company is working on
and design a few handy functions to extract meaningful values out of our data to show to the
scientific board for evaluation. As a data scientist, you are hired to evaluate the results.
We are interested in evaluating our new cognitive therapy which claims to ‘improve cognitive
performance’ of the participants. For this we took two cognitive measurements in a single
group of participants:
We start with a pilot study on 10 subjects, of which we collected IQ values before and after
treatment. We can now easily summarize all data per measurement into a 1D array (or vector)
in MATLAB, which contains an IQ score for each participant. We will make two separate
variables for now, one for the first measurement and one for the second measurement.
We now want to extract some meaningful values out of our data to test whether our therapy is
any good.
Q2.1: Let’s find the lowest score of the group. In MATLAB, navigate to the exercises f older and
open the function get_minimum.m. This is a function that we wrote for you to calculate the
lowest score of an array of values. Take a look at it and try to understand the logic of the
function.
When you’re ready, use the function in the command prompt to get the lowest IQ in our
sample:
3 Academic Skill : Cognitive Neuroscience Week 2
Q2.2: Now that we know the lowest score, let’s design a new script that gives you the highest
score. Complete the function g
et_maximum in the e
xercises folder so that it returns the
highest value an array. You are not allowed to use the in-built function max.
Hint: copy/paste the code from get_minimum and update it to return the maximum instead of
the minimum.
Q2.3: When you’re done writing your function, use it to get the highest IQ in our sample:
ERROR?! I f your function returns an error or a result you did not expect, you will need to
debug it and fix the error(s). MATLAB has a very neat debugger option that lets you run your
code inside each function, one line at a time, giving you the opportunity to locate any bugs in
the code. Inside your script or function in the editor window, simply click on one of the dashes
(-) at the left-hand side of the screen, right next to the line numbers. A red circle (breakpoint)
will appear, indicating that MATLAB will pause the code right there until you tell it to continue
to the next line.
1) Create a breakpoint somewhere in the script just before where you suspect an error
may occur.
2) In the command window, create some variable A with a few numbers, for example:
>> x = get_maximum(A)
4) Your function will now run until the breakpoint, and you can continue line by line by
4 Academic Skill : Cognitive Neuroscience Week 2
clicking the “Step” button in the editor. This also allows you to check the contents of
your variables during every step!
k>>
When you have corrected your error, you can leave the debug mode by clicking the red ‘Quit
debugging’ button or by typing d
bquit in the command line:
k>> dbquit
(Note: to return to your main script you NEED to leave debug mode, otherwise you will remain
in the function you are debugging. If you re-run your script without leaving debug mode, you
will get in a deeper layer of debugging. To leave this debug-ception, type dbquit twice).
--
Feel free to ask one of the workshop teachers to show that your function is working properly
in a demonstration. This is not a must, but we are looking forward to hearing about your
programming experiences and see that you are on the right way of making scripts, functions
and code! Of course, also ask us if you get stuck and need some tips!
5 Academic Skill : Cognitive Neuroscience Week 2
--
Q2.4: It is always informative to look at the group mean of your measurement. Open the
function g
et_mean.m and fill in the function as such that it computes the mean over an input
array A and gives back the mean value of that array (hint: the mean of a set of numbers is their
sum divided by the number of elements in the set (i.e. length of that array)). You have to make
your own function, you are allowed to use a for-loop and length, but not m
ean and s
um.
Q2.5: Compute the group mean of the first and second measurement using your new
get_mean function. Over the group of 10 subjects, did the IQ score go UP or DOWN after
treatment? (let’s ignore statistical significance for now).
Q2.6: In our dataset we have ‘paired samples’, meaning that each of the subjects was
measured twice. Each column thus matches between the two measurements. We can use this
information to first compute a difference score per subject and second compute the mean
difference score over the entire group. What do you find?
Q2.7: Our scientific board got very excited about our pilot results, so we collected a second,
much larger dataset to further study our therapy. Load in TherapyIQdata.mat
>> clear
(Reminder: clear clears all of your current variables and gives you a fresh MATLAB
workspace)
You can check which (new) variables are added to your MATLAB workspace (the ones you
loaded from the .mat file) by using the command who
>> who
How many subjects were examined in our second sample? (the board spared no expense!).
6 Academic Skill : Cognitive Neuroscience Week 2
With this many subjects, our MATLAB scripts are going to come in very handy - it would be a
lot of work to do calculations on this data manually. So let’s get started!
Q2.8 What is the minimum, maximum, and mean of measurement 1? And of measurement 2?
Use the scripts you made in the exercises above to compute these metrics.
Q2.9: Which subject(s) (provide names) performed the best and worst on measurement 1?
Hint: you can use f
ind to find the index (position) of a certain number in an array. The variable
subjectcodes describes the names of the included participants (which are in the same
order as the IQ measurements).
Q2.10: W
hich subject gained the most from our therapy? Which the least? Tip: make sure to
write your analysis down in a script (e.g., exerciseSOMETHING.m) so you can edit or rerun
the calculations easily.
Q2.11: Calculating descriptive statistics (minimum, maximum etc…) is a good way to get a
sense of the data, but it’s always best to also check the raw data points for any abnormalities.
Plot the IQ scores before the treatment and (in a separate figure) the IQ scores after the
treatment. Hint: use the functions figure and p
lot. Do you see anything unexpected? What
could be the reason?
For this, we first need to sort the data. Make your own function get_sortedvalues.m that
does not use the in-built function s
ort. Your function should take an array A and sort the
values in A from low to high values:
7 Academic Skill : Cognitive Neuroscience Week 2
>> get_sortedvalues(IQ_secondmeasurement_largesample)
51 51 57 57 60 61 61 63 63 64 64
Tip: How to sort an array of values? If you Google ‘sorting algorithms’ there are multiple
options. Two well-known algorithms are ‘bubble sort’ and ‘insertion sort’. Here are their
descriptions. Can you implement (one of them, or both if you want) in MATLAB?
Bubble sort t akes an array and runs through the array of values, 2 consecutive items at a time
(so you need a for loop for this). If item1<item2 then nothing is done (they are already in the
right order), but if item1>item2 they are switched. You run the procedure through the array of
numbers until (so you need a while loop as well) no swaps are made. The end result is a sorted
array of numbers.
Q2.13: E
ven better would be to test whether our therapy has a s
ignificant effect on cognition.
MATLAB has several built-in statistical tests, one of them being a t-test. Does our therapy have
a significant effect on cognition? (hint: help ttest gives you a description on how to use this
MATLAB built-in function).
When you have finished the exercises you can send in your results. What you have to send in
to CANVAS is: