glmnet を使用して、ペナルティ付きロジスティック回帰法を学習しようとしています。mtcars のサンプル データの車にオートマチック トランスミッションまたはマニュアルのどちらがあるかを予測しようとしています。私のコードはかなり単純だと思いますが、エラーが発生しているようです:
この最初のブロックは、mtcar を 80% のトレーニング セットと 20% のテスト セットに単純に分割します。
library(glmnet)
attach(mtcars)
smp_size <- floor(0.8 * nrow(mtcars))
set.seed(123)
train_ind <- sample(seq_len(nrow(mtcars)), size=smp_size)
train <- mtcars[train_ind,]
test <- mtcars[-train_ind,]
x データは応答のない行列形式であることがわかっているので、2 つのトレーニング セットを非応答行列 (train_x) と応答ベクトル (train_y) に分けます。
train_x <- train[,!(names(train) %in% c("am"))]
train_y <- train$am
しかし、モデルをトレーニングしようとすると、
p1 <- glmnet(train_x, train_y)
エラーが発生します:
Error in elnet(x, is.sparse, ix, jx, y, weights, offset, type.gaussian,
:(list) object cannot be coerced to type 'double'
何か不足していますか?