首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么‘forecast::TSLM()’的预测值略低于‘`stats::lm()’?

为什么‘forecast::TSLM()’的预测值略低于‘`stats::lm()’?
EN

Stack Overflow用户
提问于 2020-12-10 13:27:35
回答 1查看 179关注 0票数 1

我正在做一些工作,涉及到随着时间的推移对值进行建模,为了清晰起见,我想使用fable包来实现这一点。随着时间的推移,我希望通过日志转换创建一个线性模型--但是,我发现fable::TSLM()生成的值在某些情况下与以前在模型中使用过的stats::lm()生成的值有很大的不同。这个问题可能是由于我不正确地使用fable函数造成的,但是它也可能是包中的一个bug。下面的reprex说明了我的问题:

代码语言:javascript
复制
library(tsibble)
library(fable)
library(dplyr)
library(tidyr) # Not essential
library(ggplot2) # Not essential

# Create a toy dataset
test_data <- tsibble(
  Month = yearmonth("2020 Jan") + 0:11,
  
  # Month_Number will be used to fit a `stats` style model
  Month_Number = 1:12,
  Value = c(100, 95, 91, 75, 89, 85, 82, 75, 62, 57, 58, 50),
  index = Month
)

# Create a `fable` style model
fable_model <- test_data %>% 
  fabletools::model(m = TSLM(log(Value) ~ trend()))

# Generate modelled values using `fable`
modelled_values <- fable_model %>% 
  augment() %>% 
  mutate(Type = "Modelled") %>% 
  rename(Fable_Model = .fitted, Actual_Value = Value) %>% 
  select(-.resid) %>% 
  as_tsibble()

# generate forecasted values using `fable`
future_values <- fable_model %>% 
  forecast(h = 12, point_forecast = list(Fable_Model = mean)) %>% 
  mutate(Type = "Forecast") %>% 
  as_tsibble() %>% 
  select(-Value)

# Generate a `stats` style model
exp_model <- lm(log(Value) ~ Month_Number, data = test_data)

# Bind the modelled and forecast `fable` values together
all_values <- bind_rows(modelled_values, future_values) %>%
  
  # Mutate a column of `stats` predicted values
  mutate(Stats_Model = exp(predict(exp_model, newdata = tibble(Month_Number = 1:24))))

# Check out the mean difference in predictions - these are negligible for modelled values but are
# much more significant for forecasted values. 
all_values %>% 
  as_tibble() %>% 
  group_by(Type) %>% 
  summarise(Mean_Difference = mean(abs(Fable_Model - Stats_Model)), .groups = "drop")
#> # A tibble: 2 x 2
#>   Type     Mean_Difference
#>   <chr>              <dbl>
#> 1 Forecast        2.91e- 1
#> 2 Modelled        3.79e-14

# Can also visualise the differences with this code:
all_values %>% 
  pivot_longer(c(Actual_Value, Fable_Model, Stats_Model), names_to = "Series", values_to = "Value") %>% 
  ggplot(aes(x = as_date(Month), y = Value, colour = Series)) +
  geom_line()

Created on 2020-12-10 by the reprex package (v0.3.0)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-10 14:31:42

正如在链接上所指出的,在包寓言中有一个小的修正,用转换的数据来产生方法而不是中介。

我认为它来自于此,因为您使用了修改剩余定律的日志转换。

请注意,如果使用point_forecast = list(Fable_Model = median),两个模型都会给出相同的结果。

所以我想寓言是对的

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65235438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档