如何在 R 中使用 modelsummary(或其他 LaTeX 表包)报告 VARX 模型(MTS 类)?

问题描述 投票:0回答:1

考虑

R
中使用
MTS
包进行 VARX 模型估计的示例:

library(tidyverse)
library(MTS)
library(modelsummary)
library(vars)
library(stats)

set.seed(10)

ts1 <- rnorm(200, mean = 10, sd = 3) %>% ts()
ts2 <- ts1 + rnorm(200, mean = 1, sd = 3) %>% ts()

exg1 <- rchisq(n = 200,df = 10) + ts1 %>% ts()
exg2 <- rf(n = 200,df1 = 20,df2 = 20) + ts2 %>% ts()

zt <- cbind(ts1, ts2)
xt <- cbind(exg1, exg2)
p = 1
m = 0

varx <- VARX(zt = zt, p = p, xt = xt, m = m)

我想将此模型报告为 LaTeX 表,例如

lm()
模型的输出表:

\begin{table}
\centering
\begin{tabular}[t]{lc}
\toprule
  & ts1\\
\midrule
(Intercept) & \num{1.676}\\
 & (\num{0.640})\\
ts2\_lag & \num{0.133}\\
 & \vphantom{1} (\num{0.223})\\
exg1 & \num{0.165}\\
 & (\num{0.028})\\
exg2 & \num{0.273}\\
 & (\num{0.223})\\
\midrule
Num.Obs. & \num{200}\\
R2 & \num{0.561}\\
R2 Adj. & \num{0.554}\\
AIC & \num{832.4}\\
BIC & \num{848.9}\\
Log.Lik. & \num{-411.211}\\
F & \num{83.346}\\
RMSE & \num{1.89}\\
\bottomrule
\end{tabular}
\end{table}

根据 modelsummary,如果我创建一个

tidy.mts
函数,该函数返回一个数据帧,其中包含名为
term
的列和具有拟合优度统计的
glance.mts
函数,我可以报告此模型。 但以下代码不会生成模型汇总表:


tidy.mts <- function(s, ...) {
  
  colnames(s$se.coef) <- colnames(s$coef)
  
  ret <- data.frame(
    term= rownames(s$coef),
    estimate= round(s$coef,3),
    std.error= round(s$se.coef,3))
  
  ret
}

glance.mts <- function(s, ...) {
  
  ret <- data.frame(
    n = nrow(s$data)
  )
  ret
}

modelsummary(varx)

错误信息是:

Error: `modelsummary could not extract the required information from a model of
  class "mts". The package tried a sequence of 2 helper functions to extract
  estimates:
  
  parameters::parameters(model)
  broom::tidy(model)
  
  To draw a table, one of these commands must return a `data.frame` with a
  column named "term". The `modelsummary` website explains how to summarize
  unsupported models or add support for new models yourself:
  https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html
  
  These errors messages were generated during extraction:
  
  `parameters::parameters(model)` did not return a valid data.frame.
`broom::tidy(model)` did not return a valid data.frame.
In addition: Warning message:
`modelsummary could not extract goodness-of-fit statistics from a model
of class "mts". The package tried a sequence of 2 helper functions:

performance::model_performance(model)
broom::glance(model)

One of these functions must return a one-row `data.frame`. The `modelsummary` website explains how to summarize unsupported models or add support for new models yourself:

https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html 
r latex var modelsummary multivariate-time-series
1个回答
0
投票

你的解决方案有很多问题。

  1. varx
    对象属于
    list
    类,而不是
    mts
    类,这是您的
    tidy
    glance
    方法所寻找的。您可以手动为模型分配类别:
    class(varx)<-"mts"
  2. estimate
    中的
    std.error
    tidy()
    列不应 被舍入。它们应该是数值。
  3. estimate
    std.error
     中的 
    tidy()
     列应该是单列。但是,当您调用 
    s$coef
     时,您会得到两列。确保调用 
    tidy(varx)
     生成与在 
    lm()
     模型上调用相同函数时相同类型的数据帧。
  4. 观察的数量应该称为
  5. nobs
    。这将确保它在表中很好地重命名。
© www.soinside.com 2019 - 2024. All rights reserved.