Controlling for parameters in Matlab regression models

3 ビュー (過去 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 Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by