简体   繁体   中英

R caret nnet package

I have two R objects as below.

matrix "datamatrix" - 200 rows and 494 columns: these are my x variables

dataframe Y. Y$V1 is my Y variable. I have converted column V1 to a factor I am building a classification model.

I want to build a neural network and I ran below command.

model <- train(Y$V1 ~ datamatrix, method='nnet', linout=TRUE, trace = FALSE,
               #Grid of tuning parameters to try:
               tuneGrid=expand.grid(.size=c(1,5,10),.decay=c(0,0.001,0.1))) 

I got an error - " argument "data" is missing, with no default"

Is there a way for caret package to understand that I have my X variables in one R object and Y variable in other? I dont want to combined two data objects and then write a formula as the formula will be too long

Y~x1+x2+x3.................x199+x200....x493+x494

The argument "data" is missing error is addressed by adding a data = datamatrix argument to the train call. The way I would do it would be something like:

datafr <- as.data.frame(datamatrix)

# V1 is the first column name if dimnames aren't specified
datafr$V1 <- as.factor(datafr$V1)

model <- train(V1 ~ ., data = datafr, method='nnet', 
               linout=TRUE, trace = FALSE,
               tuneGrid=expand.grid(.size=c(1,5,10),.decay=c(0,0.001,0.1))) 

Now you don't have to pull your response variable out separately.

The . identifier allows inclusion of all variables from datafr (see here for details).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM