【问题标题】:Fail to predict woe in R无法预测 R 中的问题
【发布时间】:2020-01-25 15:16:45
【问题描述】:

我用这个公式来解决问题

library("woe")
woe.object <- woe(data, Dependent="target", FALSE, 
                  Independent="shop_id", C_Bin=20, Bad=0, Good=1)

然后我想预测测试数据的灾难

test.woe <- predict(woe.object, newdata = test, replace = TRUE)

它给了我一个错误

UseMethod("predict") 中的错误: 没有适用于“data.frame”类对象的“预测”方法

有什么建议吗?

【问题讨论】:

  • 对于预测,您可能必须使用 library(klaR)
  • 我用过,还是报同样的错误
  • 你可以在下面看到..但是我不确定你是否在 klar 的情况下运行你的模型

标签: r prediction


【解决方案1】:

对于预测,您无法使用包 woe 进行预测。您需要使用该软件包。注意函数woe的屏蔽,见下图:

#let's say we woe and then klaR was loaded
library(klaR)

data = data.frame(target=sample(0:1,100,replace=TRUE),
shop_id = sample(1:3,100,replace=TRUE),
another_var = sample(letters[1:3],100,replace=TRUE))
#make sure both dependent and independent are factors
data$target=factor(data$target)
data$shop_id = factor(data$shop_id)
data$another_var = factor(data$another_var)

您需要两个或多个因变量:

woemodel <- klaR::woe(target~ shop_id+another_var, 
data = data)

如果只提供一个,就会报错:

woemodel <- klaR::woe(target~ shop_id, 
    data = data)

woe.default(x, grouping, weights = weights, ...) 中的错误:全部 具有独特水平的因素。没有计算任何麻烦!另外:警告 消息:在 woe.default(x, grouping, weights = weights, ...) 中:仅 一个单一的输入变量。结果 object$woe 中的变量名是 只在公式调用中保存。

如果您想预测只有一个独立变量的因变量,逻辑回归之类的方法将起作用:

mdl = glm(target ~ shop_id,data=data,family="binomial")
prob = predict(mdl,data,type="response")
predicted_label = ifelse(prob>0.5,levels(data$target)[1],levels(data$target)[0])

【讨论】:

  • 我总是得到这个错误: sum(keep.fids) 中的错误:每当我运行 woemodel
  • 你知道woemodel应该是哪种类型吗?我从我用 library(woe) 编写的方程中得到了 woemodel 结果作为数据框,它是 klaR 的图像。因此,当我预测使用 klaR 时,似乎 woe 模型(数据框)的类型是错误的。
  • 底线是,如果您使用来自 klaR 的 predict.woe,它必须是“由调用 'woe' 产生的对象。”,这意味着从 klaR::woe 返回的东西而不是 woe: :woe
  • 非常感谢您提供的信息!我的意思是包裹的镜子对不起。即使重新启动 R,KlaR 也无法与我合作。您会建议我从 github 安装它吗?那会有什么不同吗?
  • 我现在明白了,您会收到该错误,因为使用 klaR::woe 时响应和预测变量都必须是分类的。
猜你喜欢
  • 2017-06-25
  • 2012-03-03
  • 2016-08-23
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
相关资源
最近更新 更多