Estimate for Residual variance function calculation
조회 수: 35 (최근 30일)
이전 댓글 표시
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;
댓글 수: 0
답변 (1개)
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)
댓글 수: 2
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
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).'
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!