Learn R Programming

qgcomp (version 2.0.0)

quantize: create variables representing indicator functions with cutpoints defined by quantiles

Description

This function creates categorical variables in place of the exposure variables named in 'expnms.' For example, a continuous exposure 'x1' will be replaced in the output data by another 'x1' that takes on values 0:(q-1), where, for example, the value 1 indicates that the original x1 value falls between the first and the second quantile.

Usage

quantize(data, expnms, q = 4, breaks = NULL)

Arguments

data

a data frame

expnms

a character vector with the names of the columns to be quantized

q

integer, number of quantiles used in creating quantized variables

breaks

(optional) list of (equal length) numeric vectors that characterize the minimum value of each category for which to break up the variables named in expnms. This is an alternative to using 'q' to define cutpoints.

Details

This function is a vectorized version of `quantile_f` from the `gWQS` package that also allows the use of externally defined breaks

Examples

Run this code
# NOT RUN {
set.seed(1232)
dat = data.frame(y=runif(100), x1=runif(100), x2=runif(100), z=runif(100))
qdata = quantize(data=dat, expnms=c("x1", "x2"), q=4)
table(qdata$data$x1)
table(qdata$data$x2)
summary(dat[c("y", "z")]);summary(qdata$data[c("y", "z")]) # not touched
dat = data.frame(y=runif(100), x1=runif(100), x2=runif(100), z=runif(100))
# using 'breaks' requires specifying min and max (the qth quantile)
# example with theoretical quartiles (could be other relevant values)
qdata2 = quantize(data=dat, expnms=c("x1", "x2"), 
   breaks=list(c(-1e64, .25, .5, .75, 1e64), 
               c(-1e64, .25, .5, .75, 1e64)
               ))
table(qdata2$data$x1)
table(qdata2$data$x2)
# }

Run the code above in your browser using DataLab