|
Question about trend line |
Posted by at on Jun-01-2022 15:59 |
|
Hi Peter, I just have a question on how the trend line is plotted.
Using the scattertrend.php in the phpdemo folder, I modified the $dataX0 to have its all values equal to 5 (scattertrend_modified_data_1.png) which then results to scattertrend_1.png, in which the first line is vertical. But when I tried to modify the $dataX0 to values 5.08619865 (scattertrend_modified_data_2.png), the line is now diagonal (scattertrend_2.png).
Can you explain this behavior? Thanks.
|
Re: Question about trend line |
Posted by Peter Kwan on Jun-01-2022 23:15 |
|
Hi at,
The trend line is computed using a standard method called "Linear Regression". Linear regression assumes y depends on x and they have a linear relationship y = mx + c. For example, if x is the time of day, and y is the temperature, we can say the temperature depends on time of day, but the time of day does not depend on the temperature. The x is the independent variable, while the y is the dependent variable.
It is assumed the data points do not lie exactly on the straight line because of random noise in the dependent variable y. Linear regression aims to compute the best slope m and intercept c by minimizing using the least square method.
https://en.wikipedia.org/wiki/Linear_regression
Unluckily, linear regression does not work if all the x-coordinates are the same, as the slope will become infinite. If x is the independent variable, normally this should not happen. However, in practice, many people just want to plot something that looks nice and input all kinds of data. If you need to handle the case that all x values are the same, you may need to write additional code to check for this case and handle it separately. (This should be easy to handle, because in this case, you can ignore the y coordinates, and just add a vertical line at the x position.)
Regards
Peter Kwan |
Re: Question about trend line |
Posted by at on Jun-02-2022 09:34 |
|
Thanks Peter. |
|