0

I am struggling to train a random forest via the library ranger while using mlr3. I set 3 parameters but don´t know how to initialize the training. Can someone help me debug this code in r?

pension_trunc<-subset(pension,select=-c(1:8,10:11,18,20,22,24:31,33:44))
names(pension_trunc)
str(pension_trunc)
#setting up the training and test sets
library(mlr3)
#
#define task
task<-TaskRegr$new(id="pension",backend=pension_trunc,target="net_tfa")
print(task)
#splitting the truncated dataset into training and test samples
trn_trunc<-sample(task$nrow,0.8*task$nrow)
test_trunc<-setdiff(seq_len(task$nrow),trn_trunc)
str(trn_trunc)
str(test_trunc)
#------------------------------------------------------------------------
#Set parameters
PS=ParamSet$new(list(
  ParamInt$new(id="mtry",default=3L,lower=1L,upper=5L,tags="train"),
  ParamInt$new(id="max.depth",default=5L,lower=1L,upper=30L,tags="train"),
  ParamInt$new(id="min.node.size",default=10L,lower=1L,upper=30L,tags="train")))
PS
#define learner for random forest using ranger library
learner8=lrn("regr.ranger")
#num.trees = 500 set as default value
#------------------------------------------------
#--------------------------------------------
#train random forest learner
lp_rf<-learner4$train(task=task,row_ids=trn_trunc,paramSet=PS)
lp_rf
#predict  rf on training sample
pp_rf=learner4$predict(task=task,row_ids=trn_trunc)
pp_rf
autoplot(pp_rf)
coef(learner4$model,newdata=trn_trunc)
#predict rf on test sample
pp_rf_test=learner4$predict(task=task,row_ids=test_trunc)
pp_rf_test
#
measure=msr("regr.mse")
prediction$score(measure)
  • 1
    Please edit your question and add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Ensure that your code is minimal and readable. Also the [mlr3book](https://mlr3book.mlr-org.com/) should get you going. – pat-s Jan 27 '21 at 19:26
  • Furthermore, you are confusing terminology: `tuning` is used to optimize parameters within a defined `ParamSet` (this is why you perhaps define a `ParamSet`). But you are trying to simply train a model in your code. `lrn("regr.ranger", mtry = 4L, max.depth = 5L) ` gives you a learner that will always use a fixed `mtry = 4` etc. You can use this learner with `$train` to simply fit a model. – pfistfl Feb 16 '21 at 16:35

0 Answers0