【问题标题】:R: Error making time series stationary using diff()R:使用 diff() 使时间序列静止时出错
【发布时间】:2021-03-14 23:49:19
【问题描述】:

我有一个数据集,其中包含一段时间内每月 1 日开始的月度数据。

这是我的日期列的 head():

> class(gas_data$Date)
[1] "Date"

> head(gas_data$Date)
[1] "2010-10-01" "2010-11-01" "2010-12-01" "2011-01-01" "2011-02-01" "2011-03-01"

试图使时间序列静止,我正在使用基本包中的 diff():

> gas_data_diff <- diff(gas_data, differences = 1, lag = 12)

> head(gas_data_diff)
data frame with 0 columns and 6 rows

> names(gas_data_diff)
character(0)

> gas_data_diff %>%
+   ggplot(aes(x=Date, y=Price.Gas)) +
+   geom_line(color="darkorchid4")


Error in FUN(X[[i]], ...) : object 'Price.Gas' not found

如您所见,我收到了一个错误,当尝试使用 head() 可视化数据或查找特征名称时,我收到了意外的输出。

为什么会出现此错误,我该如何解决?

这是我原始数据的 head()

> head(gas_data)
        Date Per.Change Domestic.Production.from.UKCS Import Per.GDP.Growth Average.Temperature Price.Electricity Price.Gas
1 2010-10-01      2.08                          3.54   5.40            0.2               10.44             43.50     46.00
2 2010-11-01     -3.04                          3.46   6.74           -0.1                5.52             46.40     49.66
3 2010-12-01      0.31                          3.54   9.00           -0.9                0.63             58.03     62.26
4 2011-01-01      2.65                          3.59   7.58            0.6                4.05             48.43     55.98
5 2011-02-01      1.52                          3.20   5.68            0.4                6.29             46.47     53.74
6 2011-03-01     -1.38                          3.40   5.93            0.5                6.59             51.41     60.39

这是原始数据的非平稳图对于实例的汽油价格的样子

【问题讨论】:

    标签: r time-series forecasting arima


    【解决方案1】:

    说明

    根据diff 的帮助,x 的参数必须是

    x : a numeric vector or matrix containing the values to be differenced.

    在您的情况下,如果 x 是一个数据帧,diff 返回一个 eplty data.frame。


    国际海事组织的最佳方法

    我认为在diff 中使用日期列没有什么意义。所以。我很可能会遵循以下方法。

    rownames(df) <- df$Date                                                                      
    diff(as.matrix(df[, - 1]), lag = 1)  
    
    # converr to a matrix and apply diff
    diff_mat <- diff(as.matrix(df[, - 1]), lag = 1)     
    
    # convert back to dataframe and set the Date column                                                 
    diff_df <- as.data.frame(diff_mat) 
    diff_df$Date <- diff_df$Date
    
    # now plot function should work 
    

    使用给定的数据来解决 cmets。

    将 Date 向量转换为数值,然后在 diff 中转换为矩阵

    df$Date <- as.numeric(df$Date)
    diff(as.matrix(df), 1, 2)                                                                    
    #      Date Per.Change Domestic.Production.from.UKCS Import Per.GDP.Growth
    # [1,]   -1       8.47                          0.16   0.92           -0.5
    # [2,]    1      -1.01                         -0.03  -3.68            2.3
    # [3,]    0      -3.47                         -0.44  -0.48           -1.7
    # [4,]   -3      -1.77                          0.59   2.15            0.3
    #      Average.Temperature Price.Electricity Price.Gas
    # [1,]                0.03              8.73      8.94
    # [2,]                8.31            -21.23    -18.88
    # [3,]               -1.18              7.64      4.04
    # [4,]               -1.94              6.90      8.89
    

    创建数据

    df <- read.table(text = "Date Per.Change Domestic.Production.from.UKCS Import Per.GDP.Growth Average.Temperature Price.Electricity Price.Gas
    2010-10-01      2.08                          3.54   5.40            0.2               10.44             43.50     46.00
    2010-11-01     -3.04                          3.46   6.74           -0.1                5.52             46.40     49.66
    2010-12-01      0.31                          3.54   9.00           -0.9                0.63             58.03     62.26
    2011-01-01      2.65                          3.59   7.58            0.6                4.05             48.43     55.98
    2011-02-01      1.52                          3.20   5.68            0.4                6.29             46.47     53.74
    2011-03-01     -1.38                          3.40   5.93            0.5                6.59             51.41     60.39", 
    header = T, sep ="")
    
    df$Date <- as.Date(df$Date)
    

    【讨论】:

    • 但是里面有一个日期......日期不应该是日期类型吗?当转换为数字类型时,它看起来像一个随机数......
    • 所有值都是数字类型,没有绘制任何内容...绘图为空白...
    • 您的选项是gas_data$Date &lt;- as.numeric(gas_data$Date),然后是gas_data &lt;- as.matrix(gas_data)
    • 如果第一步不做,那么第二步会创建一个字符类型的矩阵。
    • 要使用 ggplot2,您必须将其转换回 data.frame。我还根据我认为您正在尝试做的事情添加了一个部分。
    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2018-10-22
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多