- Fit the Linear Model: Useing MATLAB's built-in functions to fit the linear model and obtain the coefficients and their statistics.
- Extract Standard Errors and Confidence Intervals: MATLAB provides functions to calculate these directly from the fitted model.
Estimation of errors from solving A\B=X
29 views (last 30 days)
Show older comments
I have a few vectors which by a linear combination fits a different vector quite nice. E.g. I solve the equation
[vec1 vec2 vec3 vec4]*X=vec_dat.
Now I would like to get a feeling of the confidence level on the coefficients returned by matlab.
One thing I have tried is
c=coef(1)
l=linspace(c-c/5,c+c/5,20);
for i=1:length(l)
l(i);
sim=l(i).*a+coef(2).*b+coef(3).*g+coef(4).*d;
RMSa(i)=sum(sqrt((dat-sim).^2));
end
I observe parabolas with different 2nd derivatives for each of the coefficients i test. Obviously the 2nd derivative of such a parabola must be related to the confidence of the given coefficient tested?
Any one know how to extract it?
0 Comments
Answers (1)
Rahul
on 19 Nov 2024 at 10:54
Edited: Rahul
on 19 Nov 2024 at 11:18
Hi Kresten,
I am assuming that you are using the latest R2024b release of MATLAB. To assess the confidence level of the coefficients in a linear regression model, you can use the standard errors and confidence intervals of the coefficients. This could be a simpler way to observe the relation between confidence and coefficients, rather than relying on the second derivative of the RMS error with respect to the coefficients. Here's a general approach to achieve this:
You can use the‘fitlm’function to fit a linear model and get detailed statistics about the coefficients:
% Sample data
vec1 = [1; 2; 3; 4; 5];
vec2 = [2; 3; 4; 5; 6];
vec3 = [3; 4; 5; 6; 7];
vec4 = [4; 5; 6; 7; 8];
vec_dat = [10; 14; 18; 22; 26]; % Linear combination of vec1, vec2, vec3, vec4
X = [vec1, vec2, vec3, vec4];
y = vec_dat;
% Fit linear model
mdl = fitlm(X, y);
Once you have the model, you can extract various statistics, as shown below:
% Display the model summary
disp(mdl);
% Extract coefficients
coefficients = mdl.Coefficients.Estimate;
% Extract standard errors
standardErrors = mdl.Coefficients.SE;
% Extract confidence intervals (default 95%)
confidenceIntervals = mdl.coefCI;
Instead of manually testing the sensitivity of the coefficients by perturbing them and observing the RMS error, you can use the‘fitlm’function, whcih provides a more robust statistical framework for assessing the confidence in your coefficients. The standard errors and confidence intervals can give a clearer picture of the reliability of each coefficient estimate.
In case, you want to visualize how the RMS error changes with perturbations to a coefficient, you can use the following code:
% Choose a coefficient to perturb (e.g., the second one)
coef_index = 2;
c = coefficients(coef_index);
l = linspace(c - c/5, c + c/5, 20);
% Compute the RMS error for different values of the chosen coefficient
RMSa = zeros(size(l));
for i = 1:length(l)
sim = l(i) * vec1 + coefficients(2) * vec2 + coefficients(3) * vec3 + coefficients(4) * vec4;
RMSa(i) = sqrt(mean((vec_dat - sim).^2));
end
% Plot the parabola
figure;
plot(l, RMSa, '-o');
xlabel('Coefficient Value');
ylabel('RMS Error');
title('RMS Error vs. Coefficient Value');
The curvature of the RMS error plot gives an intuitive sense of the sensitivity of the fit to changes in the coefficient. A steeper parabola (greater second derivative) suggests that small changes in the coefficient lead to large changes in the error, indicating a more precise estimate (lower variance). However, the standard errors provided by‘fitlm’are a more direct and statistically sound measure of confidence in the coefficients.
To know more about the usage of ‘fitlm’ function, refer to the documentation link mentioned below:
Best!
0 Comments
See Also
Categories
Find more on Biological and Health Sciences in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!