简体   繁体   English

无法预测 R 中的灾难

[英]Fail to predict woe in R

I used this formula to get woe with我用这个公式来解决问题

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

Then I want to predict woe for the test data然后我想预测测试数据的灾难

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

And it gives me an error它给了我一个错误

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "data.frame" UseMethod("predict") 中的错误:没有适用的“预测”方法应用于“data.frame”类的对象

Any suggestions please?请问有什么建议吗?

For prediction, you cannot do it with the package woe .对于预测,你不能用包woe来做。 You need to use the package.您需要使用该软件包。 Take note of the masking of the function woe , see below:注意函数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)

You need two or more dependent variables:您需要两个或更多因变量:

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

If you only provide one, you have an error:如果您只提供一个,则会出现错误:

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

Error in woe.default(x, grouping, weights = weights, ...) : All factors with unique levels. woe.default(x, grouping, weights = weights, ...) 中的错误:具有唯一级别的所有因素。 No woes calculated!没有计算出任何问题! In addition: Warning message: In woe.default(x, grouping, weights = weights, ...) : Only one single input variable.此外:警告消息:在 woe.default(x, grouping, weights = weights, ...) 中:只有一个输入变量。 Variable name in resulting object$woe is only conserved in formula call.结果 object$woe 中的变量名称仅在公式调用中保留。

If you want to predict the dependent variable with only one independent, something like logistic regression will work:如果您只想用一个独立变量来预测因变量,那么逻辑回归之类的方法将起作用:

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM