Title: | Bayesian Dynamic Systems Modeling |
---|---|
Description: | Implements methods for building and analyzing models based on panel data 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: | Mateusz Wyszynski [aut], Marcin Dubel [ctb, cre], Krzysztof Beck [ctb] |
Maintainer: | Marcin Dubel <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2025-03-11 08:31:11 UTC |
Source: | https://github.com/cranhaven/cranhaven.r-universe.dev |
A summary of a given model space is prepared. This include things such as posterior inclusion probability (PIP), posterior mean and so on. This is the core function of the package, because it allows to make assessments and decisions about the parameters and models.
bma_summary( df, dep_var_col, timestamp_col, entity_col, model_space, exact_value = TRUE, model_prior = "uniform", run_parallel = FALSE )
bma_summary( df, dep_var_col, timestamp_col, entity_col, model_space, exact_value = TRUE, model_prior = "uniform", run_parallel = FALSE )
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) |
model_space |
A matrix (with named rows) with each column corresponding to a model. Each column specifies model parameters. Compare with optimal_model_space |
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:
|
run_parallel |
If |
List of parameters describing analyzed models
library(magrittr) data_prepared <- economic_growth[,1:7] %>% feature_standardization(timestamp_col = year, entity_col = country) %>% feature_standardization(timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) bma_result <- bma_summary(df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, model_space = economic_growth_ms)
library(magrittr) data_prepared <- economic_growth[,1:7] %>% feature_standardization(timestamp_col = year, entity_col = country) %>% feature_standardization(timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) bma_result <- bma_summary(df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, model_space = economic_growth_ms)
Data used in Growth Empirics in Panel Data under Model Uncertainty and Weak Exogeneity (Moral-Benito, 2016, Journal of Applied Econometrics).
economic_growth
economic_growth
economic_growth
A data frame with 365 rows and 12 columns:
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/
A matrix representing the summary of parameters computed with
parameters_summary
based on the economic_growth_ms
model
space. TODO: describe the matrix properly after cleaning up the code of the
function parameters_summary
.
economic_growth_bma_params
economic_growth_bma_params
economic_growth_bma_params
A double matrix with 5 rows and 8 columns
A matrix representing the summary of likelihoods computed with
likelihoods_summary
based on the economic_growth_ms
model
space. The matrix contains likelihoods, standard deviations and robust
standard deviations
economic_growth_liks
economic_growth_liks
economic_growth_stds
A double matrix with 11 rows and 16 columns.
Likelihoods for the models
Almost 1/2 * BIC_k as in Raftery's Bayesian Model Selection in Social Research eq. 19.
Standard deviations
Robust standard deviations
A matrix representing the model space built using subset of regressors from
the economic_growth
dataset. The included regressors are ish
,
sed
, pgrw
and pop
. Therefore the model space contains
2^4 = 16
models (columns).
economic_growth_ms
economic_growth_ms
economic_growth_ms
A double matrix with 51 rows and 16 columns.
A matrix representing the model space built using all regressors from
the economic_growth
dataset. Therefore the model space contains
2^9 = 512
models (columns). The same projection matrix is used for
each model.
economic_growth_ms_full_proj_const
economic_growth_ms_full_proj_const
economic_growth_ms_full_proj_const
A double matrix with 106 rows and 512 columns.
TODO: to avoid NaNs when computing estimates of standard deviations, the step size in the hessian function has to be increased to 1e-2. This is most likely cause by the fact that the likelihood values are much closer to each other after the correction for the projection matrix is introduced. Hence we have to either increase the relative tolerance of the optimization algorithm or loosen the precision when computing approximate hessian.
A matrix representing the model space built using all regressors from
the economic_growth
dataset. Therefore the model space contains
2^9 = 512
models (columns). This model space generates Posterior
Inclusion Probabilities which are consistent with the results presented by
Moral-Benito. The original results were approximated up to the 4th decimal
place. The results obtained using this model space lead to exactly the same
approximations. A different projection matrix is used for each model.
economic_growth_ms_full_proj_var
economic_growth_ms_full_proj_var
economic_growth_ms_full_proj_var
A double matrix with 106 rows and 512 columns.
Create matrix which contains exogenous variables used in the Simultaneous Equations Model (SEM) representation. Currently these are: dependent variable from the lowest time stamp and regressors from the second lowest time stamp. The matrix is then used to compute likelihood for SEM analysis.
exogenous_matrix(df, timestamp_col, entity_col, dep_var_col)
exogenous_matrix(df, timestamp_col, entity_col, dep_var_col)
df |
Data frame with data for the SEM analysis. |
timestamp_col |
Column which determines time periods. For now only natural numbers can be used as timestamps |
entity_col |
Column which determines entities (e.g. countries, people) |
dep_var_col |
Column with dependent variable |
Matrix of size N x k+1 where N is the number of entities considered and k is the number of chosen regressors
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) exogenous_matrix(df, times, entities, dep_var)
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) exogenous_matrix(df, times, entities, dep_var)
This function performs feature standarization (also known as z-score normalization), i.e. the features are centered around the mean and scaled with standard deviation.
feature_standardization( df, timestamp_col, entity_col, cross_sectional = FALSE, scale = TRUE )
feature_standardization( df, timestamp_col, entity_col, cross_sectional = FALSE, scale = TRUE )
df |
Dataframe with data that should be prepared for LIML estimation |
timestamp_col |
Column with timestamps (e.g. years) |
entity_col |
Column with entities (e.g. countries) |
cross_sectional |
Whether to perform feature standardization within cross sections |
scale |
Whether to divide by the standard deviation |
A dataframe 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) ) feature_standardization(df, year, country)
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) ) feature_standardization(df, year, country)
Creates the hessian matrix for a given likelihood function.
hessian(lik, theta, ...)
hessian(lik, theta, ...)
lik |
function |
theta |
kx1 matrix |
... |
other parameters passed to |
Hessian kxk matrix where k is the number of parameters included in the theta matrix
lik <- function(theta) { return(theta[1]^2 + theta[2]^2) } hessian(lik, c(1, 1))
lik <- function(theta) { return(theta[1]^2 + theta[2]^2) } hessian(lik, c(1, 1))
This function builds a representation of the model space, by creating a
dataframe where each column represents values of the parameters for a given
model. Real value means that the parameter is included in the model. A
parameter not present in the model is marked as NA
.
initialize_model_space( df, timestamp_col, entity_col, dep_var_col, init_value = 1 )
initialize_model_space( df, timestamp_col, entity_col, dep_var_col, init_value = 1 )
df |
Data frame with data for the SEM analysis. |
timestamp_col |
Column which determines time periods. For now only natural numbers can be used as timestamps |
entity_col |
Column which determines entities (e.g. countries, people) |
dep_var_col |
Column with dependent variable |
init_value |
Initial value for parameters present in the model. Default
is |
Currently the set of features is assumed to be all columns which remain after
excluding timestamp_col
, entity_col
and dep_var_col
.
A power set of all possible exclusions of linear dependence on the given feature is created, i.e. if there are 4 features we end up with 2^4 possible models (for each model we independently decide whether to include or not a feature).
matrix of model parameters
library(magrittr) data_prepared <- economic_growth[,1:7] %>% feature_standardization(timestamp_col = year, entity_col = country) %>% feature_standardization(timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) initialize_model_space(data_prepared, year, country, gdp)
library(magrittr) data_prepared <- economic_growth[,1:7] %>% feature_standardization(timestamp_col = year, entity_col = country) %>% feature_standardization(timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) initialize_model_space(data_prepared, year, country, gdp)
This function allows to turn data in the format with lagged values for a chosen column (i.e. there are two columns with the same quantity, but one column is lagged in time) into the format with just one column
join_lagged_col( df, col, col_lagged, timestamp_col, entity_col, timestep = NULL )
join_lagged_col( df, col, col_lagged, timestamp_col, entity_col, timestep = NULL )
df |
Dataframe with data with a column with lagged values |
col |
Column with quantity not lagged |
col_lagged |
Column with the same quantity as |
timestamp_col |
Column with timestamps (e.g. years) |
entity_col |
Column with entities (e.g. countries) |
timestep |
Difference between timestamps (e.g. 10) |
A dataframe with two columns merged, i.e. just one column with the desired quantity is left.
df <- data.frame( year = c(2000, 2001, 2002, 2003, 2004), country = c("A", "A", "B", "B", "C"), gdp = c(1, 2, 3, 4, 5), gdp_lagged = c(NA, 1, 2, 3, 4) ) join_lagged_col(df, gdp, gdp_lagged, year, country, 1)
df <- data.frame( year = c(2000, 2001, 2002, 2003, 2004), country = c("A", "A", "B", "B", "C"), gdp = c(1, 2, 3, 4, 5), gdp_lagged = c(NA, 1, 2, 3, 4) ) join_lagged_col(df, gdp, gdp_lagged, year, country, 1)
Approximate standard deviations are computed for the models in the given model space. Two versions are computed.
likelihoods_summary( df, dep_var_col, timestamp_col, entity_col, model_space, exact_value = TRUE, model_prior = "uniform", run_parallel = FALSE )
likelihoods_summary( df, dep_var_col, timestamp_col, entity_col, model_space, exact_value = TRUE, model_prior = "uniform", run_parallel = FALSE )
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) |
model_space |
A matrix (with named rows) with each column corresponding to a model. Each column specifies model parameters. Compare with optimal_model_space |
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:
|
run_parallel |
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 (see TODO in the code below). The third row is model posterior probability. Then there are rows with standard deviations for each parameter. After that we have rows with robust standard deviation (not sure yet what exactly "robust" means).
data_centered_scaled <- feature_standardization(df = bdsm::economic_growth[,1:7], timestamp_col = year, entity_col = country) data_cross_sectional_standarized <- feature_standardization(df = data_centered_scaled, timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) likelihoods_summary(df = data_cross_sectional_standarized, dep_var_col = gdp, timestamp_col = year, entity_col = country, model_space = economic_growth_ms)
data_centered_scaled <- feature_standardization(df = bdsm::economic_growth[,1:7], timestamp_col = year, entity_col = country) data_cross_sectional_standarized <- feature_standardization(df = data_centered_scaled, timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) likelihoods_summary(df = data_cross_sectional_standarized, dep_var_col = gdp, timestamp_col = year, entity_col = country, model_space = economic_growth_ms)
List of matrices for SEM model
matrices_from_df( df, timestamp_col, entity_col, dep_var_col, lin_related_regressors = NULL, which_matrices = c("Y1", "Y2", "Z", "cur_Y2", "cur_Z", "res_maker_matrix") )
matrices_from_df( df, timestamp_col, entity_col, dep_var_col, lin_related_regressors = NULL, which_matrices = c("Y1", "Y2", "Z", "cur_Y2", "cur_Z", "res_maker_matrix") )
df |
Dataframe with data for the likelihood computations. |
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 |
Vector of strings of column names. Which subset of regressors is in non trivial
linear relation with the dependent variable ( |
which_matrices |
character vector with names of matrices which should be
computed. Possible matrices are
|
Named list with matrices as its elements
matrices_from_df(economic_growth, year, country, gdp, c("pop", "sed"), c("Y1", "Y2"))
matrices_from_df(economic_growth, year, country, gdp, c("pop", "sed"), c("Y1", "Y2"))
Given a dataset and an initial value for parameters, initializes a model space with parameters equal to initial value for each model. Then for each model performs a numerical optimization and finds parameters which maximize the likelihood.
optimal_model_space( df, timestamp_col, entity_col, dep_var_col, init_value, exact_value = TRUE, run_parallel = FALSE, control = list(trace = 2, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05) )
optimal_model_space( df, timestamp_col, entity_col, dep_var_col, init_value, exact_value = TRUE, run_parallel = FALSE, control = list(trace = 2, maxit = 10000, fnscale = -1, REPORT = 100, scale = 0.05) )
df |
Data frame with data for the SEM 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. |
exact_value |
Whether the exact value of the likelihood should be
computed ( |
run_parallel |
If |
control |
a list of control parameters for the optimization which are
passed to optim. Default is
|
List of parameters describing analyzed models
library(magrittr) data_prepared <- economic_growth[,1:7] %>% feature_standardization(timestamp_col = year, entity_col = country) %>% feature_standardization(timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) model_space <- optimal_model_space(df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, init_value = 0.5)
library(magrittr) data_prepared <- economic_growth[,1:7] %>% feature_standardization(timestamp_col = year, entity_col = country) %>% feature_standardization(timestamp_col = year, entity_col = country, cross_sectional = TRUE, scale = FALSE) model_space <- optimal_model_space(df = data_prepared, dep_var_col = gdp, timestamp_col = year, entity_col = country, init_value = 0.5)
TODO This is just the code previously present in the morel-benito.R script wrapped as a function (to get rid of the script). Well written code and docs are still needed
parameters_summary( regressors, bet, pvarh, pvarr, fy, fyt, ppmsize, cout, nts, pts, variables_n )
parameters_summary( regressors, bet, pvarh, pvarr, fy, fyt, ppmsize, cout, nts, pts, variables_n )
regressors |
TODO |
bet |
TODO |
pvarh |
TODO |
pvarr |
TODO |
fy |
TODO |
fyt |
TODO |
ppmsize |
TODO |
cout |
TODO |
nts |
TODO (negatives) |
pts |
TODO (positives) |
variables_n |
TODO |
TODO dataframe with results
For now it is assumed that we can only exclude linear relationships between regressors and the dependent variable.
regressor_names_from_params_vector(params)
regressor_names_from_params_vector(params)
params |
a vector with parameters describing the model |
The vector needs to have named rows, i.e. it is assumed it comes from a model space (see initialize_model_space for details).
Names of regressors which are assumed to be linearly connected with dependent
variable within the model described by the params
vector.
params <- c(alpha = 1, beta_gdp = 1, beta_gdp_lagged = 1, phi_0 = 1, err_var = 1) regressor_names_from_params_vector(params)
params <- c(alpha = 1, beta_gdp = 1, beta_gdp_lagged = 1, phi_0 = 1, err_var = 1) regressor_names_from_params_vector(params)
Create residual maker matrix from a given matrix m
. See article about
projection matrix on
the Wikipedia.
residual_maker_matrix(m)
residual_maker_matrix(m)
m |
Matrix |
M x M matrix where M is the number of rows in the m
matrix.
residual_maker_matrix(matrix(c(1,2,3,4), nrow = 2))
residual_maker_matrix(matrix(c(1,2,3,4), nrow = 2))
Create coefficients matrix for Simultaneous Equations Model (SEM) representation.
SEM_B_matrix(alpha, periods_n, beta = c())
SEM_B_matrix(alpha, periods_n, beta = c())
alpha |
numeric |
periods_n |
integer |
beta |
numeric vector. Default is c() for no regressors case. |
List with two matrices B11 and B12
SEM_B_matrix(3, 4, 4:6)
SEM_B_matrix(3, 4, 4:6)
Create matrix for Simultaneous Equations Model (SEM) representation with coefficients placed next to initial values of regressors, dependent variable and country-specific time-invariant variables.
SEM_C_matrix(alpha, phi_0, periods_n, beta = c(), phi_1 = c())
SEM_C_matrix(alpha, phi_0, periods_n, beta = c(), phi_1 = c())
alpha |
numeric |
phi_0 |
numeric |
periods_n |
numeric |
beta |
numeric vector. Default is c() for no regressors case. |
phi_1 |
numeric vector. Default is c() for no regressors case. |
matrix
alpha <- 9 phi_0 <- 19 beta <- 11:15 phi_1 <- 21:25 periods_n <- 4 SEM_C_matrix(alpha, phi_0, periods_n, beta, phi_1)
alpha <- 9 phi_0 <- 19 beta <- 11:15 phi_1 <- 21:25 periods_n <- 4 SEM_C_matrix(alpha, phi_0, periods_n, beta, phi_1)
Create matrix which contains dependent variable data used in the Simultaneous Equations Model (SEM) representation on the left hand side of the equations. The matrix contains the data for time periods greater than or equal to the second lowest time stamp. The matrix is then used to compute likelihood for SEM analysis.
SEM_dep_var_matrix(df, timestamp_col, entity_col, dep_var_col)
SEM_dep_var_matrix(df, timestamp_col, entity_col, dep_var_col)
df |
Data frame with data for the SEM analysis. |
timestamp_col |
Column which determines time periods. For now only natural numbers can be used as timestamps |
entity_col |
Column which determines entities (e.g. countries, people) |
dep_var_col |
Column with dependent variable |
Matrix of size N x T where N is the number of entities considered and T is the number of periods greater than or equal to the second lowest time stamp.
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) SEM_dep_var_matrix(df, times, entities, dep_var)
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) SEM_dep_var_matrix(df, times, entities, dep_var)
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. regressors_n * (periods_n - 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.
regressors_n * (periods_n - 1) * periods_n / 2
The value of the likelihood for SEM model (or a part of interest of the likelihood)
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) df <- feature_standardization(df, timestamp_col = times, entity_col = entities) SEM_likelihood(0.5, df, times, entities, dep_var)
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) df <- feature_standardization(df, timestamp_col = times, entity_col = entities) SEM_likelihood(0.5, df, times, entities, dep_var)
Matrix with psi parameters for SEM representation
SEM_psi_matrix(psis, timestamps_n, features_n)
SEM_psi_matrix(psis, timestamps_n, features_n)
psis |
double vector with psi parameter values |
timestamps_n |
number of time stamps (e.g. years) |
features_n |
number of features (e.g. population size, investment rate) |
A matrix with timestamps_n
rows and
(timestamps_n - 1) * feature_n
columns. Psis are filled in row by row
in a block manner, i.e. blocks of size feature_n
are placed next to
each other
SEM_psi_matrix(1:30, 4, 5)
SEM_psi_matrix(1:30, 4, 5)
Create matrix which contains regressors data used in the Simultaneous Equations Model (SEM) representation on the left hand side of the equations. The matrix contains regressors data for time periods greater than or equal to the second lowest time stamp. The matrix is then used to compute likelihood for SEM analysis.
SEM_regressors_matrix(df, timestamp_col, entity_col, dep_var_col)
SEM_regressors_matrix(df, timestamp_col, entity_col, dep_var_col)
df |
Data frame with data for the SEM analysis. |
timestamp_col |
Column which determines time periods. For now only natural numbers can be used as timestamps |
entity_col |
Column which determines entities (e.g. countries, people) |
dep_var_col |
Column with dependent variable |
Matrix of size N x (T-1)*k where N is the number of entities considered, T is
the number of periods greater than or equal to the second lowest time stamp
and k is the number of chosen regressors. If there are no regressors returns
NULL
.
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) SEM_regressors_matrix(df, times, entities, dep_var)
set.seed(1) df <- data.frame( entities = rep(1:4, 5), times = rep(seq(1960, 2000, 10), each = 4), dep_var = stats::rnorm(20), a = stats::rnorm(20), b = stats::rnorm(20) ) SEM_regressors_matrix(df, times, entities, dep_var)
Create covariance matrix for Simultaneous Equations Model (SEM) representation. Only the part necessary to compute concentrated likelihood function is computed (cf. Appendix in the Moral-Benito paper)
SEM_sigma_matrix(err_var, dep_vars, phis = c(), psis = c())
SEM_sigma_matrix(err_var, dep_vars, phis = c(), psis = c())
err_var |
numeric |
dep_vars |
numeric vector |
phis |
numeric vector |
psis |
numeric vector |
List with two matrices Sigma11 and Sigma12
err_var <- 1 dep_vars <- c(2, 2, 2, 2) phis <- c(10, 10, 20, 20, 30, 30) psis <- c(101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112) SEM_sigma_matrix(err_var, dep_vars, phis, psis)
err_var <- 1 dep_vars <- c(2, 2, 2, 2) phis <- c(10, 10, 20, 20, 30, 30) psis <- c(101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112) SEM_sigma_matrix(err_var, dep_vars, phis, psis)