timegap indexes elements in a vector of ages such that the indexed ages are spaced integer multiples of a time interval apart, to within a given tolerance. timegap.id is a wrapper to apply timegap within levels of factor id. The selected ages can then be split into age groups the specified time interval wide, ensuring that (virtually) every subject has at most one measurement per interval.
timegap(age, gap, tol =0.1* gap, multiple =FALSE)timegap.id( age, id, data = parent.frame(), gap, tol =0.1* gap, multiple =FALSE)diffid( age, id, data = parent.frame(), lag =1, differences =1, sort =FALSE, keepNA =FALSE)
Arguments
age: vector of ages.
gap: numeric, the required positive time gap between selected ages.
tol: numeric, the positive tolerance around the gap (default 0.1 * gap).
multiple: logical, whether or not to return multiple solutions when found (default FALSE).
id: factor of subject ids.
data: data frame optionally containing age and id.
lag: an integer indicating which lag to use.
differences: an integer indicating the order of the difference.
sort: a logical indicating whether to first sort by id and age.
keepNA: a logical indicating whether to keep generated NAs.
Returns
With timegap, for unique solutions, or multiple solutions with multiple FALSE, a vector of indices named with age. With timegap.id the subject vectors are returned invisibly, concatenated.
With multiple TRUE, where there are multiple solutions they are returned as a named matrix.
diffid returns diff(age) applied within id. With keepNA TRUE a suitable number of NAs are added at the end, while if FALSE all NAs are omitted.
Details
timegap calculates all possible differences between pairs of ages, expresses them as integer multiples of gap, restricts them to those within tolerance and identifies those providing the longest sequences. For sequences of the same length, those with the smallest standard deviation of successive differences (modulo the time interval) are selected.
Examples
data(heights)## bin age into 1-year groups by id## gives multiple measurements per id per yearwith(heights, table(floor(age), id))## now select heights measured multiples of 1 year apart(tg1 <- timegap.id(age, id, heights,1))## no more than one measurement per id per yearwith(heights[tg1,], table(floor(age), id))## most time intervals close to 1 yearsummary(diffid(age, id, heights[tg1,], lag=1))