我有一个包含变量“nameDay”的数据框,它是一个因子变量。这些日子表示为字符(“星期六”、“星期一”...),但我已将它们转换为因子。以下是此数据帧中用于复制的前 6 行:
head(Casual.data)
casual casAvg Year weather season holiday humidity medWs nameDay
minTemp avgHum stdWs Hour derHum atemp Day 3131 61
43.907692 2011 1 3 0 42 11.0014 Tuesday 31.16 54.77778 5.544601 16 -3.500 42.425 19 8581 5 1.369231 2012 2 3 0 70 6.0032 Thursday 29.52 65.55556
3.282332 5 1.000 34.090 19 4452 40 34.153846 2011 1 4 0 77 7.0015 Monday 21.32 57.77778 5.598605 20
4.625 25.000 17 9610 1 2.828125 2012 1 4 0 73 7.0015 Friday 22.14 62.77778 3.206137 2 2.000 25.760 5
10235 1 1.421875 2012 1 4 1 76 11.0014
Monday 16.40 71.77778 2.962030 4 1.750 20.455 12 496 0
2.828125 2011 2 1 0 63 6.0032 Friday 5.74 49.55556 3.951886 2 2.875 8.335 4
maxAtemp maxTemp stdTemp stdAtemp derAtemp derTemp 3131 42.425 36.90 1.7608268 1.7536814 0.757500 0.7175 8581 35.605 31.16 0.7609278 0.7030059 -0.189375 -0.2050 4452 27.275 23.78 0.7609278 0.7033802 -0.189375 -0.2050 9610 31.060 27.06 2.0085816 2.4278610 -0.662500 -0.6150 10235 21.970 18.04 0.6833333 0.6310012 -0.189375 -0.2050 496 12.880 8.20 0.8961833 1.3659498 -0.283750 -0.3075
函数 cv.glmnet(来自 glmnet 库)要求我将数据作为矩阵而不是数据框传递。因此,我将我的数据框转换为矩阵:
Xcas <- as.matrix(Casual.data[,-1])
我取出第一列,因为它是我的响应变量。我为我的响应向量创建了一个数字向量:
Ycas <- as.numeric(Casual.data$casual)
最后,我尝试拟合 lasso 回归模型:
lasso.casual <- cv.glmnet(x=Xcas, y=Ycas, alpha=1)
我收到此错误消息:
elnet(x, is.sparse, ix, jx, y, weights, offset,
type.gaussian, : NA/NaN/Inf in foreign function call (arg 5)
另外:警告消息:在 elnet(x, is .sparse, ix, jx, y, weights,
offset, type.gaussian, : 强制引入的 NA
我认为这是因为我的原始数据框中的“nameDay”变量,但我不确定。有想法该怎么解决这个吗?
谢谢