# ---------------------------------------------------------------------------
# Example usage
framed <- model_frame(Sepal.Width ~ Species, iris)
model_matrix(framed$terms, framed$data)
# ---------------------------------------------------------------------------
# Missing values never result in dropped rows
iris2 <- iris
iris2$Species[1] <- NA
framed2 <- model_frame(Sepal.Width ~ Species, iris2)
model_matrix(framed2$terms, framed2$data)
# ---------------------------------------------------------------------------
# Contrasts
# Default contrasts
y <- factor(c("a", "b"))
x <- data.frame(y = y)
framed <- model_frame(~y, x)
# Setting contrasts directly
y_with_contrast <- y
contrasts(y_with_contrast) <- contr.sum(2)
x2 <- data.frame(y = y_with_contrast)
framed2 <- model_frame(~y, x2)
# Compare!
model_matrix(framed$terms, framed$data)
model_matrix(framed2$terms, framed2$data)
# Also, can set the contrasts globally
global_override <- c(unordered = "contr.sum", ordered = "contr.poly")
rlang::with_options(
.expr = {
model_matrix(framed$terms, framed$data)
},
contrasts = global_override
)
Run the code above in your browser using DataLab