2

I wanted to make a list for for loop, than I wanted to use it for ctree like below. but I couldn't. I am getting, some errors like "character is not supported" even I change the list to factor

I want to change val variable, with the y list, for for loop.

Is there anyway to make it work?

y <- c("A","B","C")
x <- as.factor(y)
for(val in x) {
      ctree_model<- ctree(FTR ~ val, data = train,controls=ctree_control(minsplit=30,minbucket=10,maxdepth=10))
    }
sanwhere
  • 23
  • 5

1 Answers1

2

The formula should be constructed either with paste or reformulate. In addition, the 'ctree_model' for storing the output can be a list with each element of list stores the model that corresponds to the 'val' of x instead of overriding

ctree_model <- vector('list', length(x))
names(ctree_model) <- levels(x)
for(val in levels(x)) {
   ctree_model[[val]] <- ctree(reformulate(val, response = 'FTR'), 
            data = train, 
     controls = ctree_control(minsplit = 30, minbucket = 10, maxdepth  =10))
 }
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I couldn't make it work. Where is "A", "B", "C" in here? – sanwhere Apr 25 '21 at 22:32
  • @sanwhere can you try now. The step where you convert the character to factor was not needed. So I changed it to `levels(x)` – akrun Apr 25 '21 at 22:34
  • @akun still can't see the "A", "B", "C" part in your code, how I am suppose to add A,B,C words to for loop? – sanwhere Apr 25 '21 at 22:35
  • @sanwhere this is your code `y <- c("A","B","C"); x <- as.factor(y);` Can you please check by typing `x` on your console. When you loop over the `x` or `levels(x)`, the 'val' is each element of 'x' – akrun Apr 25 '21 at 22:36
  • @akur I see now, but I got this error: "C_ExpectCovarInfluence: sum of weights is less than one" – sanwhere Apr 25 '21 at 22:38
  • @sanwhere that is a model error and not the code error. It is related to your data – akrun Apr 25 '21 at 22:38
  • I change it little bit and now I have ` y <- c("B365H") x <- as.factor(y) ctree_model <- vector('list', length(x)) names(ctree_model) <- levels(x) for(val in levels(x)) { ctree_model[[val]] <- ctree(reformulate(val, response = 'FTR'), data = train, controls = ctree_control(minsplit = 30, minbucket = 10, maxdepth =10)) mean(ctree_predictions==test$FTR) } ` code like this, but now, it is not working still. "In ==.default(ctree_predictions, test$FTR) :the longer object length is not a multiple of the short obje... – sanwhere Apr 25 '21 at 22:41
  • @akur my data is working without loop, there is a problem with loop I guess. – sanwhere Apr 25 '21 at 22:42
  • @sanwhere if you have only single element, there is no need for a loop – akrun Apr 25 '21 at 22:42
  • @akur I don't, this is an example of my code. I have 10-15 variables on my list. – sanwhere Apr 25 '21 at 22:45
  • I don't understand what `ctree_predictions` object is` – akrun Apr 25 '21 at 22:46
  • @akur sorry, I forgot to add it into the code: `ctree_predictions <- predict(ctree_model,newdata=test,type='response')` – sanwhere Apr 25 '21 at 22:49
  • @sanwhere you cannot do that as `ctree_model` is a `list`. You need `ctree_predictions <- lapply(ctree_model, function(model) predict(model, newdata = test, type = 'response'))` – akrun Apr 25 '21 at 22:50
  • 1
    @akur Actually I add the code, and tune it, thank you! it is working now. – sanwhere Apr 25 '21 at 22:50
  • 1
    @akur I already voted, I guess it is not visible, coz of my experience point. – sanwhere Apr 25 '21 at 22:56