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 Center および File ExchangeLinear 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