Skip to content

Commit

Permalink
Add log-likelihood draws to output of submodel_test_statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmohr committed Jul 3, 2023
1 parent 6d638bd commit 0075f19
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions R/select_submodels.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ select_submodels <- function(object, ic = "BIC", select = "order", teststats = N

# Obtain test statistics
if (is.null(teststats)) {
criteria <- submodel_test_statistics(object)
criteria <- submodel_test_statistics(object)[["teststats"]]
} else {
# Basic checks
if (length(teststats) != length(unique(names(object)))) {
stop("Number of countries in argument 'object' differs from number of countries in argument 'teststats'.")
}
criteria <- teststats
criteria <- teststats[["teststats"]]
}
names_object <- names(object)

Expand Down
15 changes: 12 additions & 3 deletions R/submodel_test_statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' \eqn{M} the number of global variables, \eqn{s} the number of lags of global variables,
#' \eqn{N} the number of deterministic terms and \eqn{T} the number of observations.
#'
#' @return A data frame.
#' @return A list.
#'
#' @examples
#' # Load data
Expand Down Expand Up @@ -96,6 +96,8 @@ submodel_test_statistics <- function(object, ...){
HQ = rep(NA, n_models),
stringsAsFactors = FALSE)

loglik <- list()

for (i in 1:n_models) {

teststats[i, "ctry"] <- names(object)[i]
Expand Down Expand Up @@ -229,6 +231,10 @@ submodel_test_statistics <- function(object, ...){
LL[, j] <- bvartools::loglik_normal(u, sigma)
}

if (keep_ll) {
loglik[[i]] <- LL
}

ll <- sum(rowMeans(LL))
teststats[i, "LL"] <- ll
teststats[i, "AIC"] <- 2 * tot_pars - 2 * ll
Expand All @@ -243,10 +249,13 @@ submodel_test_statistics <- function(object, ...){
result <- NULL
ctry_names <- unique(teststats[, "ctry"])
for (i in 1:length(ctry_names)) {
result[[i]] <- teststats[teststats[, "ctry"] == ctry_names[i], -1]
rownames(result[[i]]) <- NULL
result[[i]][["teststats"]] <- teststats[teststats[, "ctry"] == ctry_names[i], -1]
rownames(result[[i]][["teststats"]]) <- NULL
names(result)[i] <- ctry_names[i]
}

result <- list(teststats = teststats,
loglik = loglik)

return(result)
}
2 changes: 1 addition & 1 deletion man/submodel_test_statistics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0075f19

Please sign in to comment.