// [[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); }
// [[Rcpp::export(".cyclopsSetHasIntercept")]] void cyclopsSetHasIntercept(Environment x, bool hasIntercept) { using namespace bsccs; XPtr<ModelData> data = parseEnvironmentForPtr(x); data->setHasInterceptCovariate(hasIntercept); }