lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误:NA / NaN / Inf为“ y”,尝试了所有可能的方法

时间:2019-09-27 06:41:26

标签: r nan logistic-regression na lm

这里我的数据集是pd,我将其分为训练数据和测试数据pd_train1pd_train2

    sku national_inv lead_time in_transit_qty forecast_3_month forecast_6_month
1 3921548            8        12              0                0                0
2 3191009           83         2             33              157              377
3 2935810            8         4              0                0                0
4 2205847           31         4             63               70              160
5 4953497            3        12              0                0                0
6 2286884            0         8              0                0                0
  forecast_9_month sales_1_month sales_3_month sales_6_month sales_9_month min_bank
1                0             1             1             2             5        2
2              603            44            98           148           156       53
3                0             0             0             1             1        0
4              223            27            90           164           219        0
5                0             0             0             0             0        0
6                0             0             0             0             0        0
  potential_issue pieces_past_due perf_6_month_avg perf_12_month_avg local_bo_qty
1               0               0             0.63              0.75            0
2               0               0             0.68              0.66            0
3               0               0             0.73              0.78            0
4               0               0             0.73              0.78            0
5               0               0             0.81              0.74            0
6               0               0             0.91              0.96            0
  deck_risk oe_constraint ppap_risk stop_auto_buy rev_stop went_on_backorder  data
1         0             0         0             1        0                No train
2         0             0         0             1        0                No train
3         0             0         0             1        0                No train
4         0             0         1             1        0                No train
5         0             0         0             1        0                No train
6         0             0         0             1        0                No train

我想为我的训练数据pd_train1创建一个lm模型 但是我收到如下错误:

> fit=lm(went_on_backorder~.,data=pd_train1)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  NA/NaN/Inf in 'y'
In addition: Warning message:
In storage.mode(v) <- "double" : NAs introduced by coercion

我尝试搜索无限值:

sapply(pd_train1, function(x) sum(is.infinite(x)))
             sku      national_inv         lead_time    in_transit_qty  forecast_3_month 
                0                 0                 0                 0                 0 
 forecast_6_month  forecast_9_month     sales_1_month     sales_3_month     sales_6_month 
                0                 0                 0                 0                 0 
    sales_9_month          min_bank   potential_issue   pieces_past_due  perf_6_month_avg 
                0                 0                 0                 0                 0 
perf_12_month_avg      local_bo_qty         deck_risk     oe_constraint         ppap_risk 
                0                 0                 0                 0                 0 
    stop_auto_buy          rev_stop went_on_backorder              data 
                0                 0                 0                 0 

对于我想建立线性模型的训练数据中的NA / NaN值

     sku      national_inv         lead_time    in_transit_qty  forecast_3_month 
                0                 0                 0                 0                 0 
 forecast_6_month  forecast_9_month     sales_1_month     sales_3_month     sales_6_month 
                0                 0                 0                 0                 0 
    sales_9_month          min_bank   potential_issue   pieces_past_due  perf_6_month_avg 
                0                 0                 0                 0                 0 
perf_12_month_avg      local_bo_qty         deck_risk     oe_constraint         ppap_risk 
                0                 0                 0                 0                 0 
    stop_auto_buy          rev_stop went_on_backorder 
                0                 0                 0 


Inf %in% pd_train1$went_on_backorder
1] FALSE

NaN %in% pd_test$went_on_backorder
1] FALSE

此后,我无法获取数据集中的NA / NaN / Inf值 有人可以帮我理解为什么会引发错误吗? 这里went_on_backorder是我的目标变量。

2 个答案:

答案 0 :(得分:0)

went_on_backorder不是数字变量。 lm无法处理非数字因变量。查看logistic regression

答案 1 :(得分:0)

went_on_backorder是一个因素。线性回归需要一个数值响应变量。

要使用逻辑回归,请在基数R或glm之类的包中使用vgam。这是一个简单的示例:

pd_train1 <- data.frame('went_on_backorder' = c('No','Yes','Yes'), 'lead_time' = 1:3)
model <- glm(went_on_backorder ~ ., data = pd_train1, family = 'binomial')

您可以预测自己的课程:

predict(model, newdata = data.frame('lead_time' = c(0,1,2.5,3.5)), type = "response")