Lag: 'Contextual' Lag with respect to time within id

Description Usage Arguments Details Value See Also Examples

Description

Lag a vector with respect to time order

Usage

1
cLag(x, id = rep(1, length(x)), time = 1:length(x), lag = 1, at = NULL, check = TRUE, idx)

Arguments

x

vector of values to be lagged

id

identifies clusters within which lagging takes place

time

time variable for lagging, generally integer valued

lag

period for lagging: lag = 1 reaches one unit of time into the past. Negative numbers reach into the future.

at

uses the value of x at a particular value of time. e.g. at = 1 returns the value of x corresponding to time == 1.

check

that id/time combinations are unique.

idx

for compatibility with a previous version.

Details

The function can also be called as Lag which is now deprecated to avoid conflicts with Hmisc::Lag. Use cLagI for intra(extra)polative lagging.

Value

A vector of values of x lagged within levels of id. The value returned at time == t is the value of x at time == t-1. If there is no observation for which time == t-1 then the returned value is NA. To lag to the previous value of time, one can use rank. Consider, also, cLagI that intrapolates backwards one unit of time.

See Also

cLagI, cDiffI, capply, up

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (x, id = rep(1, length(x)), time = 1:length(x), lag = 1, 
    at = NULL, check = T, idx) 
{
    if (check) {
        comb <- paste(id, time)
        if (any(duplicated(comb))) 
            stop("id not unique in each level of time")
    }
    if (!missing(idx)) 
        time = idx
    ret <- x
    names(ret) <- paste(id, time, sep = "::")
    retnn <- if (is.null(at)) 
        paste(id, time - lag, sep = "::")
    else paste(id, at, sep = "::")
    ret[retnn]
  }

gmonette/spida documentation built on May 17, 2019, 7:25 a.m.