lag {plm} | R Documentation |
lag, lead, and diff functions for class pseries.
## S3 method for class 'pseries' lag(x, k = 1, shift = c("time", "row"), ...) ## S3 method for class 'pseries' lead(x, k = 1, shift = c("time", "row"), ...) ## S3 method for class 'pseries' diff(x, lag = 1, shift = c("time", "row"), ...)
x |
a |
k |
an integer, the number of lags for the |
lag |
the number of lags for the |
shift |
character, either |
... |
further arguments (currently none evaluated). |
This set of functions perform lagging, leading (lagging in the opposite direction), and differencing operations
on pseries
objects, i. e., they take the panel structure of the data into account by performing the
operations per individual.
Argument shift
controls the shifting of observations to be used by methods lag
, lead
, and
diff
:
shift = "time"
(default): Methods respect the numerical value in the time dimension of the index.
The time dimension needs to be interpretable as a sequence t, t+1, t+2, ... where t is an integer (from a
technical viewpoint, as.numeric(as.character(index(your_pdata.frame)[[2]]))
needs to result in a meaningful
integer).
shift = "row":
Methods perform the shifting operation based solely on the "physical position" of the
observations, i.e. neighbouring rows are shifted per individual. The value in the time index is not relevant in this
case.
For consecutive time periods per individual, a switch of shifting behaviour results in no difference. Different return values will occur for non-consecutive time periods per individual ("holes in time"), see also Examples.
An object of class pseries
, if the argument specifying the lag has length 1
(argument k
in functions lag
and lead
, argument lag
in function diff
).
A matrix containing the various series in its columns, if the argument specifying the lag has length > 1.
The sign of k
in lag.pseries
results in inverse behaviour compared to lag
and lag.zoo
.
Up to and including version 1.6-6 of package plm, the lag, lead and diff functions for pseries
performed the shifting based on rows and there was no argument shift
. Beginning with version 1.7-0,
the default shifting in these functions respects the time dimension (argument shift
introduced with
default value "time"
.)
Yves Croissant and Kevin Tappe
To check if the time periods are consecutive per individual, see is.pconsecutive
.
For further function for 'pseries' objects: between
, Between
,
Within
, summary.pseries
, print.summary.pseries
,
as.matrix.pseries
.
# First, create a pdata.frame data("EmplUK", package = "plm") Em <- pdata.frame(EmplUK) # Then extract a series, which becomes additionally a pseries z <- Em$output class(z) # compute the first and third lag, and the difference lagged twice lag(z) lag(z, 3) diff(z, 2) # compute negative lags (= leading values) lag(z, -1) lead(z, 1) # same as line above identical(lead(z, 1), lag(z, -1)) # TRUE # compute more than one lag and diff at once (matrix returned) lag(z, c(1,2)) diff(z, c(1,2)) ## demonstrate behaviour of shift = "time" vs. shift = "row" # delete 2nd time period for first individual (1978 is missing (not NA)): Em_hole <- Em[-2, ] is.pconsecutive(Em_hole) # check: non-consecutive for 1st individual now # original non-consecutive data: head(Em_hole$emp, 10) # for shift = "time", 1-1979 contains the value of former 1-1977 (2 periods lagged): head(lag(Em_hole$emp, k = 2, shift = "time"), 10) # for shift = "row", 1-1979 contains NA (2 rows lagged (and no entry for 1976): head(lag(Em_hole$emp, k = 2, shift = "row"), 10)