18 Multiequationals linear models
Let’s consider a multivariate linear model, i.e. with
18.1 OLS estimate
As in the uni-variate case the optimal parameters are computed as:
18.1.1 Example
Let’s consider
For
- Simulate of the explanatory variables, the regression parameters and the residuals.
- Simulate the perturbed
(regression with errors). - Fit the regression parameters on the
.
- Compute the fitted residuals from the prediction obtained with the parameters in step 3. and compute their variance covariance matrix.
- Compare the results with the true parameters.
Setup
######################## Setup ########################
set.seed(1) # random seed
n <- 500 # number of observations
p <- 2 # number of dependent variables
k <- 3 # number of regressors
# True regressor's mean
true_e_x <- matrix(rep(0.5, k), ncol = 1)
# True regressor's covariance matrix
true_cv_x <- matrix(c(v_z1 = 0.5, cv_12 = 0.2, cv_13 = 0.1,
cv_21 = 0.2, v_z2 = 1.2, cv_23 = 0.1,
cv_31 = 0.1, cv_32 = 0.1, v_z3 = 0.3),
nrow = k, byrow = FALSE)
# True covariance of the residuals
true_cv_e <- matrix(c(0.55, 0.3, 0.3, 0.70), nrow = p)
##########################################################
# Generate a synthetic data set
## Regressors
X <- rmvnorm(n, true_e_x, true_cv_x)
## Slope (Beta)
true_beta <- rnorm(p*k)
true_beta <- matrix(true_beta, ncol = k, byrow = TRUE)
## Intercept (Alpha)
true_alpha <- runif(p, min = 0, max = 1)
true_alpha <- matrix(true_alpha, ncol = 1)
## Matrix of 1 for matrix multiplication
ones <- matrix(rep(1, n), ncol = 1)
## Fitted response variable
Y <- ones %*% t(true_alpha) + X %*% t(true_beta)
## Simulated error
eps <- rmvnorm(n, sigma = true_cv_e)
## Perturbed response variable
Y_tilde <- Y + eps
Parameters fit
# True Beta
df_beta_true <- as_tibble(true_beta)
colnames(df_beta_true) <- paste0("$\\beta_", 1:ncol(df_beta_true), "$")
# Perturbed Beta (fitted)
fit_beta <- cov(Y_tilde, X) %*% solve(cov(X))
df_beta_pert <- as_tibble(fit_beta)
colnames(df_beta_pert) <- paste0("$\\beta_", 1:ncol(df_beta_pert), "$")
# True Alpha
df_alpha_true <- as_tibble(t(true_alpha))
colnames(df_alpha_true) <- paste0("$\\alpha_", 1:ncol(df_alpha_true), "$")
# Perturbed Alpha (fitted)
## Perturbed mean
e_y <- matrix(apply(Y_tilde, 2, mean), ncol = 1)
e_x <- matrix(apply(X, 2, mean), ncol = 1)
## Estimated Alpha (on perturbed data)
fit_alpha <- e_y - cov(Y_tilde, X) %*% solve(cov(X)) %*% e_x
df_alpha_pert <- as_tibble(t(fit_alpha))
colnames(df_alpha_pert) <- paste0("$\\alpha_", 1:ncol(df_alpha_pert), "$")
Code table output
dplyr::bind_rows(
dplyr::bind_cols(Type = "True", df_beta_true),
dplyr::bind_cols(Type = "Fitted", df_beta_pert)
) %>%
dplyr::mutate_if(is.numeric, format, digits = 4, scientific = FALSE) %>%
knitr::kable(escape = FALSE)
dplyr::bind_rows(
dplyr::bind_cols(Type = "True", df_alpha_true),
dplyr::bind_cols(Type = "Fitted", df_alpha_pert)
) %>%
dplyr::mutate_if(is.numeric, format, digits = 4, scientific = FALSE) %>%
knitr::kable(escape = FALSE)