0

我正在运行几个时间序列回归。我的例子是使用不同的数据,我没有注意使这些模型正确——它们只是对我的问题的说明。抱歉,我使用的是下载的数据集而不是创建的数据集。我希望没关系。

我的任务是打印各种模型的 texreg 输出,包括但不限于 ARIMA 预测。我需要包括所有模型的观察次数。texreg 提取函数不包括 ARIMA 对象的观察次数 - 或者至少观察次数永远不会打印在 gof 统计中。

我想到了两个选项,我希望能帮助实现它们中的一个或两个:

  1. 向 gof 统计数据添加一个自定义行,其中包含观察数。这应该可以使用 custom.gof.rows 来实现,正如这里这里所讨论的。但是,从 1.36.23 版开始,custom.gof.rows 似乎不是 texreg 包的一部分。也许是因为某种原因被取出来了?没有把握。

如果有包含 custom.gof.rows 的 texreg 的秘密版本,任何人都可以链接到它吗?

library('ggplot2')
library('forecast')
library('tseries')
library('texreg')
library('lmtest')

#data can be downloaded from: https://archive.ics.uci.edu/ml/datasets/Bike+Sharing+Dataset

#Change to data path
path<-c("")

#Import data
daily_data = read.csv(paste(path,'day.csv',sep=""), header=TRUE, stringsAsFactors=FALSE)

#Mark a shift date
daily_data$shift<- ifelse(daily_data$instant>12,1,0)

#Create time series data
dt <- ts(daily_data$temp, frequency=12, start= c(2011, 1))

#Define input vars for ARIMA
(xvars <- as.matrix(daily_data[, c("instant", "shift")]))

#Basic ARIMA
a<- arima(dt, xreg=xvars)

#Auto-ARIMA
b<- auto.arima(dt, xreg=xvars)

##Where I want to include number of observations either automatically or using custom.gof.rows
screenreg(list(coeftest(b),a))

这是输出,我必须链接(对不起)

请注意,模型对象本身不是原因(我认为)。我可以自己提取观察次数并单独打印。

#Both models have number of observations in their objects that I can extract
nobs_a<-nobs(a)
nobs_b<-nobs(b)

cat("Number of obs ARIMA: ",nobs_a,"\n")

cat("Number of obs Auto-ARIMA: ",nobs_b,"\n")

##Custom.gof.rows doesn't appear to be in texreg version 1.36.23
screenreg((list(coeftest(b),a)), custom.gof.rows = list(nobs_a,nobs_b))
  1. 修改提取函数以包含观察的数量,以便自动包含它们,如这里的扩展文档所述。这是我完全陌生的,但我尝试在下面修改 textreg extract.Arima 函数以使用 nobs 检索观察的数量。
function (model, include.pvalues = FALSE, include.aic = TRUE, 
          include.loglik = TRUE, ...)  {   mask <- model$mask   nam <- names(model$coef)   
   co <- model$coef   sdev <-
    sqrt(diag(model$var.coef))   if (include.pvalues == TRUE) {
    t.rat <- rep(NA, length(mask))
    t.rat[mask] <- co[mask]/sdev
    pt <- 2 * pnorm(-abs(t.rat))
    setmp <- rep(NA, length(mask))
    setmp[mask] <- sdev   }   else {
    pt <- numeric()
    setmp <- sdev   }   gof <- numeric()   gof.names <- character()   gof.decimal <- logical()   if 
     (include.aic == TRUE) {
    aic <- AIC(model)
    gof <- c(gof, aic)
    gof.names <- c(gof.names, "AIC")
    gof.decimal <- c(gof.decimal, TRUE)   }   

    ##This is the section I added - ripped more or less intact from the extract.lm function         
    if (include.nobs == TRUE) {
    gof <- c(gof, nobs)
    gof.names <- c(gof.names, "Num. obs.")
    gof.decimal <- c(gof.decimal, FALSE)   }   

    if (include.loglik == TRUE) {
    lik <- model$loglik
    gof <- c(gof, lik)
    gof.names <- c(gof.names, "Log Likelihood")
    gof.decimal <- c(gof.decimal, TRUE)   }   
    tr <- createTexreg(coef.names = nam, coef = co, se = setmp, 
                     pvalues = pt, gof.names = gof.names, gof = gof, gof.decimal = gof.decimal)   > 
      return(tr) } <bytecode:

这会运行但会破坏 textreg 函数。如果我走错了路,我也很乐意链接到一个用于更改提取功能的教程。您可能还注意到我在上面的代码中使用 coeftest() 来提取 Auto-ARIMA 系数。我对为 Auto-ARIMA 预测对象编写一个提取函数非常感兴趣。我认为无论如何这都是必要的,因为 nobs 仅适用于 b 本身,而不适用于 coeftest(b)。不过,这是一个旁白——当我到达那里时,我可以弄清楚。

任何人都可以通过其中一种或两种途径提供帮助,或者提出一种不同的方法来在 texreg 输出中包含观察数?

非常感谢您的帮助。

4

1 回答 1

1

大约一年前,该custom.gof.rows参数被添加到texreg, etc. 函数中,还没有找到 CRAN 的方式,但最终会。screenreg目前,如果您想使用此参数,可以从 GitHub 安装最新版本。您可以按以下方式进行操作(并且需要先安装该remotes软件包):

library(remotes)
install_github("leifeld/texreg")

custom.gof.rows参数采用一个命名的向量列表。您只是为观察数提供了两个值作为参数的单独custom.gof.rows参数。相反,您需要将两者包装在一个向量中并为其附加一个名称,例如“Num. obs.”。您可以按如下方式执行此操作:

screenreg(list(coeftest(b), a),
          custom.gof.rows = list("Num. obs." = c(nobs(b), nobs(a))))

也就是说,我现在已经在extractARIMA 模型的方法中添加了观察的数量,所以你不再需要这个参数了。

您自己修改方法的尝试extract几乎奏效了。您还需要include.nobs = TRUE在函数头中包含参数,并且需要将该函数注册为通用extract函数的方法,如Journal of Statistical Software 中的文章中所述

为了让你的生活更轻松,我为你做了这件事。texreg因此,如果您按照上述说明从 GitHub安装最新版本,它应该会自动包含观察次数。

arima您正在使用的功能在stats包中定义。它返回一个Arima对象。我已经extract相应地更新了它的方法。您在上面使用的auto.arima函数是同一函数的包装器,并返回一个带有类的对象forecast_ARIMA(以前是,其次是ARIMA)。这有点令人困惑,但了解这些区别很重要。我已经用观察次数为你更新了这两种方法,所以你的代码

screenreg(list(coeftest(b), a))

现在应该返回以下输出:

======================================
                Model 1    Model 2    
--------------------------------------
ar1              0.96 ***             
                (0.04)                
ar2             -0.30 ***             
                (0.05)                
ar3              0.13 *               
                (0.05)                
ar4              0.02                 
                (0.05)                
ar5              0.17 ***             
                (0.04)                
intercept        0.43 **      0.21 ***
                (0.13)       (0.05)   
instant          0.00         0.00 *  
                (0.00)       (0.00)   
shift            0.02         0.25 ***
                (0.05)       (0.05)   
--------------------------------------
AIC                        -440.26    
BIC                        -421.88    
Log Likelihood              224.13    
Num. obs.                   731       
======================================
*** p < 0.001; ** p < 0.01; * p < 0.05

但是请注意,您正在使用model 包中的coeftest函数,该函数具有单独的类和方法,并且不返回观察次数。要获得 Model 的观察数,您可以直接包含它,而不需要:lmtestbextractbcoeftest

screenreg(list(b, a), single.row = TRUE)

这将返回以下输出:

=======================================================
                Model 1              Model 2           
-------------------------------------------------------
ar1                 0.96 (0.04) ***                    
ar2                -0.30 (0.05) ***                    
ar3                 0.13 (0.05) *                      
ar4                 0.02 (0.05)                        
ar5                 0.17 (0.04) ***                    
intercept           0.43 (0.13) **      0.21 (0.05) ***
instant             0.00 (0.00)         0.00 (0.00) *  
shift               0.02 (0.05)         0.25 (0.05) ***
-------------------------------------------------------
AIC             -2183.71             -440.26           
AICc            -2183.46                               
BIC             -2142.36             -421.88           
Log Likelihood   1100.86              224.13           
Num. obs.         731                 731              
=======================================================
*** p < 0.001; ** p < 0.01; * p < 0.05

如果您坚持使用coeftest,您可以将extract函数的输出保存在中间对象中并操作该对象。在下面的代码中,我从没有版本的版本中取出 GOF 块coeftest并将其注入到版本中coeftest,然后将操作对象而不是原始对象交给screenreg函数:

tr_b_coeftest <- extract(coeftest(b))
tr_b_plain <- extract(b)
tr_b_coeftest@gof.names <- tr_b_plain@gof.names
tr_b_coeftest@gof <- tr_b_plain@gof
tr_b_coeftest@gof.decimal <- tr_b_plain@gof.decimal
screenreg(list(tr_b_coeftest, a), single.row = TRUE)

这将返回以下输出:

=======================================================
                Model 1              Model 2           
-------------------------------------------------------
ar1                 0.96 (0.04) ***                    
ar2                -0.30 (0.05) ***                    
ar3                 0.13 (0.05) *                      
ar4                 0.02 (0.05)                        
ar5                 0.17 (0.04) ***                    
intercept           0.43 (0.13) **      0.21 (0.05) ***
instant             0.00 (0.00)         0.00 (0.00) *  
shift               0.02 (0.05)         0.25 (0.05) ***
-------------------------------------------------------
AIC             -2183.71             -440.26           
AICc            -2183.46                               
BIC             -2142.36             -421.88           
Log Likelihood   1100.86              224.13           
Num. obs.         731                 731              
=======================================================
*** p < 0.001; ** p < 0.01; * p < 0.05
于 2020-05-01T10:02:19.507 回答