2

I am trying to do sensitivity analysis using R. My data set has few continuous explanatory variables and a categorical response variable (7 categories).

I tried to run the below mentioned code.

model=train(factor(mode)~Time+Cost+Age+Income, 
            method="nnet",
            preProcess("center","scale"),
            data=train, 
            verbose=F, 
            trControl=trainControl(method='cv', verboseIter = F), 
            tuneGrid=expand.grid(.size=c(1:20), .decay=c(0,0.001,0.01,0.1)))

After getting the output through this code, I tried to develop Lek's profile using the below mentioned code.

Lekprofile(model)

          

However, I got the error stating "Errors in xvars[, x_names]: subscript out of bound"

Please help me to resolve the error.

Gulnaz
  • 21
  • 2
  • I suggest you add some other tags. It helps keep your question in a more targeted community. – Hamed Baziyad Jun 23 '20 at 17:58
  • @hamedbaziyad Thank you for your suggestion. I have added more tags. – Gulnaz Jun 24 '20 at 06:01
  • try `Lekprofile(model$finalModel)` – missuse Jun 25 '20 at 11:58
  • @missuse Thank you for your response. I tried Lekprofile(model$finalModel). But again getting errors : "Error in eval(mod_in$call$data) : object 'dat' not found Error in dat_names[, !names(dat_names) %in% as.character(forms)[2], drop = F] : incorrect number of dimensions" – Gulnaz Jun 26 '20 at 17:12

1 Answers1

1

It doesn't work for a classification model , for example, if we use a regression model:

library(caret)
library(NeuralNetTools)
library(mlbench)

data(BostonHousing)
str(BostonHousing)
'data.frame':   506 obs. of  14 variables:
 $ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
 $ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
 $ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
 $ chas   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
 $ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
 $ rm     : num  6.58 6.42 7.18 7 7.15 ...
 $ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
 $ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
 $ rad    : num  1 2 2 3 3 3 5 5 5 5 ...
 $ tax    : num  296 242 242 222 222 222 311 311 311 311 ...
 $ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
 $ b      : num  397 397 393 395 397 ...
 $ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
 $ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...

We train the model, exclude the categorical chas:

model = train(medv ~ .,data=BostonHousing[,-4],method="nnet",
trControl=trainControl(method="cv",number=10),
tuneGrid=data.frame(size=c(5,10,20),decay=0.1))

lekprofile(model)

enter image description here

You can see the y-axis is meant to be continuous. We can try to discretize our response variable medv and you can see it crashes:

BostonHousing$medv = cut(BostonHousing$medv,4)

model = train(medv ~ .,data=BostonHousing[,-4],method="nnet",
trControl=trainControl(method="cv",number=10),
tuneGrid=data.frame(size=c(5,10,20),decay=0.1))

lekprofile(model)
Error in `[.data.frame`(preds, , ysel, drop = FALSE) : 
  undefined columns selected
StupidWolf
  • 45,075
  • 17
  • 40
  • 72