covariates: Transforming Covariates and Filling in Missing Values

Description Usage Arguments Value Note Author(s) Examples

Description

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

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 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 Jan. 9, 2022, 3:31 p.m.