Skip to content

Commit

Permalink
some refactors to save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Jul 16, 2024
1 parent bdfaaba commit 5ff84c7
Show file tree
Hide file tree
Showing 27 changed files with 165 additions and 185 deletions.
4 changes: 0 additions & 4 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ solve_eta2_ <- function(yadj, myadj, offset, eta) {
.Call(`_capybara_solve_eta2_`, yadj, myadj, offset, eta)
}

sqrt_ <- function(w) {
.Call(`_capybara_sqrt_`, w)
}

kendall_cor_ <- function(m) {
.Call(`_capybara_kendall_cor_`, m)
}
Expand Down
10 changes: 7 additions & 3 deletions R/generics_vcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ vcov.apes <- function(object, ...) {
#' trade_panel
#' )
#'
#' vcov(mod, type = "clustered")
#' round(vcov(mod, type = "clustered"), 5)
#'
#' @return A named matrix of covariance estimates.
#'
Expand Down Expand Up @@ -72,9 +72,9 @@ vcov.feglm <- function(
V <- matrix(Inf, p, p)
}
} else {
G <- getScoreMatrix(object)
G <- get_score_matrix_(object)
if (type == "outer.product") {
# Check if the OPG is invertible and compute its inverse
# Check if the OP is invertible and compute its inverse
V <- try(inv_(G), silent = TRUE)
if (inherits(V, "try-error")) {
V <- matrix(Inf, p, p)
Expand Down Expand Up @@ -162,6 +162,10 @@ 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.
#' @param object an object of class \code{"felm"}.
#' @inherit vcov.feglm
#' @seealso \code{\link{felm}}
#' @export
Expand Down
9 changes: 4 additions & 5 deletions R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ feglm_fit_ <- function(beta, eta, y, X, wt, k.list, family, control) {
# Compute weights and dependent variable
mu.eta <- family[["mu.eta"]](eta)
w <- (wt * mu.eta^2) / family[["variance"]](mu)
w.tilde <- sqrt_(w)
nu <- (y - mu) / mu.eta

# Centering variables
Expand All @@ -81,7 +80,7 @@ feglm_fit_ <- function(beta, eta, y, X, wt, k.list, family, control) {
# Compute update step and update eta
# beta.upd <- as.vector(qr.solve(MX * w.tilde, Mnu * w.tilde, epsilon))
# eta.upd <- nu - as.vector(Mnu - MX %*% beta.upd)
beta.upd <- solve_beta_(MX, Mnu, w.tilde, TRUE)
beta.upd <- solve_beta_(MX, Mnu, w, TRUE)
eta.upd <- solve_eta_(MX, Mnu, nu, beta.upd)

# Step-halving with three checks
Expand All @@ -101,7 +100,7 @@ feglm_fit_ <- function(beta, eta, y, X, wt, k.list, family, control) {
val.crit <- family[["valideta"]](eta) && family[["validmu"]](mu)
imp.crit <- (dev - dev.old) / (0.1 + abs(dev)) <= -dev.tol
if (dev.crit && val.crit && imp.crit) break
rho <- rho / 2.0
rho <- rho * 0.5
}

# Check if step-halving failed (deviance and invalid \eta or \mu)
Expand Down Expand Up @@ -226,7 +225,7 @@ feglm_offset_ <- function(object, offset) {

# Centering dependent variable and compute \eta update
Myadj <- center_variables_(Myadj, yadj, w, k.list, center.tol, 10000L, TRUE)
# eta.upd <- yadj - as.vector(Myadj) + offset - eta
# eta.upd <- yadj - drop(Myadj) + offset - eta
eta.upd <- solve_eta2_(yadj, Myadj, offset, eta)

# Step-halving with three checks
Expand Down Expand Up @@ -273,7 +272,7 @@ get_index_list_ <- function(k.vars, data) {

# Compute score matrix ----

getScoreMatrix <- function(object) {
get_score_matrix_ <- function(object) {
# Extract required quantities from result list
control <- object[["control"]]
data <- object[["data"]]
Expand Down
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-07-04T20:13Z
last_built: 2024-07-16T00:21Z

8 changes: 4 additions & 4 deletions docs/reference/apes.html

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

4 changes: 2 additions & 2 deletions docs/reference/bias_corr.html

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

4 changes: 2 additions & 2 deletions docs/reference/feglm.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/felm.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/fenegbin.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/fepoisson.html

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

12 changes: 6 additions & 6 deletions docs/reference/vcov.feglm.html

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

12 changes: 6 additions & 6 deletions docs/reference/vcov.felm.html

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

2 changes: 1 addition & 1 deletion man/kendall_cor_test.Rd

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

2 changes: 1 addition & 1 deletion man/vcov.feglm.Rd

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

2 changes: 1 addition & 1 deletion man/vcov.felm.Rd

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

16 changes: 4 additions & 12 deletions src/00_main.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
// #include <omp.h>

#include <algorithm>
#include <armadillo.hpp>
#include <cmath>
#include <cpp11.hpp>
#include <cpp11armadillo.hpp>
// #include <iostream>
#include <numeric>
#include <vector>
#include "Rmath.h"

// #include <iostream>

using namespace arma;
using namespace cpp11;

// helpers used across scripts

#ifndef HELPERS_H
#define HELPERS_H

uvec as_uvec(const cpp11::integers &x);

#endif // HELPERS_H
5 changes: 2 additions & 3 deletions src/01_center_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ center_variables_(const doubles_matrix<> &V_r, const doubles &v_sum_r,
// Auxiliary variables (storage)
int iter, j, k, p, J;
double delta, meanj;
Mat<double> C(N, P);
Mat<double> x(N, 1);
Mat<double> x0(N, 1);

Expand Down Expand Up @@ -79,9 +78,9 @@ center_variables_(const doubles_matrix<> &V_r, const doubles &v_sum_r,
break;
}
}
C.col(p) = x;
V.col(p) = x;
}

// Return matrix with centered variables
return as_doubles_matrix(C);
return as_doubles_matrix(V);
}
Loading

0 comments on commit 5ff84c7

Please sign in to comment.