Last data update: 2014.03.03
R: Transformation of Factors to Individual Levels
cv.trans.psa R Documentation
Transformation of Factors to Individual Levels
Description
The function cv.trans.psa
takes a covariate data frame and replaces each categorical
covariate of n >=3 levels with n
new binary covariate columns,
one for each level. Transforms covariate dataframe for use with the function cv.bal.psa
.
Usage
cv.trans.psa(covariates, fcol = NULL)
Arguments
covariates
A dataframe of covariates, presumably some factors.
fcol
An optional vector containing the factor columns in the
covariate dataframe. In NULL (default) routine to identfy
factors internally.
Value
Returns a dataframe covariates.transformed
containing new
columns for each level of more than binary factors. The rest of the
covariate dataframe stays unchanged.
Author(s)
James E. Helmreich James.Helmreich@Marist.edu
Robert M. Pruzek RMPruzek@yahoo.com
KuangNan Xiong harryxkn@yahoo.com
See Also
cv.bal.psa
, loess.psa
,
cstrata.psa
, cv.trans.psa
Examples
#Note reordering of columns, binary factor and numeric column are unchanged.
f2 <- factor(sample(c(0, 1), 20, replace = TRUE))
f4 <- factor(sample(c("a", "b", "c", "d"), 20, replace = TRUE))
cv <- rnorm(20)
X <- data.frame(f2, f4, cv)
cv.trans.psa(X)
#
f2 <- factor(sample(c('c', 'C'), 20, replace = TRUE))
f4 <- factor(sample(c("b", "A", "d", "CC"), 20, replace = TRUE))
cv <- rnorm(20)
X <- data.frame(f2, f4, cv)
cv.trans.psa(X)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(PSAgraphics)
Loading required package: rpart
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/PSAgraphics/cv.trans.psa.Rd_%03d_medium.png", width=480, height=480)
> ### Name: cv.trans.psa
> ### Title: Transformation of Factors to Individual Levels
> ### Aliases: cv.trans.psa
> ### Keywords: ~kwd1 ~kwd2
>
> ### ** Examples
>
> #Note reordering of columns, binary factor and numeric column are unchanged.
> f2 <- factor(sample(c(0, 1), 20, replace = TRUE))
> f4 <- factor(sample(c("a", "b", "c", "d"), 20, replace = TRUE))
> cv <- rnorm(20)
> X <- data.frame(f2, f4, cv)
> cv.trans.psa(X)
$covariates.transformed
cv f2_1 f4_a f4_b f4_c f4_d
1 0.725866093 0 1 0 0 0
2 -0.940557990 1 0 0 0 1
3 -0.395372553 1 0 0 1 0
4 0.627711486 0 0 0 1 0
5 1.708402091 0 0 0 0 1
6 0.350062138 1 1 0 0 0
7 0.881377629 1 0 1 0 0
8 -1.819123625 0 0 0 0 1
9 -0.802319103 0 0 1 0 0
10 -0.608593154 0 0 1 0 0
11 -0.512184113 1 0 1 0 0
12 -1.036122588 1 0 1 0 0
13 -0.879977527 1 0 1 0 0
14 -0.466238038 1 1 0 0 0
15 -1.536589191 1 1 0 0 0
16 -0.318576990 1 0 1 0 0
17 1.177442331 1 0 1 0 0
18 -0.117507248 1 0 0 0 1
19 -1.394576642 1 0 0 0 1
20 -0.002174277 1 0 0 0 1
> #
> f2 <- factor(sample(c('c', 'C'), 20, replace = TRUE))
> f4 <- factor(sample(c("b", "A", "d", "CC"), 20, replace = TRUE))
> cv <- rnorm(20)
> X <- data.frame(f2, f4, cv)
> cv.trans.psa(X)
$covariates.transformed
cv f2_c f4_A f4_CC f4_b f4_d
1 -0.5157665 1 0 0 0 1
2 -0.1574208 0 0 0 0 1
3 1.3894301 1 0 1 0 0
4 0.5319889 0 0 1 0 0
5 0.3192361 0 1 0 0 0
6 0.4523910 1 1 0 0 0
7 -0.5067806 1 1 0 0 0
8 -1.8501442 1 0 0 0 1
9 0.3277508 1 0 0 1 0
10 0.3032497 0 0 1 0 0
11 -1.0283698 1 0 1 0 0
12 -0.5008940 0 1 0 0 0
13 0.3666180 0 0 0 1 0
14 -0.4129661 0 0 0 1 0
15 -0.5082857 1 0 1 0 0
16 0.2957063 1 0 0 0 1
17 0.7676735 1 1 0 0 0
18 1.4427059 0 0 1 0 0
19 2.5867490 0 0 0 1 0
20 -0.2354624 0 0 1 0 0
>
>
>
>
>
>
>
>
>
> dev.off()
null device
1
>