-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
400 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' @rdname marginal_effects | ||
#' @importFrom prediction find_data | ||
#' @export | ||
marginal_effects.multinom <- | ||
function(model, | ||
data = find_data(model, parent.frame()), | ||
variables = NULL, | ||
type = NULL, | ||
eps = 1e-7, | ||
varslist = NULL, | ||
as.data.frame = TRUE, | ||
...) { | ||
|
||
# identify classes of terms in `model` | ||
if (is.null(varslist)) { | ||
varslist <- find_terms_in_model(model, variables = variables) | ||
} | ||
|
||
# estimate numerical derivatives with respect to each variable (for numeric terms in the model) | ||
# add discrete differences for logical terms | ||
out1 <- lapply(c(varslist$nnames, varslist$lnames), dydx, data = data, model = model, type = NULL, eps = eps, as.data.frame = as.data.frame, ...) | ||
|
||
# add discrete differences for factor terms | ||
## exact number depends on number of factor levels | ||
out2 <- list() | ||
for (i in seq_along(varslist$fnames)) { | ||
out2[[i]] <- dydx.factor(data = data, model = model, varslist$fnames[i], fwrap = FALSE, type = NULL, as.data.frame = as.data.frame, ...) | ||
} | ||
|
||
out <- c(out1, out2) | ||
if (isTRUE(as.data.frame)) { | ||
out <- do.call("cbind.data.frame", out[vapply(out, function(x) length(x) > 0, FUN.VALUE = logical(1))]) | ||
} else { | ||
out <- do.call("cbind", out[vapply(out, function(x) length(x) > 0, FUN.VALUE = logical(1))]) | ||
} | ||
return(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,37 @@ | ||
#' @rdname marginal_effects | ||
#' @importFrom prediction find_data | ||
#' @export | ||
marginal_effects.polr <- marginal_effects.nnet | ||
marginal_effects.polr <- | ||
function(model, | ||
data = find_data(model, parent.frame()), | ||
variables = NULL, | ||
type = NULL, | ||
eps = 1e-7, | ||
varslist = NULL, | ||
as.data.frame = TRUE, | ||
...) { | ||
|
||
# identify classes of terms in `model` | ||
if (is.null(varslist)) { | ||
varslist <- find_terms_in_model(model, variables = variables) | ||
} | ||
|
||
# estimate numerical derivatives with respect to each variable (for numeric terms in the model) | ||
# add discrete differences for logical terms | ||
out1 <- lapply(c(varslist$nnames, varslist$lnames), dydx, data = data, model = model, type = NULL, eps = eps, as.data.frame = as.data.frame, ...) | ||
|
||
# add discrete differences for factor terms | ||
## exact number depends on number of factor levels | ||
out2 <- list() | ||
for (i in seq_along(varslist$fnames)) { | ||
out2[[i]] <- dydx.factor(data = data, model = model, varslist$fnames[i], fwrap = FALSE, type = NULL, as.data.frame = as.data.frame, ...) | ||
} | ||
|
||
out <- c(out1, out2) | ||
if (isTRUE(as.data.frame)) { | ||
out <- do.call("cbind.data.frame", out[vapply(out, function(x) length(x) > 0, FUN.VALUE = logical(1))]) | ||
} else { | ||
out <- do.call("cbind", out[vapply(out, function(x) length(x) > 0, FUN.VALUE = logical(1))]) | ||
} | ||
return(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#' @rdname margins | ||
#' @importFrom prediction find_data | ||
#' @export | ||
margins.multinom <- | ||
function(model, | ||
data = find_data(model, parent.frame()), | ||
variables = NULL, | ||
at = NULL, | ||
type = NULL, | ||
vcov = stats::vcov(model), | ||
vce = c("delta", "simulation", "bootstrap", "none"), | ||
iterations = 50L, # if vce == "bootstrap" or "simulation" | ||
unit_ses = FALSE, | ||
eps = 1e-7, | ||
...) { | ||
|
||
# match.arg() | ||
vce <- match.arg(vce) | ||
|
||
# setup data | ||
data_list <- build_datalist(data, at = at) | ||
if (is.null(names(data_list))) { | ||
names(data_list) <- NA_character_ | ||
} | ||
at_specification <- attr(data_list, "at_specification") | ||
|
||
# identify classes of terms in `model` | ||
varslist <- find_terms_in_model(model, variables = variables) | ||
|
||
# reduce memory profile | ||
model[["model"]] <- NULL | ||
attr(model[["terms"]], ".Environment") <- NULL | ||
|
||
# calculate marginal effects | ||
out <- list() | ||
for (i in seq_along(data_list)) { | ||
out[[i]] <- build_margins(model = model, | ||
data = data_list[[i]], | ||
variables = variables, | ||
type = NULL, | ||
vcov = vcov, | ||
vce = vce, | ||
iterations = iterations, | ||
unit_ses = unit_ses, | ||
eps = eps, | ||
varslist = varslist, | ||
...) | ||
out[[i]][["_at_number"]] <- i | ||
} | ||
if (vce == "delta") { | ||
jac <- do.call("rbind", lapply(out, attr, "jacobian")) | ||
rownames(jac) <- paste0(rownames(jac), ".", rep(seq_len(length(out)), each = length(unique(rownames(jac))))) | ||
|
||
# check that vcov() only contains coefficients from model | ||
if (nrow(vcov) != length(coef(model))) { | ||
vcov <- vcov[colnames(coef(model)), colnames(coef(model))] | ||
} | ||
|
||
# sandwich | ||
vc <- jac %*% vcov %*% t(jac) | ||
} else { | ||
jac <- NULL | ||
vc <- NULL | ||
} | ||
|
||
# return value | ||
structure(do.call("rbind", out), | ||
class = c("margins", "data.frame"), | ||
at = if (is.null(at)) NULL else at_specification, | ||
type = type, | ||
call = if ("call" %in% names(model)) model[["call"]] else NULL, | ||
model_class = class(model), | ||
vce = vce, | ||
vcov = vc, | ||
jacobian = jac, | ||
weighted = FALSE, | ||
iterations = if (vce == "bootstrap") iterations else NULL) | ||
} |
Oops, something went wrong.