Least squares fitting where we pre-determine the slope

4 ビュー (過去 30 日間)
Barbara Wortham
Barbara Wortham 2018 年 5 月 16 日
コメント済み: Ameer Hamza 2018 年 8 月 13 日
Hello all,
I have a set of data, say on a scatter plot, and I have a line that has a pre-determined slope (i.e. not determined by the data on the scatter plot but determined by ideal calculations). I would like to move the line through the data until it has the least amount of offset (like least-squares fitting) but without changing the slope of the line. Is there a least-squares fitting function that lets you pre-determine the slope?
Cheers!

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 16 日
編集済み: Ameer Hamza 2018 年 5 月 16 日
Yes, you can do that following the pattern of the linear regression fitting with little modification. In linear regression, we fit the equation
a*x+b = y
and in MATLAB we write it as
[x_vector ones(size(x_vector))]\y_vector
to get a and b. But since you already know slope a, your equation become
b = y-a*x
so in MATLAB use
ones(size(x_vector))\[y_vector-a*x_vector]
it will give you the value of b which minimize the least square error.
  5 件のコメント
Curtis Baden
Curtis Baden 2018 年 8 月 12 日
Thank you for the thorough, concise explanation! I'm hoping to perform a similar calculation, except I'd like to employ a weighted least squares regression (using inverse variances associated with my dependent variable as weights). How will this calculation change in this case?
Ameer Hamza
Ameer Hamza 2018 年 8 月 13 日
For weighted least square you can use lscov(). In term of the backslash operator, you can use the following
(X.'*W*X)\(X.'*W*y)
where W is a diagonal matrix containing the weights.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by