【问题标题】:Replicating restricted s.level from rucm in Python's statsmodels在 Python 的 statsmodels 中从 rucm 复制受限的 s.level
【发布时间】:2020-12-11 07:31:05
【问题描述】:

我使用 rucm 包在 R 中创建了一个未观察到的组件模型:

# input regression formula and extract x and y variables
regression_formula <- as.formula(paste("WebsiteVisits ~ MarketingSpend"))  

target_var <- formula.tools::lhs(regression_formula)
fo <-
  as.formula(paste(formula.tools::lhs.vars(regression_formula), " ~ ", paste(
    formula.tools::rhs.vars(regression_formula), collapse = " + "
  )))

# build unrestricted UCM
unrestricted_mod <- ucm(
  fo,
  data = final_data,
  level = TRUE,
  slope = TRUE,
  season = FALSE,
)

# build restricted UCM
restricted_mod <- ucm(
  fo,
  data = final_data,
  level = TRUE,
  level.var = .1 * mean(unrestricted_mod$vs.level),
  slope = TRUE,
  season = FALSE,
)

restricted_mod$s.level

如何使用 Python 的 statsmodels 中的 UnobservedComponents 重新创建它(即使用相同的参数获取完全相同的 s.level 值)?

目前进展:

unrestricted_mod = UnobservedComponents(final_data['WebsiteVisits'], exog=final_data['MarketingSpend'], level=True, slope=True, seasonal=False)
unrestricted_mod.fit().summary()

# average of the unrestricted model's level (unsure if this is the same as vs.level in R)
np.mean(unrestricted_mod.fit().level['filtered'])

# now I need to input that value in a restricted model

# ...but I can't seem to input a value here for stochastic_level, only boolean
restricted_mod = UnobservedComponents(final_data['WebsiteVisits'], exog=final_data['MarketingSpend'], level = True, stochastic_level = True, slope=True, seasonal=False)

【问题讨论】:

    标签: python r time-series statsmodels


    【解决方案1】:

    想通了:

    unrestricted_mod = UnobservedComponents(final_data['WebsiteVisits'], exog=final_data['MarketingSpend'], trend = True, stochastic_trend = True, irregular = True, level=True, stochastic_level = True, slope = True, seasonal = False)
    print(unrestricted_mod.param_names)
    
    with unrestricted_mod.fix_params({'sigma2.level': .1 * np.mean(unrestricted_mod.fit().level['smoothed'])}):
        restricted_mod = unrestricted_mod.fit()
        
    restricted_mod.summary()
    

    关键是 1. 确保参数匹配(注意不规则的默认值在 Python 中为 False,在 R 中为 True)和 2. 使用 fix_params 设置受限模型的级别。

    我的系数在 Python 和 R 之间非常匹配,但并不完全匹配。我不知道这是否只是由于计算不同而无法修复,或者我的代码中缺少某些内容;我不认为我应该修复其他参数。

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 1970-01-01
      • 2012-11-25
      • 2012-12-21
      • 2014-08-27
      • 2012-04-04
      • 2019-11-08
      • 1970-01-01
      相关资源
      最近更新 更多