我未能 运行 caret 的 nnet 回归

I fail to run caret's nnet regression

我尝试使用 caret 的 nnet 运行 回归,但出现错误。

library(tidyverse)
library(caret)
feature = rnorm(100, 0, 1) %>% as.matrix() 
colnames(feature) = "x1"
outcome = rnorm(100, 0, 1) %>% as.matrix()
colnames(feature) = "y1"
model = caret::train(
  x = feature, y = outcome, method = "nnet",
  tuneGrid = expand.grid(size=c(1:3), decay=seq(0.1, 1, 0.1)),
  weights = NULL, linout = TRUE
  )

Error: Metric RMSE not applicable for classification models

当然,我要回归,不是分类。为了显示这一点,我设置了选项 linout = TRUE。出了什么问题?

此外,我按照,并尝试删除as.matrix,但它也显示其他错误。

library(tidyverse)
library(caret)
feature = rnorm(100, 0, 1) %>% as.double()
outcome = rnorm(100, 0, 1) %>% as.double()
CATE_model = caret::train(
  x = feature, y = outcome, method = "nnet",
  tuneGrid = expand.grid(size=c(1:3), decay=seq(0.1, 1, 0.1)),
  weights = NULL, linout = TRUE
  )

Error: Please use column names for 'x'

非常感谢

神经网络的输入似乎需要同名。 这是有道理的,因为经过训练的神经网络稍后可能需要在其输入中识别正确的列以进行预测。

如果你将你的特征强制放入带有 colnames 的 Dataframe 中(在我的例子中 x),它会起作用。

library(tidyverse)
library(caret)
feature = data.frame(x = rnorm(100, 0, 1) %>% as.double()) # changed line 
outcome = rnorm(100, 0, 1) %>% as.double()
CATE_model = caret::train(
  x = feature, y = outcome, method = "nnet",
  tuneGrid = expand.grid(size=c(1:3), decay=seq(0.1, 1, 0.1)),
  weights = NULL, linout = TRUE
)