From d998c0485df327a49c74a26f08d5589cbdbc44d3 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Sat, 2 Mar 2024 23:57:52 -0500 Subject: [PATCH] v0.3 --- DESCRIPTION | 2 +- NEWS.md | 4 + R/apes.R | 10 +- R/bias_corr.R | 6 +- R/cpp11.R | 28 ++- R/cpp11_default_parameters.R | 12 - R/generics_vcov.R | 9 +- R/helpers.R | 2 +- R/internals.R | 28 +-- README.Rmd | 10 +- README.md | 2 +- dev/benchmarks_tests_agtpa.R | 24 +- dev/benchmarks_tests_agtpa_capybara_only.R | 244 +++++++++++++++++++++ docs/404.html | 2 +- docs/LICENSE.html | 2 +- docs/articles/index.html | 2 +- docs/articles/intro.html | 2 +- docs/authors.html | 6 +- docs/index.html | 4 +- docs/news/index.html | 5 +- docs/pkgdown.yml | 2 +- docs/reference/apes.html | 4 +- docs/reference/bias_corr.html | 4 +- docs/reference/capybara-package.html | 2 +- docs/reference/feglm.html | 4 +- docs/reference/feglm_control.html | 2 +- docs/reference/felm.html | 4 +- docs/reference/fenegbin.html | 4 +- docs/reference/fepoisson.html | 4 +- docs/reference/fixed_effects.html | 2 +- docs/reference/index.html | 2 +- docs/reference/reexports.html | 2 +- docs/reference/summary.apes.html | 2 +- docs/reference/summary.feglm.html | 2 +- docs/reference/summary.felm.html | 2 +- docs/reference/trade_panel.html | 2 +- docs/reference/vcov.apes.html | 2 +- docs/reference/vcov.feglm.html | 2 +- docs/reference/vcov.felm.html | 2 +- src/01_center_variables.cpp | 7 +- src/02_solve_beta.cpp | 4 +- src/03_solve_eta.cpp | 34 +-- src/04_cross_products.cpp | 8 +- src/04_group_sums.cpp | 19 +- src/05_qr_rank.cpp | 2 +- src/cpp11.cpp | 68 +++++- 46 files changed, 456 insertions(+), 140 deletions(-) delete mode 100644 R/cpp11_default_parameters.R create mode 100644 dev/benchmarks_tests_agtpa_capybara_only.R diff --git a/DESCRIPTION b/DESCRIPTION index de7f52d..a5b2bfa 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: capybara Type: Package Title: Fast and Memory Efficient Fitting of Linear Models With High-Dimensional Fixed Effects -Version: 0.2 +Version: 0.3 Authors@R: c( person( given = "Mauricio", diff --git a/NEWS.md b/NEWS.md index cc7d824..112ac2b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# capybara 0.3 + +* Reduces memory footprint ~45% by moving some computation to Armadillo's side + # capybara 0.2 * Includes pseudo R2 (same as Stata) for Poisson models diff --git a/R/apes.R b/R/apes.R index 449e47f..d9e7d94 100644 --- a/R/apes.R +++ b/R/apes.R @@ -208,7 +208,7 @@ apes <- function( if (control[["keep.mx"]]) { MX <- object[["MX"]] } else { - MX <- center_variables_(X, w, k.list, control[["center.tol"]], 100000) + MX <- center_variables_(X, NA_real_, w, k.list, control[["center.tol"]], 100000L, FALSE) } # Compute average partial effects, derivatives, and Jacobian @@ -243,7 +243,7 @@ apes <- function( # Compute projection and residual projection of \Psi Psi <- -Delta1 / w - MPsi <- center_variables_(Psi, w, k.list, control[["center.tol"]], 100000L) + MPsi <- center_variables_(Psi, NA_real_, w, k.list, control[["center.tol"]], 100000L, FALSE) PPsi <- Psi - MPsi rm(Delta1, Psi) @@ -303,18 +303,18 @@ apes <- function( # Compute covariance matrix WinvJ <- solve(object[["Hessian"]] / nt.full, J) Gamma <- (MX %*% WinvJ - PPsi) * v / nt.full - V <- crossprod(Gamma) + V <- crossprod_(Gamma, NA_real_, FALSE, FALSE) if (adj > 0.0) { # Simplify covariance if sampling assumptions are imposed if (sampling.fe == "independence") { V <- V + adj * group_sums_var_(Delta, k.list[[1L]]) if (k > 1L) { - V <- V + adj * (group_sums_var_(Delta, k.list[[2L]]) - crossprod(Delta)) + V <- V + adj * (group_sums_var_(Delta, k.list[[2L]]) - crossprod_(Delta, NA_real_, FALSE, FALSE)) } if (panel.structure == "network") { if (k > 2L) { V <- V + adj * (group_sums_var_(Delta, k.list[[3L]]) - - crossprod(Delta)) + crossprod_(Delta, NA_real_, FALSE, FALSE)) } } } diff --git a/R/bias_corr.R b/R/bias_corr.R index 548a939..b92c2c4 100644 --- a/R/bias_corr.R +++ b/R/bias_corr.R @@ -150,7 +150,7 @@ bias_corr <- function( if (control[["keep.mx"]]) { MX <- object[["MX"]] } else { - MX <- center_variables_(X, w, k.list, control[["center.tol"]], 100000L) + MX <- center_variables_(X, NA_real_, w, k.list, control[["center.tol"]], 100000L, FALSE) } # Compute bias terms for requested bias correction @@ -200,11 +200,11 @@ bias_corr <- function( } # Update centered regressor matrix - MX <- center_variables_(X, w, k.list, control[["center.tol"]], 100000L) + MX <- center_variables_(X, NA_real_, w, k.list, control[["center.tol"]], 100000L, FALSE) colnames(MX) <- nms.sp # Update Hessian - H <- crossprod(MX * sqrt(w)) + H <- crossprod_(MX, w, TRUE, TRUE) dimnames(H) <- list(nms.sp, nms.sp) # Update result list diff --git a/R/cpp11.R b/R/cpp11.R index 557873f..90a85e0 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -1,13 +1,33 @@ # Generated by cpp11: do not edit by hand -center_variables_ <- function(V_r, w_r, klist, tol, maxiter) { - .Call(`_capybara_center_variables_`, V_r, w_r, klist, tol, maxiter) +center_variables_ <- function(V_r, v_sum_r, w_r, klist, tol, maxiter, sum_v) { + .Call(`_capybara_center_variables_`, V_r, v_sum_r, w_r, klist, tol, maxiter, sum_v) +} + +solve_beta_ <- function(mx, mnu, wtilde, epsilon, weighted) { + .Call(`_capybara_solve_beta_`, mx, mnu, wtilde, epsilon, weighted) } get_alpha_ <- function(p_r, klist, tol) { .Call(`_capybara_get_alpha_`, p_r, klist, tol) } +solve_eta_ <- function(mx, mnu, nu, beta) { + .Call(`_capybara_solve_eta_`, mx, mnu, nu, beta) +} + +solve_eta2_ <- function(yadj, myadj, offset, eta) { + .Call(`_capybara_solve_eta2_`, yadj, myadj, offset, eta) +} + +crossprod_ <- function(x, w, weighted, root_weights) { + .Call(`_capybara_crossprod_`, x, w, weighted, root_weights) +} + +chol_crossprod_ <- function(x) { + .Call(`_capybara_chol_crossprod_`, x) +} + group_sums_ <- function(M_r, w_r, jlist) { .Call(`_capybara_group_sums_`, M_r, w_r, jlist) } @@ -27,3 +47,7 @@ group_sums_cov_ <- function(M_r, N_r, jlist) { pairwise_cor_ <- function(y, yhat) { .Call(`_capybara_pairwise_cor_`, y, yhat) } + +qr_rank_ <- function(x) { + .Call(`_capybara_qr_rank_`, x) +} diff --git a/R/cpp11_default_parameters.R b/R/cpp11_default_parameters.R deleted file mode 100644 index e42e670..0000000 --- a/R/cpp11_default_parameters.R +++ /dev/null @@ -1,12 +0,0 @@ -# This file needs to be manually updated when the C++ code is changed. -# i.e., if cpp11.R is changed, this file needs to be updated. - -parse_int <- function(x) { - if (!is.integer(x)) x <- as.integer(x) - x -} - -parse_int_mat <- function(x) { - if (!(storage.mode(x) == "integer")) storage.mode(x) <- "integer" - x -} diff --git a/R/generics_vcov.R b/R/generics_vcov.R index b6704d8..3b80652 100644 --- a/R/generics_vcov.R +++ b/R/generics_vcov.R @@ -66,7 +66,7 @@ vcov.feglm <- function( G <- getScoreMatrix(object) if (type == "outer.product") { # Check if the OPG is invertible and compute its inverse - R <- try(chol(crossprod(G)), silent = TRUE) + R <- try(chol_crossprod_(G), silent = TRUE) if (inherits(R, "try-error")) { V <- matrix(Inf, p, p) } else { @@ -83,7 +83,7 @@ vcov.feglm <- function( # Compute inner part of the sandwich formula if (type == "sandwich") { - B <- crossprod(G) + B <- crossprod_(G, NA_real_, FALSE, FALSE) } else { if (isFALSE(k >= 1L)) { stop( @@ -128,10 +128,11 @@ vcov.feglm <- function( B.r <- matrix(0.0, p, p) for (j in seq.int(ncol(cl.combn))) { cl <- cl.combn[, j] - B.r <- B.r + crossprod( + B.r <- B.r + crossprod_( as.matrix( G[, lapply(.SD, sum), by = mget(cl), .SDcols = sp.vars][, (cl) := NULL] - ) + ), + NA_real_, FALSE, FALSE ) } diff --git a/R/helpers.R b/R/helpers.R index 38236e3..f1b07b0 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -224,7 +224,7 @@ model_response_ <- function(data, formula) { } check_linear_dependence_ <- function(X, p) { - if (qr(X)[["rank"]] < p) { + if (qr_rank_(X) < p) { stop("Linear dependent terms detected.", call. = FALSE) } diff --git a/R/internals.R b/R/internals.R index 7e2c7f6..b611300 100644 --- a/R/internals.R +++ b/R/internals.R @@ -21,10 +21,10 @@ felm_fit_ <- function(y, X, wt, k.list, control) { MX <- X # Centering variables - MX <- center_variables_(MX, wt, k.list, center.tol, 10000L) + MX <- center_variables_(MX, NA_real_, wt, k.list, center.tol, 10000L, FALSE) # Compute the OLS estimate - beta <- as.vector(qr.solve(MX, y, epsilon)) + beta <- solve_beta_(MX, y, NA_real_, epsilon, FALSE) # Generate result list reslist <- list( @@ -74,18 +74,20 @@ feglm_fit_ <- function(beta, eta, y, X, wt, k.list, family, control) { nu <- (y - mu) / mu.eta # Centering variables - Mnu <- center_variables_((Mnu + nu), w, k.list, center.tol, 10000L) - MX <- center_variables_(MX, w, k.list, center.tol, 10000L) + Mnu <- center_variables_(Mnu, nu, w, k.list, center.tol, 10000L, TRUE) + MX <- center_variables_(MX, NA_real_, w, k.list, center.tol, 10000L, FALSE) - # 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) + # Compute update step and update eta + beta.upd <- solve_beta_(MX, Mnu, w.tilde, epsilon, TRUE) + + eta.upd <- solve_eta_(MX, Mnu, nu, beta.upd) # Step-halving with three checks # 1. finite deviance # 2. valid \eta and \mu # 3. improvement as in glm2 rho <- 1.0 + for (inner.iter in seq.int(50L)) { eta <- eta.old + rho * eta.upd beta <- beta.old + rho * beta.upd @@ -141,9 +143,9 @@ feglm_fit_ <- function(beta, eta, y, X, wt, k.list, family, control) { w <- (wt * mu.eta^2) / family[["variance"]](mu) # Center variables - MX <- center_variables_(X, w, k.list, center.tol, 10000L) + MX <- center_variables_(X, NA_real_, w, k.list, center.tol, 10000L, FALSE) # Recompute Hessian - H <- crossprod(MX * sqrt(w)) + H <- crossprod_(MX, w, TRUE, TRUE) # Generate result list reslist <- list( @@ -219,8 +221,9 @@ feglm_offset_ <- function(object, offset) { yadj <- (y - mu) / mu.eta + eta - offset # Centering dependent variable and compute \eta update - Myadj <- center_variables_((Myadj + yadj), w, k.list, center.tol, 10000L) - eta.upd <- yadj - as.vector(Myadj) + offset - eta + Myadj <- center_variables_(Myadj, yadj, w, k.list, center.tol, 10000L, TRUE) + # eta.upd <- yadj - as.vector(Myadj) + offset - eta + eta.upd <- solve_eta2_(yadj, Myadj, offset, eta) # Step-halving with three checks # 1. finite deviance @@ -297,7 +300,7 @@ getScoreMatrix <- function(object) { attr(X, "dimnames") <- NULL # Center variables - MX <- center_variables_(X, w, k.list, control[["center.tol"]], 10000L) + MX <- center_variables_(X, NA_real_, w, k.list, control[["center.tol"]], 10000L, FALSE) colnames(MX) <- nms.sp } @@ -340,7 +343,6 @@ partial_mu_eta_ <- function(eta, family, order) { } } - # Returns suitable name for a temporary variable temp_var_ <- function(data) { repeat { diff --git a/README.Rmd b/README.Rmd index d30fa1d..b51b4cb 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,7 +18,7 @@ knitr::opts_chunk$set( [![R-CMD-check](https://github.com/pachadotdev/capybara/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/pachadotdev/capybara/actions/workflows/R-CMD-check.yaml) [![codecov](https://codecov.io/gh/pachadotdev/capybara/graph/badge.svg?token=kDP0pWmfRk)](https://codecov.io/gh/pachadotdev/capybara) -[![BuyMeACoffee](https://raw.githubusercontent.com/pachadotdev/buymeacoffee-badges/main/bmc-donate-white.svg)](https://www.buymeacoffee.com/pacha) +[![BuyMeACoffee](https://raw.githubusercontent.com/pachadotdev/buymeacoffee-badges/main/bmc-donate-yellow.svg)](https://www.buymeacoffee.com/pacha) ## About @@ -123,17 +123,17 @@ Median time for the different models in the book |package | PPML| Trade Diversion| Endogeneity| Reverse Causality| Non-linear/Phasing Effects| Globalization| |:------------|-------:|---------------:|-----------:|-----------------:|--------------------------:|-------------:| |Alpaca | 213.4ms| 2.3s| 1.35s| 1.86s| 2.59s| 4.96s| -|Base R | 1.5m| 1.53m| 23.43m| 23.52m| 23.16m| 24.85m| -|**Capybara** | 371ms| 3s| 1.34s| 1.71s| 2.46s| 4.64s| +|Base R | 1.5m | 1.53m| 23.43m| 23.52m| 23.16m| 24.85m| +|**Capybara** | 263ms | 2.98s| 1.43s| 1.78s| 2.58s| 4.51s| |Fixest | 67.4ms| 477.08ms| 95.88ms| 136.21ms| 206.12ms| 415.31ms| Memory allocation for the same models |package | PPML| Trade Diversion| Endogeneity| Reverse Causality| Non-linear/Phasing Effects| Globalization| |:------------|--------:|---------------:|-----------:|-----------------:|--------------------------:|-------------:| -|Alpaca | 304.8MB| 339.8MB| 306.3MB| 335.61MB| 393.86MB| 539.49MB| +|Alpaca | 304.8MB | 339.8MB| 306.3MB| 335.61MB| 393.86MB| 539.49MB| |Base R | 2.73GB| 2.6GB| 11.9GB| 11.94GB| 11.95GB| 11.97GB| -|**Capybara** | 307MB| 341MB| 306MB| 336MB| 395MB| 541MB| +|**Capybara** | 207MB | 231MB| 237MB| 244MB| 258MB| 293MB| |Fixest | 44.59MB| 36.59MB| 28.1MB| 32.43MB| 41.12MB| 62.87MB| # Debugging diff --git a/README.md b/README.md index cb6cdcf..b649545 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![R-CMD-check](https://github.com/pachadotdev/capybara/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/pachadotdev/capybara/actions/workflows/R-CMD-check.yaml) [![codecov](https://codecov.io/gh/pachadotdev/capybara/graph/badge.svg?token=kDP0pWmfRk)](https://codecov.io/gh/pachadotdev/capybara) -[![BuyMeACoffee](https://raw.githubusercontent.com/pachadotdev/buymeacoffee-badges/main/bmc-donate-white.svg)](https://www.buymeacoffee.com/pacha) +[![BuyMeACoffee](https://raw.githubusercontent.com/pachadotdev/buymeacoffee-badges/main/bmc-donate-yellow.svg)](https://www.buymeacoffee.com/pacha) ## About diff --git a/dev/benchmarks_tests_agtpa.R b/dev/benchmarks_tests_agtpa.R index fd3c1e6..6839b16 100644 --- a/dev/benchmarks_tests_agtpa.R +++ b/dev/benchmarks_tests_agtpa.R @@ -175,41 +175,41 @@ bench_phasing <- readRDS("dev/bench_phasing.rds") bench_globalization <- readRDS("dev/bench_globalization.rds") bench_ppml %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "PPML") %>% select(model, package, median) %>% pivot_wider(names_from = model, values_from = median) %>% left_join( bench_trade_diversion %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Trade Diversion") %>% select(model, package, median) %>% pivot_wider(names_from = model, values_from = median) ) %>% left_join( bench_endogeneity %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Endogeneity") %>% select(model, package, median) %>% pivot_wider(names_from = model, values_from = median) ) %>% left_join( bench_reverse_causality %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Reverse Causality") %>% select(model, package, median) %>% pivot_wider(names_from = model, values_from = median) ) %>% left_join( bench_phasing %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Non-linear/Phasing Effects") %>% select(model, package, median) %>% pivot_wider(names_from = model, values_from = median) ) %>% left_join( bench_globalization %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Globalization") %>% select(model, package, median) %>% pivot_wider(names_from = model, values_from = median) @@ -218,41 +218,41 @@ bench_ppml %>% kable() bench_ppml %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "PPML") %>% select(model, package, mem_alloc) %>% pivot_wider(names_from = model, values_from = mem_alloc) %>% left_join( bench_trade_diversion %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Trade Diversion") %>% select(model, package, mem_alloc) %>% pivot_wider(names_from = model, values_from = mem_alloc) ) %>% left_join( bench_endogeneity %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Endogeneity") %>% select(model, package, mem_alloc) %>% pivot_wider(names_from = model, values_from = mem_alloc) ) %>% left_join( bench_reverse_causality %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Reverse Causality") %>% select(model, package, mem_alloc) %>% pivot_wider(names_from = model, values_from = mem_alloc) ) %>% left_join( bench_phasing %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Non-linear/Phasing Effects") %>% select(model, package, mem_alloc) %>% pivot_wider(names_from = model, values_from = mem_alloc) ) %>% left_join( bench_globalization %>% - mutate(package = c("Base R", "Capybara", "Fixest", "Alpaca")) %>% + mutate(package = c("Base R", "**Capybara**", "Fixest", "Alpaca")) %>% mutate(model = "Globalization") %>% select(model, package, mem_alloc) %>% pivot_wider(names_from = model, values_from = mem_alloc) diff --git a/dev/benchmarks_tests_agtpa_capybara_only.R b/dev/benchmarks_tests_agtpa_capybara_only.R new file mode 100644 index 0000000..d52e47b --- /dev/null +++ b/dev/benchmarks_tests_agtpa_capybara_only.R @@ -0,0 +1,244 @@ +# this is not just about speed/memory, but also about obtaining the same +# slopes as in base R + +library(capybara) +library(dplyr) +library(tidyr) +library(janitor) +library(bench) + +rm(list = ls()) +gc() + +# data ---- + +ch1_application3 <- tradepolicy::agtpa_applications %>% + clean_names() %>% + filter(year %in% seq(1986, 2006, 4)) %>% + mutate( + exp_year = paste0(exporter, year), + imp_year = paste0(importer, year), + year = paste0("intl_border_", year), + log_trade = log(trade), + log_dist = log(dist), + intl_brdr = ifelse(exporter == importer, pair_id, "inter"), + intl_brdr_2 = ifelse(exporter == importer, 0, 1), + pair_id_2 = ifelse(exporter == importer, "0-intra", pair_id) + ) %>% + spread(year, intl_brdr_2, fill = 0) + +ch1_application3 <- ch1_application3 %>% + group_by(pair_id) %>% + mutate(sum_trade = sum(trade)) %>% + ungroup() + +# ppml ---- + +form <- trade ~ 0 + log_dist + cntg + lang + clny + + rta + exp_year + imp_year + +form2 <- trade ~ log_dist + cntg + lang + clny + + rta | exp_year + imp_year + +d <- filter(ch1_application3, importer != exporter) + +bench_ppml <- mark( + round(fepoisson(form2, data = d)$coefficients["rta"], 3) +) + +saveRDS(bench_ppml, "dev/bench_ppml.rds") + +rm(d) + +# trade diversion ---- + +form <- trade ~ 0 + log_dist + cntg + lang + clny + + rta + exp_year + imp_year + intl_brdr + +form2 <- trade ~ log_dist + cntg + lang + clny + + rta | exp_year + imp_year + intl_brdr + +d <- ch1_application3 + +bench_trade_diversion <- mark( + round(fepoisson(form2, data = d)$coefficients["rta"], 3) +) + +saveRDS(bench_trade_diversion, "dev/bench_trade_diversion.rds") + +rm(d) + +# endogeneity ---- + +form <- trade ~ 0 + rta + exp_year + imp_year + pair_id_2 +form2 <- trade ~ rta | exp_year + imp_year + pair_id_2 + +d <- filter(ch1_application3, sum_trade > 0) + +bench_endogeneity <- mark( + round(fepoisson(form2, data = d)$coefficients["rta"], 3) +) + +saveRDS(bench_endogeneity, "dev/bench_endogeneity.rds") + +rm(d) + +# reverse causality ---- + +form <- trade ~ 0 + rta + rta_lead4 + exp_year + imp_year + pair_id_2 +form2 <- trade ~ rta + rta_lead4 | exp_year + imp_year + pair_id_2 + +d <- filter(ch1_application3, sum_trade > 0) + +bench_reverse_causality <- mark( + round(fepoisson(form2, data = d)$coefficients["rta"], 3) +) + +saveRDS(bench_reverse_causality, "dev/bench_reverse_causality.rds") + +rm(d) + +# non-linear/phasing effects ---- + +form <- trade ~ 0 + rta + rta_lag4 + rta_lag8 + rta_lag12 + + exp_year + imp_year + pair_id_2 + +form2 <- trade ~ rta + rta_lag4 + rta_lag8 + rta_lag12 | + exp_year + imp_year + pair_id_2 + +d <- filter(ch1_application3, sum_trade > 0) + +bench_phasing <- mark( + round(fepoisson(form2, data = d)$coefficients["rta"], 3) +) + +saveRDS(bench_phasing, "dev/bench_phasing.rds") + +rm(d) + +# globalization ---- + +form <- trade ~ 0 + rta + rta_lag4 + rta_lag8 + rta_lag12 + + intl_border_1986 + intl_border_1990 + intl_border_1994 + + intl_border_1998 + intl_border_2002 + + exp_year + imp_year + pair_id_2 + +form2 <- trade ~ rta + rta_lag4 + rta_lag8 + rta_lag12 + + intl_border_1986 + intl_border_1990 + intl_border_1994 + + intl_border_1998 + intl_border_2002 | + exp_year + imp_year + pair_id_2 + +d <- filter(ch1_application3, sum_trade > 0) + +bench_globalization <- mark( + round(fepoisson(form2, data = d)$coefficients["rta"], 3) +) + +saveRDS(bench_globalization, "dev/bench_globalization.rds") + +rm(d, form, ch1_application3) + +rm(list = ls()) +gc() + +# create tables ---- + +library(knitr) + +bench_ppml <- readRDS("dev/bench_ppml.rds") + +bench_trade_diversion <- readRDS("dev/bench_trade_diversion.rds") + +bench_endogeneity <- readRDS("dev/bench_endogeneity.rds") + +bench_reverse_causality <- readRDS("dev/bench_reverse_causality.rds") + +bench_phasing <- readRDS("dev/bench_phasing.rds") + +bench_globalization <- readRDS("dev/bench_globalization.rds") + +bench_ppml %>% + mutate(package = "**Capybara**") %>% + mutate(model = "PPML") %>% + select(model, package, median) %>% + pivot_wider(names_from = model, values_from = median) %>% + left_join( + bench_trade_diversion %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Trade Diversion") %>% + select(model, package, median) %>% + pivot_wider(names_from = model, values_from = median) + ) %>% + left_join( + bench_endogeneity %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Endogeneity") %>% + select(model, package, median) %>% + pivot_wider(names_from = model, values_from = median) + ) %>% + left_join( + bench_reverse_causality %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Reverse Causality") %>% + select(model, package, median) %>% + pivot_wider(names_from = model, values_from = median) + ) %>% + left_join( + bench_phasing %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Non-linear/Phasing Effects") %>% + select(model, package, median) %>% + pivot_wider(names_from = model, values_from = median) + ) %>% + left_join( + bench_globalization %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Globalization") %>% + select(model, package, median) %>% + pivot_wider(names_from = model, values_from = median) + ) %>% + arrange(package) %>% + kable() + +bench_ppml %>% + mutate(package = "**Capybara**") %>% + mutate(model = "PPML") %>% + select(model, package, mem_alloc) %>% + pivot_wider(names_from = model, values_from = mem_alloc) %>% + left_join( + bench_trade_diversion %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Trade Diversion") %>% + select(model, package, mem_alloc) %>% + pivot_wider(names_from = model, values_from = mem_alloc) + ) %>% + left_join( + bench_endogeneity %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Endogeneity") %>% + select(model, package, mem_alloc) %>% + pivot_wider(names_from = model, values_from = mem_alloc) + ) %>% + left_join( + bench_reverse_causality %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Reverse Causality") %>% + select(model, package, mem_alloc) %>% + pivot_wider(names_from = model, values_from = mem_alloc) + ) %>% + left_join( + bench_phasing %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Non-linear/Phasing Effects") %>% + select(model, package, mem_alloc) %>% + pivot_wider(names_from = model, values_from = mem_alloc) + ) %>% + left_join( + bench_globalization %>% + mutate(package = "**Capybara**") %>% + mutate(model = "Globalization") %>% + select(model, package, mem_alloc) %>% + pivot_wider(names_from = model, values_from = mem_alloc) + ) %>% + arrange(package) %>% + kable() diff --git a/docs/404.html b/docs/404.html index 62c5e34..2ee8218 100644 --- a/docs/404.html +++ b/docs/404.html @@ -39,7 +39,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/LICENSE.html b/docs/LICENSE.html index db291f6..ed65617 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -17,7 +17,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/articles/index.html b/docs/articles/index.html index befebd4..f7c8d22 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -17,7 +17,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/articles/intro.html b/docs/articles/intro.html index af3f180..ac18f4a 100644 --- a/docs/articles/intro.html +++ b/docs/articles/intro.html @@ -40,7 +40,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/authors.html b/docs/authors.html index 0b4ad4a..2b4845a 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -17,7 +17,7 @@ capybara - 0.2 + 0.3 @@ -74,14 +74,14 @@

Citation

Vargas Sepulveda M (2024). capybara: Fast and Memory Efficient Fitting of Linear Models With High-Dimensional Fixed Effects. -R package version 0.2, https://github.com/pachadotdev/capybara, https://pacha.dev/capybara/. +R package version 0.3, https://github.com/pachadotdev/capybara, https://pacha.dev/capybara/.

@Manual{,
   title = {capybara: Fast and Memory Efficient Fitting of Linear Models With High-Dimensional
 Fixed Effects},
   author = {Mauricio {Vargas Sepulveda}},
   year = {2024},
-  note = {R package version 0.2, https://github.com/pachadotdev/capybara},
+  note = {R package version 0.3, https://github.com/pachadotdev/capybara},
   url = {https://pacha.dev/capybara/},
 }
diff --git a/docs/index.html b/docs/index.html index bc2e5fe..501e8ef 100644 --- a/docs/index.html +++ b/docs/index.html @@ -46,7 +46,7 @@ capybara - 0.2 + 0.3 @@ -369,7 +369,7 @@

Dev status

diff --git a/docs/news/index.html b/docs/news/index.html index 3bfe3eb..a8250ad 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -17,7 +17,7 @@ capybara - 0.2 + 0.3 @@ -57,6 +57,9 @@

Changelog

Source: NEWS.md +
+ +
diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index 92093a1..a438cc9 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -3,5 +3,5 @@ pkgdown: 2.0.7 pkgdown_sha: ~ articles: intro: intro.html -last_built: 2024-02-20T22:13Z +last_built: 2024-03-03T04:53Z diff --git a/docs/reference/apes.html b/docs/reference/apes.html index 15076f6..04995ab 100644 --- a/docs/reference/apes.html +++ b/docs/reference/apes.html @@ -26,7 +26,7 @@ capybara - 0.2 + 0.3 @@ -187,7 +187,7 @@

Examples

mod_bc <- bias_corr(mod) summary(mod_bc) #> Formula: trade ~ lang | year -#> <environment: 0x5f1772cbadc0> +#> <environment: 0x632e61885700> #> #> Family: Binomial #> diff --git a/docs/reference/bias_corr.html b/docs/reference/bias_corr.html index 727415c..cd523a4 100644 --- a/docs/reference/bias_corr.html +++ b/docs/reference/bias_corr.html @@ -24,7 +24,7 @@ capybara - 0.2 + 0.3 @@ -146,7 +146,7 @@

Examples

mod_bc <- bias_corr(mod) summary(mod_bc) #> Formula: trade ~ lang | year -#> <environment: 0x5f17725ab558> +#> <environment: 0x632e60ffc440> #> #> Family: Binomial #> diff --git a/docs/reference/capybara-package.html b/docs/reference/capybara-package.html index 08d6bf9..3a2113b 100644 --- a/docs/reference/capybara-package.html +++ b/docs/reference/capybara-package.html @@ -26,7 +26,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/feglm.html b/docs/reference/feglm.html index 8949fb9..f8ced82 100644 --- a/docs/reference/feglm.html +++ b/docs/reference/feglm.html @@ -22,7 +22,7 @@ capybara - 0.2 + 0.3 @@ -162,7 +162,7 @@

Examples

summary(mod) #> Formula: trade ~ log_dist + lang + cntg + clny | exp_year + imp_year -#> <environment: 0x5f17734c8290> +#> <environment: 0x632e61f1e548> #> #> Family: Poisson #> diff --git a/docs/reference/feglm_control.html b/docs/reference/feglm_control.html index 383d240..47d3166 100644 --- a/docs/reference/feglm_control.html +++ b/docs/reference/feglm_control.html @@ -18,7 +18,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/felm.html b/docs/reference/felm.html index 32e83e8..af4a065 100644 --- a/docs/reference/felm.html +++ b/docs/reference/felm.html @@ -18,7 +18,7 @@ capybara - 0.2 + 0.3 @@ -117,7 +117,7 @@

Examples

summary(mod) #> Formula: log(trade) ~ log_dist + lang + cntg + clny | exp_year + imp_year -#> <environment: 0x5f1773a65d90> +#> <environment: 0x632e625707f8> #> #> Estimates: #> diff --git a/docs/reference/fenegbin.html b/docs/reference/fenegbin.html index 83a0c37..d3a4263 100644 --- a/docs/reference/fenegbin.html +++ b/docs/reference/fenegbin.html @@ -19,7 +19,7 @@ capybara - 0.2 + 0.3 @@ -135,7 +135,7 @@

Examples

summary(mod) #> Formula: trade ~ log_dist + lang + cntg + clny | exp_year + imp_year -#> <environment: 0x5f17745b1e90> +#> <environment: 0x632e62b8a270> #> #> Family: Negative Binomial(1.1839) #> diff --git a/docs/reference/fepoisson.html b/docs/reference/fepoisson.html index 1b4d7db..e98a37e 100644 --- a/docs/reference/fepoisson.html +++ b/docs/reference/fepoisson.html @@ -18,7 +18,7 @@ capybara - 0.2 + 0.3 @@ -122,7 +122,7 @@

Examples

summary(mod) #> Formula: trade ~ log_dist + lang + cntg + clny | exp_year + imp_year -#> <environment: 0x5f177302d9a8> +#> <environment: 0x632e63368e28> #> #> Family: Poisson #> diff --git a/docs/reference/fixed_effects.html b/docs/reference/fixed_effects.html index 8ea26ab..bf1d47a 100644 --- a/docs/reference/fixed_effects.html +++ b/docs/reference/fixed_effects.html @@ -20,7 +20,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/index.html b/docs/reference/index.html index 3355c69..01e70c3 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -17,7 +17,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/reexports.html b/docs/reference/reexports.html index 146fb6e..4862463 100644 --- a/docs/reference/reexports.html +++ b/docs/reference/reexports.html @@ -24,7 +24,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/summary.apes.html b/docs/reference/summary.apes.html index 5dc51ee..79872ac 100644 --- a/docs/reference/summary.apes.html +++ b/docs/reference/summary.apes.html @@ -18,7 +18,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/summary.feglm.html b/docs/reference/summary.feglm.html index 622bd6a..5c11811 100644 --- a/docs/reference/summary.feglm.html +++ b/docs/reference/summary.feglm.html @@ -19,7 +19,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/summary.felm.html b/docs/reference/summary.felm.html index 41c6217..26a4098 100644 --- a/docs/reference/summary.felm.html +++ b/docs/reference/summary.felm.html @@ -19,7 +19,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/trade_panel.html b/docs/reference/trade_panel.html index d08a1e1..9134e42 100644 --- a/docs/reference/trade_panel.html +++ b/docs/reference/trade_panel.html @@ -17,7 +17,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/vcov.apes.html b/docs/reference/vcov.apes.html index c945cec..64b0e98 100644 --- a/docs/reference/vcov.apes.html +++ b/docs/reference/vcov.apes.html @@ -18,7 +18,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/vcov.feglm.html b/docs/reference/vcov.feglm.html index a7194ab..2baf928 100644 --- a/docs/reference/vcov.feglm.html +++ b/docs/reference/vcov.feglm.html @@ -19,7 +19,7 @@ capybara - 0.2 + 0.3 diff --git a/docs/reference/vcov.felm.html b/docs/reference/vcov.felm.html index c3276c0..c0f44cf 100644 --- a/docs/reference/vcov.felm.html +++ b/docs/reference/vcov.felm.html @@ -19,7 +19,7 @@ capybara - 0.2 + 0.3 diff --git a/src/01_center_variables.cpp b/src/01_center_variables.cpp index 1b41236..33bd57f 100644 --- a/src/01_center_variables.cpp +++ b/src/01_center_variables.cpp @@ -1,9 +1,10 @@ #include "00_main.h" // Method of alternating projections (Halperin) -[[cpp11::register]] doubles_matrix<> center_variables_( - const doubles_matrix<> &V_r, const doubles &v_sum_r, const doubles &w_r, - const list &klist, const double tol, const int maxiter, bool sum_v) { +[[cpp11::register]] doubles_matrix<> +center_variables_(const doubles_matrix<> &V_r, const doubles &v_sum_r, + const doubles &w_r, const list &klist, const double tol, + const int maxiter, bool sum_v) { // Types conversion Mat V = as_Mat(V_r); Col w = as_Col(w_r); diff --git a/src/02_solve_beta.cpp b/src/02_solve_beta.cpp index 290b3a7..1690b4c 100644 --- a/src/02_solve_beta.cpp +++ b/src/02_solve_beta.cpp @@ -1,7 +1,7 @@ #include "00_main.h" -[[cpp11::register]] doubles solve_beta_(const doubles_matrix<>& mx, - const doubles_matrix<>& mnu, +[[cpp11::register]] doubles solve_beta_(const doubles_matrix<> &mx, + const doubles_matrix<> &mnu, const doubles wtilde, double epsilon, bool weighted) { // Types conversion diff --git a/src/03_solve_eta.cpp b/src/03_solve_eta.cpp index b658658..d4ba2ef 100644 --- a/src/03_solve_eta.cpp +++ b/src/03_solve_eta.cpp @@ -2,33 +2,33 @@ #include "00_main.h" -[[cpp11::register]] doubles solve_eta_(const doubles_matrix<>& mx, - const doubles_matrix<>& mnu, - const doubles& nu, const doubles& beta) { +[[cpp11::register]] doubles solve_eta_(const doubles_matrix<> &mx, + const doubles_matrix<> &mnu, + const doubles &nu, const doubles &beta) { // Types conversion Mat MX = as_Mat(mx); Mat Mnu = as_Mat(mnu); Col Nu = as_Col(nu); Col Beta = as_Col(beta); - Col Eta = Nu - (Mnu - (MX * Beta)); + Col res = Nu - (Mnu - (MX * Beta)); - return as_doubles(Eta); + return as_doubles(res); } // eta.upd <- yadj - as.vector(Myadj) + offset - eta -// [[cpp11::register]] doubles solve_eta2_(const doubles& yadj, -// const doubles_matrix<>& myadj, -// const doubles& offset, -// const doubles& eta) { -// // Types conversion -// Col Yadj = as_Col(yadj); -// Mat Myadj = as_Mat(myadj); -// Col Offset = as_Col(offset); -// Col Eta = as_Col(eta); +[[cpp11::register]] doubles solve_eta2_(const doubles &yadj, + const doubles_matrix<> &myadj, + const doubles &offset, + const doubles &eta) { + // Types conversion + Col Yadj = as_Col(yadj); + Col Myadj = as_Mat(myadj); + Col Offset = as_Col(offset); + Col Eta = as_Col(eta); -// Col res = Yadj - Myadj + Offset - Eta; + Col res = Yadj - Myadj + Offset - Eta; -// return as_doubles(res); -// } + return as_doubles(res); +} diff --git a/src/04_cross_products.cpp b/src/04_cross_products.cpp index 39a1f36..507da2f 100644 --- a/src/04_cross_products.cpp +++ b/src/04_cross_products.cpp @@ -3,8 +3,8 @@ #include "00_main.h" -[[cpp11::register]] doubles_matrix<> crossprod_(const doubles_matrix<>& x, - const doubles& w, bool weighted, +[[cpp11::register]] doubles_matrix<> crossprod_(const doubles_matrix<> &x, + const doubles &w, bool weighted, bool root_weights) { // Types conversion Mat X = as_Mat(x); @@ -28,8 +28,8 @@ // chol(crossprod(X)) -[[cpp11::register]] doubles_matrix<> chol_crossprod_( - const doubles_matrix<>& x) { +[[cpp11::register]] doubles_matrix<> +chol_crossprod_(const doubles_matrix<> &x) { // Types conversion Mat X = as_Mat(x); diff --git a/src/04_group_sums.cpp b/src/04_group_sums.cpp index 499f7d1..4ef8cdc 100644 --- a/src/04_group_sums.cpp +++ b/src/04_group_sums.cpp @@ -42,9 +42,10 @@ return as_doubles_matrix(num); } -[[cpp11::register]] doubles_matrix<> group_sums_spectral_( - const doubles_matrix<> &M_r, const doubles_matrix<> &v_r, - const doubles_matrix<> &w_r, const int K, const list &jlist) { +[[cpp11::register]] doubles_matrix<> +group_sums_spectral_(const doubles_matrix<> &M_r, const doubles_matrix<> &v_r, + const doubles_matrix<> &w_r, const int K, + const list &jlist) { // Types conversion Mat M = as_Mat(M_r); Mat v = as_Mat(v_r); @@ -95,8 +96,8 @@ return as_doubles_matrix(num); } -[[cpp11::register]] doubles_matrix<> group_sums_var_( - const doubles_matrix<> &M_r, const list &jlist) { +[[cpp11::register]] doubles_matrix<> +group_sums_var_(const doubles_matrix<> &M_r, const list &jlist) { // Types conversion Mat M = as_Mat(M_r); @@ -139,9 +140,9 @@ return as_doubles_matrix(V); } -[[cpp11::register]] doubles_matrix<> group_sums_cov_( - const doubles_matrix<> &M_r, const doubles_matrix<> &N_r, - const list &jlist) { +[[cpp11::register]] doubles_matrix<> +group_sums_cov_(const doubles_matrix<> &M_r, const doubles_matrix<> &N_r, + const list &jlist) { // Types conversion Mat M = as_Mat(M_r); Mat N = as_Mat(N_r); @@ -150,7 +151,7 @@ const int J = jlist.size(); const int P = M.n_cols; const int I = as_cpp(jlist[0]) - .size(); // assuming all groups have the same size + .size(); // assuming all groups have the same size // Auxiliary variables (storage) Mat V(P, P); diff --git a/src/05_qr_rank.cpp b/src/05_qr_rank.cpp index e160842..f0811ad 100644 --- a/src/05_qr_rank.cpp +++ b/src/05_qr_rank.cpp @@ -1,6 +1,6 @@ #include "00_main.h" -[[cpp11::register]] int qr_rank_(const doubles_matrix<>& x) { +[[cpp11::register]] int qr_rank_(const doubles_matrix<> &x) { Mat X = as_Mat(x); Mat Q; diff --git a/src/cpp11.cpp b/src/cpp11.cpp index e4fb7c3..117b92f 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -6,64 +6,112 @@ #include // 01_center_variables.cpp -doubles_matrix<> center_variables_(const doubles_matrix<> & V_r, const doubles & w_r, const list & klist, const double tol, const int maxiter); -extern "C" SEXP _capybara_center_variables_(SEXP V_r, SEXP w_r, SEXP klist, SEXP tol, SEXP maxiter) { +doubles_matrix<> center_variables_(const doubles_matrix<> & V_r, const doubles & v_sum_r, const doubles & w_r, const list & klist, const double tol, const int maxiter, bool sum_v); +extern "C" SEXP _capybara_center_variables_(SEXP V_r, SEXP v_sum_r, SEXP w_r, SEXP klist, SEXP tol, SEXP maxiter, SEXP sum_v) { BEGIN_CPP11 - return cpp11::as_sexp(center_variables_(cpp11::as_cpp &>>(V_r), cpp11::as_cpp>(w_r), cpp11::as_cpp>(klist), cpp11::as_cpp>(tol), cpp11::as_cpp>(maxiter))); + return cpp11::as_sexp(center_variables_(cpp11::as_cpp &>>(V_r), cpp11::as_cpp>(v_sum_r), cpp11::as_cpp>(w_r), cpp11::as_cpp>(klist), cpp11::as_cpp>(tol), cpp11::as_cpp>(maxiter), cpp11::as_cpp>(sum_v))); END_CPP11 } -// 02_get_alpha.cpp +// 02_solve_beta.cpp +doubles solve_beta_(const doubles_matrix<>& mx, const doubles_matrix<>& mnu, const doubles wtilde, double epsilon, bool weighted); +extern "C" SEXP _capybara_solve_beta_(SEXP mx, SEXP mnu, SEXP wtilde, SEXP epsilon, SEXP weighted) { + BEGIN_CPP11 + return cpp11::as_sexp(solve_beta_(cpp11::as_cpp&>>(mx), cpp11::as_cpp&>>(mnu), cpp11::as_cpp>(wtilde), cpp11::as_cpp>(epsilon), cpp11::as_cpp>(weighted))); + END_CPP11 +} +// 03_get_alpha.cpp list get_alpha_(const doubles_matrix<> & p_r, const list & klist, const double tol); extern "C" SEXP _capybara_get_alpha_(SEXP p_r, SEXP klist, SEXP tol) { BEGIN_CPP11 return cpp11::as_sexp(get_alpha_(cpp11::as_cpp &>>(p_r), cpp11::as_cpp>(klist), cpp11::as_cpp>(tol))); END_CPP11 } -// 03_group_sums.cpp +// 03_solve_eta.cpp +doubles solve_eta_(const doubles_matrix<>& mx, const doubles_matrix<>& mnu, const doubles& nu, const doubles& beta); +extern "C" SEXP _capybara_solve_eta_(SEXP mx, SEXP mnu, SEXP nu, SEXP beta) { + BEGIN_CPP11 + return cpp11::as_sexp(solve_eta_(cpp11::as_cpp&>>(mx), cpp11::as_cpp&>>(mnu), cpp11::as_cpp>(nu), cpp11::as_cpp>(beta))); + END_CPP11 +} +// 03_solve_eta.cpp +doubles solve_eta2_(const doubles& yadj, const doubles_matrix<>& myadj, const doubles& offset, const doubles& eta); +extern "C" SEXP _capybara_solve_eta2_(SEXP yadj, SEXP myadj, SEXP offset, SEXP eta) { + BEGIN_CPP11 + return cpp11::as_sexp(solve_eta2_(cpp11::as_cpp>(yadj), cpp11::as_cpp&>>(myadj), cpp11::as_cpp>(offset), cpp11::as_cpp>(eta))); + END_CPP11 +} +// 04_cross_products.cpp +doubles_matrix<> crossprod_(const doubles_matrix<>& x, const doubles& w, bool weighted, bool root_weights); +extern "C" SEXP _capybara_crossprod_(SEXP x, SEXP w, SEXP weighted, SEXP root_weights) { + BEGIN_CPP11 + return cpp11::as_sexp(crossprod_(cpp11::as_cpp&>>(x), cpp11::as_cpp>(w), cpp11::as_cpp>(weighted), cpp11::as_cpp>(root_weights))); + END_CPP11 +} +// 04_cross_products.cpp +doubles_matrix<> chol_crossprod_(const doubles_matrix<>& x); +extern "C" SEXP _capybara_chol_crossprod_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(chol_crossprod_(cpp11::as_cpp&>>(x))); + END_CPP11 +} +// 04_group_sums.cpp doubles_matrix<> group_sums_(const doubles_matrix<> & M_r, const doubles_matrix<> & w_r, const list & jlist); extern "C" SEXP _capybara_group_sums_(SEXP M_r, SEXP w_r, SEXP jlist) { BEGIN_CPP11 return cpp11::as_sexp(group_sums_(cpp11::as_cpp &>>(M_r), cpp11::as_cpp &>>(w_r), cpp11::as_cpp>(jlist))); END_CPP11 } -// 03_group_sums.cpp +// 04_group_sums.cpp doubles_matrix<> group_sums_spectral_(const doubles_matrix<> & M_r, const doubles_matrix<> & v_r, const doubles_matrix<> & w_r, const int K, const list & jlist); extern "C" SEXP _capybara_group_sums_spectral_(SEXP M_r, SEXP v_r, SEXP w_r, SEXP K, SEXP jlist) { BEGIN_CPP11 return cpp11::as_sexp(group_sums_spectral_(cpp11::as_cpp &>>(M_r), cpp11::as_cpp &>>(v_r), cpp11::as_cpp &>>(w_r), cpp11::as_cpp>(K), cpp11::as_cpp>(jlist))); END_CPP11 } -// 03_group_sums.cpp +// 04_group_sums.cpp doubles_matrix<> group_sums_var_(const doubles_matrix<> & M_r, const list & jlist); extern "C" SEXP _capybara_group_sums_var_(SEXP M_r, SEXP jlist) { BEGIN_CPP11 return cpp11::as_sexp(group_sums_var_(cpp11::as_cpp &>>(M_r), cpp11::as_cpp>(jlist))); END_CPP11 } -// 03_group_sums.cpp +// 04_group_sums.cpp doubles_matrix<> group_sums_cov_(const doubles_matrix<> & M_r, const doubles_matrix<> & N_r, const list & jlist); extern "C" SEXP _capybara_group_sums_cov_(SEXP M_r, SEXP N_r, SEXP jlist) { BEGIN_CPP11 return cpp11::as_sexp(group_sums_cov_(cpp11::as_cpp &>>(M_r), cpp11::as_cpp &>>(N_r), cpp11::as_cpp>(jlist))); END_CPP11 } -// 04_pairwise_correlation.cpp +// 05_pairwise_correlation.cpp double pairwise_cor_(const doubles & y, const doubles & yhat); extern "C" SEXP _capybara_pairwise_cor_(SEXP y, SEXP yhat) { BEGIN_CPP11 return cpp11::as_sexp(pairwise_cor_(cpp11::as_cpp>(y), cpp11::as_cpp>(yhat))); END_CPP11 } +// 05_qr_rank.cpp +int qr_rank_(const doubles_matrix<>& x); +extern "C" SEXP _capybara_qr_rank_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(qr_rank_(cpp11::as_cpp&>>(x))); + END_CPP11 +} extern "C" { static const R_CallMethodDef CallEntries[] = { - {"_capybara_center_variables_", (DL_FUNC) &_capybara_center_variables_, 5}, + {"_capybara_center_variables_", (DL_FUNC) &_capybara_center_variables_, 7}, + {"_capybara_chol_crossprod_", (DL_FUNC) &_capybara_chol_crossprod_, 1}, + {"_capybara_crossprod_", (DL_FUNC) &_capybara_crossprod_, 4}, {"_capybara_get_alpha_", (DL_FUNC) &_capybara_get_alpha_, 3}, {"_capybara_group_sums_", (DL_FUNC) &_capybara_group_sums_, 3}, {"_capybara_group_sums_cov_", (DL_FUNC) &_capybara_group_sums_cov_, 3}, {"_capybara_group_sums_spectral_", (DL_FUNC) &_capybara_group_sums_spectral_, 5}, {"_capybara_group_sums_var_", (DL_FUNC) &_capybara_group_sums_var_, 2}, {"_capybara_pairwise_cor_", (DL_FUNC) &_capybara_pairwise_cor_, 2}, + {"_capybara_qr_rank_", (DL_FUNC) &_capybara_qr_rank_, 1}, + {"_capybara_solve_beta_", (DL_FUNC) &_capybara_solve_beta_, 5}, + {"_capybara_solve_eta2_", (DL_FUNC) &_capybara_solve_eta2_, 4}, + {"_capybara_solve_eta_", (DL_FUNC) &_capybara_solve_eta_, 4}, {NULL, NULL, 0} }; }