| Title: | Bayesian Averaging for Dynamic Panels |
|---|---|
| Description: | Implements Bayesian model averaging for dynamic panels with weakly exogenous regressors as described in the paper by Moral-Benito (2013, <doi:10.1080/07350015.2013.818003>). The package provides functions to estimate dynamic panel data models and analyze the results of the estimation. |
| Authors: | Krzysztof Beck [aut], Piotr Cukier [aut], Marcin Dubel [aut, cre], Mariusz Szczepanczyk [aut], Mateusz Wyszynski [aut] |
| Maintainer: | Marcin Dubel <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.5.0 |
| Built: | 2026-06-13 07:19:08 UTC |
| Source: | https://github.com/cran/badp |
This function ranks the best models according to posterior model probabilities calculated using one of the available model priors: binomial or beta-binomial. It returns three types of tables in three different formats: an inclusion table, where 1 indicates that a regressor is included in the model and 0 indicates that it is excluded; an estimation results table, which displays the best models and their estimation output, including point estimates, standard errors, and significance levels; and an estimation results table with robust standard errors.
best_models( bma_list, prior = "binomial", best = 5, round = 3, estimate = TRUE, robust = TRUE )best_models( bma_list, prior = "binomial", best = 5, round = 3, estimate = TRUE, robust = TRUE )
bma_list |
An object of class |
prior |
Character string specifying the model prior used for the
ranking. Options are |
best |
Integer. The number of best models to display (default: 5). |
round |
Integer indicating the decimal place to which numbers in the tables should be rounded (default: 3). |
estimate |
A parameter with values TRUE or FALSE indicating which table should be displayed when
TRUE - table with the estimation results |
robust |
A parameter with values TRUE or FALSE indicating which type of standard errors should be displayed
when the function finishes calculations. Works only if estimate = TRUE. Works well when best is small. |
A list with best_models objects:
matrix with inclusion of the regressors in the best models
matrix with estimation output in the best models with regular standard errors
matrix with estimation output in the best models with robust standard errors
knitr_kable table with inclusion of the regressors in the best models (the best for the display on the console - up to 11 models)
knitr_kable table with estimation output in the best models with regular standard errors (the best for the display on the console - up to 6 models)
knitr_kable table with estimation output in the best models with robust standard errors (the best for the display on the console - up to 6 models)
gTree table with inclusion of the regressors in the best models (displayed as a plot). Use grid::grid.draw() to display.
gTree table with estimation output in the best models with regular standard errors (displayed as a plot). Use grid::grid.draw() to display.
gTree table with estimation output in the best models with robust standard errors (displayed as a plot). Use grid::grid.draw() to display.
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) best_5_models <- best_models(bma_results, prior = "binomial", best = 5, estimate = TRUE)library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) best_5_models <- best_models(bma_results, prior = "binomial", best = 5, estimate = TRUE)
This function calculates BMA statistics based on the provided model space. Other objects for further analysis are also returned.
bma(model_space, round = 4, EMS = NULL, dilution = 0, omega = 0.5)bma(model_space, round = 4, EMS = NULL, dilution = 0, omega = 0.5)
model_space |
An object of class |
round |
Integer indicating the decimal place to which numbers in the BMA tables and prior and posterior model sizes should be rounded (default: 4). |
EMS |
Numeric. Expected model size for binomial and binomial-beta model prior (default: R/2, where R is the number of regressors). |
dilution |
Integer. Use 0 for no dilution prior (default), or 1 to apply a dilution prior (George 2010). |
omega |
Numeric. The exponent of the determinant for the dilution prior (George 2010). Only used when |
An object of class badp_bma, which is a list containing:
A table containing the results based on the binomial model prior.
A table containing the results based on the binomial-beta model prior.
A vector containing the names of the regressors, used by the functions.
The total number of regressors.
The number of models present in the model space.
A table containing model IDs and posterior model probabilities (PMPs) for the jointness function.
A table containing model IDs, PMPs, coefficients, standard deviations, and standardized regression coefficients (stdRs) for the best_models function.
The expected model size for the binomial and binomial-beta model priors, as specified by the user (default is EMS = R/2).
A table of uniform and random model priors distributed over model sizes for the model_sizes function.
A table containing the posterior model probabilities for use in the model_sizes function.
A table containing the model priors, used by the model_pmp function.
A parameter indicating whether the priors were diluted, used in the model_sizes function.
A matrix of coefficients for the lagged dependent variable across all models, used in the coef_hist function.
A matrix of nonzero coefficients for the regressors, used in the coef_hist function.
A table containing the degrees of freedom for the estimated models in the best_models function.
A table containing the prior and posterior expected model sizes for the binomial and binomial-beta model priors.
The dilution parameter used (the exponent of the determinant). Relevant only when dilution = 1.
Objects of class badp_bma have the following methods available:
print.badp_bma - Display results
summary.badp_bma - Detailed statistical summary
coef.badp_bma - Extract coefficients
plot.badp_bma - Visualize results
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 )library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 )
This function draws graphs of the distribution (in the form of histogram or kernel density) of the coefficients for all the considered regressors over the part of the model space that includes these regressors (half of the model space).
coef_hist( bma_list, weight = NULL, bin_method = c("FD", "SC", "vec"), bin_widths = NULL, use_bin_count = 0, bin_counts = NULL, use_kernel = 0 )coef_hist( bma_list, weight = NULL, bin_method = c("FD", "SC", "vec"), bin_widths = NULL, use_bin_count = 0, bin_counts = NULL, use_kernel = 0 )
bma_list |
An object of class |
weight |
Parameter indicating whether the coefficients should be weighted by posterior model probabilities:
|
bin_method |
Character string specifying the method for bin widths (default: |
bin_widths |
A vector with bin widths to be used to construct histograms for the regressors. The vector must be of the size equal to total number of regressors plus one for the lagged dependent variable. The vector with bin widths is used only if parameter |
use_bin_count |
Parameter taking the values (default: |
bin_counts |
A vector with the numbers of bins to be used to construct histograms for the regressors. The vector must be of the size equal to total number of regressors plus one for the lagged dependent variable. The vector with bin counts is used only if parameter |
use_kernel |
A parameter taking the values (default: |
A list with the graphs of the distribution of coefficients for all the considered regressors and the lagged dependent variable.
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) coef_plots <- coef_hist(bma_results, use_kernel = 1)library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) coef_plots <- coef_hist(bma_results, use_kernel = 1)
Coefficient extraction method for objects of class badp_bma.
## S3 method for class 'badp_bma' coef( object, prior = "both", conditional = FALSE, se = FALSE, robustSE = FALSE, PIP = TRUE, ... )## S3 method for class 'badp_bma' coef( object, prior = "both", conditional = FALSE, se = FALSE, robustSE = FALSE, PIP = TRUE, ... )
object |
An object of class |
prior |
Character string specifying which prior to use. Options are
|
conditional |
Logical. If |
se |
Logical. If |
robustSE |
Logical. Only meaningful when |
PIP |
Logical. If |
... |
Additional arguments (currently unused). |
This function extracts coefficient estimates from Bayesian Model Averaging
results. By default both priors are reported so the user can compare them
at a glance; set prior = "binomial" or prior = "beta" to
obtain the legacy single-prior return values (useful when feeding
coefficients into downstream code).
The shape of the return value depends on prior,
conditional, se, robustSE and PIP:
prior = "both": always a badp_bma_coef data frame.
Columns are grouped by prior; for each prior the columns
binom_PM / beta_PM (or binom_PMcon / beta_PMcon
when conditional = TRUE), standard error columns
binom_PSD / beta_PSD (or binom_PSDR / beta_PSDR
when robustSE = TRUE, or their con variants when
conditional = TRUE) when se = TRUE, and
binom_PIP / beta_PIP when PIP = TRUE.
prior = "binomial" or "beta": a named numeric
vector of estimates when se = FALSE and PIP = FALSE;
otherwise a data frame with columns PM (or PMcon
when conditional = TRUE), posterior standard deviation column
PSD / PSDR (or PSDcon / PSDRcon)
when se = TRUE, and PIP when PIP = TRUE.
bma, summary.badp_bma,
print.badp_bma_coef
data(full_model_space) results <- bma(full_model_space) # Posterior means under both priors with PIP coef(results) # With standard errors coef(results, se = TRUE) # With robust standard errors coef(results, se = TRUE, robustSE = TRUE) # Conditional posterior means and SEs coef(results, conditional = TRUE, se = TRUE) # Suppress PIP column coef(results, PIP = FALSE) # Single-prior numeric vector (legacy behaviour) coef(results, prior = "binomial", PIP = FALSE)data(full_model_space) results <- bma(full_model_space) # Posterior means under both priors with PIP coef(results) # With standard errors coef(results, se = TRUE) # With robust standard errors coef(results, se = TRUE, robustSE = TRUE) # Conditional posterior means and SEs coef(results, conditional = TRUE, se = TRUE) # Suppress PIP column coef(results, PIP = FALSE) # Single-prior numeric vector (legacy behaviour) coef(results, prior = "binomial", PIP = FALSE)
Approximate standard deviations are computed for the models in the given model space. Two versions are computed.
compute_model_space_stats( df, dep_var_col, timestamp_col, entity_col, params, nested = TRUE, exact_value = FALSE, model_prior = "uniform", cl = NULL )compute_model_space_stats( df, dep_var_col, timestamp_col, entity_col, params, nested = TRUE, exact_value = FALSE, model_prior = "uniform", cl = NULL )
df |
Data frame with data for the SEM analysis. |
dep_var_col |
Column with the dependent variable |
timestamp_col |
The name of the column with timestamps |
entity_col |
Column with entities (e.g. countries) |
params |
A matrix (with named rows) with each column corresponding to a model. Each column specifies model parameters. Compare with optim_model_space_params |
nested |
Logical. If |
exact_value |
Whether the exact value of the likelihood should be
computed ( |
model_prior |
Which model prior to use. For now there are two options:
|
cl |
An optional cluster object. If supplied, the function will use this
cluster for parallel processing. If |
Matrix with columns describing likelihood and standard deviations for each model. The first row is the likelihood for the model (computed using the parameters in the provided model space). The second row is almost 1/2 * BIC_k as in Raftery's Bayesian Model Selection in Social Research eq. 19. Then there are rows with standard deviations for each parameter. After that we have rows with robust standard deviation.
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) compute_model_space_stats( df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, params = small_model_space$params )library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) compute_model_space_stats( df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, params = small_model_space$params )
Data used in Growth Empirics in Panel Data under Model Uncertainty and Weak Exogeneity (Moral-Benito, 2016, Journal of Applied Econometrics).
economic_growtheconomic_growth
economic_growthA data frame with 365 rows and 12 columns (73 countries and 4 periods + extra one for lagged dependent variable):
Year
Country ID
Logarithm of GDP per capita (2000 US dollars at PP)
Ratio of real domestic investment to GDP
Stock of years of secondary education in the total population
Average growth rate of population
Population in millions of people
Purchasing-power-parity numbers for investment goods
Exports plus imports as a share of GDP
Ratio of government consumption to GDP
Logarithm of the life expectancy at birth
Composite index given by the democracy score minus the autocracy score
http://qed.econ.queensu.ca/jae/datasets/moral-benito001/
This function performs feature standardization (also known as z-score normalization) by centering the features around their mean and scaling by their standard deviation.
feature_standardization(df, excluded_cols, group_by_col, scale = TRUE)feature_standardization(df, excluded_cols, group_by_col, scale = TRUE)
df |
Data frame with the data. |
excluded_cols |
Unquoted column names to exclude from standardization. If missing, all columns are standardized. |
group_by_col |
Unquoted column names to group the data by before applying standardization. If missing, no grouping is performed. |
scale |
Logical. If |
A data frame with standardized features.
df <- data.frame( year = c(2000, 2001, 2002, 2003, 2004), country = c("A", "A", "B", "B", "C"), gdp = c(1, 2, 3, 4, 5), ish = c(2, 3, 4, 5, 6), sed = c(3, 4, 5, 6, 7) ) # Standardize every column df_with_only_numeric_values <- df[, setdiff(names(df), "country")] feature_standardization(df_with_only_numeric_values) # Standardize all columns except 'country' feature_standardization(df, excluded_cols = country) # Standardize across countries (grouped by 'country') feature_standardization(df, group_by_col = country) # Standardize, excluding 'country' and group-wise by 'year' feature_standardization(df, excluded_cols = country, group_by_col = year)df <- data.frame( year = c(2000, 2001, 2002, 2003, 2004), country = c("A", "A", "B", "B", "C"), gdp = c(1, 2, 3, 4, 5), ish = c(2, 3, 4, 5, 6), sed = c(3, 4, 5, 6, 7) ) # Standardize every column df_with_only_numeric_values <- df[, setdiff(names(df), "country")] feature_standardization(df_with_only_numeric_values) # Standardize all columns except 'country' feature_standardization(df, excluded_cols = country) # Standardize across countries (grouped by 'country') feature_standardization(df, group_by_col = country) # Standardize, excluding 'country' and group-wise by 'year' feature_standardization(df, excluded_cols = country, group_by_col = year)
A badp_bma object summarising the BMA analysis
full_bma_resultsfull_bma_results
An object of class badp_bma
optim_model_space
A badp_model_space object created with optim_model_space using the
economic_growth dataset.
full_model_spacefull_model_space
An object of class badp_model_space:
A numeric matrix with 40 rows and 512 columns, containing parameter values for the full model space. Each column represents a different model.
A numeric matrix of statistics computed by
compute_model_space_stats based on params.
Row 1 contains model likelihoods. Row 2 contains a quantity proportional
to 0.5 * BIC (cf. Raftery, Bayesian Model Selection in Social Research,
Eq. 19). Rows 3–7 contain standard deviations, and rows 8–12 contain
robust standard deviations.
A character vector with the names of the variables.
The total number of observations in the panel (292).
The data frame used in the analysis.
A logical indicating whether the model space uses nested specifications.
This function calculates four types of the jointness measures based on the posterior model probabilities calculated using binomial and binomial-beta model prior. The four measures are:
HCGHM - for Hofmarcher et al. (2018) measure;
LS - for Ley & Steel (2007) measure;
DW - for Doppelhofer & Weeks (2009) measure;
PPI - for posterior probability of including both variables.
The measures under binomial model prior will appear in a table above the diagonal, and the measure calculated under binomial-beta model prior below the diagonal.
REFERENCES
Doppelhofer G, Weeks M (2009) Jointness of growth determinants. Journal of Applied Econometrics., 24(2), 209-244. doi: 10.1002/jae.1046
Hofmarcher P, Crespo Cuaresma J, Grün B, Humer S, Moser M (2018) Bivariate jointness measures in Bayesian Model Averaging: Solving the conundrum. Journal of Macroeconomics, 57, 150-165. doi: 10.1016/j.jmacro.2018.05.005
Ley E, Steel M (2007) Jointness in Bayesian variable selection with applications to growth regression. Journal of Macroeconomics, 29(3), 476-493. doi: 10.1016/j.jmacro.2006.12.002
jointness( bma_list, measure = c("HCGHM", "LS", "DW", "PPI"), rho = 0.5, round = 3 )jointness( bma_list, measure = c("HCGHM", "LS", "DW", "PPI"), rho = 0.5, round = 3 )
bma_list |
An object of class |
measure |
Character string specifying the measure of jointness. One of: |
rho |
The parameter "rho" ( |
round |
Parameter indicating the decimal place to which the jointness measures should be rounded (default round = 3). |
A table with jointness measures for all the pairs of regressors used in the analysis. The results obtained with the binomial model prior are above the diagonal, while the ones obtained with the binomial-beta prior are below.
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) jointness_table <- jointness(bma_results, measure = "HCGHM", rho = 0.5, round = 3)library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) jointness_table <- jointness(bma_results, measure = "HCGHM", rho = 0.5, round = 3)
Data used in the manuscript Afonso, A., Alves, J., & Beck, K. (2025). Drivers of migration flows in the European Union: Earnings or unemployment? International Labour Review, 164(2), 1-23. doi:10.16995/ilr.18845
migration_datamigration_data
migration_dataA data frame with 1012 rows and 8 columns (253 country pairs and 4 periods + one additional observation for the lagged dependent variable):
Year
Country pair ID
Net migration between two countries
Lagged net migration between two countries
Difference in average real after-tax earnings in PPP
Difference in the unemployment rate
Difference in average social benefits in PPP
Difference in average tax rate
optim_model_space in the case of migration dataA badp_model_space object created with optim_model_space using the
migration_data dataset.
migration_model_spacemigration_model_space
An object of class badp_model_space:
A numeric matrix with 51 rows and 16 columns, containing parameter values for the full model space. Each column represents a different model.
A numeric matrix of statistics computed by
compute_model_space_stats based on params.
Row 1 contains model likelihoods. Row 2 contains a quantity proportional
to 0.5 * BIC (cf. Raftery, Bayesian Model Selection in Social Research,
Eq. 19). Rows 3–7 contain standard deviations, and rows 8–12 contain
robust standard deviations.
A character vector with the names of the variables.
The total number of observations in the panel (1012).
The data frame used in the analysis.
A logical indicating whether the model space uses nested specifications.
optim_model_space in the case of migration data obtained with nonnested approach.A badp_model_space object created with optim_model_space using the
migration_data dataset with nonnested approach.
migration_model_space_nonnestedmigration_model_space_nonnested
An object of class badp_model_space:
A numeric matrix with 51 rows and 16 columns, containing parameter values for the full model space. Each column represents a different model.
A numeric matrix of statistics computed by
compute_model_space_stats based on params.
Row 1 contains model likelihoods. Row 2 contains a quantity proportional
to 0.5 * BIC (cf. Raftery, Bayesian Model Selection in Social Research,
Eq. 19). Rows 3–7 contain standard deviations, and rows 8–12 contain
robust standard deviations.
A character vector with the names of the variables.
The total number of observations in the panel (1012).
The data frame used in the analysis.
A logical indicating whether the model space uses nested specifications.
This function draws four graphs of prior and posterior model probabilities for the best individual models:
a) The results with binomial model prior (based on PMP - posterior model probability)
b) The results with binomial-beta model prior (based on PMP - posterior model probability)
Models on the graph are ordered according to their posterior model probability.
model_pmp(bma_list, top = NULL)model_pmp(bma_list, top = NULL)
bma_list |
bma_list object (the result of the bma function) |
top |
The number of the best model to be placed on the graphs |
A list with three graphs with prior and posterior model probabilities for individual models:
The results with binomial model prior (based on PMP - posterior model probability)
The results with binomial-beta model prior (based on PMP - posterior model probability)
One graph combining the aforementioned graphs
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) model_graphs <- model_pmp(bma_results, top = 16)library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) model_graphs <- model_pmp(bma_results, top = 16)
This function draws two graphs of prior and posterior model probabilities:
a) The results with binomial model prior
b) The results with binomial-beta model prior
c) One graph combining all the aforementioned graphs
model_sizes(bma_list)model_sizes(bma_list)
bma_list |
bma_list object (the result of the bma function) |
A list with three graphs with prior and posterior model probabilities for model sizes:
The results with binomial model prior
The results with binomial-beta model prior
One graph combining all the aforementioned graphs
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) size_graphs <- model_sizes(bma_results)library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) size_graphs <- model_sizes(bma_results)
optim_model_space for non-nested modelsA badp_model_space object created with optim_model_space using the
economic_growth dataset.
model_space_nonnestedmodel_space_nonnested
An object of class badp_model_space:
A numeric matrix with 40 rows and 512 columns, containing parameter values for the model space. Each column represents a different model.
A numeric matrix of statistics computed by
compute_model_space_stats based on params.
Row 1 contains model likelihoods. Row 2 contains a quantity proportional
to 0.5 * BIC (cf. Raftery, Bayesian Model Selection in Social Research,
Eq. 19). Rows 3–7 contain standard deviations, and rows 8–12 contain
robust standard deviations.
A character vector with the names of the variables.
The total number of observations in the panel (292).
The data frame used in the analysis.
A logical indicating whether the model space uses nested specifications.
This function calculates model space, values of the maximized likelihood function, BICs, and standard deviations of the parameters that will be used in Bayesian model averaging. Moreover, it provides a vector with the names of the variables for bma function and the number of observations.
optim_model_space( df, timestamp_col, entity_col, dep_var_col, init_value, nested = TRUE, exact_value = FALSE, cl = NULL, control = list(trace = 0, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05) )optim_model_space( df, timestamp_col, entity_col, dep_var_col, init_value, nested = TRUE, exact_value = FALSE, cl = NULL, control = list(trace = 0, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05) )
df |
Data frame with data for the analysis. |
timestamp_col |
The name of the column with time stamps |
entity_col |
Column with entities (e.g. countries) |
dep_var_col |
Column with the dependent variable |
init_value |
The value with which the model space will be initialized. This will be the starting point for the numerical optimization. |
nested |
Logical. If |
exact_value |
Whether the exact value of the likelihood should be
computed ( |
cl |
An optional cluster object. If supplied, the function will use this
cluster for parallel processing. If |
control |
a list of control parameters for the optimization which are
passed to optim. Default is
|
An object of class badp_model_space, which is a list with the following elements:
params - table with parameters of all estimated models
stats - table with the value of maximized likelihood function, BIC, and
standard errors for all estimated models
reg_names - vector with the names of the variables
observations_num - number of observations
df - data frame used in estimation
is_nested - logical indicating whether nested approach was used
Objects of class badp_model_space have the following methods available:
print.badp_model_space - Display model space information
library(magrittr) data_prepared <- badp::economic_growth[, 1:5] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) optim_model_space( df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, init_value = 0.5 )library(magrittr) data_prepared <- badp::economic_growth[, 1:5] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) optim_model_space( df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, init_value = 0.5 )
Given a dataset and an initial value for parameters, initializes a model space with parameters equal to the initial value for each model. Then for each model performs a numerical optimization and finds parameters which maximize the likelihood.
optim_model_space_params( df, timestamp_col, entity_col, dep_var_col, init_value, nested, exact_value = FALSE, cl = NULL, control = list(trace = 0, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05) )optim_model_space_params( df, timestamp_col, entity_col, dep_var_col, init_value, nested, exact_value = FALSE, cl = NULL, control = list(trace = 0, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05) )
df |
Data frame with data for the analysis. |
timestamp_col |
The name of the column with time stamps. |
entity_col |
Column with entities (e.g. countries). |
dep_var_col |
Column with the dependent variable. |
init_value |
The value with which the model space will be initialized. This will be the starting point for the numerical optimization. |
nested |
Logical. If |
exact_value |
Whether the exact value of the likelihood should be
computed ( |
cl |
An optional cluster object. If supplied, the function will use this
cluster for parallel processing. If |
control |
a list of control parameters for the optimization which are
passed to optim. Default is
|
List (or matrix) of parameters describing analyzed models.
Data used in Growth Empirics in Panel Data under Model Uncertainty and Weak Exogeneity (Moral-Benito, 2016, Journal of Applied Econometrics).
original_economic_growthoriginal_economic_growth
original_economic_growthA data frame with 292 rows and 13 columns (73 countries and 4 periods + extra one for lagged dependent variable):
Year
Country ID
Logarithm of GDP per capita (2000 US dollars at PP)
Lagged logarithm of GDP per capita (2000 US dollars at PP)
Ratio of real domestic investment to GDP
Stock of years of secondary education in the total population
Average growth rate of population
Population in millions of people
Purchasing-power-parity numbers for investment goods
Exports plus imports as a share of GDP
Ratio of government consumption to GDP
Logarithm of the life expectancy at birth
Composite index given by the democracy score minus the autocracy score
http://qed.econ.queensu.ca/jae/datasets/moral-benito001/
Plot method for objects of class badp_bma.
## S3 method for class 'badp_bma' plot(x, which = "model_sizes", ...)## S3 method for class 'badp_bma' plot(x, which = "model_sizes", ...)
x |
An object of class |
which |
Character string specifying which plot to create. Options are:
|
... |
Additional arguments passed to the underlying plot function. |
This function dispatches to the appropriate visualization function based on the
which parameter. The default plot shows model size distributions, which
provides a comprehensive overview of the prior and posterior distributions over
model sizes.
The object returned by the selected visualization helper. Depending on
which, this may be a single plot object or a list containing plots
and/or tables; some helpers may also print output as a side effect.
bma, model_sizes, best_models,
jointness, coef_hist, posterior_dens,
model_pmp
data(full_model_space) results <- bma(full_model_space) # Default plot (model sizes) plot(results) # Other plot types plot(results, which = "best_models") plot(results, which = "jointness")data(full_model_space) results <- bma(full_model_space) # Default plot (model sizes) plot(results) # Other plot types plot(results, which = "best_models") plot(results, which = "jointness")
This function draws graphs of the posterior densities of all the coefficients of interest.
posterior_dens(bma_list, prior = "binomial", SE = "standard")posterior_dens(bma_list, prior = "binomial", SE = "standard")
bma_list |
bma object (the result of the bma function) |
prior |
Parameter indicating which model prior should be used for calculations:
|
SE |
Parameter indicating which standard errors should be used in calculation of posterior standard deviation:
|
A list with the graphs of the posterior densities of coefficients for all the considered regressors.
library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) posterior_graphs <- posterior_dens(bma_results, prior = "binomial", SE = "robust")library(magrittr) data_prepared <- badp::economic_growth[, 1:6] %>% badp::feature_standardization( excluded_cols = c(country, year, gdp) ) %>% badp::feature_standardization( group_by_col = year, excluded_cols = country, scale = FALSE ) bma_results <- bma( model_space = badp::small_model_space, round = 3, dilution = 0 ) posterior_graphs <- posterior_dens(bma_results, prior = "binomial", SE = "robust")
Print method for objects of class badp_best_models returned by
best_models. Draws the table chosen by the estimate
and robust arguments of the original best_models
call to the active graphics device.
## S3 method for class 'badp_best_models' print(x, ...)## S3 method for class 'badp_best_models' print(x, ...)
x |
An object of class |
... |
Additional arguments (currently unused). |
Because R only auto-prints expressions evaluated at the top level, calling
best_models(bma_list) without assignment triggers this method (and
hence the table is drawn), while best <- best_models(bma_list)
stays silent.
Invisibly returns the input object x.
Print method for objects of class badp_bma.
## S3 method for class 'badp_bma' print(x, ...)## S3 method for class 'badp_bma' print(x, ...)
x |
An object of class |
... |
Additional arguments (currently unused). |
This method is a thin wrapper that delegates to
summary.badp_bma and then prints the resulting summary, so
print(x) and print(summary(x)) produce identical output.
The output displays BMA statistics for both the binomial and binomial-beta
priors to allow direct comparison.
Invisibly returns the input object x.
bma, summary.badp_bma, coef.badp_bma
data(full_model_space) results <- bma(full_model_space) print(results)data(full_model_space) results <- bma(full_model_space) print(results)
Print method for objects of class badp_bma_coef produced by
coef.badp_bma when prior = "both".
## S3 method for class 'badp_bma_coef' print(x, digits = 4, ...)## S3 method for class 'badp_bma_coef' print(x, digits = 4, ...)
x |
An object of class |
digits |
Integer. Number of significant digits used when printing
numeric columns. Defaults to |
... |
Additional arguments passed to |
When the result contains only point estimates (se = FALSE,
PIP = FALSE), the two priors are printed side by side. Otherwise
the output is split into two stacked panels - one per prior - each with
the requested combination of estimate, std.error and
PIP columns. The header reflects whether the estimates are
unconditional or conditional on inclusion, and whether standard errors
are robust.
Invisibly returns the input object x.
Print method for the captured gTree objects stored in positions
7-9 of the list returned by best_models. Renders the grob
to the active graphics device via grid.draw so that
typing e.g. best[[9]] at the console displays the picture
instead of a description string.
## S3 method for class 'badp_drawable_grob' print(x, newpage = TRUE, ...)## S3 method for class 'badp_drawable_grob' print(x, newpage = TRUE, ...)
x |
A |
newpage |
Logical; if |
... |
Additional arguments (currently unused). |
Invisibly returns the input object x.
Print method for objects of class badp_model_space.
## S3 method for class 'badp_model_space' print(x, ...)## S3 method for class 'badp_model_space' print(x, ...)
x |
An object of class |
... |
Additional arguments forwarded to |
This method is a thin wrapper that delegates to
summary.badp_model_space and then prints the resulting
summary, so print(x) and print(summary(x)) produce
identical output.
Invisibly returns the input object x.
summary.badp_model_space,
optim_model_space, bma
data(full_model_space) print(full_model_space)data(full_model_space) print(full_model_space)
Print method for summary.badp_bma objects.
## S3 method for class 'summary.badp_bma' print(x, ...)## S3 method for class 'summary.badp_bma' print(x, ...)
x |
An object of class |
... |
Additional arguments (currently unused). |
Invisibly returns the input object x.
Print method for summary.badp_model_space objects.
## S3 method for class 'summary.badp_model_space' print(x, ...)## S3 method for class 'summary.badp_model_space' print(x, ...)
x |
An object of class |
... |
Additional arguments (currently unused). |
Invisibly returns the input object x.
Likelihood for the SEM model
sem_likelihood( params, data, timestamp_col, entity_col, dep_var_col, lin_related_regressors = NULL, per_entity = FALSE, exact_value = TRUE )sem_likelihood( params, data, timestamp_col, entity_col, dep_var_col, lin_related_regressors = NULL, per_entity = FALSE, exact_value = TRUE )
params |
Parameters describing the model. Can be either a vector or a list with named parameters. See 'Details' |
data |
Data for the likelihood computations. Can be either a list of matrices or a dataframe. If the dataframe, additional parameters are required to build the matrices within the function. |
timestamp_col |
Column which determines time stamps. For now only natural numbers can be used. |
entity_col |
Column which determines entities (e.g. countries, people) |
dep_var_col |
Column with dependent variable |
lin_related_regressors |
Which subset of columns should be used as
regressors for the current model. In other words |
per_entity |
Whether to compute overall likelihood or a vector of likelihoods with per entity value |
exact_value |
Whether the exact value of the likelihood should be
computed ( |
The params argument is a list that should contain the following
components:
alpha scalar value which determines linear dependence on lagged
dependent variable
phi_0 scalar value which determines linear dependence on the value
of dependent variable at the lowest time stamp
err_var scalar value which determines classical error component
(Sigma11 matrix, sigma_epsilon^2)
dep_vars double vector of length equal to the number of time stamps
(i.e. time stamps greater than or equal to the second lowest time stamp)
beta double vector which determines the linear dependence on
regressors different than the lagged dependent variable; The vector should
have length equal to the number of regressors.
phi_1 double vector which determines the linear dependence on
initial values of regressors different than the lagged dependent variable;
The vector should have length equal to the number of regressors.
phis double vector which together with psis determines upper
right and bottom left part of the covariance matrix; The vector should have
length equal to the number of regressors times number of time stamps minus 1,
i.e. n_regressors * (n_periods - 1)
psis double vector which together with psis determines upper
right and bottom left part of the covariance matrix; The vector should have
length equal to the number of regressors times number of time stamps minus 1
times number of time stamps divided by 2, i.e.
n_regressors * (n_periods - 1) * n_periods / 2
The value of the likelihood for SEM model (or a part of interest of the likelihood)
data(economic_growth) eg <- feature_standardization(economic_growth, excluded_cols = c(year, country)) sem_likelihood(0.5, eg, year, country, gdp)data(economic_growth) eg <- feature_standardization(economic_growth, excluded_cols = c(year, country)) sem_likelihood(0.5, eg, year, country, gdp)
optim_model_space (small version)A list created with optim_model_space using the
economic_growth dataset and only three regressors:
ish, sed, and pgrw.
small_model_spacesmall_model_space
An object of class badp_model_space:
A numeric matrix with 40 rows and 8 columns (corresponding to
models), containing parameter values for the model space.
Each column represents a different model.
A numeric matrix of statistics computed by
compute_model_space_stats based on params.
Row 1 contains model likelihoods. Row 2 contains a quantity proportional
to 0.5 * BIC (cf. Raftery, Bayesian Model Selection in Social Research,
Eq. 19). Rows 3–7 contain standard deviations, and rows 8–12 contain
robust standard deviations.
A character vector with the names of the regressors.
The total number of observations in the panel (292).
The data frame used in the analysis.
A logical indicating whether the model space uses nested specifications.
Summary method for objects of class badp_bma.
## S3 method for class 'badp_bma' summary(object, ...)## S3 method for class 'badp_bma' summary(object, ...)
object |
An object of class |
... |
Additional arguments (currently unused). |
This function creates a comprehensive summary object that includes model space information, BMA statistics for both priors, and highlights variables with high posterior inclusion probabilities. The summary always displays results for both the binomial and binomial-beta priors to allow direct comparison.
An object of class summary.badp_bma containing:
model_space_size - Total number of models in the model space
num_regressors - Number of regressors (excluding lagged dependent variable)
expected_model_size - Expected model size
dilution_applied - Logical indicating if dilution prior was used
omega - Numeric value of the dilution parameter (omega); only relevant when dilution_applied is TRUE
results_binomial - Coefficient table for binomial prior
results_beta - Coefficient table for binomial-beta prior
model_sizes - Prior and posterior model sizes table
reg_names - Variable names
bma, print.badp_bma, coef.badp_bma
data(full_model_space) results <- bma(full_model_space) summary(results)data(full_model_space) results <- bma(full_model_space) summary(results)
Summary method for objects of class badp_model_space. Replaces
the default summary() output (which just lists the structure of
the underlying list) with a structured object describing the dimensions
of the model space, its variables, and a brief look at the per-model
log-likelihoods stored in object$stats.
## S3 method for class 'badp_model_space' summary(object, ...)## S3 method for class 'badp_model_space' summary(object, ...)
object |
An object of class |
... |
Additional arguments (currently unused). |
An object of class summary.badp_model_space containing:
num_models - Number of models in the model space ().
num_regressors - Number of regressors excluding the lagged
dependent variable ().
num_params - Number of parameters in the full parameter
vector (rows of object$params).
observations_num - Number of observations used.
is_nested - Logical, whether the model space is nested.
reg_names - All variable names (lagged dependent first).
dep_var_name - The (lagged) dependent variable name.
regressor_names - The regressor names.
data_dim - Dimensions of the source data frame, or
NULL if not stored.
likelihoods - Per-model log-likelihood values (row 1 of
object$stats), or NULL if not available.
print.badp_model_space,
print.summary.badp_model_space,
optim_model_space, bma
data(full_model_space) summary(full_model_space)data(full_model_space) summary(full_model_space)