最优两变量线性回归计算

时间:2010-05-09 20:23:35

标签: mysql sql statistics linear-regression

问题

我希望将y = mx + b等式(其中m为SLOPE,b为INTERCEPT)应用于数据集,该数据集将按照SQL代码中的说明进行检索。 (MySQL)查询的值是:

SLOPE = 0.0276653965651912
INTERCEPT = -57.2338357550468

SQL代码

SELECT
  ((sum(t.YEAR) * sum(t.AMOUNT)) - (count(1) * sum(t.YEAR * t.AMOUNT))) /
  (power(sum(t.YEAR), 2) - count(1) * sum(power(t.YEAR, 2))) as SLOPE,

  ((sum( t.YEAR ) * sum( t.YEAR * t.AMOUNT )) -
  (sum( t.AMOUNT ) * sum(power(t.YEAR, 2)))) /
  (power(sum(t.YEAR), 2) - count(1) * sum(power(t.YEAR, 2))) as INTERCEPT,
FROM
(SELECT
  D.AMOUNT,
  Y.YEAR
FROM
  CITY C, STATION S, YEAR_REF Y, MONTH_REF M, DAILY D
WHERE
  -- For a specific city ...
  --
  C.ID = 8590 AND
  -- Find all the stations within a 15 unit radius ...
  --
  SQRT( POW( C.LATITUDE - S.LATITUDE, 2 ) + POW( C.LONGITUDE - S.LONGITUDE, 2 ) ) < 15 AND
  -- Gather all known years for that station ...
  --
  S.STATION_DISTRICT_ID = Y.STATION_DISTRICT_ID AND
  -- The data before 1900 is shaky; insufficient after 2009.
  --
  Y.YEAR BETWEEN 1900 AND 2009 AND
  -- Filtered by all known months ...
  --
  M.YEAR_REF_ID = Y.ID AND
  -- Whittled down by category ...
  --
  M.CATEGORY_ID = '001' AND
  -- Into the valid daily climate data.
  --
  M.ID = D.MONTH_REF_ID AND
  D.DAILY_FLAG_ID <> 'M'
  GROUP BY Y.YEAR
  ORDER BY Y.YEAR
) t

问题

以下结果(计算线的起点和终点)显示不正确。为什么结果会偏离~10度(例如,异常值会使数据偏斜)?

  

(1900 * 0.0276653965651912)+   (-57.2338357550468)= -4.66958228

     

(2009 * 0.0276653965651912)+   (-57.2338357550468)= -1.65405406

(请注意,数据不再与图像匹配;代码。)

我原本预计1900年的结果大约是10(不是-4.67),2009年的结果大约是11.50(不是-1.65)。

相关网站

2 个答案:

答案 0 :(得分:1)

尝试拆分功能,你错误地计算了参数。看看here以供参考。

我会做类似以下的事情(请原谅我不太记得SQL语法和临时变量的事实,所以代码可能实际上是错误的):

SELECT

sum(t.YEAR) / count(1) AS avgX,

sum(t.AMOUNT) / count(1) AS avgY,

sum(t.AMOUNT*t.YEAR) / count(1) AS avgXY,

sum(power(t.YEAR, 2)) / count(1) AS avgXsq,

( avgXY - avgX * avgY ) / ( avgXsq - power(avgX, 2) )  as SLOPE,

avgY - SLOPE * avgX as INTERCEPT,

答案 1 :(得分:0)

现在已经证实这是正确的:

SELECT
  ((sum(t.YEAR) * sum(t.AMOUNT)) - (count(1) * sum(t.YEAR * t.AMOUNT))) /
  (power(sum(t.YEAR), 2) - count(1) * sum(power(t.YEAR, 2))) as SLOPE,

  ((sum( t.YEAR ) * sum( t.YEAR * t.AMOUNT )) -
  (sum( t.AMOUNT ) * sum(power(t.YEAR, 2)))) /
  (power(sum(t.YEAR), 2) - count(1) * sum(power(t.YEAR, 2))) as INTERCEPT,

  ((avg(t.AMOUNT * t.YEAR)) - avg(t.AMOUNT) * avg(t.YEAR)) /
  (stddev( t.AMOUNT ) * stddev( t.YEAR )) as CORRELATION
FROM (
  SELECT
    AVG(D.AMOUNT) as AMOUNT,
    Y.YEAR as YEAR
  FROM
    CITY C,
    STATION S,
    YEAR_REF Y,
    MONTH_REF M,
    DAILY D
  WHERE
    C.ID = 8590 AND

    SQRT(
      POW( C.LATITUDE - S.LATITUDE, 2 ) +
      POW( C.LONGITUDE - S.LONGITUDE, 2 ) ) < 15 AND

    S.STATION_DISTRICT_ID = Y.STATION_DISTRICT_ID AND

    Y.YEAR BETWEEN 1900 AND 2009 AND

    M.YEAR_REF_ID = Y.ID AND

    M.CATEGORY_ID = '001' AND

    M.ID = D.MONTH_REF_ID AND
    D.DAILY_FLAG_ID <> 'M'
  GROUP BY
    Y.YEAR
) t

有关斜率,截距和(Pearson)相关性的详细信息,请参见图像。