inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
73 stars 21 forks source link

Confusing use of ~ in the component for fixed effects #220

Closed gregorgorjanc closed 6 months ago

gregorgorjanc commented 7 months ago

We are following https://inlabru-org.github.io/inlabru/articles/component.html to define various model components. Each component should be something like:

intercept_comp <- ~ my_intercept(
  main = rep(1, n),
  model = "linear"
)

my_fixed_effects_comp <- ~ fixed_effects(
  main = ~ x1 + x2,
  model = "fixed"
)

etc.

The second one needs to have ~ in the main argument, which can be confusing compared to other model components that don't need ~ - it took me a while to debug this yesterday and I suspect it might trip others too. I assume this is required so that the main is a formula for the MatrixModels::model.Matrix(main, .data.) call. Could this be changed to as.formula(main) instead? Aha, this won't work because we could not do main = x1 + x2, right?

Anyway, I wanted to share this struggle we encountered as beginner users.

finnlindgren commented 7 months ago

The "fixed" model is a special model that takes a model matrix as input. When a formula specification is provided instead of a matrix, it automatically calls MatrixModels::model.Matrix(formula, data = .data.) to construct the corresponding model matrix. If you want a different model matrix construction, replace formula = ~ x1 + x2 by some other R code that generates the needed matrix.

finnlindgren commented 7 months ago

This can surely be more extensively documented somewhere; I'm not sure where the main documentation for this feature actually appears, so I'd be happy about suggestions/actual text.

gregorgorjanc commented 7 months ago

@finnlindgren i think that if you add this explanation into https://inlabru-org.github.io/inlabru/articles/component.html it would be beneficial to the users (assuming they read it!).