Check if mtrx()
will give matching results to model.matrix()
Source: R/mtrx_compatible.R
mtrx_compatible.Rd
When the following are true, mtrx()
returns a matrix that matches
that returned by model.matrix()
:
There are no non-default factor contrasts.
Every column is one of a numeric, integer, character, or factor.
The
na.action
option is"na.pass"
.
In that case, in code where model.matrix()
results in slowdowns, one can
write:
Usage
mtrx_compatible(data, na_action = getOption("na.action"))
Arguments
- data
A data frame.
- na_action
What to do about missing values–see
na.pass()
. Can be either a function or character, just as the option can be.
Value
A logical value. Returns TRUE if mtrx()
will return a similar
matrix to model.matrix()
, and FALSE
otherwise.
Details
In this case, "matching" output means that dummy variables will be encoded
in the same way, missing values will be handled in the same way (returned
as-is), and that mtrx()
won't error due to the presence of
unsupported column types.
Examples
# Compatible data frame
df1 <- data.frame(a = 1:3, b = letters[1:3], c = factor(letters[1:3]))
mtrx_compatible(df1)
#> [1] TRUE
# Incompatible due to non-default contrasts
df2 <- df1
contrasts(df2$c) <- "contr.sum"
mtrx_compatible(df2)
#> [1] FALSE
# Incompatible due to unsupported column type
df3 <- data.frame(a = 1:3, b = as.Date(c("2023-01-01", "2023-01-02", "2023-01-03")))
mtrx_compatible(df3)
#> [1] FALSE
# Missing values are problematic only when na_action is not `na.pass`
df4 <- data.frame(a = c(1, NA, 3), b = letters[1:3])
mtrx_compatible(df4, na_action = "na.omit")
#> [1] FALSE
mtrx_compatible(df4, na_action = "na.pass")
#> [1] TRUE