<jupyter_start><jupyter_text>GLM - General Linear ModelToday we are going to take preprocessed data from the whole class, make a design matrix for our GLM and run it.In addition to the slides from class here is a link to a good overview of GLM in fMRI:http://www.fil.ion.ucl.ac.uk/mfd_archive/2011/page1/mfd2011_GLM.pdfOur class data has been preprocessed using the following steps:Slice timingRealignment/unwarpingNormalization (To EPI template)Smoothing (6mm)Next we want to be able to attribute variance to our moments of interest OR nuissance variables. We are going to make regressors for each of these, create a design matrix and run it. We are going to do this for a relatively simple example...  looking at when the movie is on vs when it is not.  Reconnection VNC sessionReconnect to your VNC session using Mac Scree Share or Windows UltraVNC  Close old SPM and matlab sessionsClose SPM and exit matlab from the terminal within your VNC session using using the following command:<jupyter_code>exit<jupyter_output><empty_output><jupyter_text>Navigate to your home directory and make a new directory to work inIn your terminal type the following which will move you to your base directory.<jupyter_code>cd ~<jupyter_output><empty_output><jupyter_text>Now you can see what directory you are currently in by typing `pwd` which stands for print working directory.<jupyter_code>pwd<jupyter_output><empty_output><jupyter_text>Make a new directory using `mkdir` then move into it using `cd`<jupyter_code>mkdir ./GLM
cd GLM<jupyter_output><empty_output><jupyter_text>Restart matlabFrom the terminal within your VNC session type<jupyter_code>spm12<jupyter_output><empty_output><jupyter_text>Start SPMOnce matlab starts type the following into the MATLAB terminal<jupyter_code>spm fmri<jupyter_output><empty_output><jupyter_text>Loading regressors We have some premade regressors for our example subject, specifically onsets (when our event starts), durations (how long our event happens for) and regressors of non-interest (motion & run specific variables)Load the subject independent movie onsets and durations here:<jupyter_code>load ~/../DATA/ONSETS/movieon.mat<jupyter_output><empty_output><jupyter_text>This should load 2 variables into your matlab workspace, `movieon` and `movieon_dur`.   You can check this by typing `whos` in the MATLAB terminal. Next load the subject-specific motion and run regressors:<jupyter_code>load /afs/dbic.dartmouth.edu/usr/PBS60/DATA/ONSETS/s006_regressors.txt<jupyter_output><empty_output><jupyter_text>Again type "whos" in the terminal you are running matlab from to see what you loaded.  Single subject GLM: Specify 1st-levelNow return to the SPM GUI.  To run a GLM on a single subject we need to chose `Specify 1st-level` from the SPM menu.   Chose output directory of GLMYou want to select the current directory, which will be labeled as ./ or . in the right hand panel of the file selection dialog.   Once you do this it should say something like .../PBS60/pbs60a/GLM  Units for designWe can specify our study design in second OR in TRs. We are going to use TR's here. Click `Timing parameters` > `Units for design` and select `Scans`.Next select `Interscan interval`, click button `Specify` and type in 2.5 since our scans were acquired every 2.5 seconds  Loading our preprocessed filesOur class data was preprocessed and is good to go for us to make a 1st-level (single subject) GLM.We are going to be using 4D .nii files for this part. These files contain data from multiple TRs (scans) and make data management easier.Click `Data & Design` then `Scans`Navigate to /afs/dbic.dartmouth.edu/usr/PBS60/DATA/s006  Type swuaepi* in filter  Below filter type 1:456  This should show 912 files, 456 from each run. The top file should be highlighted or click on the top most file, thenscroll to the bottom, hold down shift and click the last one which should move all of them to the Selected menu then click `Done`.  ConditionsThis tab is where we setup how many experimental conditions we have. We could have multiple conditions if we wanted, but for this example we are going to only have one, when the movie is on. Click on `Conditions`, `New Condition`, `Name` then `Specify`. You want to type in MOVIE_ON or something similar like that so you can remember that we are marking when the movie is on in this condition.Lets use the onsets we loaded. The onsets are values that tell the computer/model what times  the events we care about started.Next click on `Onsets` then `Specify`Since we already loaded the movieon.mat file that contains our onsets we are can load them by just naming the matlab variable. Type<jupyter_code>movieon<jupyter_output><empty_output><jupyter_text>Click OKThis will take the onsets we loaded into matlab earlier and if you did this correctly you should see 6 values populate this field.We could have typed them in but as you may have found out through these labs typos happen quite often and they would alter our results considerably!Next we can use the duration information we loaded to to tell the computer/model how long each of the events that we specified with our onsets was.Click on `Durations`, `Specify`<jupyter_code>movieon_dur<jupyter_output><empty_output><jupyter_text>You should see 6 values in there.   Nuisance regressorsGo to the terminal you are running matlab from and type<jupyter_code>whos<jupyter_output><empty_output><jupyter_text>This shows us what variables are in the workspace and how large they are. How large is the regressors variable? Why might we have this many columns?Take a quick look at what we put in here:<jupyter_code>figure('color','w')
imagesc(s006_regressors)
colormap(gray)<jupyter_output><empty_output><jupyter_text>What do you think each of these columns are?Do you see anything that looks odd?If you are happy with what we have for regressors then click Multiple regressors, Specify and Navigate to /afs/dbic.dartmouth.edu/usr/PBS60/DATA/Lab_03/ONSETS and select s006_regressors.txt  Saving GLM and running modelBefore you try to run your GLM you should save it. Click `File` then `Save Batch`Save it as GLM_MovieON.mat or something similar.Once you have saved you can click play (green arrow).  Once we have made our model...You should see something relatively similar to this popup:The column on the left specifies your movie condition, the remaining columns are your regressors of non-interest1:6 motion7 linear trends within each run to deal with scanner drift8 run regressor 9 constantDid we miss anything that we should have specified? Have we specified too much?Does your image look different than the image above? Why?If you are happy with what we have specified then we need to estimate the model. This means we want to apply the results of the GLM we estimated above to the brain data.  Click `Estimate` from the SPM menu. This only asks us for one thing... the SPM.mat file.Select the SPM.mat file in your GLM folder then click the green play button and wait a while!  While waiting for the GLM to run...Please review:http://www.fil.ion.ucl.ac.uk/mfd_archive/2011/page1/mfd2011_GLM.pdf Once its finished take a look at the files that it created<jupyter_code>ls<jupyter_output><empty_output><jupyter_text>You should see the following filesThe beta images are the beta weights from the general linear model for each of your regressors. How do the beta weights differ between movie on VS motion VS run regressor? Open xjview and take a look at a few by typing the following in the command window<jupyter_code>xjview<jupyter_output><empty_output><jupyter_text>ResI_0*.nii are residual files from each TR, SPM only saves 64 by default so we don't fill up our whole hard drive. ResMS.nii is the residual variance left after we calculated our model. Open this in xjview - are there some brain areas that we did a better job of modeling (ie lower residual variance) than others?  Contrast of interestGo back to the SPM menu and click ResultsSelect your SPM.mat fileClick define contrast, name it MOVIEONWe want to identify regions that are more active when the movie is on so we want to weight the first column and ignore the others. Click t-contrast since we want a directional contrast. Click in the contrast weights vector and enter 1 then click submit. Doing this weights our movie column by a factor of 1 and treats all of the other columns as nuisance regressors. You also could have entered 1 followed by 0's for all of the nuisance regressor columns but SPM does this for you. Click DoneA different window will ask you if you want to do any masking... click noneTitle for comparison: movieonYou don't need to do any adjustment for p-value, click through the defaults for p-value and extent. You can open this image with xjview by typing xjview on the command line. In xjview look for the spmT image.When you are done close xjviewAll of the subjects have already been preprocessed and run through the basic GLM and contrast of interest above.   QA: Identifying subjects that we might not want to include in our group analysisLogin to canvas and go to Files/Class Project QA and download all the files there. Scroll through the files... are there any subjects that look particularly alarming in data quality?Pay particular attention to motion and SNR. In these figures brighter color means higher SNR. We are using the same scale across all subjects. Do you see anything suspect?  2nd level analysisFor the 2nd level analysis we are going to use contrasts created from the following GLM:How is it different than what we ran before? Now that you have decided which subjects to include in your random effects analysis we can proceed. Lets start this part with a clean slate so from matlab type<jupyter_code>close all
cd ~
mkdir('RFX_MOVIEON')
cd('RFX_MOVIEON')
spm fmri<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup