wenjie2wang / splines2

Regression Spline Functions and Classes
https://wwenjie.org/splines2
GNU General Public License v3.0
42 stars 3 forks source link

function 'Rcpp_precious_remove' not provided by package 'Rcpp' #13

Closed Capsellab closed 3 years ago

Capsellab commented 3 years ago

Hi, thaks for using a useful package. But I got an error in splines2 version 0.4.5

Error in rcpp_bSpline(x = xx, df = df, degree = degree, internal_knots = knots,  : 
  function 'Rcpp_precious_remove' not provided by package 'Rcpp'

In my R environment, Rccp is version 1.0.6 , RccpArmagillo is version 0.10.7.0.0 amd aspline is version 0.1.0. It ’s an error that wasn’t before version 0.4.4 .

my source code is

```{r}
# load the library
library(ggplot2)

# configure multicore
library(doParallel)
cl <- makePSOCKcluster(4)
registerDoParallel(cl)

library(aspline)
library(tidyverse)
library(splines2)
data(helmet)
x <- data.frame(x=helmet$x)
y <- data.frame(y=helmet$y)

pen <- 10 ^ seq(-4, 4, 0.25)

# tuning grid parameters
k <- c(10,20,30,40,50,60)
degree <- c(2,3,4)

# cross validation parameters
initialWindow <- 65
horizon <- 1 # 1 is desirable
fixedWindow <- TRUE

# custom model object
aspline_func <- function(y, x, pen, k, degree) {
    knots <- seq(min(x), max(x), length = k + 2)[-c(1, k + 2)]
    x_seq <- seq(min(x), max(x), length = length(x) + k + 2)
    aridge <- aridge_solver(x, y, knots, pen, degree = degree)
    a_fit <- lm(y ~ splines2::bSpline(x, knots = aridge$knots_sel[[which.min(aridge$ebic)]], degree = degree))
    a_predict <- data_frame(x = x_seq, pred = predict(a_fit, data.frame(x = x_seq)))
    return(a_predict)
}

# custom time series cross validation
tscv_func <- function(y, forecastfunction, initialWindow = 1, horizon = 1, fixedWindow = TRUE, x, pen, k, degree) {
    n <- nrow(y)
    result <- matrix(NA_real_, nrow = n - horizon, ncol = horizon)
    if((initialWindow + horizon) >= n) stop("initial window is too long.")
    if (is.null(x)) {
        x <- seq(1, n, by = 1L)
    } else {
        if(nrow(x) != n) stop("x must be of the same size as y")
    }
    if (fixedWindow) {
        indx <- seq(initialWindow, n - horizon, by = 1L)
    } else {
        indx <- seq(1, n - horizon) # all start at 1
    }
    for (i in indx) {
        if(i - initialWindow < 0L) stop("initial window is too large.")
        y_subset <- y[(i - initialWindow + 1L):i,]
        x_subset <- x[(i - initialWindow + 1L):i,]
        fc <- try(suppressWarnings(
            forecastfunction(y = y_subset, x = x_subset, pen = pen, k = k, degree = degree)
        ), silent = TRUE)
        print(fc)
        if (!is.element("try-error", class(fc))) {
            result[i, ] <- tail(fc, n=1)
        } 
    }
    return(result)
}

# custom tuning grid loop
for (h in k) {
    for (j in degree) {
        lastknots <- tscv_func(y = y, aspline_func, initialWindow = initialWindow, horizon = horizon, fixedWindow = fixedWindow, x = x, pen = pen, k = h, degree = j)
        print(lastknots)
    }
}

Thanks.
wenjie2wang commented 3 years ago

@Capsellab Did you try updating the {Rcpp} package to its latest version? See e.g., https://stackoverflow.com/questions/68416435/rcpp-package-doesnt-include-rcpp-precious-remove

Capsellab commented 3 years ago

@wenjie2wang thanks, With Rcpp version 1.0.7, the error is gone. I'm really thankful to you.