How do I restrict beta to look like Thepaperbeta? Load necessary packages library(urca)…
- User
How do I restrict beta to look like The_paper_beta?
Load necessary packages
library(urca) library(vars) library(tseries) library(MASS) library(tidyverse)
Assuming log_data is already loaded in the environment
Correct the order of variables
Y <- log_data[, c("log_Spot", "log_Fut", "log_USO", "log_USL", "log_OIL")]
Step 1: Unit root tests (code remains the same)
Step 2: Determine the optimal lag length using VARselect
VARselect_result <- VARselect(Y, lag.max=15, type="const") print("Optimal lag length selection:") [1] "Optimal lag length selection:" print(VARselect_result$selection) AIC(n) HQ(n) SC(n) FPE(n) 15 9 5 15
Choose the number of lags based on SC
p <- VARselect_result$selection["HQ(n)"]
K <- p - 1 # Lagged differences in Johansen test
Step 3: Johansen cointegration test with corrected settings
johansen_test <- ca.jo(Y, type="trace", ecdet="const", K=K, spec="longrun") summary(johansen_test)
######################
Johansen-Procedure
######################
Test type: trace statistic , without linear trend and constant in cointegration
Eigenvalues (lambda): [1] 9.194013e-02 6.137328e-02 1.082481e-02 5.234867e-03 7.584923e-04 9.339895e-17
Values of teststatistic and critical values of test:
codetest 10pct 5pct 1pctr <= 4 | 2.29 7.52 9.24 12.97 r <= 3 | 18.16 17.85 19.96 24.60 r <= 2 | 51.06 32.00 34.91 41.07 r <= 1 | 242.53 49.65 53.12 60.16 r = 0 | 534.08 71.86 76.07 84.45
Eigenvectors, normalised to first column: (These are the cointegration relations)
codelog_Spot.l8 log_Fut.l8 log_USO.l8 log_USL.l8 log_OIL.l8 constantlog_Spot.l8 1.000000000 1.0000000 1.000000 1.0000000 1.000000 1.000000 log_Fut.l8 -1.003644123 -1.5256307 -3.331999 1.4487665 -5.903652 -1.197880 log_USO.l8 -0.006998315 -0.9992708 8.252360 13.6547508 8.825436 -2.604882 log_USL.l8 -0.002521254 0.7607663 4.716197 -8.9283064 -1.004589 1.305870 log_OIL.l8 0.008398995 0.6448770 -8.722794 -8.0800039 -5.309850 1.247534 constant 0.008065544 0.7922468 5.954291 0.1266867 10.436638 2.071152
Weights W: (This is the loading matrix)
codelog_Spot.l8 log_Fut.l8 log_USO.l8 log_USL.l8 log_OIL.l8 constantlog_Spot.d -0.061910397 0.004041937 -0.003984373 -0.0027565091 0.0003042509 6.558352e-15 log_Fut.d 0.338164478 0.016601908 -0.003969795 -0.0024759995 0.0002939173 2.942492e-14 log_USO.d -0.022184653 0.037273171 -0.004320462 -0.0016669625 0.0002652621 1.311205e-14 log_USL.d 0.001740984 0.018450168 -0.005337932 -0.0004077368 0.0002501097 1.067847e-14 log_OIL.d -0.060171749 0.039992249 -0.002479446 -0.0012813480 0.0003394962 8.429320e-15
- Assistant
- User
I just want the 1 and 0 to be restricted and everything else BE ESTIMATED
- Assistant
- User
Get the unrestricted beta from the Johansen test
unrestricted_beta <- johansen_test@V
Apply your manual restrictions
Note: Leave positions where free estimation
constrained_beta <- The_paper_beta for (i in seq_len(nrow(unrestricted_beta))) {
- for (j in seq_len(ncol(unrestricted_beta))) {
-
code
if (The_paper_beta[i, j] != 1 && The_paper_beta[i, j] != 0) { -
code
constrained_beta[i, j] <- unrestricted_beta[i, j] -
code
} - }
- } Error in The_paper_beta[i, j] : subscript out of bounds
- Assistant
The error "subscript out of bounds" indicates that you're trying to access an element in
The_paper_betathat doesn't exist. This means either the dimensions ofunrestricted_betadon't match the dimensions ofThe_paper_beta, or the loop indices exceed the bounds ofThe_paper_beta.To identify the issue and correct it, follow these steps:
- User
Check dimensions
dim_unrestricted <- dim(unrestricted_beta) dim_paper <- dim(The_paper_beta)
print(dim_unrestricted) [1] 6 6 print(dim_paper) [1] 5 4
Ensure dimensions match:
if (dim_unrestricted != dim_paper) {
- stop("Dimensions of unrestricted_beta do not match The_paper_beta")
- } Error in if (dim_unrestricted != dim_paper) { : the condition has length > 1
- Assistant
The error you're encountering is due to the fact that the
!=operator is being used to compare two vectors (dim_unrestrictedanddim_paper), which results in a logical vector rather than a single logical value. In R, theifstatement expects a single logical value, not a vector.To properly compare the dimensions, you should use the
identical()function or compare each element individually. Here's how you can adjust your code:r