如何在vertica中进行线性回归的平均移动线

时间:2018-02-12 13:56:57

标签: linear-regression vertica

我是Vertica中的回归新手。

我已经阅读了不同的文档,并尝试使用图表。

我所做的是,

  

创建表

     

创建模型

     

创建Reg

 `SELECT LINEAR_REG('fModel', 'tablenameTraining', 'col1', 'col2'
                  USING PARAMETERS optimizer='BFGS');

  SELECT SUMMARIZE_MODEL('fModel')


  SELECT *,PREDICT_LINEAR_REG(col2 USING PARAMETERS model_name='fModel') as pred
  FROM tableTesting`

我收到这张图表 enter image description here

现在,我想在我的图表中再增加一条线,它将是蓝线的移动平均线。它不应该是恒定的直线,而是每个峰值点的平均值。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您只需应用普通的vanilla OLAP AVG()函数。 像这样:

   WITH predictoutput (tm,val) AS (
    -- approx form of your blue curve;
    -- minutes instead of quarters
              SELECT TIME '00:00:00' , 1
    UNION ALL SELECT TIME '00:01:00' , 2
    UNION ALL SELECT TIME '00:02:00' , 2
    UNION ALL SELECT TIME '00:03:00' , 0
    UNION ALL SELECT TIME '00:04:00' , 3
    UNION ALL SELECT TIME '00:05:00' , 1
    UNION ALL SELECT TIME '00:06:00' , 0
    UNION ALL SELECT TIME '00:07:00' , 5
    UNION ALL SELECT TIME '00:08:00' , 4
    UNION ALL SELECT TIME '00:09:00' ,10
    UNION ALL SELECT TIME '00:10:00' , 5
    UNION ALL SELECT TIME '00:11:00' , 1
    UNION ALL SELECT TIME '00:12:00' ,14
    UNION ALL SELECT TIME '00:13:00' , 2
    UNION ALL SELECT TIME '00:14:00' ,10
    UNION ALL SELECT TIME '00:15:00' , 1
    UNION ALL SELECT TIME '00:16:00' , 0
    UNION ALL SELECT TIME '00:17:00' ,16
    UNION ALL SELECT TIME '00:18:00' , 5
    UNION ALL SELECT TIME '00:19:00' ,10
    UNION ALL SELECT TIME '00:20:00' , 7
    )
    SELECT
      tm
    , (AVG(val) OVER(ORDER BY tm))::NUMERIC(9,4) AS running_avg
    FROM predictoutput
    ;
    tm      |running_avg
    00:00:00|     1.0000
    00:01:00|     1.5000
    00:02:00|     1.6667
    00:03:00|     1.2500
    00:04:00|     1.6000
    00:05:00|     1.5000
    00:06:00|     1.2857
    00:07:00|     1.7500
    00:08:00|     2.0000
    00:09:00|     2.8000
    00:10:00|     3.0000
    00:11:00|     2.8333
    00:12:00|     3.6923
    00:13:00|     3.5714
    00:14:00|     4.0000
    00:15:00|     3.8125
    00:16:00|     3.5882
    00:17:00|     4.2778
    00:18:00|     4.3158
    00:19:00|     4.6000
    00:20:00|     4.7143

全部......

干杯 -