I'm running several time series regressions. My example is using different data, and I have taken NO care in making these models correct - they are just illustrative for my question. Sorry, I used a downloaded dataset rather than a created one. I hope that's okay.
My task is printing texreg outputs for various models, including but not limited to ARIMA forecasts. I need to include the number of observations for all models. The texreg extract functions do not include the number of observations for ARIMA objects - or at least the number of observations will never print in the gof statistics.
Two options I've thought of, and I'm hoping for help implementing either or both of them:
- Add a custom row to the gof statistics with the number of observations included. This should be possible using custom.gof.rows, as discussed here and here. However, custom.gof.rows does not appear to a part of the texreg package as of version 1.36.23. Perhaps it was taken out for some reason? Not sure.
If there is a secret version of texreg that includes custom.gof.rows, can anyone link to it?
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))
This is the output, which I had to link (sorry)
Note that the model objects themselves aren't the reason (I think). I can extract the number of observations myself and print them separately.
#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))
- Modify the extract function to include the number of observations so that they will automatically be included, as discussed the extended documentation here. This I'm completely new to, but I've made an attempt below to modify the textreg extract.Arima function to retrieve the number of observations using 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:
This runs but breaks the textreg function. If I'm on the wrong track, I'd also be happy to be linked to a tutorial for changing the extract functions generally. You may also notice that I'm using coeftest() in the code above to extract the Auto-ARIMA coefficients. I'm tangentially interested in writing an exract function for Auto-ARIMA forecast objects. I think this would be necessary anyway, because nobs only works on b itself, and not on coeftest(b). That's an aside though - I can figure that out when I get there.
Can anyone help with either or both avenues, or perhaps propose a different method for including the number of observations in a texreg output?
Thank you very much for any help.