vif in matlab

To calculate the Variance Inflation Factor (VIF) in MATLAB, you can use the regstats function from the Statistics and Machine Learning Toolbox. Here's an example of how to use it:

main.m
% Sample data
X = [ones(10,1), randn(10,3)];  % Independent variables
y = randn(10,1);  % Dependent variable

% Fit a multiple linear regression model
mdl = fitlm(X, y);

% Calculate the VIF for each independent variable
vif = diag(inv(X' * X))';

% Display the results
disp("Variable    VIF");
disp("----------------");
for i = 1:size(X, 2)
    disp([mdl.CoefficientNames{i+1}, "   ", num2str(vif(i))]);
end
409 chars
17 lines

In this example, we first create a matrix X representing the independent variables and a column vector y representing the dependent variable. Then, we fit a multiple linear regression model using the fitlm function. Finally, we calculate the VIF for each independent variable using the diag and inv functions, and display the results using a loop.

related categories

gistlibby LogSnag