Info

This question is closed. Reopen it to edit or answer.

Defining variables and Not enough input arguments

1 view (last 30 days)
Hi to all, I am having trouble defining my x and y variables. I think i may have defined them correctly but i am not too sure if it is right and when i do this the code does not run and i get that Linear regression function does not have enough input arguments. I have to make a linear regression model for my data for rows 1:2000 for columns 9:10. This is what i have so far:
function [c m] = Linear_Regression(x,y);
N = length(x);
m = (N*sum(x.*y) - sum(x)*sum(y))/(N*sum(x^2)-(sum(x)))^2;
c = mean(y)-m*mean(x);
x = X(1:2000,9:10);
y = m.*x +c;
[c m] = Linear_Regression(x,y);
x_model = X(1:2000,9:10);
y_model = c + m.*x_model;
plot(x,y,'r',x_model,y_model);
end
I would appreciate a quick response as i have my deadline for this project coming up pretty fast. Cheers!!
  1 Comment
sandhya
sandhya on 26 Feb 2015
kindly mention clearly the code for the regression function and the calling function....

Answers (3)

John D'Errico
John D'Errico on 26 Feb 2015
Edited: John D'Errico on 26 Feb 2015
Almost always when one sees an anguished question from a new user, where they tell us they get a Not enough input arguments error, it is because they are trying to RUN that function from the editor (or by using the command run.)
That is not how you use a function. You call it from the command line.
As for the code you posted, I think you have a problem there too. Here is what you posted.
function [c m] = Linear_Regression(x,y);
N = length(x);
m = (N*sum(x.*y) - sum(x)*sum(y))/(N*sum(x^2)-(sum(x)))^2;
c = mean(y)-m*mean(x);
x = X(1:2000,9:10);
y = m.*x +c;
[c m] = Linear_Regression(x,y);
x_model = X(1:2000,9:10);
y_model = c + m.*x_model;
plot(x,y,'r',x_model,y_model);
end
That suggests that you have written the function Linear_Regression to call itself, recursively, because you have put an end statement at the end of the function, as if all the code was part of that single function.
There are other issues too. This line extracts TWO columns from the array X. I might geuss that one of them was supposed to be x, the other was y.
x = X(1:2000,9:10);
Then you define y BEFORE you have computed m and c, because you called Linear_Regression after that line.
y = m.*x +c;
So I think overall, you need to rethink things a bit. Basically, these lines should go at the command line, NOT in your function:
x = X(1:2000,9);
y = X(1:2000,10);
[c m] = Linear_Regression(x,y);
x_model = linspace(min(x),max(x),100);
y_model = c + m.*x_model;
plot(x,y,'r',x_model,y_model);
See that I've changed things a bit. Think about what I did, and why I did those things.

the cyclist
the cyclist on 26 Feb 2015
I have several fundamental questions about your code. The main issue in my mind is that you seem to be indicating that your X data is size 2000x2 (i.e. two explanatory variables), but the formula you have written is for a regression with just one explanatory variable.
So, I did some tweaks that will at least get you some output for a one-variable regression. You'll need to figure out the rest.
Here is the function:
function [c, m] = Linear_Regression(x,y)
N = length(x);
m = (N*sum(x.*y) - sum(x)*sum(y))/(N*sum(x.^2)-(sum(x))^2); % You had mistakes in this line
c = mean(y)-m*mean(x);
end
Here is some code where I made up some random data, and call the regression function:
rng 'default'
% Some made-up data
x_data = randn(2000,1);
y_data = randn(2000,1) + 0.8*x_data;
[c, m] = Linear_Regression(x_data,y_data);
x_model = x_data;
y_model = c + m.*x_model;
figure
plot(x_data,y_data,'r.',x_model,y_model);

Jon Becerra Booth
Jon Becerra Booth on 26 Feb 2015
Thanks guys, it was really helpful, i think i've got it though, i was complicating my life a bit too much, but cheers!

Community Treasure Hunt

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

Start Hunting!