Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. function QT_parCorrVsRegression
  2. % QT_parCorrVsRegression.m
  3. % When comparing a reference RDM to a candidate RDM, we might want to
  4. % partial out the contribution of another RDM on the correlation between
  5. % the candidate and the reference RDMs.
  6. % this could be done via partial correlations or linear regression.
  7. % this scripts demonstrates that the two approaches are identical.
  8. % clear;clc;close all
  9. %% control parameters
  10. nCond = 72;
  11. nVox = 100;
  12.  
  13. %% simulating the RDMs
  14. rdm_ref = pdist(randn(nCond,nVox))';
  15. rdm_cand1 = pdist(randn(nCond,nVox))';
  16. rdm_cand2 = pdist(randn(nCond,nVox))';
  17. %% partial correlation
  18. r1 = partialcorr(rdm_ref,rdm_cand1,rdm_cand2);
  19. fprintf('partial correlation coefficient = %.3f \n',r1)
  20.  
  21. % for understanding it's better to code it up yourself:
  22. % z score so linear fit is equivalent to pearson r
  23. rdm_ref_z = zscore(rdm_ref);
  24. rdm_cand1_z = zscore(rdm_cand1);
  25. rdm_cand2_z = zscore(rdm_cand2);
  26. % remove the fitted contribution of cand2 from data and cand1
  27. projectout = @(y,c) y - (c * (c \ y));
  28. rdm_cand1_zp = projectout(rdm_cand1_z,rdm_cand2_z);
  29. rdm_ref_zp = projectout(rdm_ref_z,rdm_cand2_z);
  30. % now partial r is
  31. r1_alt = rdm_cand1_zp \ rdm_ref_zp;
  32. fprintf('partial correlation coefficient (alt) = %.3f \n',r1)
  33.  
  34. % from the above it becomes pretty obvious that the reason why the below
  35. % might not produce similar results is that you haven't Z-scored the
  36. % predictors and data.
  37.  
  38. %% linear regression
  39. % regress-out rdm2 from the reference rdm
  40. b1 = regress(rdm_ref,rdm_cand2);
  41. % you use b before defined here. Not sure if you were planning to use b or
  42. % b1. Assume b1.
  43. res_ref = rdm_ref-b1*rdm_cand2;
  44. % regress-out rdm2 from the candidate rdm
  45. b = regress(rdm_cand1,rdm_cand2);
  46. res_cand = rdm_cand1-b*rdm_cand2;
  47. r2 = corr(res_cand,res_ref);
  48. fprintf('regression-based results = %.3f \n',r2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement