Confidence interval in Linear Regression

6 views (last 30 days)
Imtiaz Syed
Imtiaz Syed on 13 Sep 2019
Edited: Adam Danz on 13 Sep 2019
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')

Answers (1)

the cyclist
the cyclist on 13 Sep 2019
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.

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!