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.
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.
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).
Assistant
User
Created print statements for everything:
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 # Correctly set as 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, nrow(R)) # Replicates zeros to match the number of rows in R
Imposing the restrictions based on the paper
Assistant
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.
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:
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:
Proceed with further analysis using the restricted beta and alpha matrices
Final Steps
Proceed with Calculations:
Extract the restricted alpha matrix from vecm_restrictedrlmcoefficients.
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_restrictedbetaandvecmrestrictedrlm$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.
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)'
The error message you're encountering suggests there's a problem with performing the blrtest due to incorrect setup related to the rank parameter. The blrtest() function in the urca package is expecting the rank of the cointegration relationships (r) as a scalar value, rather than a vector.
Let’s go through the steps to resolve this issue:
Key Steps and Fixes
Understand the Error Message: The part saying 'length = 3' in coercion to 'logical(1)' suggests there might be a mismatch or misunderstanding in how the rank r and the restrictions vector r_vec are being specified.
Confirm the Rank r: Make sure that the rank r is defined correctly as a single integer value representing the number of cointegrating vectors to test, not a vector itself.
Correct Usage of blrtest():
Ensure that the restrictions matrix R and vector r_vec are constructed correctly.
Ensure r is provided as an integer indicating the number of cointegrating vectors used to test the hypothesis, rather than misinterpreting it as a restriction vector.
Adjusting Restrictions:
Review the indexing logic of your restrictions matrix R to ensure that it is consistent with your cointegrating vector setup.
Test the Implementation Below: Here's an adjusted version of your code to be sure the right setup is followed.
r
# 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 # Correctly set as 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, nrow(R)) # Replicates zeros to match the number of rows in R
# 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, make sure r is an integer, not r_vec which is the null hypothesis vector
blr_test <- blrtest(johansen_test, H=R, r=r)
# 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))
Final Notes
Double-check your restrictions: Ensure they are aligned with the theoretical basis and specified correctly in R.
Parameter r: Always pass r as an integer to the functions like blrtest() and cajorls(), reflecting the number of cointegrating vectors you are testing or imposing.
Make sure your package versions are updated: Sometimes package updates can resolve unusual behavior or bugs from prior versions.
If you continue to experience issues, ensure all logical assumptions and calculations align with the setup described in the paper or relevant theory.
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, make sure r is an integer, not r_vec which is the null hypothesis vector
blr_test <- blrtest(johansen_test, H=R, r=r)
Estimate the model with imposed restrictions
vecm_restricted <- cajorls(johansen_test, r = r, beta = blr_test$beta)
It looks like you've set up your code correctly, and now you want to add print statements to track the values and progress through the various steps. I'll suggest where you might add these print statements to help with debugging or better understanding what your code is doing at each step.
Here's how you might incorporate print statements:
r
# Assuming log_data is already loaded in the environment
print("Loaded log_data and created Y matrix:")
Y <- log_data[, c("log_Spot", "log_Fut", "log_USO", "log_USL", "log_OIL")]
print(head(Y))
# Step 2: Determine the optimal lag length using VARselect
print("Determining optimal lag length:")
VARselect_result <- VARselect(Y, lag.max=15, type="const")
print(VARselect_result)
p <- VARselect_result$selection["HQ(n)"]
print(paste("Optimal lag length (based on HQ criterion):", p))
K <- p - 1 # Lagged differences in Johansen test
print(paste("Using K =", K, "for lagged differences in Johansen test"))
# Step 3: Johansen cointegration test with adjusted settings
print("Performing Johansen cointegration test:")
johansen_test <- ca.jo(Y, type="trace", ecdet="const", K=K, spec="longrun")
print(summary(johansen_test))
r <- 4 # Correctly set as number of cointegrating vectors
print(paste("Number of cointegrating vectors set to:", r))
# Define the restriction matrix R and vector r_vec
n_vars <- ncol(Y)
print(paste("Number of variables:", n_vars))
n_beta_coeffs <- n_vars * r
print(paste("Total number of beta coefficients:", n_beta_coeffs))
R <- matrix(0, nrow=3, ncol=n_beta_coeffs)
r_vec <- rep(0, nrow(R)) # Replicates zeros to match the number of rows in R
print("Restriction matrix R initialized:")
print(R)
# Imposing the restrictions based on the paper
print("Imposing restrictions based on the paper:")
# Restriction 1
R[1, (1 - 1) * n_vars + 3] <- 1 # beta[3,1]
print("Restriction 1 applied on beta[3,1]")
# Restriction 2
R[2, (1 - 1) * n_vars + 4] <- 1 # beta[4,1]
print("Restriction 2 applied on beta[4,1]")
# Restriction 3
R[3, (2 - 1) * n_vars + 5] <- 1 # beta[5,2]
print("Restriction 3 applied on beta[5,2]")
print("Complete Restriction Matrix R:")
print(R)
# Test the restrictions
print("Testing the restrictions with blrtest:")
blr_test <- blrtest(johansen_test, H=R, r=r)
print("Restrictions tested. Test Output:")
print(summary(blr_test))
# Estimate the model with imposed restrictions
print("Estimating the model with imposed restrictions:")
vecm_restricted <- cajorls(johansen_test, r = r, beta = blr_test$beta)
print("Model with restrictions estimated.")
# Extract the restricted beta matrix
beta_restricted <- vecm_restricted$beta
print("Restricted beta matrix extracted:")
# Transpose beta for better readability
print("Restricted Transposed Beta:")
print(t(beta_restricted))
Key Points:
Print Statements: These are used to provide insight into the data at various points. Checking the head of the data frames or matrices can quickly show if they appear as expected.
Outputs and Summaries: Including print(summary(...)) for objects like the Johansen test results and the restriction test can give a lot of valuable output that might provide diagnostic information.
Debugging Tips: If the results are unexpected, use additional print statements to print intermediate results or parts of data - for example, specific rows or columns that should or should not be zero.
Adding these print statements can help debug by providing a step-by-step record of variable values and progress as the script runs. This feedback is particularly useful for tuning parameters, checking assumptions and constraints, and verifying output.