Linear regression, finding slope in MySQL

末鹿安然 提交于 2019-12-22 11:31:22

问题


I'm trying to find the slope of a dataset that has DATETIME as the x axis, and a number on the y axis.

I've tried the a number of approaches, and nothing will match the slope of the line when I plug the data into Excel, it's off by multiple orders of magnitude.

This is what I have right now, but it's giving me a slope of -1.13e-13 instead of -0.008

SELECT (SUM((x-xBar)*(y-yBar)))/(SUM((x-xBar))*SUM((x-xBar)))) as slope
from (select unix_timestamp(date) as x, 
  (select avg(unix_timestamp(date)) from datatable) as xBar, 
   value as y, 
   (select avg(value) from datatable)  as yBar  from datatable) as d;

Any help would be greatly appreciated, thanks.

Update:

I've also tried

SELECT effortId, ( COUNT(*)*SUM(unix_timestamp(date)*value) -SUM(unix_timestamp(date))*SUM(value) ) / (COUNT(*)*SUM(unix_timestamp(date)^2)-SUM(unix_timestamp(date))^2) AS Slope FROM datatable;

and get a completely different answer (-0.0019), is this more accurate? Anyone know?


回答1:


What are the X units? You'll probably have to convert the time values explicitly to what you expect, whether it is seconds, hours, or days.



来源:https://stackoverflow.com/questions/4400440/linear-regression-finding-slope-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!