Skip to content

Commit 4d4a13d

Browse files
authored
Merge pull request #30 from bsvars/estimated-country-grouping
Estimated country grouping
2 parents bfb51e4 + 451e720 commit 4d4a13d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+10309
-7484
lines changed

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
# Generated by roxygen2: do not edit by hand
22

33
S3method(compute_variance_decompositions,PosteriorBVARPANEL)
4+
S3method(estimate,BVARGROUPPANEL)
45
S3method(estimate,BVARPANEL)
6+
S3method(estimate,PosteriorBVARGROUPPANEL)
57
S3method(estimate,PosteriorBVARPANEL)
68
S3method(forecast,PosteriorBVARPANEL)
79
S3method(plot,ForecastsPANEL)
810
S3method(plot,PosteriorFEVDPANEL)
911
S3method(summary,ForecastsPANEL)
1012
S3method(summary,PosteriorBVARPANEL)
1113
S3method(summary,PosteriorFEVDPANEL)
14+
export(specify_bvarGroupPANEL)
1215
export(specify_bvarPANEL)
1316
export(specify_panel_data_matrices)
17+
export(specify_posterior_bvarGroupPANEL)
1418
export(specify_posterior_bvarPANEL)
1519
export(specify_prior_bvarPANEL)
20+
export(specify_starting_values_bvarGroupPANEL)
1621
export(specify_starting_values_bvarPANEL)
1722
import(RcppProgress)
1823
importFrom(R6,R6Class)

R/RcppExports.R

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,12 @@
11
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

4-
bvarPANEL <- function(S, Y, X, prior, starting_values, thin, show_progress, adptive_alpha_gamma) {
5-
.Call(`_bvarPANELs_bvarPANEL`, S, Y, X, prior, starting_values, thin, show_progress, adptive_alpha_gamma)
6-
}
7-
8-
Sigma2B_c <- function(posterior_Sigma_c, lower = TRUE) {
9-
.Call(`_bvarPANELs_Sigma2B_c`, posterior_Sigma_c, lower)
10-
}
11-
12-
panel_variance_decompositions <- function(posterior_Sigma, posterior_A, global_Sigma, global_A, horizon, p, lower = TRUE) {
13-
.Call(`_bvarPANELs_panel_variance_decompositions`, posterior_Sigma, posterior_A, global_Sigma, global_A, horizon, p, lower)
14-
}
15-
16-
rmniw1 <- function(A, V, S, nu) {
17-
.Call(`_bvarPANELs_rmniw1`, A, V, S, nu)
18-
}
19-
20-
sample_m <- function(aux_A, aux_V, aux_s, aux_w, prior) {
21-
.Call(`_bvarPANELs_sample_m`, aux_A, aux_V, aux_s, aux_w, prior)
22-
}
23-
24-
sample_w <- function(aux_V, prior) {
25-
.Call(`_bvarPANELs_sample_w`, aux_V, prior)
26-
}
27-
28-
sample_s <- function(aux_A, aux_V, aux_Sigma, aux_m, prior) {
29-
.Call(`_bvarPANELs_sample_s`, aux_A, aux_V, aux_Sigma, aux_m, prior)
30-
}
31-
32-
log_kernel_nu <- function(aux_nu, aux_Sigma_c_cpp, aux_Sigma_c_inv, aux_Sigma, prior_lambda, C, N, K) {
33-
.Call(`_bvarPANELs_log_kernel_nu`, aux_nu, aux_Sigma_c_cpp, aux_Sigma_c_inv, aux_Sigma, prior_lambda, C, N, K)
34-
}
35-
36-
cov_nu <- function(aux_nu, C, N) {
37-
.Call(`_bvarPANELs_cov_nu`, aux_nu, C, N)
38-
}
39-
40-
sample_nu <- function(aux_nu, adaptive_scale, aux_Sigma_c_cpp, aux_Sigma_c_inv, aux_Sigma, prior, iteration, adptive_alpha_gamma) {
41-
.Call(`_bvarPANELs_sample_nu`, aux_nu, adaptive_scale, aux_Sigma_c_cpp, aux_Sigma_c_inv, aux_Sigma, prior, iteration, adptive_alpha_gamma)
4+
bvarGroupPANEL <- function(S, Y, X, prior, starting_values, thin, show_progress, adptive_alpha_gamma, estimate_groups = FALSE) {
5+
.Call(`_bvarPANELs_bvarGroupPANEL`, S, Y, X, prior, starting_values, thin, show_progress, adptive_alpha_gamma, estimate_groups)
426
}
437

44-
sample_Sigma <- function(aux_Sigma_c_inv, aux_s, aux_nu, prior) {
45-
.Call(`_bvarPANELs_sample_Sigma`, aux_Sigma_c_inv, aux_s, aux_nu, prior)
46-
}
47-
48-
sample_AV <- function(aux_A_c_cpp, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior) {
49-
.Call(`_bvarPANELs_sample_AV`, aux_A_c_cpp, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior)
50-
}
51-
52-
sample_A_c_Sigma_c <- function(Y_c, X_c, aux_A, aux_V, aux_Sigma, aux_nu) {
53-
.Call(`_bvarPANELs_sample_A_c_Sigma_c`, Y_c, X_c, aux_A, aux_V, aux_Sigma, aux_nu)
8+
bvarPANEL <- function(S, Y, X, prior, starting_values, thin, show_progress, adptive_alpha_gamma) {
9+
.Call(`_bvarPANELs_bvarPANEL`, S, Y, X, prior, starting_values, thin, show_progress, adptive_alpha_gamma)
5410
}
5511

5612
# Register entry points for exported C++ functions

R/country_grouping_incomegroup.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#' @title A vector with country grouping by income group for 189 countries
3+
#'
4+
#' @description Each of the country is classified into one of the 4 categories
5+
#' according to their geographical location. The categories are:
6+
#' \describe{
7+
#' \item{1}{Low-income countries}
8+
#' \item{2}{Lower-middle-income countries}
9+
#' \item{3}{Upper-middle-income countries}
10+
#' \item{4}{High-income countries}
11+
#' }
12+
#'
13+
#' @usage data(country_grouping_incomegroup)
14+
#'
15+
#' @format A numeric vector with values from \code{1} to \code{4}
16+
#'
17+
#' @source
18+
#' International Labour Organization. (2020). ILO modelled estimates database,
19+
#' ILOSTAT [database]. Available from \url{https://ilostat.ilo.org/data/}.
20+
#'
21+
#' @examples
22+
#' data(country_grouping_incomegroup) # upload the data
23+
#'
24+
#' # setup a fixed group allocation Panel VAR model
25+
#' spec = specify_bvarGroupPANEL$new(
26+
#' ilo_dynamic_panel,
27+
#' group_allocation = country_grouping_incomegroup
28+
#' )
29+
"country_grouping_incomegroup"

R/country_grouping_region.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#' @title A vector with country grouping by region for 189 countries
3+
#'
4+
#' @description Each of the country is classified into one of the 5 categories
5+
#' according to their geographical location. The categories are:
6+
#' \describe{
7+
#' \item{1}{Asia and the Pacific}
8+
#' \item{2}{Africa}
9+
#' \item{3}{Europe and Central Asia}
10+
#' \item{4}{Arab States}
11+
#' \item{5}{Americas}
12+
#' }
13+
#'
14+
#' @usage data(country_grouping_region)
15+
#'
16+
#' @format A numeric vector with values from \code{1} to \code{5}
17+
#'
18+
#' @source
19+
#' International Labour Organization. (2020). ILO modelled estimates database,
20+
#' ILOSTAT [database]. Available from \url{https://ilostat.ilo.org/data/}.
21+
#'
22+
#' @examples
23+
#' data(country_grouping_region) # upload the data
24+
#'
25+
#' # setup a fixed group allocation Panel VAR model
26+
#' spec = specify_bvarGroupPANEL$new(
27+
#' ilo_dynamic_panel,
28+
#' group_allocation = country_grouping_region
29+
#' )
30+
"country_grouping_region"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#' @title A vector with country grouping by subregion for 189 countries
3+
#'
4+
#' @description Each of the country is classified into one of the 11 categories
5+
#' according to their geographical location. The categories are:
6+
#' \describe{
7+
#' \item{1}{Southern Asia}
8+
#' \item{2}{Sub-Saharan Africa}
9+
#' \item{3}{Northern, Southern and Western Europe}
10+
#' \item{4}{Arab States}
11+
#' \item{5}{Latin America and the Caribbean}
12+
#' \item{6}{Central and Western Asia}
13+
#' \item{7}{South-Eastern Asia and the Pacific}
14+
#' \item{8}{Eastern Europe}
15+
#' \item{9}{Northern America}
16+
#' \item{10}{Eastern Asia}
17+
#' \item{11}{Northern Africa}
18+
#' }
19+
#'
20+
#' @usage data(country_grouping_subregionbroad)
21+
#'
22+
#' @format A numeric vector with values from \code{1} to \code{11}
23+
#'
24+
#' @source
25+
#' International Labour Organization. (2020). ILO modelled estimates database,
26+
#' ILOSTAT [database]. Available from \url{https://ilostat.ilo.org/data/}.
27+
#'
28+
#' @examples
29+
#' data(country_grouping_subregionbroad) # upload the data
30+
#'
31+
#' # setup a fixed group allocation Panel VAR model
32+
#' spec = specify_bvarGroupPANEL$new(
33+
#' ilo_dynamic_panel,
34+
#' group_allocation = country_grouping_subregionbroad
35+
#' )
36+
"country_grouping_subregionbroad"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
#' @title A vector with country grouping by detailed subregion for 189 countries
3+
#'
4+
#' @description Each of the country is classified into one of the 20 categories
5+
#' according to their geographical location. The categories are:
6+
#' \describe{
7+
#' \item{1}{Southern Asia}
8+
#' \item{2}{Central Africa}
9+
#' \item{3}{Southern Europe}
10+
#' \item{4}{Arab States}
11+
#' \item{5}{South America}
12+
#' \item{6}{Western Asia}
13+
#' \item{7}{Pacific Islands}
14+
#' \item{8}{Western Europe}
15+
#' \item{9}{Eastern Africa}
16+
#' \item{10}{Western Africa}
17+
#' \item{11}{Eastern Europe}
18+
#' \item{12}{Caribbean}
19+
#' \item{13}{Central America}
20+
#' \item{14}{South-Eastern Asia}
21+
#' \item{15}{Southern Africa}
22+
#' \item{16}{Northern America}
23+
#' \item{17}{Northern Europe}
24+
#' \item{18}{Eastern Asia}
25+
#' \item{19}{Northern Africa}
26+
#' \item{20}{Central Asia}
27+
#' }
28+
#'
29+
#' @usage data(country_grouping_subregiondetailed)
30+
#'
31+
#' @format A numeric vector with values from \code{1} to \code{20}
32+
#'
33+
#' @source
34+
#' International Labour Organization. (2020). ILO modelled estimates database,
35+
#' ILOSTAT [database]. Available from \url{https://ilostat.ilo.org/data/}.
36+
#'
37+
#' @examples
38+
#' data(country_grouping_subregiondetailed) # upload the data
39+
#'
40+
#' # setup a fixed group allocation Panel VAR model
41+
#' spec = specify_bvarGroupPANEL$new(
42+
#' ilo_dynamic_panel,
43+
#' group_allocation = country_grouping_subregiondetailed
44+
#' )
45+
"country_grouping_subregiondetailed"

R/estimate.BVARGROUPPANEL.R

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
#' @title Bayesian estimation of a Bayesian Hierarchical Panel Vector
3+
#' Autoregression with fixed or estimated country grouping
4+
#'
5+
#' @description Estimates the Bayesian Hierarchical Panel VAR
6+
#' with fixed or estimated country grouping using the Gibbs
7+
#' sampler proposed by Sanchez-Martinez & Woźniak (2024).
8+
#'
9+
#' @details
10+
#' The Bayesian Hierarchical Panel Vector Autoregressive model with fixed or
11+
#' estimated country grouping is estimated using the Gibbs sampler. In this
12+
#' estimation procedure all the parameters of the model are estimated jointly.
13+
#' The list of parameters of the model includes:
14+
#' \describe{
15+
#' \item{\eqn{\mathbf{A}_c}}{a \code{KxN} country-specific autoregressive
16+
#' parameters matrix for each of the countries \eqn{c = 1,\dots,C}}
17+
#' \item{\eqn{\mathbf{\Sigma}_c}}{an \code{NxN} country-specific covariance
18+
#' matrix for each of the countries \eqn{c = 1,\dots,C}}
19+
#' \item{\eqn{\mathbf{A}}}{a \code{KxN} global autoregressive parameters matrix}
20+
#' \item{\eqn{\mathbf{\Sigma}}}{an \code{NxN} global covariance matrix}
21+
#' \item{\eqn{\mathbf{V}}}{a \code{KxK} covariance matrix of prior for global autoregressive parameters}
22+
#' \item{\eqn{\nu}}{prior degrees of freedom parameter}
23+
#' \item{\eqn{m}}{prior average global persistence parameter}
24+
#' \item{\eqn{w}}{prior scaling parameter}
25+
#' \item{\eqn{s}}{prior scaling parameter}
26+
#' }
27+
#' Parameters \eqn{\mathbf{A}_c} and \eqn{\mathbf{\Sigma}_c} are subject to
28+
#' country grouping which sets them to the group-specific values.
29+
#'
30+
#' \strong{Gibbs sampler}
31+
#' is an algorithm to sample random draws from the posterior distribution of the
32+
#' parameters of the model given the data. The algorithm is briefly explained
33+
#' on an example of a two-parameter model with parameters \eqn{\theta_1} and
34+
#' \eqn{\theta_2}. In order to sample from the joint posterior distribution
35+
#' \eqn{p(\theta_1,\theta_2|\mathbf{Y})} the Gibbs sampler proceeds by sampling
36+
#' from full-conditional posterior distributions of each parameter given data
37+
#' and all the other parameters, denoted by \eqn{p(\theta_1|\theta_2,\mathbf{Y})}
38+
#' and \eqn{p(\theta_2|\theta_1,\mathbf{Y})}. These distributions are available
39+
#' from derivations and should be in a form of distributions that are easy to
40+
#' sample random numbers from.
41+
#'
42+
#' To obtain \code{S} draws from the posterior distribution:
43+
#' \enumerate{
44+
#' \item Set the initial values of the parameters \eqn{\theta_2^{(0)}}
45+
#' \item At each of the \code{s} iterations:
46+
#' \enumerate{
47+
#' \item Sample \eqn{\theta_1^{(s)}} from \eqn{p(\theta_1|\theta_2^{(s-1)},\mathbf{Y})}
48+
#' \item Sample \eqn{\theta_2^{(s)}} from \eqn{p(\theta_2|\theta_1^{(s)},\mathbf{Y})}
49+
#' }
50+
#' \item Repeat step 2. \code{S} times. Return \eqn{\{\theta_1^{(s)},\theta_2^{(s)}\}_{s=1}^{S}}
51+
#' as a sample drawn from the posterior distribution \eqn{p(\theta_1,\theta_2|\mathbf{Y})}.
52+
#' }
53+
#' The \code{estimate()} function returns the draws from the posterior distribution
54+
#' of the parameters of the hierarchical panel VAR model listed above.
55+
#'
56+
#' \strong{Thinning.}
57+
#' Thinning is a procedure to reduce the dependence in the returned sample from
58+
#' the posterior distribution. It is obtained by returning every \code{thin}
59+
#' draw in the final sample. This procedure reduces the number of draws returned
60+
#' by the \code{estimate()} function.
61+
#'
62+
#' @param specification an object of class \code{BVARGROUPPANEL} generated using the
63+
#' \code{specify_bvarPANEL$new()} function.
64+
#' @param S a positive integer, the number of posterior draws to be generated
65+
#' @param thin a positive integer, specifying the frequency of MCMC output thinning
66+
#' @param show_progress a logical value, if \code{TRUE} the estimation progress
67+
#' bar is visible
68+
#'
69+
#' @return An object of class \code{PosteriorBVARGROUPPANEL} containing the Bayesian
70+
#' estimation output and containing two elements:
71+
#'
72+
#' \describe{
73+
#' \item{\code{posterior}}{a list with a collection of \code{S} draws from the
74+
#' posterior distribution generated via Gibbs sampler. Elements of the list
75+
#' correspond to the parameters of the model listed in section \strong{Details}
76+
#' and are named respectively:
77+
#' \code{A_c}, \code{Sigma_c}, \code{A}, \code{Sigma},
78+
#' \code{V}, \code{nu}, \code{m}, \code{w}, \code{s}.
79+
#' }
80+
#' \item{\code{last_draw}}{an object of class \code{BVARGROUPPANEL} with the last draw of the
81+
#' current MCMC run as the starting value to be passed to the continuation of
82+
#' the MCMC estimation using the \code{estimate()} method. }
83+
#' }
84+
#'
85+
#' @seealso \code{\link{bvarPANELs}}, \code{\link{specify_bvarGroupPANEL}},
86+
#' \code{\link{specify_posterior_bvarGroupPANEL}}, \code{\link{summary.PosteriorBVARPANEL}}
87+
#'
88+
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
89+
#'
90+
#' @method estimate BVARGROUPPANEL
91+
#'
92+
#' @examples
93+
#' data(ilo_dynamic_panel) # load the data
94+
#' data(ilo_exogenous_variables) # load the exogenous variables
95+
#' set.seed(123)
96+
#' # specify the model
97+
#' specification = specify_bvarGroupPANEL$new(
98+
#' data = ilo_dynamic_panel,
99+
#' exogenous = ilo_exogenous_variables,
100+
#' group_allocation = rep(1:2, length.out = 189)
101+
#' )
102+
#' burn_in = estimate(specification, 10) # run the burn-in; use say S = 10000
103+
#' posterior = estimate(burn_in, 10) # estimate the model; use say S = 10000
104+
#'
105+
#' @export
106+
estimate.BVARGROUPPANEL <- function(
107+
specification,
108+
S,
109+
thin = 1L,
110+
show_progress = TRUE
111+
) {
112+
113+
# get the inputs to estimation
114+
prior = specification$prior$get_prior()
115+
starting_values = specification$starting_values$get_starting_values()
116+
data_matrices = specification$data_matrices$get_data_matrices()
117+
adaptiveMH = specification$adaptiveMH
118+
estimate_groups = specification$estimate_groups
119+
G = specification$G
120+
121+
# estimation
122+
qqq = .Call(`_bvarPANELs_bvarGroupPANEL`, S, data_matrices$Y, data_matrices$X, prior, starting_values, thin, show_progress, adaptiveMH, estimate_groups)
123+
124+
specification$starting_values$set_starting_values(qqq$last_draw)
125+
output = specify_posterior_bvarGroupPANEL$new(specification, qqq$posterior)
126+
127+
return(output)
128+
} # END estimate.BVARGROUPPANEL
129+
130+
131+
132+
#' @inherit estimate.BVARGROUPPANEL
133+
#'
134+
#' @method estimate PosteriorBVARGROUPPANEL
135+
#'
136+
#' @param specification an object of class \code{PosteriorBVARGROUPPANEL} generated using
137+
#' the \code{estimate.BVARGROUPPANEL()} function. This setup facilitates the
138+
#' continuation of the MCMC sampling starting from the last draw of the previous
139+
#' run.
140+
#'
141+
#' @export
142+
estimate.PosteriorBVARGROUPPANEL <- function(
143+
specification,
144+
S,
145+
thin = 1,
146+
show_progress = TRUE
147+
) {
148+
149+
# get the inputs to estimation
150+
prior = specification$last_draw$prior$get_prior()
151+
starting_values = specification$last_draw$starting_values$get_starting_values()
152+
data_matrices = specification$last_draw$data_matrices$get_data_matrices()
153+
adaptiveMH = specification$last_draw$adaptiveMH
154+
estimate_groups = specification$last_draw$estimate_groups
155+
G = specification$last_draw$G
156+
157+
# estimation
158+
qqq = .Call(`_bvarPANELs_bvarGroupPANEL`, S, data_matrices$Y, data_matrices$X, prior, starting_values, thin, show_progress, adaptiveMH, estimate_groups)
159+
160+
specification$last_draw$starting_values$set_starting_values(qqq$last_draw)
161+
output = specify_posterior_bvarGroupPANEL$new(specification$last_draw, qqq$posterior)
162+
163+
return(output)
164+
} # END estimate.PosteriorBVARGROUPPANEL

0 commit comments

Comments
 (0)