// [[Rcpp::export(".cyclopsGetInterceptLabel")]] SEXP cyclopsGetInterceptLabel(Environment x) { using namespace bsccs; XPtr<ModelData> data = parseEnvironmentForPtr(x); if (data->getHasInterceptCovariate()) { size_t index = data->getHasOffsetCovariate() ? 1 : 0; return Rcpp::wrap(data->getColumn(index).getNumericalLabel()); } else { return R_NilValue; } }
//' @title Get covariate types //' //' @description //' \code{getCovariateTypes} returns a vector covariate types in an OHDSI Cyclops data object //' //' @param object An OHDSI Cyclops data object //' @param covariateLabel Integer vector: covariate identifiers to return //' // [[Rcpp::export("getCovariateTypes")]] CharacterVector cyclopsGetCovariateType(Environment object, const std::vector<int64_t>& covariateLabel) { using namespace bsccs; XPtr<bsccs::RcppModelData> data = parseEnvironmentForRcppPtr(object); CharacterVector types(covariateLabel.size()); for (size_t i = 0; i < covariateLabel.size(); ++i) { size_t index = data->getColumnIndex(covariateLabel[i]); types[i] = data->getColumn(index).getTypeString(); } return types; }
//' @title Get covariate identifiers //' //' @description //' \code{getCovariateIds} returns a vector of integer covariate identifiers in an OHDSI Cyclops data object //' //' @param object An OHDSI Cyclops data object //' // [[Rcpp::export("getCovariateIds")]] std::vector<int64_t> cyclopsGetCovariateIds(Environment object) { using namespace bsccs; XPtr<ModelData> data = parseEnvironmentForPtr(object); ProfileVector covariates; size_t i = 0; if (data->getHasOffsetCovariate()) i++; // if (data->getHasInterceptCovariate()) i++; for (; i < data->getNumberOfColumns(); ++i) { covariates.push_back(data->getColumn(i).getNumericalLabel()); } return covariates; }
// [[Rcpp::export(".cyclopsFinalizeData")]] void cyclopsFinalizeData( Environment x, bool addIntercept, SEXP sexpOffsetCovariate, bool offsetAlreadyOnLogScale, bool sortCovariates, SEXP sexpCovariatesDense, bool magicFlag = false) { using namespace bsccs; XPtr<ModelData> data = parseEnvironmentForPtr(x); if (data->getIsFinalized()) { ::Rf_error("OHDSI data object is already finalized"); } if (addIntercept) { if (data->getHasInterceptCovariate()) { ::Rf_error("OHDSI data object already has an intercept"); } // TODO add intercept as INTERCEPT_TYPE if magicFlag == true data->insert(0, DENSE); // add to front, TODO fix if offset data->setHasInterceptCovariate(true); // CompressedDataColumn& intercept = data->getColumn(0); const size_t numRows = data->getNumberOfRows(); for (size_t i = 0; i < numRows; ++i) { data->getColumn(0).add_data(i, static_cast<real>(1.0)); } } if (!Rf_isNull(sexpOffsetCovariate)) { // TODO handle offset IdType covariate = as<IdType>(sexpOffsetCovariate); int index; if (covariate == -1) { // TODO Bad, magic number //std::cout << "Trying to convert time to offset" << std::endl; //data->push_back(NULL, NULL, offs.begin(), offs.end(), DENSE); // TODO Do not make copy //data->push_back(NULL, &(data->getTimeVectorRef()), DENSE); data->moveTimeToCovariate(true); index = data->getNumberOfColumns() - 1; } else { index = data->getColumnIndexByName(covariate); if (index == -1) { std::ostringstream stream; stream << "Variable " << covariate << " not found."; stop(stream.str().c_str()); //error->throwError(stream); } } data->moveToFront(index); data->getColumn(0).add_label(-1); // TODO Generic label for offset? data->setHasOffsetCovariate(true); } if (data->getHasOffsetCovariate() && !offsetAlreadyOnLogScale) { //stop("Transforming the offset is not yet implemented"); data->getColumn(0).transform([](real x) { return std::log(x); }); } if (!Rf_isNull(sexpCovariatesDense)) { // TODO handle dense conversion ProfileVector covariates = as<ProfileVector>(sexpCovariatesDense); for (auto it = covariates.begin(); it != covariates.end(); ++it) { IdType index = data->getColumnIndex(*it); data->getColumn(index).convertColumnToDense(data->getNumberOfRows()); } } data->setIsFinalized(true); }