I am trying to develop the most efficient method for finding the linear regression equation (y = mx + c) for the data set specified by array 2 by n.
Basically, I want to know what the value of Y is when X, for example, is 50.
My current method is poor:
inputData is my array 2 through n, with X in the first column and Y in the second.
x = 50 for i = 1 : size(inputData,1) % for every line in the inputData array if (inputData(i,1) < x + 5) | (inputData(i,1) > x - 5) % if we're within 5 of the specified X value arrayOfCloseYValues(i) = inputData(i, 2); % add the other position to the array end end y = mean(arrayOfCloseYValues) % take the mean to find Y
As you can see, my above method just tries to find Y values โโthat are within 5 of the given X value and get the average value. This is a terrible method, plus it requires adulthood to process.
I really need a reliable method of calculating linear regression for X and Y, so that I can find the value through the equation y = mx + c ...
PS. In my method above, I really predetermine memory and delete trailing zeros at the end, but I deleted this part for simplicity.
source share