Need help verifying I have the correct polyfit line for my function.

1 回表示 (過去 30 日間)
Kristin Aldridge
Kristin Aldridge 2021 年 11 月 18 日
コメント済み: Walter Roberson 2021 年 11 月 18 日
Question: Ps7.mat contain scores for 100 subjects on two different tests. Use the linear regression function you wrote in class to fit a linear model to these data. Plot the data using a scatter plot and plot the fitted model with a line. Report the estimated parameters a and b in the figure title. Then, use polyfit to find the best-fit line predicting score2 from score1. Your coefficients should be the same as the ones you found using your linear regression function in problem 2. List these coefficients in a comment or show them on the figure.
Below I have included my regression line, my a (named alpha) and b (named beta) parameters (that I hope are correct), and what I plugged in for polyfit. I am a bit concerned because my polyfit gives me 0 -0.1606 , and my b is also equal to -0.1606 so I want to make sure that is correct, and if not where I'm going wrong.
load('ps7.mat')
function [b,a,rsq] = regressline(x,y)
n=length(x);
xbar=mean(x);
ybar=mean(y);
sigxsq= sum(x.^2) - (sum(x)^2)/n;
sigxy=sum(x.*y)-(sum(x)*sum(y))/n;
beta=sigxy./sigxsq; % = -0.1606
alpha=ybar-beta*xbar;% = -0.5841
plot(x,y,'o')
hold on
title('alpha=0.5841, beta=-0.1606')
plot(x,(beta*x) + alpha, 'r')
end
p=polyfit(alpha,beta,1)
plot(p)
%p=0 -0.1606
Thanks!

採用された回答

Walter Roberson
Walter Roberson 2021 年 11 月 18 日
No you should polyfit x and y.
  2 件のコメント
Kristin Aldridge
Kristin Aldridge 2021 年 11 月 18 日
So it would be
p=polyfit(x,y,1)
plot(p)
%p=[-0.1606,0.5841]
Walter Roberson
Walter Roberson 2021 年 11 月 18 日
Do not plot() the output of polyfit. If you want to plot the prediction line then use polyval to create projected locations and plot() those

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by