【问题标题】:How can i Apply logistic regression in R我如何在 R 中应用逻辑回归
【发布时间】:2019-06-25 21:31:21
【问题描述】:

我的数据集很小,我想对其应用逻辑回归来预测 myData$Meeting

我正在粘贴我的 data.frame 对象的 dput 输出

myData <- structure(list(Item.Name = structure(c(1L, 14L, 2L, 12L, 2L, 
11L), .Label = c("brinjal", "chocolate", "cold drink", "injections", 
"jeans", "onion", "potato", "shirts", "skirts", "suit", "syrup", 
"tablet", "tee", "wafer"), class = "factor"), Item.Group.Name = 
 structure(c(4L, 
 2L, 2L, 3L, 2L, 3L), .Label = c("apparel", "food", "medicine", 
"vegetable"), class = "factor"), Quantity = c(44L, 97L, 53L, 
11L, 5L, 71L), Sales.Employee.Name = structure(c(14L, 10L, 8L, 
10L, 5L, 10L), .Label = c("Alysa Fontell", "Breanne Kissock", 
"Clovis Mance", "Eadie Tidcomb", "Ella Tregidga", "Georg Hollyard", 
"Gib Hanalan", "Jade Postle", "Jewelle Woodall", "Kent Franciottoi", 
"Mychal Elix", "Ralina Wraight", "Shaughn Avrahamian", "Sibelle Santino", 
"Sigfrid Alejandro"), class = "factor"), Sales.Employee.Manager = 
structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "Hanny Stokey", class = "factor"), 
Sales.Employee.Region = structure(c(2L, 5L, 4L, 5L, 4L, 5L
), .Label = c("America/Chicago", "America/Denver", "America/Kentucky/Louisville", 
"America/Los_Angeles", "America/New_York"), class = "factor"), 
Sales.Enquiry.Stage = structure(c(6L, 3L, 3L, 6L, 4L, 5L), .Label = c("Lead", 
"Lost", "Meeting", "Proposal", "Qualified", "Won"), class = "factor"), 
Final.Status = structure(c(1L, 1L, 1L, 1L, 2L, 2L), .Label = c("Closed", 
"Open"), class = "factor"), Enquiry.Source.Sub.Type = structure(c(2L, 
3L, 4L, 3L, 1L, 2L), .Label = c("Existing", "IB Call", "OB Call", 
"Reference", "Website"), class = "factor"), Enquiry.Source.Type = structure(c(1L, 
2L, 2L, 2L, 1L, 1L), .Label = c("Inbound", "Outbound"), class = "factor"), 
Rate.per.Quantity = c(90L, 130L, 400L, 120L, 400L, 150L), 
Estimate.Value = c(3960L, 12610L, 21200L, 1320L, 2000L, 10650L
), Employee.Gender = structure(c(2L, 1L, 2L, 2L, 1L, 2L), .Label = c("Female", 
"Male"), class = "factor"), Meeting = structure(c(2L, 2L, 
2L, 2L, 2L, NA), .Label = c("No", "Yes"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")      

当我运行这段代码时

glm(data = meetingData, formula = meetingData$Meeting ~. , family = binomial(link = "logit"))

我收到此错误,

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels

任何帮助都会非常感激和感激。

【问题讨论】:

    标签: r dataframe logistic-regression


    【解决方案1】:
    > summary(myData$Meeting)
    #>   No  Yes NA's 
    #>    0    5    1 
    

    您尝试预测的列仅具有两个类之一中的值。这使得无法训练逻辑回归。

    【讨论】:

    • 那么你有什么推荐的吗?如何使模型更好,以便我可以预测新数据的结果。
    • 您将需要更多数据,特别是您需要一些属于“否”类别的观察结果。为了让回归了解 No 和 Yes 之间的区别,它需要一些 No 案例的示例。
    • 谢谢先生!我得到了它。将记住这一点以进行进一步采样。
    【解决方案2】:

    此外,您的Sales.Employee.Manager 是一个只有一个级别的因素(Hanny Stokey)。因为它是一个常数,没有方差,所以它对回归没有任何增加,所以如果你删除它,错误就不再出现了

    myData$Sales.Employee.Manager&lt;-NULL

    【讨论】:

    • 哦,谢谢你让我明白了。有效。但它给出了额外的警告。警告消息:glm.fit:算法没有收敛..这是什么意思?我该如何进一步进行?
    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2018-02-12
    • 2014-06-20
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多