approxMap: Create an approximate mapping function

View source: R/approxMap.R

approxMapR Documentation

Create an approximate mapping function

Description

Define a function that generates an approximation function for a given subset of data. This is designed to be used to predict values in one data.table using values in another via the j slot. See examples.

Usage

approxMap(
  dt,
  id.val,
  x = "x",
  y = "y",
  id.col = "ID",
  rand.val = NULL,
  rand.range = c(0, 1),
  FUN = stats::approxfun,
  ...
)

Arguments

dt

A data.table containing the observed data

id.val

The value to look up in dt[[id.col]]. When used in a data.table target's j slot, this would be the name of the column shared between dt and target without quotes.

x

A character string representing the name of the independent variable column in dt. Default is x = 'x'.

y

A character string representing the name of the dependent variable column in dt. Default is y = 'y'.

id.col

A character string representing the name of the group column in dt. Default is id.col = 'ID'

rand.val

A character vector with values present in dt[[id.col]] for which to return a random value. Used for psuedo-observations where the outcome is known regardless of the value of the predicted variable.

rand.range

A vector of two numbers repretenting t he range of potential values for rand.range. Default is rand.range = c(0,1).

FUN

The approximation function. Default is approxfun, but any function that accepts a two-column data.frame is acceptable.

...

Additional parameters to pass to FUN.

Examples

# Generate a data.table with different observations for different categories
data <- data.table(ID = rep(c("A","B","C"), each = 10), 
                   x  = runif(30),
                   y  = runif(30, min = 10, max = 20))

# Create a target data.table
target <- data.table(ID = rep(c("A","B","C"), each = 101),
                     x = rep(seq(0,1,length.out=101), each = 3))

target[, y := approxMap(data, id.val = ID, rule = 2, na.rm=TRUE)(x),by='ID']

andresgmejiar/lbmech documentation built on Feb. 2, 2025, 12:37 a.m.