how to calculate adjusted r-squared in matlab

To calculate the adjusted r-squared in Matlab, you can use the regress function to perform linear regression and then calculate the adjusted r-squared using the formula:

main.m
AdjR2 = 1 - (1 - R2) * (n - 1) / (n - p - 1)
45 chars
2 lines

where R2 is the ordinary r-squared, n is the number of observations, and p is the number of predictor variables in the regression model.

Here's an example code snippet:

main.m
% Generate some sample data
x = 1:10;
y = 2*x + randn(size(x));

% Fit a linear regression model
[beta,~,r,~,stats] = regress(y', [ones(size(x')); x']);

% Calculate the r-squared and adjusted r-squared
n = length(y);
p = length(beta) - 1;
R2 = 1 - sum(r.^2) / sum((y - mean(y)).^2);
AdjR2 = 1 - (1 - R2) * (n - 1) / (n - p - 1);

% Display the results
disp(['R-squared = ' num2str(R2)]);
disp(['Adjusted R-squared = ' num2str(AdjR2)]);
437 chars
17 lines

In this example, we generate some random data, fit a linear regression model using regress, and then calculate the r-squared and adjusted r-squared values using the formula above. The output should look something like this:

main.m
R-squared = 0.97835
Adjusted R-squared = 0.97397
49 chars
3 lines

gistlibby LogSnag