Skip to content

Commit

Permalink
Fixed C++ version of reg() (closes #28)
Browse files Browse the repository at this point in the history
The translation of the original R definition of `reg()` was done in
d4e3ef1, but that version incorrectly
assigns the slope of `new1` instead of the intercept to `beta01(e)`.
This is fixed here
  • Loading branch information
wleoncio committed Mar 1, 2024
1 parent 4ace926 commit 0a92a27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ multiples_of <- function(x, divisor, subset_out = FALSE) {
.Call(`_MADMMplasso_multiples_of`, x, divisor, subset_out)
}

lm_arma <- function(R, Z) {
.Call(`_MADMMplasso_lm_arma`, R, Z)
}

reg <- function(r, Z) {
.Call(`_MADMMplasso_reg`, r, Z)
}
Expand Down
13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// lm_arma
arma::vec lm_arma(const arma::vec& R, const arma::mat& Z);
RcppExport SEXP _MADMMplasso_lm_arma(SEXP RSEXP, SEXP ZSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const arma::vec& >::type R(RSEXP);
Rcpp::traits::input_parameter< const arma::mat& >::type Z(ZSEXP);
rcpp_result_gen = Rcpp::wrap(lm_arma(R, Z));
return rcpp_result_gen;
END_RCPP
}
// reg
Rcpp::List reg(const arma::mat r, const arma::mat Z);
RcppExport SEXP _MADMMplasso_reg(SEXP rSEXP, SEXP ZSEXP) {
Expand Down Expand Up @@ -143,6 +155,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_MADMMplasso_model_p", (DL_FUNC) &_MADMMplasso_model_p, 6},
{"_MADMMplasso_modulo", (DL_FUNC) &_MADMMplasso_modulo, 2},
{"_MADMMplasso_multiples_of", (DL_FUNC) &_MADMMplasso_multiples_of, 3},
{"_MADMMplasso_lm_arma", (DL_FUNC) &_MADMMplasso_lm_arma, 2},
{"_MADMMplasso_reg", (DL_FUNC) &_MADMMplasso_reg, 2},
{"_MADMMplasso_scale_cpp", (DL_FUNC) &_MADMMplasso_scale_cpp, 2},
{"_MADMMplasso_sqrt_sum_squared_rows", (DL_FUNC) &_MADMMplasso_sqrt_sum_squared_rows, 1},
Expand Down
16 changes: 14 additions & 2 deletions src/reg.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::vec lm_arma(const arma::vec &R, const arma::mat &Z) {
// Add a column of ones to Z
arma::mat Z_intercept = arma::join_rows(arma::ones<arma::vec>(Z.n_rows), Z);

// Solve the system of linear equations
arma::vec coefficients = arma::solve(Z_intercept, R);

return coefficients;
}

// [[Rcpp::export]]
Rcpp::List reg(
const arma::mat r,
Expand All @@ -10,9 +22,9 @@ Rcpp::List reg(
arma::mat theta01(Z.n_cols, r.n_cols, arma::fill::zeros);

for (arma::uword e = 0; e < r.n_cols; e++) {
arma::vec new1 = arma::solve(Z, r.col(e));
arma::vec new1 = lm_arma(r.col(e), Z);
beta01(e) = new1(0);
theta01.col(e) = new1.tail(new1.n_elem);
theta01.col(e) = new1.tail(new1.n_elem - 1);
}

Rcpp::List out = Rcpp::List::create(
Expand Down

0 comments on commit 0a92a27

Please sign in to comment.