如何在R中进行Ljung Box测试?

如何在R中进行Ljung Box测试?,r,time-series,forecasting,autocorrelation,R,Time Series,Forecasting,Autocorrelation,我试图在R中做一个Ljung-Box测试,但是我得到了一个错误,我不知道问题出在哪里 让我们使用中示例中的代码来获取谷歌的每日收盘价 library(fpp3) google_stock <- gafa_stock %>% filter(Symbol == "GOOG") %>% mutate(day = row_number()) %>% update_tsibble(index = day, regular = TRUE) google_2015 &

我试图在R中做一个Ljung-Box测试,但是我得到了一个错误,我不知道问题出在哪里

让我们使用中示例中的代码来获取谷歌的每日收盘价

library(fpp3)
google_stock <- gafa_stock  %>%
  filter(Symbol == "GOOG")  %>%
  mutate(day = row_number()) %>%
  update_tsibble(index = day, regular = TRUE)

google_2015 <- google_stock %>% filter(year(Date) == 2015)
但是我在输出中得到了这个错误:

# A tibble: 1 x 2
  Symbol .model      
  <chr>  <chr>       
1 GOOG   NAIVE(Close)
Warning message:
1 error encountered for feature 1
[1] 'ts' object must have one or more observations

看起来您需要先选择感兴趣的列(
.resid
),然后再传递到
features()
函数:

aug %>% 
select(.resid) %>% 
features(.resid, ljung_box, lag = 10, dof = 0)
# Output
# Selecting index: "day"
# A tibble: 1 x 2
  lb_stat lb_pvalue
    <dbl>     <dbl>
1    7.91     0.637

看起来您需要先选择感兴趣的列(
.resid
),然后再传递到
features()
函数:

aug %>% 
select(.resid) %>% 
features(.resid, ljung_box, lag = 10, dof = 0)
# Output
# Selecting index: "day"
# A tibble: 1 x 2
  lb_stat lb_pvalue
    <dbl>     <dbl>
1    7.91     0.637

请提供一个带有dput(head(df,10))的数据集示例。我使用dput()编辑了这篇文章。
ljung_box(aug[,'.resid'],lag=10,dof=0)
返回值没有错误,因此这表明处理
%>%
时存在一些问题。这是github上fabletools包中修复的一个错误。请参阅请提供一个带有dput(head(df,10))的数据集示例。我使用dput()编辑了这篇文章。
ljung_box(aug[,'.resid'],lag=10,dof=0)
返回值没有错误,因此这表明处理
%%
时存在一些问题。这是github上fabletools包中修复的一个错误。看见
    > dput(head(google_stock, 10))
structure(list(Symbol = c("GOOG", "GOOG", "GOOG", "GOOG", "GOOG", 
"GOOG", "GOOG", "GOOG", "GOOG", "GOOG"), Date = structure(c(16072, 
16073, 16076, 16077, 16078, 16079, 16080, 16083, 16084, 16085
), class = "Date"), Open = c(554.125916, 553.897461, 552.908875, 
558.865112, 569.297241, 568.025513, 565.859619, 559.595398, 565.298279, 
572.769714), High = c(555.26355, 554.856201, 555.814941, 566.162659, 
569.953003, 568.413025, 565.859619, 569.749329, 571.781128, 573.768188
), Low = c(550.549194, 548.894958, 549.645081, 556.95752, 562.983337, 
559.143311, 557.499023, 554.975403, 560.400146, 568.199402), 
    Close = c(552.963501, 548.929749, 555.049927, 565.750366, 
    566.927673, 561.468201, 561.438354, 557.861633, 570.986267, 
    570.598816), Adj_Close = c(552.963501, 548.929749, 555.049927, 
    565.750366, 566.927673, 561.468201, 561.438354, 557.861633, 
    570.986267, 570.598816), Volume = c(3666400, 3355000, 3561600, 
    5138400, 4514100, 4196000, 4314700, 4869100, 4997400, 3925700
    ), day = 1:10), row.names = c(NA, -10L), key = structure(list(
    Symbol = "GOOG", .rows = list(1:10)), row.names = c(NA, -1L
), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), index = structure("day", ordered = TRUE), index2 = "day", interval = structure(list(
    year = 0, quarter = 0, month = 0, week = 0, day = 0, hour = 0, 
    minute = 0, second = 0, millisecond = 0, microsecond = 0, 
    nanosecond = 0, unit = 1), class = "interval"), class = c("tbl_ts", 
"tbl_df", "tbl", "data.frame"))
aug %>% 
select(.resid) %>% 
features(.resid, ljung_box, lag = 10, dof = 0)
# Output
# Selecting index: "day"
# A tibble: 1 x 2
  lb_stat lb_pvalue
    <dbl>     <dbl>
1    7.91     0.637
aug %>% 
select(.resid) %>% 
features(features = ljung_box, lag = 10, dof = 0)
# Selecting index: "day"
# Feature variable not specified, automatically selected `.var = .resid`
# A tibble: 1 x 2
  lb_stat lb_pvalue
    <dbl>     <dbl>
1    7.91     0.637