Rcpp函数中不允许负长度向量

我正在使用Rcpp编写一些函数来完成论文工作。 cpp文件“ all_functions.cpp”如下所示:

// [[Rcpp::plugins("cpp11")]]
#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector get_extrema_cpp(NumericVector A, NumericMatrix X, NumericVector Y,
                              NumericVector fitted_prob, double gamma = 0.0, 
                              CharacterVector estimand = "all") {
  Environment base("package:base");
  Function order = base["order"];
  IntegerVector c = ifelse(estimand == CharacterVector("all"), 1, 0);

  NumericVector eg = exp(-qlogis(fitted_prob));
  Y = Y[A == 1];
  eg = eg[A == 1];
  IntegerVector v = order(-Y);
  eg = eg[v - 1];
  Y = Y[v - 1];

  //maximization
  NumericVector num_each_low = Y * (c[0] + exp(-gamma) * eg);
  NumericVector num_each_up = Y * (c[0] + exp(gamma) * eg);

  NumericVector num_up_max = cumsum(num_each_up);
  NumericVector num_low_max = rev(NumericVector(cumsum(NumericVector(rev(num_each_low)))));
  num_up_max.push_front(0), num_low_max.push_back(0);
  NumericVector num_max = num_up_max + num_low_max;

  NumericVector den_each_low = (c[0] + exp(-gamma) * eg);
  NumericVector den_each_up = (c[0] + exp(gamma) * eg);

  NumericVector den_up_max = cumsum(den_each_up);
  NumericVector den_low_max = rev(NumericVector(cumsum(NumericVector(rev(den_each_low)))));
  den_up_max.push_front(0), den_low_max.push_back(0);
  NumericVector den_max = den_up_max + den_low_max;

  double maximum = max(NumericVector(num_max / den_max));

  //minimization
  NumericVector num_low_min = cumsum(num_each_low);
  NumericVector num_up_min = rev(NumericVector(cumsum(NumericVector(rev(num_each_up)))));
  num_low_min.push_front(0), num_up_min.push_back(0);
  NumericVector num_min = num_up_min + num_low_min;

  NumericVector den_low_min = cumsum(den_each_low);
  NumericVector den_up_min = rev(NumericVector(cumsum(NumericVector(rev(den_each_up)))));
  den_low_min.push_front(0), den_up_min.push_back(0);
  NumericVector den_min = den_up_min + den_low_min;

  double minimum = min(NumericVector(num_min / den_min));
  return NumericVector({minimum, maximum});
}


// [[Rcpp::export]]
NumericVector extrema_md_cpp(NumericVector A, NumericMatrix X, NumericVector Y,
                             double gamma = 0.0, CharacterVector estimand = "all",
                             bool reg_adjust = false,
                             NumericVector start = NA_REAL) {
  Environment RcppNumerical = Environment::namespace_env("RcppNumerical");
  Environment stats("package:stats");

  Function fastLR = RcppNumerical["fastLR"];
  Function lm_fit = stats["lm.fit"];

  int nrows = Y.size();
  List ps_model;

  if(is_na(start)[0]) {
    ps_model = fastLR(X, A);
  } else {
    ps_model = fastLR(X, A, start);
  }

  NumericVector fitted_prob = plogis(as<NumericVector>(ps_model["linear.predictors"]));
  NumericVector Y_fitted;
  if(reg_adjust) {
    List out_model = lm_fit(X, Y);
    Y_fitted = as<NumericVector>(out_model["fitted.values"]);
  } else {
    Y_fitted = rep(NumericVector({0}), nrows);
  }

  NumericVector out = get_extrema_cpp(A, X, Y - Y_fitted, fitted_prob,
                                      gamma, estimand);

  if(estimand[0] == "all") {
    out = out + mean(Y_fitted);
  } else {
    out = out + mean(as<NumericVector>(Y_fitted[A == 0]));
  }

  return out;
}


// [[Rcpp::export]]
NumericVector bootsens_md_cpp(NumericVector A, NumericMatrix X, NumericVector Y,
                              double gamma = 0.0, CharacterVector estimand = "all",
                              bool reg_adjust = false, double alpha = 0.05,
                              NumericVector start = NA_REAL, int B = 1000) {
  Environment base("package:base"), stats("package:stats");
  Function sample_R = base["sample"], quantile_R = stats["quantile"];

  int n = A.length(), ncols = X.ncol();
  IntegerVector index = seq(0, n-1);

  auto iter = [&]() -> NumericVector {
    IntegerVector s = sample_R(index, n, true);
    NumericVector A_s = A[s], Y_s = Y[s];

    NumericMatrix X_s(n, ncols);
    for(int i = 0; i < n; i++) {
      X_s(i, _) = X(s[i], _);
    }
    return extrema_md_cpp(A_s, X_s, Y_s, gamma,
                          estimand, reg_adjust, start);
  };

  NumericMatrix out(B, 2);

  for(int i = 0; i < B; i++) {
    out(i, _) = iter();
  }
  double lower_ci = as<double>(quantile_R(out(_, 0), alpha /2));
  double upper_ci = as<double>(quantile_R(out(_, 1), 1- alpha /2));
  return NumericVector({lower_ci, upper_ci});
}



// [[Rcpp::export]]
NumericVector extrema_os_cpp(NumericVector A, NumericMatrix X, NumericVector Y,
                             double gamma = 0.0, CharacterVector estimand = "ate",
                             bool reg_adjust = false,
                             NumericVector start = NA_REAL) {
  NumericVector out;

  if (estimand[0] == "att") {
    if (is_na(start)[0]) {
      out = mean(as<NumericVector>(Y[A == 1])) - 
        rev(extrema_md_cpp(1 - A, X, Y, gamma, "missing", reg_adjust, - start));
    } else {
      out = mean(as<NumericVector>(Y[A == 1])) - 
        rev(extrema_md_cpp(1 - A, X, Y, gamma, "missing", reg_adjust));
    }
  } else {
    if (is_na(start)[0]) {
      out = extrema_md_cpp(A, X, Y, gamma, "all", reg_adjust) - 
        rev(extrema_md_cpp(1 - A, X, Y, gamma, "all", reg_adjust));
    } else {
      out = extrema_md_cpp(A, X, Y, gamma, "all", reg_adjust, start) - 
        rev(extrema_md_cpp(1 - A, X, Y, gamma, "all", reg_adjust, -start));
    }
  }
  return out;
}

使用SourceCpp()编译后,前三个函数将按预期工作。但是,每当我尝试使用最后一个函数,即extrema_os_cpp()时,都会出现一个奇怪的错误:

Error in extrema_os(A = A, X = cbind(1, as.matrix(X)), Y = Y) : 
  negative length vectors are not allowed

据我所知,当向量或矩阵的维数很大时,就会出现此错误。但是我的输入向量和矩阵的维数很低。我用来验证结果的模拟数据如下:

popn <- c(-2.5, -1.28, -0.54, -0.16, -0.02, 0, 0.02, 0.16, 0.54, 1.28, 2.5)

X <- sample(x = popn,size = 1000,replace = T)
prob_Y <- (1+exp(1.5*X))^-1

Y <- NULL
for (i in 1:1000) {
  Y[i] <- rbinom(n = 1,size = 1,prob = prob_Y[i])
}

prob_A <- (1+ exp(-1.5*X - 0.1*(X^2)))^-1

A <- NULL
for (i in 1:1000) {
  A[i] <- rbinom(n = 1,size = 1,prob = prob_A[i])
}

library(Rcpp)
sourceCpp(file = "all_functions.cpp")
extrema_os_cpp(A = A,X = cbind(1, as.matrix(X)), Y = Y)

如前所述,它会产生以下错误:

Error in extrema_os_cpp(A = A, X = cbind(1, as.matrix(X)), Y = Y) : 
  negative length vectors are not allowed

我已经尝试修复此错误很长时间了,但是对这里的问题一无所知。有人可以帮我吗?