Model.Matrix返回无限值

时间:2017-08-16 16:00:11

标签: r dataframe

我有一个没有NA的数据框(由na.omit()删除),行数= 1,450,683,同时将其转换为model.matrix以输入glmnet,最终矩阵有一些值是无限的。

 str(train_again)
 Classes 'tbl_df', 'tbl' and 'data.frame':  1450683 obs. of  24  variables:
$ vendor_id          : Factor w/ 2 levels "1","2": 2 1 2 2 2 2 1 2 1 2 ...
$ passenger_count    : int  1 1 1 1 1 6 4 1 1 1 ...
$ pickup_longitude   : num  -74 -74 -74 -74 -74 ...
$ pickup_latitude    : num  40.8 40.7 40.8 40.7 40.8 ...
$ dropoff_longitude  : num  -74 -74 -74 -74 -74 ...
$ dropoff_latitude   : num  40.8 40.7 40.7 40.7 40.8 ...
$ store_and_fwd_flag : Factor w/ 2 levels "N","Y": 1 1 1 1 1 1 1 1 1 1 ...
$ trip_duration      : int  455 663 2124 429 435 443 341 1551 255 1274 ...
$ month              : Factor w/ 6 levels "1","2","3","4",..: 3 6 1 4 3 1 6 5 5 5 ...
$ wday               : Factor w/ 7 levels "Fri","Mon","Sat",..: 2 4 6 7 3 3 1 3 1 6 ...
$ hour               : int  17 0 11 19 13 22 22 7 23 22 ...
$ work               : Factor w/ 2 levels "FALSE","TRUE": 2 1 2 1 1 1 1 1 1 1 ...
$ jfk_trip           : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
$ lg_trip            : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
$ average.temperature: num  45.5 72.5 22 39 46.5 33.5 70.5 60 80 56.5 ...
$ rain               : num  25 2 2 2 2 2 2 6 2 2 ...
$ s_fall             : num  2 2 2 2 2 2 2 2 2 2 ...
$ s_depth            : num  1 1 0.01 1 1 8 1 1 1 1 ...
$ total_distance     : num  2009 2513 11061 1779 1615 ...
$ number_of_steps    : int  5 6 16 4 5 5 5 17 2 6 ...
$ fastest_speed      : num  43.9 27.3 51.9 27.2 41.5 ...
$ left_turns         : int  1 2 5 2 2 1 1 4 0 2 ...
$ right_turns        : int  1 2 7 1 2 3 3 9 1 2 ...
$ turns              : int  1 2 9 1 3 3 2 6 0 3 ...


 x = model.matrix(trip_duration~.,train_again) #here train_again is a data frame with no NA's
 y = train_again$trip_duration
 sum(is.infinite(x)) #gives output as 537

可能的原因是什么?我的原始数据集一定有问题吗?

1 个答案:

答案 0 :(得分:1)

鉴于您的评论中有一个变量的值为Inf(非NA),我建议您使用以下内容:

(使用tidyverse,因为你已经证明你有一个tibble。)删除响应变量:

predvars <- dplyr::select(train_again,-trip_duration)

查找所有有限行(无NANaNInf):

all_finite <- apply(is.finite(predvars),1,all)

您可以在model.matrix()中使用单边公式:

x <- model.matrix(~.,predvars[all_finite,])