regression line in quadratic form

Hi, all
During my exercise, I'd like to draw a regression line fitted with the following quadratic equation.
The followings are the code I wrote down.
y = log(wage(index));
x = [ones(length(y), 1) experience(index) experience(index).^2];
b = (x'*x)^(-1)*x'*y;
yhat = x*b;
figure;
plot(x(:, 2), y, '.', x(:, 2), yhat);
title('exercise');
xlabel('experience');
ylabel('log(wage)');
print('-dpdf', 'excercise');
close;
And this is the result.
I firstly guess the single curve will be shown on the figure, but... why is there a bunch of lines, and how can I fix this with minimization of fixing?
Thank you in advance, guys.

Answers (1)

J. Alex Lee
J. Alex Lee on 9 Sep 2020
Edited: J. Alex Lee on 12 Sep 2020
[edited] Alternatively, just create a new variable to hold an already sorted "model" experience
N = 200; % choose an arbitrary number of points to define model
ExperienceMdl = linspace(0,70,N)';
xMdl = [ones(N,1),ExperienceMdl,ExperienceMdl.^2]
yhat = ExperienceMdl * b;
And also, your compuation b looks inefficient, you can simply do
b = x\y

9 Comments

Thx for the efficient notation. However, I'm afraid creating new variable does not affect the figure.
are you plotting as
plot(ExperienceMdl,yhat);
I am plotting as following,
plot(x(:,2), y, '.', x(:, 2), yhat);
since it does not work with
ExperienceMdl = linspace(0,70,200)';
of course the plot will not change if you don't change the code for plotting...as for "not working", what exactly is the issue?
I did not understand why you use linspace() and elemets are 0,70,200.
index = find(female==0&race==1&education==12);
y=logwage(index)
ExperienceMdl = linspace(0,70,200)';
xMdl = [ones(size(y)),ExperienceMdl(index),ExperienceMdl(index).^2]
b = xMdl\y
yhat = ExperienceMdl * b;
number of index is 6969x1.
Index exceeds the number of array elements (200).
Error in Untitled (line 19)
xMdl = [ones(size(y)),ExperienceMdl(index),ExperienceMdl(index).^2]
yes, sorry about that, i edited the answer, but xMdl should be defined
xMdl = [ones(size(ExperienceMdl)),ExperienceMdl,ExperienceMdl.^2]
I am sorry, but why did you use linspace()?
and could you explain why there are multiple regression lines, not a single non-linear regrssion line?
You saw multiple regression lines because you plotted the results of your model based on the data, which is not ordered in "experience" variable.
One of the previous answeres suggested you to sort your data in order of your "experience" variable. But you can realize that there is no need to plot your model based on the data points alone.
linspace(a,b,N) will generate a vector of N-values evenly spaced (and sorted) from a to b. I chose N=200 points because that should be more than enough to resolve the shape of your model over the domain 0~70, which is roughly the extent of your data.

Sign in to comment.

Categories

Asked:

on 9 Sep 2020

Commented:

on 13 Sep 2020

Community Treasure Hunt

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

Start Hunting!