Skip to content

Commit

Permalink
purrr -> dplyr for clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Feb 8, 2024
1 parent 62b7a54 commit cd75b35
Show file tree
Hide file tree
Showing 23 changed files with 48 additions and 101 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Imports:
Formula,
magrittr,
MASS,
purrr,
rlang,
stats
Suggests:
Expand Down
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export(fixed_effects)
importFrom(Formula,Formula)
importFrom(MASS,negative.binomial)
importFrom(MASS,theta.ml)
importFrom(dplyr,all_of)
importFrom(dplyr,across)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
Expand All @@ -45,7 +45,6 @@ importFrom(dplyr,ungroup)
importFrom(dplyr,vars)
importFrom(dplyr,where)
importFrom(magrittr,"%>%")
importFrom(purrr,map_dfr)
importFrom(rlang,":=")
importFrom(rlang,sym)
importFrom(stats,as.formula)
Expand Down
5 changes: 2 additions & 3 deletions R/capybara-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
#' and Wanner (2020).
#'
#' @name capybara-package
#' @importFrom dplyr all_of filter group_by mutate mutate_at select summarise
#' ungroup vars where
#' @importFrom purrr map_dfr
#' @importFrom dplyr across filter group_by mutate mutate_at select
#' summarise ungroup vars where
#' @importFrom rlang sym :=
#' @importFrom Formula Formula
#' @importFrom MASS negative.binomial theta.ml
Expand Down
5 changes: 2 additions & 3 deletions R/feglm.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#' @param formula an object of class \code{"formula"}: a symbolic description of
#' the model to be fitted. \code{formula} must be of type \code{y ~ x | k},
#' where the second part of the formula refers to factors to be concentrated
#' out. It is also possible to pass additional variables to \code{\link{feglm}}
#' (e.g. to cluster standard errors). This can be done by specifying the third
#' part of the formula: \code{y ~ x | k | add}.
#' out. It is also possible to pass clustering variables to \code{\link{feglm}}
#' as \code{y ~ x | k | c}.
#' @param data an object of class \code{"data.frame"} containing the variables
#' in the model.
#' @param family the link function to be used in the model. Similar to
Expand Down
14 changes: 5 additions & 9 deletions R/generics_summary.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @export
#' @noRd
summary.apes <- function(object, ...) {
summary.apes <- function(object) {
# Compute coefficent matrix
est <- object[["delta"]]
se <- sqrt(diag(object[["vcov"]]))
Expand All @@ -18,12 +18,10 @@ summary.apes <- function(object, ...) {
#' @noRd
summary.feglm <- function(
object,
type = c("hessian", "outer.product", "sandwich", "clustered"),
cluster = NULL,
...) {
type = c("hessian", "outer.product", "sandwich", "clustered")) {
# Compute coefficients matrix
est <- object[["coefficients"]]
se <- sqrt(diag(vcov(object, type, cluster)))
se <- sqrt(diag(vcov(object, type)))
z <- est / se
p <- 2.0 * pnorm(-abs(z))
cm <- cbind(est, se, z, p)
Expand Down Expand Up @@ -64,12 +62,10 @@ summary.feglm <- function(
#' @noRd
summary.felm <- function(
object,
type = "hessian",
cluster = NULL,
...) {
type = "hessian") {
# Compute coefficients matrix
est <- object[["coefficients"]]
se <- sqrt(diag(vcov(object, type, cluster)))
se <- sqrt(diag(vcov(object, type)))
z <- est / se
p <- 2.0 * pnorm(-abs(z))
cm <- cbind(est, se, z, p)
Expand Down
17 changes: 7 additions & 10 deletions R/generics_vcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
#' @description Covariance matrix for the estimator of the
#' average partial effects from objects returned by \code{\link{apes}}.
#' @param object an object of class \code{"apes"}.
#' @param ... other arguments.
#' @return A named matrix of covariance estimates.
#' @seealso \code{\link{apes}}
#' @export
vcov.apes <- function(object, ...) {
vcov.apes <- function(object) {
object[["vcov"]]
}

Expand All @@ -21,16 +20,14 @@ vcov.apes <- function(object, ...) {
#' estimator. \code{"sandwich"} is the sandwich estimator (sometimes also
#' referred as robust estimator), and \code{"clustered"} computes a clustered
#' covariance matrix given some cluster variables.
#' @param ... other arguments.
#' @return A named matrix of covariance estimates.
#' @references Cameron, C., J. Gelbach, and D. Miller (2011). "Robust Inference
#' With Multiway Clustering". Journal of Business & Economic Statistics 29(2).
#' @seealso \code{\link{feglm}}
#' @export
vcov.feglm <- function(
object,
type = c("hessian", "outer.product", "sandwich", "clustered"),
...) {
type = c("hessian", "outer.product", "sandwich", "clustered")) {
# Check validity of input argument 'type'
type <- match.arg(type)

Expand Down Expand Up @@ -130,8 +127,9 @@ vcov.feglm <- function(
B.r <- B.r + crossprod(
as.matrix(
G %>%
split(.[, cl]) %>%
map_dfr(~ colSums(.x[sp.vars]))
group_by(!!sym(cl)) %>%
summarise(across(sp.vars, sum), .groups = "drop") %>%
select(-!!sym(cl))
)
)
}
Expand Down Expand Up @@ -161,7 +159,6 @@ vcov.feglm <- function(
#' @export
vcov.felm <- function(
object,
type = c("hessian", "outer.product", "sandwich", "clustered"),
...) {
vcov.feglm(object, type, ...)
type = c("hessian", "outer.product", "sandwich", "clustered")) {
vcov.feglm(object, type)
}
2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pkgdown: 2.0.7
pkgdown_sha: ~
articles:
intro: intro.html
last_built: 2024-02-05T19:03Z
last_built: 2024-02-08T00:11Z

6 changes: 3 additions & 3 deletions docs/reference/apes.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/bias_corr.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions docs/reference/feglm.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions docs/reference/felm.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions docs/reference/fenegbin.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions docs/reference/fepoisson.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions docs/reference/vcov.apes.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions docs/reference/vcov.feglm.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions docs/reference/vcov.felm.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/feglm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cd75b35

Please sign in to comment.