Fixing Compilation Errors and Creating an R Function from Rcpp Code

I try to implement RCPP code for the R function below. I m very new to C++. Below I put R function and the RCPP function. There errors occur in the RCPP in line 43,44,50,51,55.

#include <Rcpp.h>
using namespace Rcpp;

// Helper function to extract a row slice from a NumericMatrix
NumericVector extract_row_slice(const NumericMatrix& data, int row, int start, int end) {
  if (end >= data.ncol()) end = data.ncol() - 1;
  NumericVector slice(end - start + 1);
  for (int i = start; i <= end; i++) {
    slice[i - start] = data(row, i);
  }
  return slice;
}

// Main function to compute critical values
// [[Rcpp::export]]
NumericVector crit_val_cpp(int reps, int k, int n, double Quantile, double eps) {
  NumericVector W(reps);
  int sublength = n - 1;
  double n_squared = n * n;
  
  for (int j = 0; j < reps; j++) {
    NumericMatrix data(k, n, Rcpp::rnorm(n * k).begin());
    NumericVector substat(sublength);
    NumericVector scale(sublength);
    
    // Precompute the scaling factor for each t
    for (int i = 0; i < sublength; i++) {
      scale[i] = (i + 1) * (sublength - i) / n_squared;
    }
    
    for (int t = 0; t < sublength; t++) {
      NumericVector mean1(k), mean2(k);
      
      // Calculate means for each segment
      for (int i = 0; i < k; i++) {
        mean1[i] = mean(extract_row_slice(data, i, 0, t));
        mean2[i] = mean(extract_row_slice(data, i, t + 1, n - 1));
      }
      
      // Calculate inter1 and inter2
      NumericMatrix inter1(k, t + 1), inter2(k, n - t - 1);
      for (int h = 0; h < k; h++) {
        NumericVector temp1 = cumsum(extract_row_slice(data, h, 0, t)) - (seq_len(t + 1) - 1) * mean1[h];
        NumericVector temp2 = cumsum(extract_row_slice(data, h, t + 1, n - 1)) - (seq_len(n - t - 1) - 1) * mean2[h];
        std::copy(temp1.begin(), temp1.end(), inter1(h, _).begin());
        std::copy(temp2.begin(), temp2.end(), inter2(h, _).begin());
      }
      
      // Compute M1 and M2 matrices
      NumericMatrix M1 = inter1.t() * inter1 / n_squared;
      NumericMatrix M2 = inter2.t() * inter2 / n_squared;
      
      // Calculate substat[t] using the quadratic form
      NumericVector diff = scale[t] * (mean1 - mean2);
      substat[t] = n * crossprod(diff, solve(M1 + M2, diff));
    }
    
    // Store the maximum of the substatistics adjusted for eps
    int lower_idx = floor(n * eps);
    int upper_idx = ceil(n * (1 - eps)) - 1;
    W[j] = max(abs(substat[Range(lower_idx, upper_idx)]));
  }
  
  // Return W 
  return W;
}

This RCPP function returns W. I would use W to get the critical values as in the R script below. Here is the R script;

library(Rcpp)
crit.val <- function(reps, k, n, Quantile, eps){
  W         <- rep(0,reps)
  sublength <- n-1
  scale     <- (1:(n-1))/n^2*((n-1):1)
  
  for (j in 1:reps){
    data    <- matrix( rnorm(n*k, 0, 1), k, n )
    substat <- rep(0, sublength)
    for (t in 1:(n-1)){
      if (k==1){
        mean1 <- mean( data[1, 1:t] )
        mean2 <- mean( data[1, (1+t):n] )
      }
      if (k>1){
        mean1 <- apply( matrix(data[,     1:t], ncol = t),   1, mean )
        mean2 <- apply( matrix(data[, (t+1):n], ncol = n-t), 1, mean )
      }
      inter1 <- NULL
      inter2 <- NULL
      for (h in 1:k){
        inter1 <- rbind(inter1, cumsum( data[h, 1:t    ] ) - (1:t)*mean1[h])
        inter2 <- rbind(inter2, cumsum( data[h, n:(t+1)] ) - (1:(n-t))*mean2[h])
      }
      M1 <- inter1 %*% t(inter1)/n^2
      M2 <- inter2 %*% t(inter2)/n^2
      
      substat[t] <- n * matrix(scale[t]*(mean1 - mean2), 1, k) %*% solve(M1+M2) %*% 
                        matrix(scale[t]*(mean1 - mean2), k, 1) 
    }
    W[j] <- max( abs(substat)[ floor(n*eps):ceiling(n*(1-eps)) ] )
    print(j)
    print(Sys.time())
  }
  return( quantile( W, Quantile ) )
}


crit.val(reps=100, k=1, n=100, Quantile = c(0.9,0.95,0.99,0.995,0.999), eps=0.01)

Here are the errors in RCPP script;

-Line 43 invalid operands to binary expression ('sugar::Cumsum<14, true, Vector<14>>' and 'typename traits::enable_if<traits::is_convertible<typename traits::remove_const_and_reference<double>::type, typename traits::storage_type<13>::type>::value, sugar::Times_Vector_Primitive<13, true, Minus_Vector_Primitive<13, false, SeqLen>>>::type' (aka 'Rcpp::sugar::Times_Vector_Primitive<13, true, Rcpp::sugar::Minus_Vector_Primitive<13, false, Rcpp::sugar::SeqLen>>'))

Line 44 invalid operands to binary expression ('sugar::Cumsum<14, true, Vector<14>>' and 'typename traits::enable_if<traits::is_convertible<typename traits::remove_const_and_reference<double>::type, typename traits::storage_type<13>::type>::value, sugar::Times_Vector_Primitive<13, true, Minus_Vector_Primitive<13, false, SeqLen>>>::type' (aka 'Rcpp::sugar::Times_Vector_Primitive<13, true, Rcpp::sugar::Minus_Vector_Primitive<13, false, Rcpp::sugar::SeqLen>>'))

-Line 50 no member named 't' in 'Rcpp::Matrix<14>'
-Line 51no member named 't' in 'Rcpp::Matrix<14>'
-Line 55use of undeclared identifier 'solve'


I initially created a row_slicer function in Rcpp to handle specific errors successfully. However, I’ve encountered subsequent errors that I’m struggling to resolve, despite attempting to use various troubleshooting methods, including AI tools. My background is primarily in R, and I’m relatively new to C++, which is why I’m finding it challenging to address these issues independently.

I would greatly appreciate any guidance, comments, or assistance in troubleshooting and integrating my Rcpp code into a functional R function.

Thank you in advance for any help!

New contributor

user857222 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật