Skip to content

Commit

Permalink
partial changes for ropensci standard
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Aug 6, 2024
1 parent 83864f7 commit 8fda07c
Show file tree
Hide file tree
Showing 52 changed files with 337 additions and 716 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ LazyData: true
RoxygenNote: 7.3.1
Encoding: UTF-8
NeedsCompilation: yes
LinkingTo: cpp11, cpp11armadillo
LinkingTo: cpp11, armadillo
VignetteBuilder: knitr
Config/testthat/edition: 3
Remotes:
pachadotdev/cpp11armadillo,
pachadotdev/armadillo,
ropenscilabs/srr
Roxygen: list(markdown = TRUE, roclets = c("namespace", "rd", "srr::srr_stats_roclet"))
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export(apes)
export(augment)
export(bias_corr)
export(feglm)
export(feglm_control)
export(felm)
export(fenegbin)
export(fepoisson)
Expand Down
4 changes: 2 additions & 2 deletions R/feglm.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ feglm <- function(
k <- length(k_vars)

# Generate temporary variable ----
tmp.var <- temp_var_(data)
tmp_var <- temp_var_(data)

# Drop observations that do not contribute to the log likelihood ----
data <- drop_by_link_type_(data, lhs, family, tmp.var, k_vars, control)
data <- drop_by_link_type_(data, lhs, family, tmp_var, k_vars, control)

# Transform fixed effects and clusters to factors ----
data <- transform_fe_(data, formula, k_vars)
Expand Down
2 changes: 2 additions & 0 deletions R/feglm_control.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#' @return A named list of control parameters.
#'
#' @seealso \code{\link{feglm}}
#'
#' @export
feglm_control <- function(
dev_tol = 1.0e-08,
center_tol = 1.0e-08,
Expand Down
12 changes: 10 additions & 2 deletions R/feglm_offset.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Efficient offset algorithm to update the linear predictor ----

#' @title GLM offset
#'
#' @description Efficient offset algorithm to update the linear predictor
#'
#' @param object an object of class \code{feglm}
#' @param offset a numeric vector of length equal to the number of observations
#'
#' @return an object of class \code{feglm}
#'
#' @noRd
feglm_offset_ <- function(object, offset) {
# Check validity of 'object'
if (!inherits(object, "feglm")) {
Expand Down
7 changes: 7 additions & 0 deletions R/fixed_effects.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#' @title Recover the estimates of the fixed effects after fitting (G)LMs
#'
#' @description The system might not have a unique solution since we do not take
#' collinearity into account. If the solution is not unique, an estimable
#' function has to be applied to our solution to get meaningful estimates of
#' the fixed effects.
#'
#' @param object an object of class \code{"feglm"}.
#' @param alpha_tol tolerance level for the stopping condition. The algorithm is
#' stopped at iteration \eqn{i} if \eqn{||\boldsymbol{\alpha}_{i} -
#' \boldsymbol{\alpha}_{i - 1}||_{2} < tol ||\boldsymbol{\alpha}_{i - 1}||
#' {2}}{||\Delta \alpha|| < tol ||\alpha_old||}. Default is \code{1.0e-08}.
#'
#' @return A named list containing named vectors of estimated fixed effects.
#'
#' @references Stammann, A. (2018). "Fast and Feasible Estimation of Generalized
#' Linear Models with High-Dimensional k-way Fixed Effects". ArXiv e-prints.
#' @references Gaure, S. (n. d.). "Multicollinearity, identification, and
#' estimable functions". Unpublished.
#'
#' @seealso \code{\link{felm}}, \code{\link{feglm}}
#'
#' @examples
#' # same as the example in feglm but extracting the fixed effects
#' mod <- fepoisson(
Expand All @@ -22,6 +28,7 @@
#' )
#'
#' fixed_effects(mod)
#'
#' @export
fixed_effects <- function(object = NULL, alpha_tol = 1.0e-08) {
# Check validity of 'object'
Expand Down
4 changes: 4 additions & 0 deletions R/generics_glance.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#' @export
generics::glance

#' @title Glance method for 'feglm' objects
#' @description Integration with the 'broom' package
#' @export
#' @noRd
glance.feglm <- function(x, ...) {
Expand All @@ -21,6 +23,8 @@ glance.feglm <- function(x, ...) {
res
}

#' @title Glance method for 'felm' objects
#' @description Integration with the 'broom' package
#' @export
#' @noRd
glance.felm <- function(x, ...) {
Expand Down
4 changes: 4 additions & 0 deletions R/generics_predict.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#' @title Predict method for 'feglm' objects
#' @description Similar to the 'predict' method for 'glm' objects
#' @export
#' @noRd
predict.feglm <- function(object, type = c("link", "response"), ...) {
Expand All @@ -14,6 +16,8 @@ predict.feglm <- function(object, type = c("link", "response"), ...) {
x
}

#' @title Predict method for 'felm' objects
#' @description Similar to the 'predict' method for 'lm' objects
#' @export
#' @noRd
predict.felm <- function(object, ...) {
Expand Down
33 changes: 33 additions & 0 deletions R/generics_print.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#' @title Refactors for and 'feglm' summaries
#' @description Reduces the cyclomatic complexity of print.summary.feglm
#' @noRd
summary_formula_ <- function(x) {
cat("Formula: ")
print(x[["formula"]])
}

#' @title Refactors for and 'feglm' summaries
#' @description Reduces the cyclomatic complexity of print.summary.feglm
#' @noRd
summary_family_ <- function(x) {
cat(
"\nFamily: ", gsub("^([a-z])", "\\U\\1", x[["family"]][["family"]],
Expand All @@ -12,6 +18,9 @@ summary_family_ <- function(x) {
)
}

#' @title Refactors for and 'feglm' summaries
#' @description Reduces the cyclomatic complexity of print.summary.feglm
#' @noRd
summary_estimates_ <- function(x, digits) {
cat("\nEstimates:\n\n")
coefmat <- as.data.frame(x[["cm"]])
Expand Down Expand Up @@ -113,6 +122,9 @@ summary_estimates_ <- function(x, digits) {
cat("\nSignificance codes: *** 99.9%; ** 99%; * 95%; . 90%\n")
}

#' @title Refactors for and 'feglm' summaries
#' @description Reduces the cyclomatic complexity of print.summary.feglm
#' @noRd
summary_r2_ <- function(x, digits) {
cat(
sprintf("\nR-squared%*s:", nchar("Adj. "), " "),
Expand All @@ -124,6 +136,9 @@ summary_r2_ <- function(x, digits) {
)
}

#' @title Refactors for and 'feglm' summaries
#' @description Reduces the cyclomatic complexity of print.summary.feglm
#' @noRd
summary_pseudo_rsq_ <- function(x, digits) {
if (x[["family"]][["family"]] == "poisson") {
cat(
Expand All @@ -133,6 +148,9 @@ summary_pseudo_rsq_ <- function(x, digits) {
}
}

#' @title Refactors for and 'feglm' summaries
#' @description Reduces the cyclomatic complexity of print.summary.feglm
#' @noRd
summary_nobs_ <- function(x) {
cat(
"\nNumber of observations:",
Expand All @@ -142,6 +160,9 @@ summary_nobs_ <- function(x) {
)
}

#' @title Refactors for and 'feglm' summaries
#' @description Reduces the cyclomatic complexity of print.summary.feglm
#' @noRd
summary_fisher_ <- function(x, digits) {
if (is.null(x[["theta"]])) {
cat("\nNumber of Fisher Scoring iterations:", x[["iter"]], "\n")
Expand All @@ -159,12 +180,16 @@ summary_fisher_ <- function(x, digits) {
}
}

#' @title Print method for 'apes' objects
#' @description Similar to the 'print' method for 'glm' objects
#' @export
#' @noRd
print.apes <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
print(x[["delta"]], digits = digits)
}

#' @title Print method for 'feglm' objects
#' @description Similar to the 'print' method for 'glm' objects
#' @export
#' @noRd
print.feglm <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
Expand All @@ -177,12 +202,16 @@ print.feglm <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
print(x[["coefficients"]], digits = digits)
}

#' @title Print method for 'felm' objects
#' @description Similar to the 'print' method for 'lm' objects
#' @export
#' @noRd
print.felm <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
print(x[["coefficients"]], digits = digits)
}

#' @title Print method for 'apes' summary objects
#' @description Similar to the 'print' method for 'glm' objects
#' @export
#' @noRd
print.summary.apes <- function(
Expand All @@ -191,6 +220,8 @@ print.summary.apes <- function(
printCoefmat(x[["cm"]], P.values = TRUE, has.Pvalue = TRUE, digits = digits)
}

#' @title Print method for 'feglm' summary objects
#' @description Similar to the 'print' method for 'glm' objects
#' @export
#' @noRd
print.summary.feglm <- function(
Expand All @@ -209,6 +240,8 @@ print.summary.feglm <- function(
summary_fisher_(x, digits)
}

#' @title Print method for 'felm' summary objects
#' @description Similar to the 'print' method for 'lm' objects
#' @export
#' @noRd
print.summary.felm <- function(
Expand Down
4 changes: 4 additions & 0 deletions R/generics_tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#' @export
generics::tidy

#' @title Tidy method for 'feglm' objects
#' @description Integration with the 'broom' package
#' @export
#' @noRd
tidy.feglm <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
Expand All @@ -15,6 +17,8 @@ tidy.feglm <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
res
}

#' @title Tidy method for 'felm' objects
#' @description Integration with the 'broom' package
#' @export
#' @noRd
tidy.felm <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
Expand Down
20 changes: 19 additions & 1 deletion R/generics_vcov.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
#' @title Covariance matrix for APEs
#'
#' @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 ... additional arguments.
#'
#' @return A named matrix of covariance estimates.
#'
#' @seealso \code{\link{apes}}
#'
#' @export
#'
#' @noRd
vcov.apes <- function(object, ...) {
object[["vcov"]]
}

#' @title Covariance matrix for GLMs
#'
#' @description Covariance matrix for the estimator of the structural parameters
#' from objects returned by \code{\link{feglm}}. The covariance is computed
#' from the hessian, the scores, or a combination of both after convergence.
#'
#' @param object an object of class \code{"feglm"}.
#' @param type the type of covariance estimate required. \code{"hessian"} refers
#' to the inverse of the negative expected hessian after convergence and is the
#' default option. \code{"outer.product"} is the outer-product-of-the-gradient
#' 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 ... additional 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}}
#'
#' @examples
#' mod <- fepoisson(
#' trade ~ log_dist + lang + cntg + clny | exp_year + imp_year | pair,
Expand Down Expand Up @@ -161,12 +174,17 @@ vcov.feglm <- function(
}

#' @title Covariance matrix for LMs
#'
#' @description Covariance matrix for the estimator of the structural parameters
#' from objects returned by \code{\link{felm}}. The covariance is computed
#' from the hessian, the scores, or a combination of both after convergence.
#' from the hessian, the scores, or a combination of both after convergence.
#'
#' @param object an object of class \code{"felm"}.
#'
#' @inherit vcov.feglm
#'
#' @seealso \code{\link{felm}}
#'
#' @export
vcov.felm <- function(
object,
Expand Down
Loading

0 comments on commit 8fda07c

Please sign in to comment.