When I ran this, it did not work:

I understand that you're observing differences in the cointegrating vectors (beta matrix) between your results and those reported in the paper, particularly that the paper's beta matrix contains zeros in certain positions while yours does not. This discrepancy is likely due to the imposition of identifying restrictions on the cointegrating relationships in the paper.

Key Points to Address:

Identification in Cointegration Analysis
Imposing Restrictions on the beta Matrix
Implementing Restrictions in R using urca Package
Re-estimating the Model with Restrictions
Interpreting the Results
Let's go through each point step by step.

1. Identification in Cointegration Analysis
In the context of cointegration analysis, particularly with the Johansen method, the cointegrating vectors are not uniquely identified without further restrictions because they are determined up to a nonsingular transformation. This means that without imposing additional identifying restrictions, the estimated cointegrating vectors can be any linear combination of the true cointegrating vectors.

Implications:

The estimated beta matrix may not match the one reported in the paper unless the same restrictions are applied.
The presence of zeros in the beta matrix in the paper suggests that the authors imposed exact-identifying restrictions based on economic theory or prior knowledge.
2. Imposing Restrictions on the beta Matrix
To obtain a specific form of the beta matrix with zeros in certain positions, you need to impose linear constraints on the cointegrating vectors during estimation. These constraints are typically based on theoretical considerations, such as:

Exclusion Restrictions: Setting certain coefficients in the cointegrating vectors to zero.
Normalization Constraints: Fixing one coefficient to one to normalize the vector.
Example:

Suppose the authors believe that in one of the cointegrating relationships, the coefficient on log_USO is zero. They would impose a restriction like:

𝛽
USO
(
1
)
=
0
β 
USO
(1)
​
 =0
By imposing such restrictions, the estimated cointegrating vectors will have zeros in the specified positions.

3. Implementing Restrictions in R using urca Package
The urca package provides functionality to estimate cointegrating vectors with linear restrictions using the blrtest() function for testing restrictions and the cajorls() function for estimating the VECM with imposed restrictions.

Steps to Impose Restrictions:

Define the Restriction Matrix (R):

The restriction matrix R specifies which elements of the beta matrix are constrained. Each row represents a linear restriction.

Define the Restriction Vector (r):

The vector r contains the values to which the linear combinations are set (usually zeros for exclusion restrictions).

Use the blrtest() Function:

To test the validity of the imposed restrictions.

Use the cajorls() Function:

To estimate the VECM with the imposed restrictions.

Important Note:

The urca package does not directly allow imposing restrictions during the initial estimation with ca.jo(). However, you can estimate the model first and then impose restrictions using these functions.

4. Re-estimating the Model with Restrictions
Example Implementation:

Suppose we want to impose the following restrictions based on the paper:

In the first cointegrating vector, set the coefficients on log_USO and log_USL to zero.
In the second cointegrating vector, set the coefficient on log_OIL to zero.
And so on, based on the specific zeros in the paper's beta matrix.
Implementing in R:

R
Copy code
# Suppose we have r = 4 cointegrating vectors
r <- 4

# Initial estimation
johansen_test <- ca.jo(Y, type="trace", ecdet="const", K=K, spec="longrun")

# Define the restriction matrix R and vector r
# The dimensions of R are (number of restrictions) x (number of parameters)
# The beta coefficients are stacked column-wise in the order they appear

# Let's create an example where we set certain coefficients to zero
# For demonstration, suppose we have the following restrictions:

# Restriction 1: In the first cointegrating vector, beta[3,1] = 0 (coefficient on log_USO)
# Restriction 2: In the first cointegrating vector, beta[4,1] = 0 (coefficient on log_USL)
# Restriction 3: In the second cointegrating vector, beta[5,2] = 0 (coefficient on log_OIL)

# Number of variables (excluding constant)
n_vars <- ncol(Y)

# Total number of beta coefficients (including constants if any)
n_beta_coeffs <- n_vars * r

# Initialize R matrix with zeros
R <- matrix(0, nrow=3, ncol=n_beta_coeffs)

# Initialize r vector with zeros
r_vec <- rep(0, 3)

# Restriction 1: beta[3,1] = 0
R[1, (1 - 1) * n_vars + 3] <- 1

# Restriction 2: beta[4,1] = 0
R[2, (1 - 1) * n_vars + 4] <- 1

# Restriction 3: beta[5,2] = 0
R[3, (2 - 1) * n_vars + 5] <- 1

# 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))
Notes:

The restriction matrix R is constructed carefully to impose the desired zeros.
The indices in R depend on how the beta coefficients are stacked.
The function blrtest() tests the overidentifying restrictions.
The function cajorls() estimates the VECM with the imposed restrictions.
5. Interpreting the Results
After imposing the restrictions and re-estimating the model:

Check the beta_restricted Matrix: It should now have zeros in the positions where you imposed restrictions.
Compare with the Paper's Results: The estimated cointegrating vectors should now be closer to those reported in the paper.
Proceed with the Analysis: Use the restricted beta and corresponding alpha matrices for further calculations.
Important Considerations
Restrictions Must Be Theoretically Justified: Imposing arbitrary restrictions without economic justification can lead to misleading results.
Check the Identifiability: Ensure that the model remains identified after imposing restrictions.
Consistency with the Paper: Carefully read the methodology section of the paper to understand which restrictions were imposed and why.
Putting It All Together
Here's the full code snippet with the implementation:

R
Copy code
# 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
Final Steps
Proceed with Calculations:

Extract the restricted alpha matrix from vecm_restricted$rlm$coefficients.
Recalculate beta_perp and alpha_perp using the restricted beta and alpha.
Continue with the computation of Psi_1, Omega, F_M, and GIS measures.
Ensure Consistency:

Verify that the imposed restrictions match those used in the paper.
Double-check the indices and positions in the restriction matrix R.
Compare Results:

After re-estimating the model with the restrictions, compare your beta matrix with that in the paper.
The zeros should now appear in the same positions.
Additional Tips
Understanding Variable Ordering:

Ensure that the variables in Y are ordered exactly as in the paper.
The ordering affects the indices in the beta matrix.
Interpreting Output:

The cajorls() function returns a list containing the estimated coefficients, including beta and alpha.
Use vecm_restricted$beta and vecm_restricted$rlm$coefficients to extract beta and alpha.
Handling Constants:

Be cautious if the model includes constants or trends; adjust the dimensions of R accordingly.
The constant term in the cointegrating vectors may be included in beta.
Conclusion
By imposing the appropriate restrictions on the beta matrix, you should be able to replicate the cointegrating vectors with zeros in the positions specified in the paper. This approach aligns your estimation procedure with that of the authors, increasing the likelihood of obtaining comparable results.


> # 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)
Error in r >= z@P || r < 1 : 'length = 3' in coercion to 'logical(1)'