Estimate for Residual variance function calculation

조회 수: 35 (최근 30일)
isaac raggatt
isaac raggatt 2022년 9월 4일
편집: Torsten 2022년 9월 4일
Hi, need some help on how to code residual varience, i have the formula and have attached code that i have so far, note slope = B1, intercept = B0
%data
x = normrnd(10, 1, 1, 100);
y = 1 + 2 .* x + normrnd(0, 1, 1, 100);
n = 100;
xBar = mean(x);
yBar = mean(y);
%slope
sxy = cov(x,y) * (n-1);
sxy = sxy(2,1);
sxx = var(x) * (n-1);
slope = sxy/sxx;
%intercept
intercept = yBar - (slope * xBar);
%residual varience
frontThing = 1/n-2;

답변 (1개)

Torsten
Torsten 2022년 9월 4일
x = normrnd(10, 1, 1, 100);
y = 1 + 2 .* x + normrnd(0, 1, 1, 100);
n = 100;
xBar = mean(x);
yBar = mean(y);
%slope
sxy = cov(x,y) * (n-1);
sxy = sxy(2,1);
sxx = var(x) * (n-1);
slope = sxy/sxx;
%intercept
intercept = yBar - (slope * xBar);
%residual variance
yhat = intercept + slope*x;
residual_variance = (n-1)/(n-2)*var((y-yhat).^2)
residual_variance = 1.5632
  댓글 수: 2
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2022년 9월 4일
the var get the variance of vector itself. by the difinition of formula you should take sum over it !!
which is not the formula is.
i think the correct one is :
residual_variance = (1/(n-2))*sum((y-yhat).^2)
ans if you want to have , you should take sqrt from above value.
residual_variance = sqrt((1/(n-2))*sum((y-yhat).^2))
Torsten
Torsten 2022년 9월 4일
편집: Torsten 2022년 9월 4일
You are right - should be
rng('default')
x = normrnd(10, 1, 1, 100);
y = 1 + 2 .* x + normrnd(0, 1, 1, 100);
n = 100;
xBar = mean(x);
yBar = mean(y);
%slope
sxy = cov(x,y) * (n-1);
sxy = sxy(2,1);
sxx = var(x) * (n-1);
slope = sxy/sxx;
%intercept
intercept = yBar - (slope * xBar);
%residual variance
yhat = intercept + slope*x;
residual_variance = 1/(n-2)*(y-yhat)*(y-yhat).'
residual_variance = 1.0146

댓글을 달려면 로그인하십시오.

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by