add print statement to everyhing: Load necessary packages library(urca) library(vars)…
- User
add print statement to everyhing:
Load necessary packages
library(urca) library(vars) library(tseries) library(MASS) library(tidyverse)
Assuming log_data is already loaded in the environment
Y <- log_data[, c("log_Spot", "log_Fut", "log_USO", "log_USL", "log_OIL")]
Step 2: Determine the optimal lag length using VARselect
VARselect_result <- VARselect(Y, lag.max=15, type="const") p <- VARselect_result$selection["HQ(n)"] K <- p - 1 # Lagged differences in Johansen test
Step 3: Johansen cointegration test with adjusted settings
johansen_test <- ca.jo(Y, type="trace", ecdet="const", K=K, spec="longrun") r <- 4 # Number of cointegrating vectors
Define the restriction matrix R and vector r_vec
n_vars <- ncol(Y) n_beta_coeffs <- n_vars * r R <- matrix(0, nrow=3, ncol=n_beta_coeffs) r_vec <- rep(0, 3)
Imposing the restrictions based on the paper
Adjust the indices according to your variables and desired restrictions
Example restrictions:
Restriction 1: In the first cointegrating vector, beta[3,1] = 0 (log_USO coefficient)
R[1, (1 - 1) * n_vars + 3] <- 1 # beta[3,1]
Restriction 2: In the first cointegrating vector, beta[4,1] = 0 (log_USL coefficient)
R[2, (1 - 1) * n_vars + 4] <- 1 # beta[4,1]
Restriction 3: In the second cointegrating vector, beta[5,2] = 0 (log_OIL coefficient)
R[3, (2 - 1) * n_vars + 5] <- 1 # beta[5,2]
Test the restrictions
blr_test <- blrtest(johansen_test, H=R, r=r_vec)
Estimate the model with imposed restrictions
vecm_restricted <- cajorls(johansen_test, r = r, beta = blr_test$beta)
Extract the restricted beta matrix
beta_restricted <- vecm_restricted$beta
Transpose beta for better readability
print("Restricted Transposed Beta:") print(t(beta_restricted))
Proceed with further analysis using the restricted beta and alpha matrices
- Assistant