我正在运行几个时间序列回归。我的例子是使用不同的数据,我没有注意使这些模型正确——它们只是对我的问题的说明。抱歉,我使用的是下载的数据集而不是创建的数据集。我希望没关系。
我的任务是打印各种模型的 texreg 输出,包括但不限于 ARIMA 预测。我需要包括所有模型的观察次数。texreg 提取函数不包括 ARIMA 对象的观察次数 - 或者至少观察次数永远不会打印在 gof 统计中。
我想到了两个选项,我希望能帮助实现它们中的一个或两个:
- 向 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))
- 修改提取函数以包含观察的数量,以便自动包含它们,如这里的扩展文档所述。这是我完全陌生的,但我尝试在下面修改 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 输出中包含观察数?
非常感谢您的帮助。