Confidence interval in Linear Regression

조회 수: 3 (최근 30일)
Imtiaz Syed
Imtiaz Syed 2019년 9월 13일
편집: Adam Danz 2019년 9월 13일
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')

답변 (1개)

the cyclist
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 CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by