Which function should I use for generating the weighted least squares fit linear line for a given data?

조회 수: 14 (최근 30일)
I have a set of data which I generate using the following code:
x = linspace(-10, 10, 20);
slope = 1.5;
intercept = -1;
noiseAmplitude = 15;
y = slope .* x + intercept + noiseAmplitude * rand(1, length(x));
I want to generate a weighted linear least squares fit regression line for the above data points. I don't have the weights matrix so I can go with using the formula w(i) = 1/variance(i)^2 or any other default formula that a MATLAB function may use for generating the weights matrix. I don't know how to generate this matrix and make sure its diagonal and then fit a regression line in the data, is there a function in MATLAB which can help me achieve this?

채택된 답변

Sindar
Sindar 2020년 10월 4일
편집: Sindar 2020년 10월 5일
fitlm accepts weights as a vector, but doesn't come with any pre-designed ones
mdl = fitlm(x,y,'Weights',weight);
Ypred = predict(mdl,x);
plot(x,y,'k*',x,Ypred,'r')
determining variance values for your points is a separate question (my answer would be "huh? I don't think that's a sensible quantity," but if you ask clearly, others may have an answer)
  댓글 수: 5
Vihaan Misra
Vihaan Misra 2020년 10월 5일
Thanks a lot!! I have been trying to generate the weights matrix but can't seem to do so, I am very new to MATLAB so any input will be really helpful
Sindar
Sindar 2020년 10월 5일
I'd check out the Matlab online trainings (I believe most are currently free), starting with the Onramp (always free).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by