Controlling for parameters in Matlab regression models
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
TED MOSBY
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!