To plot the adjusted R-squared and sum of squared error separately in MATLAB, you would need to calculate these values first. Here is an example of how you can do it:
main.m814 chars47 lines
In this code, we first define the sample data x
and y
. Then we perform a linear regression using the polyfit
function to obtain the coefficient values. The predicted y
values are calculated using polyval
.
Next, we calculate the residuals by subtracting the predicted y
values from the actual y
values. The mean squared error (MSE) is computed by taking the mean of squared residuals.
We also calculate the total sum of squares (TSS) to obtain the R-squared value, which is given by 1 minus the ratio of the sum of squared residuals to TSS.
Finally, we calculate the adjusted R-squared using the formula which takes into account the number of observations n
and the number of predictors p
.
The results are then plotted using the scatter
, plot
, and bar
functions in MATLAB.
Note: This example assumes a simple linear regression with only one predictor variable (x
), but the code can be adapted for more complex models.
gistlibby LogSnag