【问题标题】:Mutate a column in a tsibble dataframe, applying a Box-Cox transformation改变 tsibble 数据框中的列,应用 Box-Cox 转换
【发布时间】:2021-02-16 22:43:35
【问题描述】:

我是 Hyndman 软件包的忠实粉丝,但偶然发现了 Box-Cox 转换。

我有一个数据框

class(chicago_sales)
[1] "tbl_ts"     "tbl_df"     "tbl"        "data.frame"

我正在尝试改变一个额外的列,其中 Mean_price 变量将被转换。

foo <- chicago_sales %>% 
mutate(bc = BoxCox(x = chicago_sales$Median_price, lambda = 
BoxCox.lambda(chicago_sales$Median_price)))

给了我一些结果(也可能是错误的)并且不能申请autoplot

我也尝试应用来自Hyndman's book 的代码,但失败了。

我做错了什么?谢谢!

更新:

【问题讨论】:

    标签: r time-series forecast tsibble fable


    【解决方案1】:

    问题,在tsibbles里面,使用dplyr的时候,不调用chicago_sales$Median_price,只调用Median_price。使用 tsibbles 时,我建议使用 fable 和 fabletools,但如果您使用的是预测,它应该像这样工作:

    library(tsibble)
    library(dplyr)
    library(forecast)
    
    pedestrian %>% 
      mutate(bc = BoxCox(Count, BoxCox.lambda(Count)))
    # A tsibble: 66,037 x 6 [1h] <Australia/Melbourne>
    # Key:       Sensor [4]
       Sensor         Date_Time           Date        Time Count    bc
       <chr>          <dttm>              <date>     <int> <int> <dbl>
     1 Birrarung Marr 2015-01-01 00:00:00 2015-01-01     0  1630 11.3 
     2 Birrarung Marr 2015-01-01 01:00:00 2015-01-01     1   826  9.87
     3 Birrarung Marr 2015-01-01 02:00:00 2015-01-01     2   567  9.10
     4 Birrarung Marr 2015-01-01 03:00:00 2015-01-01     3   264  7.65
     5 Birrarung Marr 2015-01-01 04:00:00 2015-01-01     4   139  6.52
     6 Birrarung Marr 2015-01-01 05:00:00 2015-01-01     5    77  5.54
     7 Birrarung Marr 2015-01-01 06:00:00 2015-01-01     6    44  4.67
     8 Birrarung Marr 2015-01-01 07:00:00 2015-01-01     7    56  5.04
     9 Birrarung Marr 2015-01-01 08:00:00 2015-01-01     8   113  6.17
    10 Birrarung Marr 2015-01-01 09:00:00 2015-01-01     9   166  6.82
    # ... with 66,027 more rows
    

    我使用了来自 tsibble 包的内置数据集,因为您没有提供 chicago_sales 的输入。

    【讨论】:

    • 谢谢,我确实这样做了,但数字真的让我很害怕。请看我的打印屏幕。我认为它太高了。
    • @AnakinSkywalker,检查兰巴。看起来好像太高了。检查BoxCox.lambda(chicago_sales$Median_price) 以查看它使用的是什么 lambda。对于我使用的示例,lambda 返回 0.108。但是看看你的结果,它更接近 2,这将导致 bc 转换中的值更高。如果您查看forecasting book version 2,您可以看到 bc 如何与预测一起使用。
    • 我的 lambda 是 1.484661。我检查了所有可能的资源,但不确定出了什么问题。我的数据具有非常强烈的季节性趋势
    最近更新 更多