Controlling for parameters in Matlab regression models

6 次查看(过去 30 天)
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

回答(1 个)

TED MOSBY
TED MOSBY 2025-6-10
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!

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by