R中的风险评估模型,以获得一个因子的特定水平的概率



我是一名风险分析师,我的老板给我分配了一项我不知道该怎么做的任务。

现在我想得到一些特定条件下的概率。例如,数据如下所示

sex      hair_color Credit_Score Loan_Status
"Male"    "Red"      "256"        "bad"        
"Female"  "black"    "133"        "bad"        
"Female"  "brown"    "33"         "bad"        
"Male"    "yellow"   "123"        "good"  

因此,我们希望预测每个客户的Loan_Status。我能做的就是把"性"、"hair_color"、"credit_score"当作因素。并将它们放入 R 中的 glm(( 中。

但我的老板想知道"如果一个新客户是男性,红头发,他的贷款状态是'好'的概率是多少?

"或者"男性客户的贷款状态变成'好'的概率是多少?

我应该使用什么样的方法?如何获得概率?我正在考虑边际分布,但我不知道这是否有效或如何计算它。

我希望我使这个问题易于理解,对于谁会帮助我,非常感谢您的时间

我认为本教程非常适合您的问题:http://www.theanalysisfactor.com/r-tutorial-glm1/

如果你在数据上使用它,它看起来像这样:

sex <- factor(c("m", "f", "f", "m"))
hair_color <- factor(c("red", "black", "brown", "yellow"))
credit_score <- c(256, 133, 33, 123)
loan_status <- factor(c("b", "b", "b", "g"))
data <- data.frame(sex, hair_color, credit_score, loan_status)
model <- glm(formula = loan_status ~ sex + hair_color + credit_score, 
         data = data, 
         family = "binomial")
predict(object = model, 
    newdata = data.frame(sex = "f", hair_color = "yellow", credit_score =     100),
    type = "response")

最新更新