covariates: Transforming Covariates and Filling in Missing Values

covariatesR Documentation

Transforming Covariates and Filling in Missing Values

Description

This function standardizes continuous variables, transforms categorical variables to indicator variables and centers the transformed variables.

Usage

covariates(x.con, x.cat, con.rescale = TRUE, cat.center = FALSE, fill.missing = TRUE) 

Arguments

x.con

a matrix or data frame consisting of continuous covariates.

x.cat

a matrix or data frame consisting of categorical covariates.

con.rescale

if con.rescale = TRUE, standardize continuous variables.

cat.center

if cat.center = TRUE, center categorical variables.

fill.missing

If fill.missing = TRUE, missing values of a variable are replaced by the mean of the observed data.

Value

This function returns a data frame containing the rescaled variables.

Note

For Bayesian hierarchical or penalization modeling, it is important to transform all variables to have a common scale before fitting data.

Author(s)

Nengjun Yi, nyi@uab.edu

Examples

# fake data
age = rnorm(50, 30, 0.1)
sex = sample(x = c("male", "female"), size = 50, replace = T, prob = c(0.5, 0.5))
diet = sample(x = c(1, 5), size = 50, replace = T, prob = c(0.3, 0.7))
race = sample(c("Asian", "White", "Black"), size = 50, replace = T, prob = c(0.2, 0.4, 0.4))

x.con = age # continuous variable
x.cat = cbind(sex, diet, race) # categorical variables

x.con[1:2] = NA
x.cat[1,] = NA

x1 = covariates(x.con = x.con, x.cat = x.cat, con.rescale = F, cat.center = F, 
                fill.missing = T)
x1
x2 = covariates(x.con = x.con, x.cat = x.cat, con.rescale = T, cat.center = F, 
                fill.missing = T)
x2
x3 = covariates(x.con = x.con, x.cat = x.cat, con.rescale = T, cat.center = T, 
                fill.missing = T)
x3

nyiuab/BhGLM documentation built on June 12, 2024, 9:28 p.m.