Question

I'm trying to apply the function embed(x=time series, dimension=n), which returns the original time series (ts) and the ts lagged n-1 times. My time series (8) are 20 years long and I'm using n=5. This is the output after apply embed() to one of my ts:

L4  L3  L2  L1  L0
NA  NA  NA  NA  NA
NA  NA  NA  NA  NA
NA  NA  NA  NA  NA
495.7   NA  NA  NA  NA
495.7   495.7   NA  NA  NA
577 495.7   495.7   NA  NA
577 577 495.7   495.7   NA
577 577 577 495.7   495.7
577 577 577 577 495.7
577 577 577 577 577
577 577 577 577 577
577 577 577 577 577
577 577 577 577 577
577 577 577 577 577
577 577 577 577 577
266 577 577 577 577

I want to apply this function to my 8 ts and combine the outpus by rows. This is the function that I wrote to do that:

lagging<-function(df,col){#df= data frame, col=column of df to be lagged
  dfL<-data.frame(rep(0,128),rep(0,128),rep(0,128),rep(0,128),rep(0,128))#128= 8 categories*16 rows 
                                                                         #after application of embed* 
    for(i in c('nsw','tas',"sasz","sanz","vicwz","vicez","nzC8","nzC4")){
      for(j in seq(1,128,16)){
    dfL[j:(j+15),]<-rbind(embed(df[df$fishery==i,col],5))#* allow lag a time series
      }
    }
  colnames(dfL)<-c("L4","L3","L2","L1","L0")
  return(dfL)
}

lagging(lob,3)

My function is not working well and it's returning the output for the first ts (repeated 8 times). I guess the problem is the indexing. Any suggestion will be appreciated. Below is my data. Thanks!

Data:

season  fishery tac cpue
1990    nsw  NA      NA 
1991    nsw  NA      NA 
1992    nsw  NA      NA 
1993    nsw  NA      NA 
1994    nsw  NA     1.83
1995    nsw  NA     2.22
1996    nsw 106 2.23
1997    nsw 117 2.3
1998    nsw 125 2.51
1999    nsw 140 2.34
2000    nsw 150 1.62
2001    nsw 150 1.73
2002    nsw 135 2.22
2003    nsw 135 2.38
2004    nsw 102 2.47
2005    nsw 102 2.89
2006    nsw 112 3.43
2007    nsw 124 4.29
2008    nsw 128 3.94
2009    nsw 128 3.15
1990    tas  NA     0.97
1991    tas  NA     0.97
1992    tas  NA     0.89
1993    tas  NA     0.85
1994    tas  NA     0.82
1995    tas  NA     0.94
1996    tas  NA     0.95
1997    tas  NA     0.88
1998    tas 1502.5  0.94
1999    tas 1502.5  1.01
2000    tas 1502.5  1.02
2001    tas 1502.5  1.04
2002    tas 1523.5  1.12
2003    tas 1523.5  1.09
2004    tas 1523.5  1.16
2005    tas 1523.5  1.2
2006    tas 1523.5  1.18
2007    tas 1523.5  1.17
2008    tas 1523.5  1.01
2009    tas 1470.98 0.89
1990    sasz     NA     0.83
1991    sasz     NA     0.96
1992    sasz     NA     1.01
1993    sasz    1720    1.03
1994    sasz    1740    1.15
1995    sasz    1720    1.07
1996    sasz    1720    0.95
1997    sasz    1720    0.97
1998    sasz    1720    1.13
1999    sasz    1720    1.49
2000    sasz    1770    1.66
2001    sasz    1770    1.90
2002    sasz    1770    2.08
2003    sasz    1900    1.83
2004    sasz    1900    1.81
2005    sasz    1900    1.60
2006    sasz    1900    1.41
2007    sasz    1900    1.12
2008    sasz    1770    0.74
2009    sasz    1400    0.61
1990    sanz     NA     1.51
1991    sanz     NA     1.52
1992    sanz     NA     1.43
1993    sanz     NA     1.29
1994    sanz     NA     1.27
1995    sanz     NA     1.25
1996    sanz     NA     1.25
1997    sanz     NA     1.31
1998    sanz     NA     1.41
1999    sanz     NA     1.43
2000    sanz     NA     1.24
2001    sanz     NA     1.08
2002    sanz     NA     1.04
2003    sanz    625 0.84
2004    sanz    520 0.8
2005    sanz    520 0.81
2006    sanz    520 0.86
2007    sanz    520 0.76
2008    sanz    470 0.88
2009    sanz    310 1.07
1990    vicwz    NA     0.5
1991    vicwz    NA     0.56
1992    vicwz    NA     0.55
1993    vicwz    NA     0.59
1994    vicwz    NA     0.58
1995    vicwz    NA     0.56
1996    vicwz    NA     0.52
1997    vicwz    NA     0.55
1998    vicwz    NA     0.59
1999    vicwz    NA     0.6
2000    vicwz    NA     0.6
2001    vicwz   320 0.58
2002    vicwz   450 0.65
2003    vicwz   450 0.71
2004    vicwz   450 0.66
2005    vicwz   450 0.55
2006    vicwz   450 0.47
2007    vicwz   380 0.47
2008    vicwz   381 0.41
2009    vicwz   240 0.38
1990    vicez    NA     0.42
1991    vicez    NA     0.39
1992    vicez    NA     0.34
1993    vicez    NA     0.32
1994    vicez    NA     0.28
1995    vicez    NA     0.27
1996    vicez    NA     0.26
1997    vicez    NA     0.3
1998    vicez    NA     0.3
1999    vicez    NA     0.32
2000    vicez    NA     0.32
2001    vicez   42  0.34
2002    vicez   60  0.36
2003    vicez   60  0.43
2004    vicez   60  0.42
2005    vicez   60  0.4
2006    vicez   60  0.43
2007    vicez   66  0.37
2008    vicez   67  0.39
2009    vicez   66  0.37
1990    nzC8    1152.4  0.87
1991    nzC8    1054.6  0.82
1992    nzC8    986.8   0.68
1993    nzC8    888.1   0.92
1994    nzC8    888.1   0.88
1995    nzC8    888.1   0.9
1996    nzC8    888.1   0.87
1997    nzC8    888.1   0.72
1998    nzC8    888.1   0.79
1999    nzC8    711 0.84
2000    nzC8    711 0.98
2001    nzC8    568 0.92
2002    nzC8    568 1.11
2003    nzC8    568 1.67
2004    nzC8    603.4   1.58
2005    nzC8    603.4   1.75
2006    nzC8    755.2   2.19
2007    nzC8    755.2   2.47
2008    nzC8    966 3.22
2009    nzC8    1019    3.13
1990    nzC4    NA  NA
1991    nzC4    NA  NA
1992    nzC4    NA  NA
1993    nzC4    NA  NA
1994    nzC4    NA  NA
1995    nzC4    NA  NA
1996    nzC4    NA  NA
1997    nzC4    495.7   NA
1998    nzC4    495.7   NA
1999    nzC4    577 1.27
2000    nzC4    577 1.26
2001    nzC4    577 1.06
2002    nzC4    577 1.09
2003    nzC4    577 1.14
2004    nzC4    577 1
2005    nzC4    577 0.88
2006    nzC4    577 0.65
2007    nzC4    577 0.6
2008    nzC4    577 0.52
2009    nzC4    266 0.57
Was it helpful?

Solution

The problem of the repetition of the result for the last fishery index data is due to the variable j. For each value of i, all values of j are used. Hence, in each run of the outer for loop, the data of the preceding run are replaced by the new data associated with the current fishery index.

Here is a more efficient approach with tapply:

lagging <- function(df, col, dimension = 5) {
  setNames(as.data.frame(do.call(rbind, tapply(df[[col]], df$fishery, 
                                               embed, dimension))), 
           paste0("L", seq(dimension - 1, 0)))
}

lagging(lob, 3)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top