Skip to content

Commit

Permalink
style r code
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Sep 7, 2024
1 parent 43b4d89 commit 7d03fb6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 114 deletions.
32 changes: 19 additions & 13 deletions R/apes.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ apes <- function(

# Center regressor matrix (if required)
if (control[["keep_mx"]]) {
MX <- object[["MX"]]
mx <- object[["mx"]]
} else {
MX <- center_variables_r_(x, w, k_list, control[["center_tol"]], 10000L)
mx <- center_variables_r_(x, w, k_list, control[["center_tol"]], 10000L)
}

# Compute average partial effects, derivatives, and Jacobian
PX <- x - MX
PX <- x - mx
Delta <- matrix(NA_real_, nt, p)
Delta1 <- matrix(NA_real_, nt, p)
J <- matrix(NA_real_, p, p)
Expand All @@ -193,7 +193,7 @@ apes <- function(
} else {
Delta[, j] <- beta[[j]] * Delta[, j]
Delta1[, j] <- beta[[j]] * Delta1[, j]
J[, j] <- colSums(MX * Delta1[, j]) / nt_full
J[, j] <- colSums(mx * Delta1[, j]) / nt_full
J[j, j] <- sum(mu.eta) / nt_full + J[j, j]
}
}
Expand All @@ -209,18 +209,22 @@ apes <- function(

# Compute analytical bias correction of average partial effects
if (bias_corr) {
b <- apes_bias_correction_(eta, family, x, beta, binary, nt, p, PPsi, z,
w, k_list, panel_structure, L, k, MPsi, v)
b <- apes_bias_correction_(
eta, family, x, beta, binary, nt, p, PPsi, z,
w, k_list, panel_structure, L, k, MPsi, v
)
delta <- delta - b
}
rm(eta, w, z, MPsi)

# Compute covariance matrix
Gamma <- gamma_(MX, object[["hessian"]], J, PPsi, v, nt_full)
Gamma <- gamma_(mx, object[["hessian"]], J, PPsi, v, nt_full)
V <- crossprod(Gamma)

V <- apes_adjust_covariance_(V, Delta, Gamma, k_list, adj, sampling_fe,
weak_exo, panel_structure)
V <- apes_adjust_covariance_(
V, Delta, Gamma, k_list, adj, sampling_fe,
weak_exo, panel_structure
)

# Add names
names(delta) <- nms.sp
Expand Down Expand Up @@ -268,8 +272,9 @@ apes_set_adj_ <- function(n_pop, nt_full) {
return(adj)
}

apes_adjust_covariance_ <- function(V, Delta, Gamma, k_list, adj, sampling_fe,
weak_exo, panel_structure) {
apes_adjust_covariance_ <- function(
V, Delta, Gamma, k_list, adj, sampling_fe,
weak_exo, panel_structure) {
if (adj > 0.0) {
# Simplify covariance if sampling assumptions are imposed
if (sampling_fe == "independence") {
Expand Down Expand Up @@ -298,8 +303,9 @@ apes_adjust_covariance_ <- function(V, Delta, Gamma, k_list, adj, sampling_fe,
return(V)
}

apes_bias_correction_ <- function(eta, family, x, beta, binary, nt, p, PPsi,
z, w, k_list, panel_structure, L, k, MPsi, v) {
apes_bias_correction_ <- function(
eta, family, x, beta, binary, nt, p, PPsi,
z, w, k_list, panel_structure, L, k, MPsi, v) {
# Compute second-order partial derivatives
Delta2 <- matrix(NA_real_, nt, p)
Delta2[, !binary] <- partial_mu_eta_(eta, family, 3L)
Expand Down
4 changes: 2 additions & 2 deletions R/feglm.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ feglm <- function(
x <- NULL
eta <- NULL

# Add names to beta, hessian, and MX (if provided) ----
# Add names to beta, hessian, and mx (if provided) ----
names(fit[["coefficients"]]) <- nms_sp
if (control[["keep_mx"]]) {
colnames(fit[["MX"]]) <- nms_sp
colnames(fit[["mx"]]) <- nms_sp
}
dimnames(fit[["hessian"]]) <- list(nms_sp, nms_sp)

Expand Down
14 changes: 7 additions & 7 deletions R/feglm_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ get_score_matrix_ <- function(object) {

# Center regressor matrix (if required)
if (control[["keep_mx"]]) {
MX <- object[["MX"]]
mx <- object[["mx"]]
} else {
# Extract additional required quantities from result list
formula <- object[["formula"]]
Expand All @@ -483,24 +483,24 @@ get_score_matrix_ <- function(object) {
attr(x, "dimnames") <- NULL

# Center variables
MX <- center_variables_r_(x, w, k.list, control[["center_tol"]], 10000L)
colnames(MX) <- nms_sp
mx <- center_variables_r_(x, w, k.list, control[["center_tol"]], 10000L)
colnames(mx) <- nms_sp
}

# Return score matrix
MX * (nu * w)
mx * (nu * w)
}

#' @title Gamma computation
#' @description Computes the gamma matrix for the APES function
#' @param MX Regressor matrix
#' @param mx Regressor matrix
#' @param H Hessian matrix
#' @param J Jacobian matrix
#' @param PPsi Psi matrix
#' @param v Vector of weights
#' @param nt Number of observations
#' @noRd
gamma_ <- function(MX, H, J, PPsi, v, nt) {
gamma_ <- function(mx, H, J, PPsi, v, nt) {
inv_nt <- 1.0 / nt
(MX %*% solve(H * inv_nt, J) - PPsi) * v * inv_nt
(mx %*% solve(H * inv_nt, J) - PPsi) * v * inv_nt
}
15 changes: 9 additions & 6 deletions R/fenegbin.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,17 @@ fenegbin <- function(
# Information if convergence failed ----
if (!conv && trace) cat("Algorithm did not converge.\n")

# Add names to beta, hessian, and MX (if provided) ----
# Add names to beta, hessian, and mx (if provided) ----
names(fit[["coefficients"]]) <- nms_sp
if (control[["keep_mx"]]) {
colnames(fit[["MX"]]) <- nms_sp
colnames(fit[["mx"]]) <- nms_sp
}
dimnames(fit[["hessian"]]) <- list(nms_sp, nms_sp)

fenegbin_result_list_(fit, theta, iter, conv, nobs, lvls_k, nms_fe,
formula, data, family, control)
fenegbin_result_list_(
fit, theta, iter, conv, nobs, lvls_k, nms_fe,
formula, data, family, control
)
}

# Convergence Check ----
Expand All @@ -222,8 +224,9 @@ fenegbin_check_convergence_ <- function(dev, dev_old, theta, theta_old, tol) {

# Generate result list ----

fenegbin_result_list_ <- function(fit, theta, iter, conv, nobs, lvls_k,
nms_fe, formula, data, family, control) {
fenegbin_result_list_ <- function(
fit, theta, iter, conv, nobs, lvls_k,
nms_fe, formula, data, family, control) {
reslist <- c(
fit, list(
theta = theta,
Expand Down
17 changes: 10 additions & 7 deletions R/generics_vcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ vcov.feglm <- function(
# it is totally fine not to have a cluster variable
cl_vars <- vcov_feglm_vars_(object)
k <- length(cl_vars)

if (isTRUE(k >= 1L) && type != "clustered") {
type <- "clustered"
}

# Compute requested type of covariance matrix
H <- object[["hessian"]]
p <- ncol(H)

if (type == "hessian") {
# If the hessian is invertible, compute its inverse
V <- vcov_feglm_hessian_covariance_(H, p)
Expand All @@ -87,8 +87,10 @@ vcov.feglm <- function(
# Check if the OP is invertible and compute its inverse
V <- vcov_feglm_outer_covariance_(G, p)
} else {
V <- vcov_feglm_clustered_sandwich_covariance_(object, type, H, G,
cl_vars, sp_vars, k, p)
V <- vcov_feglm_clustered_sandwich_covariance_(
object, type, H, G,
cl_vars, sp_vars, k, p
)
}
}

Expand Down Expand Up @@ -117,8 +119,9 @@ vcov_feglm_outer_covariance_ <- function(G, p) {
V
}

vcov_feglm_clustered_sandwich_covariance_ <- function(object, type, H, G,
cl_vars, sp_vars, k, p) {
vcov_feglm_clustered_sandwich_covariance_ <- function(
object, type, H, G,
cl_vars, sp_vars, k, p) {
# Check if the hessian is invertible and compute its inverse
V <- try(solve(H), silent = TRUE)
if (inherits(V, "try-error")) {
Expand Down Expand Up @@ -198,7 +201,7 @@ vcov_feglm_clustered_covariance_ <- function(G, cl_vars, sp_vars, p) {
)
)
}

# Update outer product
if (i %% 2L) {
B <- B + B_r
Expand Down
79 changes: 0 additions & 79 deletions tests/testthat/test-kendall.R

This file was deleted.

0 comments on commit 7d03fb6

Please sign in to comment.