Why does the matrix not work in l?

I am trying to keep up with the matrix:

> B = matrix( c(2, 4, 3, 1, 5, 7), nrow=3, ncol=2) > B [,1] [,2] [1,] 2 1 [2,] 4 5 [3,] 3 7 > lag(B) [,1] [,2] [1,] 2 1 [2,] 4 5 [3,] 3 7 

Why lag(B) DOES NOT:

 > lag(B) [,1] [,2] [1,] 0 0 [2,] 2 1 [3,] 4 5 
+6
source share
4 answers

This is because lag shifts the times of the object, not the data value. It is really intended for time series objects.

When lag used on a simple matrix, B , the lag.default method is lag.default . Since there is no time associated with a simple matrix, it is assumed that times 1, 2, ..., NROW(B) :

 > time(B) [1] 1 2 3 attr(,"tsp") [1] 1 3 1 

and shifts the time by one so that now they start at 0:

 > time(lag(B)) [1] 0 1 2 attr(,"tsp") [1] 0 2 1 

Use the time series class if you want to combine objects that have time. (The first column is the time in the figures below.)

 > library(zoo) > > # zooreg - regular series or almost so > B.zr <- zooreg(B) > merge(B.zr, lag(B.zr)) B.zr.1 B.zr.2 lag(B.zr).1 lag(B.zr).2 0 NA NA 2 1 1 2 1 4 5 2 4 5 3 7 3 3 7 NA NA > # zoo - irregular series > Bz <- zoo(B) > merge(Bz, lag(Bz)) Bz1 Bz2 lag(Bz).1 lag(Bz).2 1 2 1 4 5 2 4 5 3 7 3 3 7 NA NA 

Pay attention to the difference between lag.zooreg , which can extend beyond the original time, creating 0 times and lag.zoo , which cannot, because the latter does not have a regularity assumption.

We can also use the ts class, which assumes regularity, so it can be created 0 times, but there is no merge.ts , which makes it less useful here.

+3
source

I never understood the lag function, instead I would use the lag from the quantmod package:

 > # library(quantmod) > apply(B, 2, Lag) [,1] [,2] [1,] NA NA [2,] 2 1 [3,] 4 5 

If you want (or need) to lag behind the matrix without ts attributes, one way can use apply and lag from the quantmod package, but if you do not want to install the package for only one function, then you can write your own function, the idea would be:

 lag.matrix <- function(x, k=1){ N <- ncol(B) l <- matrix(embed(x,k+1)[, -c(1:(k*N))], ncol=N) NAs <- matrix(rep(NA, k*N), ncol=N) rbind(NAs, l) } > lag.matrix(B, k=1) [,1] [,2] [1,] NA NA [2,] 2 1 [3,] 4 5 > lag.matrix(B, k=2) [,1] [,2] [1,] NA NA [2,] NA NA [3,] 2 1 

Another solution can be posted as a comment from @GSee, which uses lag as you wish.

 > lag(xts(B, .POSIXct(0)+0:(nrow(B)-1))) [,1] [,2] 1970-01-01 01:00:00 NA NA 1970-01-01 01:00:01 2 1 1970-01-01 01:00:02 4 5 
+2
source
 l <- matrix(0,nrow(B),nrow(B)) l[-1,-nrow(B)] <- diag(nrow(B)-1) l ## [,1] [,2] [,3] ## [1,] 0 0 0 ## [2,] 1 0 0 ## [3,] 0 1 0 l %*% B ## [,1] [,2] ## [1,] 0 0 ## [2,] 2 1 ## [3,] 4 5 
+1
source

An EASY way to delay the matrix in R is to use the embed () function. The following is a quick example:

 > # Create a 5x2 matrix > m <- replicate(2, 1:5) > m [,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3 [4,] 4 4 [5,] 5 5 > > # Use the 'embed()' function to lag the matrix > lag_m <- embed(m, 3) > lag_m [,1] [,2] [,3] [,4] [,5] [,6] [1,] 3 3 2 2 1 1 [2,] 4 4 3 3 2 2 [3,] 5 5 4 4 3 3 

A few important words about the embed () function and its output.

  • The function is used as follows: embed (matrix, lag + 1)

The first argument to the function is the matrix we want to lag. Second argument; however, the number of lags we want is "plus 1". Therefore, insertion (matrix, 3) means that we want to delay the matrix by 2 time periods.

  • The output of the function is a spaced matrix, and it looks like this:

. the first two columns represent the original matrix m, but the number of rows is adjusted for the number of lags (5 rows initially minus a lag of 2 times)

. the second set of columns (columns 3 and 4) is an m-matrix, delayed by 1 period of time

. the third set of columns (columns 5 and 6) is an m-matrix spaced from two time periods.

+1
source

Source: https://habr.com/ru/post/945419/


All Articles