I fail to run caret's nnet regression

Viewed 96

I tried to run regression using caret's nnet, but I got a error.

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

Of course, I want to regress, not classify. In order to show this, I set the option linout = TRUE. What went wrong?

Also, I followed this question, and tried to remove as.matrix, but it also shows other error.

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'

Thanks, a lot

1 Answers

The input of the Neural Network seems to require colnames. This makes sense as the trained neural net might later need to identify the correct columns in its input for prediction.

if you force your features into a Dataframe with colnames (in my case x), it works.

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
)
Related