我如何在 R 中应用逻辑回归

How can i Apply logistic regression in R

我的数据集很小,我想对其应用逻辑回归来预测 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

如有任何帮助,我们将不胜感激。

> summary(myData$Meeting)
#>   No  Yes NA's 
#>    0    5    1 

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

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

myData$Sales.Employee.Manager<-NULL