I have a two part answer to your question: (1) I'll show you how to subset complete
correctly, (2) I'll point you to a resource for impulse response functions of nonlinear VARs.
Subsetting Multivariate Time Series
There are a couple of issues with the way you attempted to subset complete
: (1) you were essentially subsetting a matrix like it was a vector, and (2) you were using a logical vector of length length(GDPdiff2)
when you wanted a logical vector of length nrow(complete)
. To illustrate, I first make some example data since you didn't provide yours:
# set the seed for reproducibility
set.seed(123)
# make example data
ts1 <- ts(rnorm(10))
ts2 <- ts(rnorm(8))
complete <- ts.intersect(ts1, ts2)
complete
Time Series:
Start = 1
End = 8
Frequency = 1
ts1 ts2
1 -0.56047565 1.2240818
2 -0.23017749 0.3598138
3 1.55870831 0.4007715
4 0.07050839 0.1106827
5 0.12928774 -0.5558411
6 1.71506499 1.7869131
7 0.46091621 0.4978505
8 -1.26506123 -1.9666172
Now we'll try subsetting it like you did:
# attempt to subset like yours
complete[ts1 > 0]
[1] 1.55870831 0.07050839 0.12928774 1.71506499 0.46091621 -0.55584113
[7] 1.78691314 0.49785048 -1.96661716
That resulted in a vector instead of a matrix. Why? From Hadley Wickham's Advanced R:
Because matrices and arrays are implemented as vectors with special attributes, you can subset them with a single vector. In that case, they will behave like a vector.
Moreover, the elements it pulled from the second column of complete
don't line up with the positive elements of the first column. Why? Let's look at the logical vector you'd use to subset:
ts1 > 0
[1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
There are 10 elements, while complete
only has 8 rows because you've used ts.intersect
which only gives complete cases (ts2
has less observations than ts1
). The combination of these two issues are why your subsetting strategy didn't work. Here's how to do it right:
complete[complete[, 'ts1'] > 0, ]
ts1 ts2
[1,] 1.55870831 0.4007715
[2,] 0.07050839 0.1106827
[3,] 0.12928774 -0.5558411
[4,] 1.71506499 1.7869131
[5,] 0.46091621 0.4978505
Alternatively, if you know and would prefer to use the column number:
complete[complete[, 1] > 0, ]
ts1 ts2
[1,] 1.55870831 0.4007715
[2,] 0.07050839 0.1106827
[3,] 0.12928774 -0.5558411
[4,] 1.71506499 1.7869131
[5,] 0.46091621 0.4978505
Impulse Response Function for TVAR
I would caution you to do more Googling before you ever make a statement like "Since nonlinear irf's are not possible in R". Almost anything is possible in R, and because of the great R community, so many of the things you could want to do (regarding extant statistical methods) have already been implemented by someone if you know where to look.
In your case, if you had looked at the GitHub repository for the package you're using you'd see that while a generalized impulse response function hasn't been implemented in the tsDyn package, one of the authors has made code for it that you can find here that does this for TVAR
results.