Creates a new virtual track.

emr_vtrack.create(
  vtrack,
  src,
  func = NULL,
  params = NULL,
  keepref = FALSE,
  time.shift = NULL,
  id.map = NULL,
  filter = NULL
)

Arguments

vtrack

virtual track name. If 'NULL' is used, a unique name is generated.

src

data source. either a track name or a list of two members: ID-Time Values table (see "User Manual") and a logical. If the logical is 'TRUE', the data in the table is treated as categorical, otherwise as quantitative.

func, params

see below.

keepref

see below.

time.shift

time shift and expansion for iterator time.

id.map

id mapping.

filter

virtual track filter. Note that filters with a source of another virtual track are not allowed in order to avoid loops.

Value

Name of the virtual track (invisibly)

Details

This function creates a new virtual track named 'vtrack'.

During the evaluation of track expression that contains a virtual track 'vtrack' the iterator point of id-time (ID1, Time, Ref) form is transformed first to an id-time interval: (ID2, Time1, Time2, Ref).

If 'id.map' is 'NULL' then ID1 == ID2, otherwise ID2 is derived from the translation table provided in 'id.map'. This table is a data frame with two first columns named 'id1' and 'id2', where 'id1' is mapped to 'id2'. If 'id.map' contains also a third optional column named 'time.shift' the value V of this column is used to shift the time accordingly, i.e. Time1 = Time2 = Time + V.

'time.shift' parameter (not to be confused with 'time.shift' column of 'id.map') can be either a single number X, in which case Time1 = Time2 = Time + X. Alternatively 'time.shift' can be a vector of two numbers, i.e. 'c(X1, X2)', which would result in Time1 = Time + X1, Time2 = Time + X2.

Both 'time.shift' parameter and 'time.shift' column within 'id.map' may be used simultaneously. In this case the time shifts are applied sequentially.

At the next step values from the data source 'src' that fall into the new id-time interval and pass the 'filter' are collected. 'src' may be either a track name or a list of two members: ID-Time Values table (see "User Manual") and a logical. If the logical is 'TRUE', the data in the table is treated as categorical, otherwise as quantitative.

If 'keepref' is 'TRUE' the reference of these values must match 'ref' unless either the reference or 'ref' are '-1'.

Function 'func' (with 'params') is applied then on the collected values and produces a single value which is considered to be the value of 'vtrack' for the given iterator point. If 'NULL' is used as a value for 'func', 'func' is set then implicitly to 'value', if the data source is categorical, or 'avg', if the data source is quantitative.

Use the following table for a reference of all valid functions and parameters combinations.

CATEGORICAL DATA SOURCE

FUNCPARAMDESCRIPTION
valuevals/NULLA source value or -1 if there is more than one.
existsvals1 if any of the 'vals' exist otherwise 0.
sampleNULLUniformly sampled source value.
sample.timeNULLTime of the uniformly sampled source value.
frequentvals/NULLThe most frequent source value or -1 if there is more than one value.
sizevals/NULLNumber of values.
earliestvals/NULLEarliest value or -1 if there is more than one.
latestvals/NULLLatest value or -1 if there is more than one.
closestvals/NULLValues closest to the middle of the interval or -1 if there is more than one.
earliest.timevals/NULLTime of the earliest value.
latest.timevals/NULLTime of the latest value.
closest.earlier.timevals/NULLTime of the of the earlier of the closest values.
closest.later.timevals/NULLTime of the of the later of the closest values.
dt1.earliestvals/NULLTime difference between the earliest value and T1
dt1.latestvals/NULLTime difference between the latest value and T1
dt2.earliestvals/NULLTime difference between T2 and the earliest value
dt2.latestvals/NULLTime difference between T2 and the latest value

* 'vals' is a vector of values. If not 'NULL' it serves as a filter: the function is applied only to the data source values that appear among 'vals'. 'vals' can be a single NA value, in which case all the values of the track would be filtered out.

QUANTITATIVE DATA SOURCE

FUNCPARAMDESCRIPTION
avgNULLAverage of all values.
minNULLMinimal value.
maxNULLMaximal value.
sampleNULLUniformly sampled source value.
sample.timeNULLTime of the uniformly sampled source value.
sizeNULLNumber of values.
earliestNULLAverage of the earliest values.
latestNULLAverage of the latest values.
closestNULLAverage of values closest to the middle of the interval.
stddevNULLUnbiased standard deviation of the values.
sumNULLSum of values.
quantilePercentile in the range of [0, 1]Quantile of the values.
percentile.upperNULLAverage of upper-bound values percentiles.*
percentile.upper.minNULLMinimum of upper-bound values percentiles.*
percentile.upper.maxNULLMaximum of upper-bound values percentiles.*
percentile.lowerNULLAverage of lower-bound values percentiles.*
percentile.lower.minNULLMinimum of lower-bound values percentiles.*
percentile.lower.maxNULLMaximum of lower-bound values percentiles.*
lm.interceptNULLIntercept (aka "alpha") of the simple linear regression (X = time, Y = values)
lm.slopeNULLSlope (aka "beta") of the simple linear regression (X = time, Y = values)
earliest.timeNULLTime of the earliest value.
latest.timeNULLTime of the latest value.
closest.earlier.timeNULLTime of the of the earlier of the closest values.
closest.later.timeNULLTime of the of the later of the closest values.
dt1.earliestNULLTime difference between the earliest value and T1
dt1.latestNULLTime difference between the latest value and T1
dt2.earliestNULLTime difference between T2 and the earliest value
dt2.latestNULLTime difference between T2 and the latest value

* Percentile is calculated based on the values of the whole data source even if a subset or a filter are defined.

Note: 'time.shift' can be used only when 'keepref' is 'FALSE'. Also when 'keepref' is 'TRUE' only 'avg', 'percentile.upper' and 'percentile.lower' can be used in 'func'.

Examples


emr_db.init_examples()
#> NULL

emr_vtrack.create("vtrack1", "dense_track",
    time.shift = 1,
    func = "max"
)
emr_vtrack.create("vtrack2", "dense_track",
    time.shift = c(-5, 10), func = "min"
)
res <- emr_extract("dense_track", keepref = TRUE, names = "value")
emr_vtrack.create("vtrack3", list(res, FALSE),
    time.shift = c(-5, 10),
    func = "min"
)
emr_extract(c("dense_track", "vtrack1", "vtrack2", "vtrack3"),
    keepref = TRUE, iterator = "dense_track"
)
#>    id time ref dense_track vtrack1 vtrack2 vtrack3
#> 1  22    1   3          13     NaN      13      13
#> 2  24    1   3          13     NaN      13      13
#> 3  25    1   0          10      28      10      10
#> 4  25    2   0          20      34      10      10
#> 5  25    2   2          22      34      10      10
#> 6  25    2   5          24      34      10      10
#> 7  25    2   6          26      34      10      10
#> 8  25    2   8          28      34      10      10
#> 9  25    3   4          34     NaN      10      10
#> 10 25    6   0          60     NaN      10      10
#> 11 25    6   2          62     NaN      10      10
#> 12 25    8   1          80      94      34      34
#> 13 25    8   4          84      94      34      34
#> 14 25    9   2          92     104      60      60
#> 15 25    9   4          94     104      60      60
#> 16 25   10   4         104     NaN      60      60
#> 17 25   12   4         124     NaN      80      80
#> 18 27   23   4         234     NaN     234     234
#> 19 27   50   0         500     NaN     500     500
#> 20 28    1   3          13     NaN      13      13