# NOT RUN {
# basic example using linear model
require("datasets")
x <- lm(mpg ~ cyl * hp + wt, data = head(mtcars))
margins(x)
# obtain unit-specific standard errors
# }
# NOT RUN {
margins(x, unit_ses = TRUE)
# }
# NOT RUN {
# 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))))
## control row order of summary() output
summary(margins(x, at = list(hp = c(95, 150))), by_factor = FALSE)
# alternative 'vce' estimation
# }
# NOT RUN {
# bootstrap
margins(x, vce = "bootstrap", iterations = 100L)
# simulation (ala Clarify/Zelig)
margins(x, vce = "simulation", iterations = 100L)
# }
# NOT RUN {
# 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")
# }
Run the code above in your browser using DataLab