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 ccf2773 commit 1daa09a
Show file tree
Hide file tree
Showing 26 changed files with 158 additions and 182 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
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);
}
14 changes: 5 additions & 9 deletions src/02_get_alpha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}

// Start alternating between normal equations
field<Mat<double>> Alpha0(size(Alpha));
field<Mat<double>> Alpha0(K);

for (iter = 0; iter < 10000; ++iter) {
if ((iter % 1000) == 0) {
Expand All @@ -42,16 +42,16 @@

for (l = 0; l < K; ++l) {
if (l != k) {
list klist_l = klist[l];
const list &klist_l = klist[l];
for (int j = 0; j < list_sizes[l]; ++j) {
uvec indexes = as_uvec(as_cpp<integers>(klist_l[j]));
y(indexes) -= Alpha(l)(j);
}
}
}

list klist_k = as_cpp<list>(klist[k]);
Mat<double> alpha(list_sizes[k], 1);
const list &klist_k = as_cpp<list>(klist[k]);
Mat<double> &alpha = Alpha(k);

for (int j = 0; j < list_sizes[k]; ++j) {
// Subset the j-th group of category k
Expand All @@ -60,19 +60,15 @@
// Store group mean
alpha(j) = mean(y(indexes));
}

// Update alpha_k
Alpha(k) = alpha;
}

// Compute termination criterion and check convergence
num = 0.0;
denom = 0.0;
for (k = 0; k < K; ++k) {
Mat<double> diff = Alpha(k) - Alpha0(k);
const Mat<double> &diff = Alpha(k) - Alpha0(k);
num += accu(diff % diff);
denom += accu(Alpha0(k) % Alpha0(k));
Alpha0(k) = Alpha(k);
}

crit = sqrt(num / denom);
Expand Down
Loading

0 comments on commit 1daa09a

Please sign in to comment.