错误:在使用caret()进行逻辑回归时,请为x使用列名 [英] Error: Please use column names for `x` when using caret() for logistic regression

查看:701
本文介绍了错误:在使用caret()进行逻辑回归时,请为x使用列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用插入符号包构建逻辑回归模型.

I'd like to build a logistic regression model using the caret package.

这是我的代码.

library(caret)
df <- data.frame(response = sample(0:1, 200, replace=TRUE),  predictor = rnorm(200,10,45)) 

outcomeName <-"response"
predictors <- names(df)[!(names(df) %in% outcomeName)]
index <- createDataPartition(df$response, p=0.75, list=FALSE)
trainSet <- df[ index,]
testSet <- df[-index,]

model_glm <- train(trainSet[,outcomeName], trainSet[,predictors], method='glm', family="binomial", data = trainSet)

我收到错误Error: Please use column names for x.

trainSet[,predictors]替换为列名predictors时,会收到相同的错误.

I receive the same error when I replace trainSet[,predictors] with the column name predictors.

推荐答案

不幸的是,当仅对像df[,1]这样的一列设置子集以将结果更改为vector时,R的行为令人讨厌,而且由于只有一个预测变量,因此遇到了这种情况特征.您可以通过将结果保存为data.frame

Unfortunately R has a nasty behavior when subsetting just one column like df[,1] to change outcome to a vector and as you have only one predictor you encountered this feature. You can preserve results as data.frame by either

trainSet[,predictors, drop = FALSE]

trainSet[predictors]

顺便说一句.该代码还有两个其他问题:

BTW. there are two additional issues with the code:

  1. 第一个参数应该是预测变量,而不是响应
  2. 对于使用caret的逻辑回归,您需要做出响应才能成为factor
  1. First argument should be predictors, not response
  2. For logistic regression with caret you need response to be a factor

完整代码应为:

library(caret)
df <- data.frame(response = sample(0:1, 200, replace=TRUE),  
                 predictor = rnorm(200,10,45)) 

df$response <- as.factor(df$response)

outcomeName <-"response"
predictors <- names(df)[!(names(df) %in% outcomeName)]
index <- createDataPartition(df$response, p=0.75, list=FALSE)
trainSet <- df[ index,]
testSet <- df[-index,]

model_glm <- train(trainSet[predictors], trainSet[[outcomeName]], method='glm', family="binomial", data = trainSet)

*将trainSet[,outcomeName]更改为trainSet[[outcomeName]],以便更明确地转换为vector

*changed trainSet[,outcomeName] to trainSet[[outcomeName]] for more explicit transformation to vector

这篇关于错误:在使用caret()进行逻辑回归时,请为x使用列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆