Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Week 2 - PART II: Computer Script For Analysis of Cognitive Experiment On Intelligence

This document provides instructions for analyzing cognitive experiment data using MATLAB. It describes: 1) Loading IQ score data for 10 participants before and after a cognitive therapy into arrays. 2) Writing MATLAB functions to calculate metrics like minimum, maximum, and mean IQ scores. 3) Using these functions to analyze a larger dataset with IQ scores for over 100 participants. 4) Plotting the IQ scores to check for abnormalities and calculating other metrics like individual score changes.

Uploaded by

Andrei Firte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Week 2 - PART II: Computer Script For Analysis of Cognitive Experiment On Intelligence

This document provides instructions for analyzing cognitive experiment data using MATLAB. It describes: 1) Loading IQ score data for 10 participants before and after a cognitive therapy into arrays. 2) Writing MATLAB functions to calculate metrics like minimum, maximum, and mean IQ scores. 3) Using these functions to analyze a larger dataset with IQ scores for over 100 participants. 4) Plotting the IQ scores to check for abnormalities and calculating other metrics like individual score changes.

Uploaded by

Andrei Firte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Week 2 - PART II: Computer script for analysis of 

cognitive experiment on intelligence 


Data files: ​The files needed for PART II can be found on CANVAS. Download the zip file 

Files_for_Programming_Practicum_CogNeuro2020_week2.zip 

Unzip the zip file and move put the folder 


Files_for_Programming_Practicum_CogNeuro2020_week2 

on your Desktop of your computer 

Go to the directory in your MATLAB command line 

>> cd ​~martijn/Desktop/​Files_for_Programming_Practicum_CogNeuro2020_week2 

NOTE: the part ​~martijn/Desktop/​ will be different on your computer. it 


should be the location on your machine (if you use a windows machine 
it may for example look like C:\ etcetera). 

Instead of typing in cd ~martijn/Desktop/ etc you can also go to the 


folder using the folder button on top of your MATLAB window: 

using the folder button: 


2  Academic Skill : ​Cognitive Neuroscience  Week 2  

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: 

1) an IQ test BEFORE treatment and 2) an IQ test AFTER treatment. 

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. 

>> IQ_firstmeasurement = [100 102 98 112 85 45 120 70 120 125]  

>> IQ_secondmeasurement = [103 100 101 120 85 105 90 70 120 126] 

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  

>> lowestIQ = get_minimum(IQ_firstmeasurement)  

What is the lowest score? 

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: 

>> highestIQ = get_maximum(IQ_firstmeasurement)  

What is the highest score? 

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.  

If you want to debug your ​get_maximum​ function, follow these steps: 

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:

>> A = [1, 4, 2, 6, 10];  

3) Call your function by typing it in the command window:

>> 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! 

Notice that your >> in your command window changes to: 

k>>  

indicating that you are now in ‘debug mode’ 

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 

>> load TherapyIQdata.mat 

(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 

There are now three new variables, d


​ ataset2_IQ_firstmeasurement_largesample​, 
dataset2_IQ_secondmeasurement_largesample​, and ​subjectcodes​. 

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? 

>> figure, plot(IQ_firstmeasurement_largesample, '.') 

Q2.12 [OPTIONAL ASSIGNMENT]: A


​ nother way to get insight into the data is to compute the 
median of the dataset (i.e. the middle value after sorting the set from lowest to highest), which 
is less sensitive to outliers than the mean. Let’s calculate the median of the effect of our 
therapy. 

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). 

>> [H, P, CI, STATS]=ttest(IQ_secondmeasurement_largesample - 


IQ_firstmeasurement_largesample) 

 
When you have finished the exercises you can send in your results. What you have to send in 
to CANVAS is: 

- a matlab .m script file (named : 


week2_assignments_YOURNAME_YOURSTUDENTNUMBER.m, for example 
week2_assignments_MartijnvandenHeuvel_1020304) which lists all the code you 
made to solve the questions of this week. An example script can be found on 
CANVAS. 
- your matlab function files of (.m files): 
- get_maximum.m 
- get_minimum.m 
- get_sortedvalues.m (optional assignment) 
- a document (PDF file) that summarizes the MATLAB command output from your 
code (so the SOLUTIONS to the questions) 

You might also like