r random forest error - type of predictors in new data do not match

前端 未结 8 1404
挽巷
挽巷 2020-12-04 14:37

I am trying to use quantile regression forest function in R (quantregForest) which is built on Random Forest package. I am getting a type mismatch error that I can\'t quite

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 15:28

    I just solved doing the following:

    ## Creating sample data
    values_development=factor(c("a", "b", "c")) ## Values used when building the random forest model
    values_production=factor(c("a", "b", "c", "ooops")) ## New values to used when using the model
    
    ## Deleting cases which were not present when developing
    values_production=sapply(as.character(values_production), function(x) if(x %in% values_development) x else NA)
    
    ## Creating the factor variable, (with the correct NA value level)
    values_production=factor(values_production)
    
    ## Checking
    values_production # =>  a     b     c   
    

提交回复
热议问题