Skip to content

Get the spline design matrix in Rcpp #16

Answered by wenjie2wang
mengqi00 asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for posting the question here. A simple example is as follows:

// example.cpp

#include <RcppArmadillo.h>
#include <splines2Armadillo.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(splines2)]]

// [[Rcpp::export]]
arma::mat splineDesign2(const arma::vec& knot_seq,
                        const arma::vec& x,
                        const unsigned int ord = 4,
                        const unsigned int derivs = 0)
{
    splines2::BSpline obj { x, ord - 1, knot_seq };
    if (derivs > 0) {
        return obj.derivative(derivs);
    }
    return obj.basis();
}
# example.R

Rcpp::sourceCpp("example.cpp")

set.seed(123)
x <- runif(100, 0, 10)
ord <- 4

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mengqi00
Comment options

@wenjie2wang
Comment options

Answer selected by wenjie2wang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants