Controlling for parameters in Matlab regression models

조회 수: 6 (최근 30일)
Barry Greene
Barry Greene 2011년 7월 14일
답변: TED MOSBY 2025년 6월 10일
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