Azzera filtri
Azzera filtri

How to calculate the standard error of my linear regression coefficient?

150 visualizzazioni (ultimi 30 giorni)
I want to create a linear regression for my data through the origin. My code works fine, but I also need to determine the error of the coefficient K. My y data also has an error of ±0.001 (the x data has not) which have to be taken into account.
x=[-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7];
y=[-9 -7.65 -6.35 -5.05 -3.8 -2.6 -1.2 0 1.1 2.4 3.7 4.9 6.2 7.25 8.35]*0.01;
% Computing fitted line
K = x(:)\y(:);
yfit = x(:)*K;
scatter(x, y)
hold on
plot (x, yfit, '-r')
hold off
K

Risposte (1)

Star Strider
Star Strider il 24 Apr 2021
You can certainly look this up and calculate it on your own, however using fitlm and predict is easier —
x=[-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7];
y=[-9 -7.65 -6.35 -5.05 -3.8 -2.6 -1.2 0 1.1 2.4 3.7 4.9 6.2 7.25 8.35]*0.01;
% Computing fitted line
K = x(:)\y(:);
yfit = x(:)*K;
mdl = fitlm(x,y,'Intercept',false)
mdl =
Linear regression model: y ~ x1 Estimated Coefficients: Estimate SE tStat pValue ________ __________ ______ __________ x1 0.012436 9.7275e-05 127.84 7.0505e-23 Number of observations: 15, Error degrees of freedom: 14 Root Mean Squared Error: 0.00163
[ypred,yci] = predict(mdl,x(:));
figure
scatter(x, y)
hold on
plot (x, yfit, '-r')
plot(x, yci, '-g')
hold off
K
K = 0.0124

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by