Controlling for parameters in Matlab regression models
4 views (last 30 days)
Show older comments
Hi,
I wish to run a multiple linear regression analysis on some biomedical data, controlling for age and gender. How do I do this using the regress or glm functions?
_Barry
0 Comments
Answers (1)
TED MOSBY
on 10 Jun 2025
Hi,
To control for covariates like age and gender in a multiple‐linear regression, you simply include them as columns in your design matrix (for regress) or as terms in your model formula (for fitglm).
To use "regress" :
% Suppose you have:
% Y : n×1 vector of your primary response
% age : n×1 vector of age
% gender : n×1 vector coded 0/1
% Xbio : n×p matrix of your p predictors of interest
n = 100;
Y = randn(n,1) * 10 + 50;
Age = randi([20, 70], n, 1);
Gender = randi([0, 1], n, 1);
Bio1 = randn(n,1);
Bio2 = randn(n,1);
% Design matrix (intercept + age + gender + biomarkers)
X = [ones(n,1), Age, Gender, Bio1, Bio2];
% Multiple linear regression
[beta, betaCI, residuals, ~, stats] = regress(Y, X);
% beta : (p+3)×1 vector of coefficients
% betaCI : 95% confidence intervals for each coefficient
% residuals : n×1 vector of residuals
% stats : [R^2, F-stat p-value, estimate of error variance, ...]
To use "fitglm":
T = table(Y, Age, Gender, Bio1, Bio2);
% Convert Gender to categorical
T.Gender = categorical(T.Gender, [0 1], {'Female','Male'});
% Linear regression
mdl = fitglm(T, 'Y ~ Age + Gender + Bio1 + Bio2');
disp(mdl)
disp('R-squared:'), disp(mdl.Rsquared.Ordinary)
disp('Coefficient table:'), disp(mdl.Coefficients)
Refer to these documentation links for more information on these functions:
Hope this helps!
0 Comments
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!