首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >caret虚拟人-vars排除目标

caret虚拟人-vars排除目标
EN

Stack Overflow用户
提问于 2016-11-18 13:19:56
回答 1查看 2.7K关注 0票数 0

如何在不破坏目标变量的情况下使用插入符号中的虚拟vars?

代码语言:javascript
复制
set.seed(5)
data <- ISLR::OJ
data<-na.omit(data)

dummies <- dummyVars( Purchase ~ ., data = data)
data2 <- predict(dummies, newdata = data)
split_factor = 0.5
n_samples = nrow(data2)
train_idx <- sample(seq_len(n_samples), size = floor(split_factor * n_samples))
train <- data2[train_idx, ]
test <- data2[-train_idx, ]
modelFit<- train(Purchase~ ., method='lda',preProcess=c('scale', 'center'), data=train)

将失败,因为缺少购买变量。如果我事先用data$Purchase <- ifelse(data$Purchase == "CH",1,0)替换它,插入符号抱怨这不再是一个分类而是一个回归问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-18 14:04:22

至少在下面的注释中,示例代码似乎存在一些问题。回答你的问题:

  • ifelse的结果是一个整数向量,而不是一个因子,因此训练函数默认为回归。
  • 直接将dummyVars传递给函数的方法是使用列车(x =,y=,.)而不是公式

为了避免这些问题,请仔细检查对象的class

请注意,preProcess选项在train()中将对所有数值变量(包括虚拟变量)应用预处理。下面的选项2避免了这一点,在调用train()之前对数据进行标准化。

代码语言:javascript
复制
set.seed(5)
data <- ISLR::OJ
data<-na.omit(data)

# Make sure that all variables that should be a factor are defined as such
newFactorIndex <- c("StoreID","SpecialCH","SpecialMM","STORE")
data[, newFactorIndex] <- lapply(data[,newFactorIndex], factor)

library(caret)
# See help for dummyVars. The function does not take a dependent variable and predict will give an error
# I don't include the target variable here, so predicting dummies on new data will drop unknown columns
# including the target variable
dummies <- dummyVars(~., data = data[,-1])
# I don't change the data yet to apply standardization to the numeric variables, 
# before turning the categorical variables into dummies

split_factor = 0.5
n_samples = nrow(data)
train_idx <- sample(seq_len(n_samples), size = floor(split_factor * n_samples))

# Option 1 (as asked): Specify independent and dependent variables separately
# Note that dummy variables will be standardized by preProcess as per the original code

# Turn the categorical variabels to (unstandardized) dummies
# The output of predict is a matrix, change it to data frame
data2 <- data.frame(predict(dummies, newdata = data))

modelFit<- train(y = data[train_idx, "Purchase"], x = data2[train_idx,], method='lda',preProcess=c('scale', 'center'))

# Option 2: Append dependent variable to the independent variables (needs to be a data frame to allow factor and numeric)
# Note that I also shift the proprocessing away from train() to
# avoid standardizing the dummy variables 

train <- data[train_idx, ]
test <- data[-train_idx, ]

preprocessor <- preProcess(train[!sapply(train, is.factor)], method = c('center',"scale"))
train <- predict(preprocessor, train)
test <- predict(preprocessor, test)

# Turn the categorical variabels to (unstandardized) dummies
# The output of predict is a matrix, change it to data frame
train <- data.frame(predict(dummies, newdata = train))
test <- data.frame(predict(dummies, newdata = test))

# Reattach the target variable to the training data that has been 
# dropped by predict(dummies,...)
train$Purchase <- data$Purchase[train_idx]
modelFit<- train(Purchase ~., data = train, method='lda')
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40678170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档