r语言 - 在文本管理 ARIMA 输出中包含观测值的数量



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

我的任务是打印各种模型的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. >修改提取函数以包含观测值的数量,以便自动包含它们,如此处的扩展文档所讨论的那样。这是我完全陌生的,但我在下面尝试修改文本提取。有马函数,用于使用 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 预测对象编写一个 exract 函数非常感兴趣。我认为无论如何这是必要的,因为nobs只适用于b本身,而不适用于coeftest(b)。不过这是一个旁白 - 当我到达那里时,我可以弄清楚。

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

非常感谢您的任何帮助。

大约一年前,custom.gof.rows参数被添加到texregscreenreg等函数中,还没有找到进入CRAN的途径,但最终会。目前,如果您想使用此参数,可以从 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))))

也就是说,我现在已将观测值的数量添加到 ARIMA 模型的extract方法中,因此您不再需要此参数。

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

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

您正在使用的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

但是请注意,您使用的是模型blmtest包中的coeftest函数,该函数具有单独的类,因此extract方法,并且不返回观测值的数量。要获得模型b的观测值数,您可以直接包含它,而无需coeftest

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函数的输出保存在中间对象中并操作此对象。在下面的代码中,我从不coeftest版本中获取 GOF 块,并使用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

最新更新