Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:37:20+00:00 2026-06-15T14:37:20+00:00

I started playing around with Rcpp and would like to use the fastLm function

  • 0

I started playing around with Rcpp and would like to use the fastLm function as an example (also because it’s useful for potential later work). I know that fastLm is part of the RcppArmadillo package but I would like to compile it using sourceCpp. The code can be found here and is also below.
The first problem I encounter is that I can’t simply run sourceCpp("fastLm.cpp") in R after installing and loading Rcpp and RcppArmadillo. I get this error error: RcppArmadillo.h: No such file or directory and then all kind of things, which I guess follow from that.

The second issue is that I think I need to change some stuff in the fastLm.cpp. My changes are also below but I am sure something is missing or wrong. I included #include <Rcpp.h> and using namespace Rcpp; and // [[Rcpp::export]] to export the function to R and I changed the arguments from SEXP to NumericVector and NumericMatrix. I don’t see why that shouldn’t work and a similar adjustment is probably possible for the return value?

fastLm.cpp

#include <RcppArmadillo.h>

extern "C" SEXP fastLm(SEXP ys, SEXP Xs) {

    Rcpp::NumericVector yr(ys);                 // creates Rcpp vector from SEXP
    Rcpp::NumericMatrix Xr(Xs);                 // creates Rcpp matrix from SEXP
    int n = Xr.nrow(), k = Xr.ncol();

    arma::mat X(Xr.begin(), n, k, false);       // reuses memory and avoids extra copy
    arma::colvec y(yr.begin(), yr.size(), false);

    arma::colvec coef = arma::solve(X, y);      // fit model y ~ X
    arma::colvec resid = y - X*coef;            // residuals

    double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
                                                // std.error of estimate
    arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );

    return Rcpp::List::create(
        Rcpp::Named("coefficients") = coef,
        Rcpp::Named("stderr")       = stderrest
    ) ;

}

fastLm.cpp changed

#include <Rcpp.h>
#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
extern "C" SEXP fastLm(NumericVector yr, NumericMatrix Xr) {

    int n = Xr.nrow(), k = Xr.ncol();

    arma::mat X(Xr.begin(), n, k, false);       // reuses memory and avoids extra copy
    arma::colvec y(yr.begin(), yr.size(), false);

    arma::colvec coef = arma::solve(X, y);      // fit model y ~ X
    arma::colvec resid = y - X*coef;            // residuals

    double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
                                                // std.error of estimate
    arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );

    return Rcpp::List::create(
        Rcpp::Named("coefficients") = coef,
        Rcpp::Named("stderr")       = stderrest
    ) ;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:37:21+00:00Added an answer on June 15, 2026 at 2:37 pm

    You need to indicate dependency on RcppArmadillo with the Rcpp::depends pseudo attribute. This will take care of finding RcppArmadillo headers and link against blas, lapack etc …

    #include <RcppArmadillo.h>
    // [[Rcpp::depends(RcppArmadillo)]]
    
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    List fastLm(NumericVector yr, NumericMatrix Xr) {
    
        int n = Xr.nrow(), k = Xr.ncol();
    
        arma::mat X(Xr.begin(), n, k, false);       // reuses memory and avoids extra copy
        arma::colvec y(yr.begin(), yr.size(), false);
    
        arma::colvec coef = arma::solve(X, y);      // fit model y ~ X
        arma::colvec resid = y - X*coef;            // residuals
    
        double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
                                                    // std.error of estimate
        arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
    
        return Rcpp::List::create(
            Rcpp::Named("coefficients") = coef,
            Rcpp::Named("stderr")       = stderrest
        ) ;
    
    }
    

    Also, it is very important that you use #include <RcppArmadillo.h> and not #include <Rcpp.h>. RcppArmadillo.h takes care of including Rcpp.h at the right time, and order of include files is very important here.

    Also, you can return a List and drop the extern "C".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started playing around with OpenGL and GLUT. I would like to draw some
I just started playing around with neural networks and, as I would expect, in
I just started playing around with JSON and I have created this example. var
Just started playing around with ice_cube I've got a weekly schedule (with a granularity
I've started playing around with SourceMaps and am wondering if there's a way to
I just started playing around with threading today and I ran into something that
I just started playing around with the 960 CSS framework and found that it
I have started playing around with Apache camel recently. So being the experimental type
I've started playing around with transitions and have come up against quite a big
I've started playing around with CSS3 transitions in one of my projects, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.