Confidence interval in Linear Regression
6 次查看(过去 30 天)
显示 更早的评论
I have used the Data Set (attached with this question). It contains TV ads vs Sales. I used the curve fitting toolbox for linear regression and got the following results.
% Linear model Poly1:
f(x) = p1*x + p2
% Coefficients (with 95% confidence bounds):
p1 = 0.04754 (0.04223, 0.05284)
p2 = 7.033 (6.13, 7.935)
My question is, if I want to find the same 95% confidence bound using a Matlab Code, how would I do it?
I can find the p1 and P2 using the follwoing code.
load('Advertisement.mat')
Axe = 0:0.1:300;
Num = 0;
Denum = 0;
for i = 1:length(Tv)
Num = (Tv(i)-meanTv)*(Sales(i)-meanSales)+Num;
Denum = (Tv(i)-meanTv)^2+Denum;
B1(i) = Num/Denum;
B0(i) = meanSales-B1(i)*meanTv;
end
yProjected = B0(end)+B1(end)*Axe;
polyfit(Tv,Sales,1)
scatter(Tv,Sales)
hold on
plot(Axe,yProjected)
title('TV ads')
xlabel('Tv Ads')
ylabel('Sales')
0 个评论
回答(1 个)
the cyclist
2019-9-13
I'm curious why you want to code them from scratch, but the formulas for the standard errors and confidence intervals for the slope and intercept coefficients can be found in the wikipedia page for simple linear regression. The formulas are in the Normality Assumptions section, which that link should take you directly to.
By some code spelunking, you may be able to find those formulas implemented inside the code you used from the curve fitting toolbox (or the statistics and machine learning toolbox), which might save you some time.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!