Apllying linear equation with standard error
5 views (last 30 days)
Show older comments
Hi, I have a set of x,y values (triplicates) and fit a linear equation and obtain an equation with error on the slope and intersect. Until here is ok. R^2=926
I want to apply the the equation for a set of x values, but I have an error bigger than the standard errors for each x,y values, Basically MatLab is giving all these errors...But they don't make sense... I should get y= ... +- ...
Can somenone help me?
1 Comment
Star Strider
on 30 Apr 2014
It would help to see your code, and at least a representative subset of your data.
Answers (1)
Prateekshya
on 17 Oct 2024
Hello Alexandre,
To apply a linear regression model to a set of x values and obtain predictions with associated errors, you need to take into account both the uncertainty in the model parameters (slope and intercept) and the variability in the data. Here is how you can achieve this in MATLAB:
Step 1: Fit a Linear Model
First, fit a linear model to your data and obtain the slope, intercept, and their standard errors.
% Example data
x = [1, 2, 3, 4, 5];
y = [2.1, 4.2, 6.1, 8.4, 10.2]; % Replace with your actual data
% Fit a linear model
mdl = fitlm(x, y);
% Get the slope, intercept, and their standard errors
slope = mdl.Coefficients.Estimate(2);
intercept = mdl.Coefficients.Estimate(1);
slopeSE = mdl.Coefficients.SE(2);
interceptSE = mdl.Coefficients.SE(1);
% Display R-squared
R2 = mdl.Rsquared.Ordinary;
fprintf('R^2 = %.3f\n', R2);
Step 2: Predict y Values and Calculate Errors
To predict y values for new x values and calculate the associated errors, you can use the predict function along with the standard errors of the coefficients.
% New x values for prediction
x_new = [6, 7, 8]; % Replace with your actual x values
% Predict y values
[y_pred, y_pred_ci] = predict(mdl, x_new');
% Display predictions with confidence intervals
for i = 1:length(x_new)
fprintf('For x = %.2f, predicted y = %.2f, CI = [%.2f, %.2f]\n', ...
x_new(i), y_pred(i), y_pred_ci(i, 1), y_pred_ci(i, 2));
end
Step 3: Understanding the Errors
- Standard Errors: The standard errors of the slope and intercept are used to calculate the confidence intervals for the predictions. These reflect the uncertainty in the parameter estimates.
- Prediction Intervals: The predict function provides confidence intervals for the predicted y values. These intervals account for both the uncertainty in the model parameters and the variability in the data.
- Confidence Intervals: By default, predict provides 95% confidence intervals. You can adjust this level by specifying an additional argument in the predict function.
I hope this helps!
0 Comments
See Also
Categories
Find more on Linear and Nonlinear Regression 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!