throwic
throwic

Reputation: 163

Is there a way to change default formula in mlr3?

I'm studying {mlr3} package and i would like to compare 2 models:

I did not find in the documentation a way to specify the formula in the task creation. The default is ~ .. I can find a hacky way with model.matrix and set another taks with these new columns but i might be missing something.

Upvotes: 1

Views: 198

Answers (1)

missuse
missuse

Reputation: 19716

You are probably looking for PipeOpModelMatrix function from mlr3pipelines.

For instance

task <- as_task_regr(iris[1:4], target = "Sepal.Length")
lrn <- lrn("regr.lm")
pop <- po("modelmatrix", formula = ~ .  ^ 2)

pop %>>%
  lrn -> gr
  
GraphLearner$new(gr) -> gr

gr$train(task)
gr$model$modelmatrix$outtasklayout
#output
                         id    type
1:              (Intercept) numeric
2:             Petal.Length numeric
3: Petal.Length:Petal.Width numeric
4: Petal.Length:Sepal.Width numeric
5:              Petal.Width numeric
6:  Petal.Width:Sepal.Width numeric
7:              Sepal.Width numeric

gr$model$regr.lm$model
#output
Call:
stats::lm(formula = task$formula(), data = task$data())

Coefficients:
               (Intercept)               `(Intercept)`                Petal.Length                 Petal.Width                 Sepal.Width  
                   1.39837                          NA                     1.15756                    -1.66219                     0.84812  
`Petal.Length:Petal.Width`  `Petal.Length:Sepal.Width`   `Petal.Width:Sepal.Width`  
                   0.06695                    -0.16772                     0.27626 

Upvotes: 3

Related Questions