# basic example using linear model
require("datasets")
x <- lm(mpg ~ cyl * hp + wt, data = head(mtcars))
margins(x)
# obtain unit-specific standard errors
if (FALSE) {
margins(x, unit_ses = TRUE)
}
# use of 'variables' argument to estimate only some MEs
summary(margins(x, variables = "hp"))
# use of 'at' argument
## modifying original data values
margins(x, at = list(hp = 150))
## AMEs at various data values
margins(x, at = list(hp = c(95, 150), cyl = c(4,6)))
# use of 'data' argument to obtain AMEs for a subset of data
margins(x, data = mtcars[mtcars[["cyl"]] == 4,])
margins(x, data = mtcars[mtcars[["cyl"]] == 6,])
# return discrete differences for continuous terms
## passes 'change' through '...' to dydx()
margins(x, change = "sd")
# summary() method
summary(margins(x, at = list(hp = c(95, 150))))
margins_summary(x, at = list(hp = c(95, 150)))
## control row order of summary() output
summary(margins(x, at = list(hp = c(95, 150))), by_factor = FALSE)
# alternative 'vce' estimation
if (FALSE) {
# bootstrap
margins(x, vce = "bootstrap", iterations = 100L)
# simulation (ala Clarify/Zelig)
margins(x, vce = "simulation", iterations = 100L)
}
# specifying a custom `vcov` argument
if (require("sandwich")) {
x2 <- lm(Sepal.Length ~ Sepal.Width, data = head(iris))
summary(margins(x2))
## heteroskedasticity-consistent covariance matrix
summary(margins(x2, vcov = vcovHC(x2)))
}
# generalized linear model
x <- glm(am ~ hp, data = head(mtcars), family = binomial)
margins(x, type = "response")
margins(x, type = "link")
# multi-category outcome
if (requireNamespace("nnet")) {
data("iris3", package = "datasets")
ird <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]),
species = factor(c(rep("s",50), rep("c", 50), rep("v", 50))))
m <- nnet::nnet(species ~ ., data = ird, size = 2, rang = 0.1,
decay = 5e-4, maxit = 200, trace = FALSE)
margins(m) # default
margins(m, category = "v") # explicit category
}
# using margins_summary() for concise grouped operations
list_data <- split(mtcars, mtcars$gear)
list_mod <- lapply(list_data, function(x) lm(mpg ~ cyl + wt, data = x))
mapply(margins_summary, model = list_mod, data = list_data, SIMPLIFY = FALSE)
Run the code above in your browser using DataLab