Skip to content

Instantly share code, notes, and snippets.

@schluppeck
Last active November 7, 2022 19:47
Show Gist options
  • Save schluppeck/e43a511a2f1708e4a0d71e1662ff7184 to your computer and use it in GitHub Desktop.
Save schluppeck/e43a511a2f1708e4a0d71e1662ff7184 to your computer and use it in GitHub Desktop.
Matlab code for regression example from PSGY4009 class

script example from PSGY4009 / lecture 6

denis schluppeck, 2022-11-07

Make sure you have access to data. I used the following dataset / files for the demos here:

https://github.com/schluppeck/hands-on-brain-data

Download the data (click the green button, download ZIP, unzip). Or if you know how to use git/github, you can also clone the repo

git clone https://github.com/schluppeck/hands-on-brain-data
cd hands-on-brain-data

Then step through the Matlab code.

Notes

  1. 3blue1brown -- basic concepts of linear algebra. youtube playlist 3blue1brown

  2. Gilbert Strang -- MIT course. Amazing resource. MIT OCW playlist

%% script example from PSGY4009 / lecture 6
%
% denis schluppeck, 2022-11-07
%% make sure you have access to data
% I used the following dataset / files
% for the demos here:
theURL = 'https://github.com/schluppeck/hands-on-brain-data';
web(theURL)
% download the data (green button, download ZIP
%
% if you know how to use git/github, you can also
% git clone https://github.com/schluppeck/hands-on-brain-data
%% change directory / navigate matlab to the folder
% then change into the "data" directory
cd('data')
clear all, close all
%% you should have nothing in your workspace
whos
who
% load in timecourse
y = load('timecourse.txt')
figure, plot(y)
% load in design matrix
X = load('design-3.txt')
plot(X)
title('fMRI response timecourse')
% make a figure of design matrix
figure, imagesc(X)
colormap(gray())
title('design matrix, three columns')
% look at the data in command window
y
X
%% there are several different approaches
% backslash
doc \ % to get help
X\y
p = X\y
%% use matlab regress()
doc regress
regress(y, X)
%% pseudoinverse
doc pinv
pinv(X)*y
%% Notes
%
% 3blue1brown -- basic concepts of linear algebra
%
% - https://youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab
%
% Gilbert Strang -- MIT course. Amazing resource.
%
% - https://youtube.com/playlist?list=PL49CF3715CB9EF31D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment