LagI: Lag with respect to time within id with interpolation for...

Usage Arguments Examples

Usage

1
cLagI(x, id, time, lag = 1, delta = 0.01, check = T)

Arguments

x

the values to be lagged.

id

values are lagged within each level of id

time

measure of time used for lagging

lag

distance to lag: e.g. -1 takes the value of x 1 unit of time in the past.

delta

increment used for extrapolation

check

uniqueness of id/time combinations

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
##---- 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, time, lag = 1, delta = 0.01, check = T) 
{
    if (check) {
        comb <- paste(id, time)
        if (any(duplicated(comb))) 
            stop("id not unique in each level of idx")
    }
    ret <- x
    id <- as.character(id)
    names(x) <- id
    names(time) <- id
    for (nn in unique(id)) {
        pos <- id == nn
        xx <- x[pos]
        tt <- time[pos]
        topred <- tt - delta
        drop <- is.na(xx) | is.na(tt)
        xxc <- xx[!drop]
        ttc <- tt[!drop]
        nl <- length(xxc)
        if (nl > 0) {
            if (nl > 1) 
                xx.p <- approx(ttc, xxc, topred)$y
            else xx.p <- NA
            xx.lag <- xx - lag * (xx - xx.p)/delta
            ret[pos] <- xx.lag
        }
    }
    ret
  }

gmonette/spida15 documentation built on May 17, 2019, 7:26 a.m.