2012-12-23 62 views
13

我的问题与this one有关使用Caret包时的分类数据(R因子)。我从链接的帖子了解到,如果您使用“公式界面”,某些功能可能会成为因素,并且培训可以正常工作。我的问题是如何使用preProcess()函数来缩放数据?如果我尝试做了一些列的数据帧的因素,我得到这个错误信息:如何预处理某些特征时的特征?

Error in preProcess.default(etitanic, method = c("center", "scale")) : 
    all columns of x must be numeric 

这里看到一些示例代码:

library(earth) 
data(etitanic) 

a <- preProcess(etitanic, method=c("center", "scale")) 
b <- predict(etitanic, a) 

谢谢。

回答

18

这与您链接的帖子确实是同一个问题。 preProcess仅适用于数字数据,你必须:

> str(etitanic) 
'data.frame': 1046 obs. of 6 variables: 
$ pclass : Factor w/ 3 levels "1st","2nd","3rd": 1 1 1 1 1 1 1 1 1 1 ... 
$ survived: int 1 1 0 0 0 1 1 0 1 0 ... 
$ sex  : Factor w/ 2 levels "female","male": 1 2 1 2 1 2 1 2 1 2 ... 
$ age  : num 29 0.917 2 30 25 ... 
$ sibsp : int 0 1 1 1 1 0 1 0 2 0 ... 
$ parch : int 0 2 2 2 2 0 0 0 0 0 ... 

不能中心和规模pclasssex原样所以需要转换为虚拟变量。您可以使用model.matrix或插入符号的dummyVars做到这一点:

> new <- model.matrix(survived ~ . - 1, data = etitanic) 
> colnames(new) 
[1] "pclass1st" "pclass2nd" "pclass3rd" "sexmale" "age"  
[6] "sibsp"  "parch" 

-1摆脱了拦截。现在你可以在这个对象上运行preProcess

btw使preProcess忽略非数字数据是在我的“待办事项”列表中,但它可能会导致不注意的人的错误。

最大

+1

我认为我们确实只需要pclass的两个变量。 (“pclass1st,pclass2nd”或“pclass2nd,pclass3rd”或“pclass3rd,pclass1st”)。就像性别可变的情况一样,我们只考虑过sexmale和丢弃sexfemale。纠正我,如果它不够。 – Sandeep

+0

@topepo,我想下面的答案忽略了待办事项列表。我建议为那些不注意的人添加一些警告。 –

5

这里的排除因素的快捷方式或者任何你想考虑这样的:

set.seed(1) 
N <- 20 
dat <- data.frame( 
    x = factor(sample(LETTERS[1:5],N,replace=TRUE)), 
    y = rnorm(N,5,12), 
    z = rnorm(N,-5,17) + runif(N,2,12) 
) 

#' Function which wraps preProcess to exclude factors from the model.matrix 
ppWrapper <- function(x, excludeClasses=c("factor"), ...) { 
    whichToExclude <- sapply(x, function(y) any(sapply(excludeClasses, function(excludeClass) is(y,excludeClass)))) 
    processedMat <- predict(preProcess(x[!whichToExclude], ...), newdata=x[!whichToExclude]) 
    x[!whichToExclude] <- processedMat 
    x 
} 

> ppWrapper(dat) 
    x   y   z 
1 C 1.6173595 -0.44054795 
2 A -0.2933705 -1.98856921 
3 C 1.2177384 0.65420288 
4 D -0.8710374 0.62409408 
5 D -0.4504202 -0.34048640 
6 D -0.6943283 0.24236671 
7 E 0.7778192 0.91606677 
8 D 0.2184563 -0.44935163 
9 C -0.3611408 0.26075970 
10 B -0.7066441 -0.23046073 
11 D -1.5154339 -0.75549761 
12 D 0.4504825 0.38552988 
13 B 1.5692675 0.04093040 
14 C 0.4127541 0.13161807 
15 D 0.5426321 1.09527418 
16 B -2.1040322 -0.04544407 
17 C 0.6928574 1.12090541 
18 B 0.3580960 1.91446230 
19 E 0.3619967 -0.89018040 
20 A -1.2230522 -2.24567237 

你可以传递你想进入ppWrapper任何东西,它会被一起preProcess通过。

+0

好的答案!我认为你应该使用给出的例子(而不是一个可能令人困惑的人为例子)。基本上,'图书馆(地球);数据(etitanic); 数据(etitanic); ppWrapper(etitanic)' –