{"id":43781153,"url":"https://github.com/osofr/condensier","last_synced_at":"2026-02-05T18:15:06.928Z","repository":{"id":80724163,"uuid":"91924094","full_name":"osofr/condensier","owner":"osofr","description":"Non-parametric conditional density estimation with binned conditional histograms","archived":false,"fork":false,"pushed_at":"2019-01-18T09:19:28.000Z","size":1725,"stargazers_count":3,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-16T04:11:25.369Z","etag":null,"topics":["bin-hazard","cross-validation","density","hazard","learner-density","likelihood"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/osofr.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-05-21T00:27:14.000Z","updated_at":"2019-07-28T12:37:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"f06435ca-e8a6-4acb-a0e6-afed5d4ea5e1","html_url":"https://github.com/osofr/condensier","commit_stats":{"total_commits":195,"total_committers":3,"mean_commits":65.0,"dds":0.08205128205128209,"last_synced_commit":"6bcc973291cb311f586d3035a43a2468eae541a9"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/osofr/condensier","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fcondensier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fcondensier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fcondensier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fcondensier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osofr","download_url":"https://codeload.github.com/osofr/condensier/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fcondensier/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29128626,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T17:12:17.649Z","status":"ssl_error","status_checked_at":"2026-02-05T17:11:23.670Z","response_time":65,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bin-hazard","cross-validation","density","hazard","learner-density","likelihood"],"created_at":"2026-02-05T18:15:04.525Z","updated_at":"2026-02-05T18:15:06.919Z","avatar_url":"https://github.com/osofr.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"README-\"\n)\nlibrary(RefManageR)\nbib \u003c- ReadBib(system.file(\"Bib\", \"README-refs.bib\", package = \"condensier\"), check = FALSE)\n# bib2 \u003c- ReadBib(system.file(\"Bib\", \"RJC.bib\", package = \"RefManageR\"))[[seq_len(20)]]\nBibOptions(check.entries = FALSE, style = \"markdown\", cite.style = \"authoryear\", bib.style = \"numeric\")\n```\n\n# R/`condensier`: Non-parametric Multivariate Conditional Density Estimation with Binned Histograms\n\n\n[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/condensier)](https://CRAN.R-project.org/package=condensier)\n[![](https://cranlogs.r-pkg.org/badges/condensier)](https://CRAN.R-project.org/package=condensier)\n[![Travis-CI Build Status](https://travis-ci.org/osofr/condensier.svg?branch=master)](https://travis-ci.org/osofr/condensier)\n[![codecov](https://codecov.io/gh/osofr/condensier/branch/master/graph/badge.svg)](https://codecov.io/gh/osofr/condensier)\n[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)\n\n\u003e Fit a conditional density `f(A|W)`, where `A` can be continuous and multivariate and `W` is set of predictors.\n\u003e This estimator breaks up the support of a continuous `A` into discrete bins and fits the conditional hazard for each bin. By default the logistic regression will be used for fitting each bin hazard. Alternatively, arbitrary machine learning algorithms can be used via learners available in `sl3` R package (see example below).\n\u003e Given several competing candidate density estimators, one can find the optimal convex combination these candidate estimators by using Super Learner [`sl3`].\n\u003e For detailed description of the estimator implemented in this package see `r AutoCite(bib, \"diaz2011super\")` and `r AutoCite(bib, \"munoz2012population\")`.\n\n__Authors:__ [Oleg Sofrygin](https://github.com/osofr), [Frank Blaauw](https://github.com/frbl), [Antoine Chambaz](https://github.com/achambaz), Mark van der Laan\n\n\n### Installation\n\nTo install the development version of `condensier` (requires the `devtools` package):\n\n```{r, eval = FALSE}\ndevtools::install_github('osofr/condensier', build_vignettes = FALSE)\n```\n\n\n### Instructions\n\nSimulate some data with continuous outcome (`\"sA\"`):\n\n```{r}\nlibrary(\"simcausal\")\nD \u003c- DAG.empty()\nD \u003c-\nD + node(\"W1\", distr = \"rbern\", prob = 0.5) +\n  node(\"W2\", distr = \"rbern\", prob = 0.3) +\n  node(\"W3\", distr = \"rbern\", prob = 0.3) +\n  node(\"sA.mu\", distr = \"rconst\", const = (0.98 * W1 + 0.58 * W2 + 0.33 * W3)) +\n  node(\"sA\", distr = \"rnorm\", mean = sA.mu, sd = 1)\nD \u003c- set.DAG(D, n.test = 10)\ndatO \u003c- sim(D, n = 10000, rndseed = 12345)\n```\n\nFit conditional density using equal mass bins (same number of observations per bin):\n\n```{r}\nlibrary(\"condensier\")\ndens_fit \u003c- fit_density(\n    X = c(\"W1\", \"W2\", \"W3\"), \n    Y = \"sA\", \n    input_data = datO, \n    nbins = 20, \n    bin_method = \"equal.mass\",\n    bin_estimator = speedglmR6$new())\n```\n\nWrapper function to predict the conditional probability (likelihood) for new observations:\n\n```{r}\nnewdata \u003c- datO[1:5, c(\"W1\", \"W2\", \"W3\", \"sA\"), with = FALSE]\npreds \u003c- predict_probability(dens_fit, newdata)\n```\n\nWrapper function to sample the values from the conditional density fit:\n\n```{r}\nsampledY \u003c- sample_value(dens_fit, newdata)\n```\n\nFit conditional density using custom bin definitions (argument `intrvls`):\n\n```{r}\ndens_fit \u003c- fit_density(\n    X = c(\"W1\", \"W2\", \"W3\"),\n    Y = \"sA\",\n    input_data = datO,\n    bin_estimator = speedglmR6$new(),\n    intrvls = list(sA = seq(-4,4, by = 0.1)))\n```\n\nFit conditional density using custom bin definitions and\npool all bin indicators into a single long-format dataset.\nThe pooling results in a single regression that is fit for all bin hazards,\nwith a bin indicator added as an additional covariate.\n\n```{r}\ndens_fit \u003c- fit_density(\n    X = c(\"W1\", \"W2\", \"W3\"),\n    Y = \"sA\",\n    input_data = datO,\n    bin_estimator = speedglmR6$new(),\n    intrvls = list(sA = seq(-4,4, by = 0.1)),\n    pool = TRUE)\n```\n\n### Fitting Super Learner density with `sl3` package\n\nAny binary-outcome regression learner available in `sl3` package can be used as a \"drop-in\" learner for conditional bin hazard. Below, we use `xgboost` R package to define a new estimator of the bin hazard. Note that below, we are setting the tuning parameter `pool` to `TRUE`. This will have an effect of \"pooling\" all discrete bin indicators into a single dataset (with bin number added as a new covariate). This is followed by a single regression fit that is performed for all bins simultaneously (hence saving a lot of computation time and allowing the algorithm to perform smoothing over the bins).\n\n```{r}\nlibrary(\"sl3\")\n\ntask \u003c- sl3_Task$new(datO, covariates=c(\"W1\", \"W2\", \"W3\"), outcome=\"sA\")\nlrn \u003c- Lrnr_condensier$new(nbins = 10, bin_method = \"equal.len\", pool = TRUE, \n  bin_estimator = Lrnr_xgboost$new(nrounds = 5, objective = \"reg:logistic\"))\n\ntrained_lrn = lrn$train(task)\n\nnewdata \u003c- datO[1:5, c(\"W1\", \"W2\", \"W3\", \"sA\")]\nnew_task \u003c- sl3_Task$new(newdata, covariates=c(\"W1\", \"W2\", \"W3\"),outcome=\"sA\" )\npred_probs = trained_lrn$predict(new_task)\npred_probs\n```\n\nNow that we have defined the candidate bin hazard estimator, it is time to train the model and obtained predictions (likelihood) based on new observations\n\n```{r}\ntrained_lrn = lrn$train(task)\n\nnewdata \u003c- datO[1:5, c(\"W1\", \"W2\", \"W3\", \"sA\")]\nnew_task \u003c- sl3_Task$new(newdata, covariates=c(\"W1\", \"W2\", \"W3\"),outcome=\"sA\" )\npred_probs = trained_lrn$predict(new_task)\npred_probs\n```\n\nFinally, multiple candidate density estimators can be optimally stacked or combined with a Super Learner. The convex combination of the candidates is found by minimizing the cross-validated negative loglikelihood loss function. In this example we define 3 candidate density learners:\n\n```{r, eval = FALSE}\nlrn1 \u003c- Lrnr_condensier$new(nbins = 25, bin_method = \"equal.len\", pool = TRUE, \n  bin_estimator = Lrnr_glm_fast$new(family = \"binomial\"))\nlrn2 \u003c- Lrnr_condensier$new(nbins = 20, bin_method = \"equal.mass\", pool = TRUE,\n  bin_estimator = Lrnr_xgboost$new(nrounds = 50, objective = \"reg:logistic\"))\nlrn3 \u003c- Lrnr_condensier$new(nbins = 35, bin_method = \"equal.len\", pool = TRUE,\n  bin_estimator = Lrnr_xgboost$new(nrounds = 50, objective = \"reg:logistic\"))\n```\n\nWe proceed by training the Super Learner (with 10 fold cross-validation) and then finding the optimal convex combination of the candidate densities with the meta-learner `Lrnr_solnp_density`:\n\n```{r, eval = FALSE}\nsl \u003c- Lrnr_sl$new(learners = list(lrn1, lrn2, lrn3),\n                  metalearner = Lrnr_solnp_density$new())\nsl_fit \u003c- sl$train(task)\n```\n\nTo predict for new data, wrap the desired dataset into an `sl3-task` object and call predict on above `sl_fit` object:\n\n```{r, eval = FALSE}\nnewdata \u003c- datO[1:5, c(\"W1\", \"W2\", \"W3\", \"sA\")]\nnew_task \u003c- sl3_Task$new(newdata, covariates=c(\"W1\", \"W2\", \"W3\"),outcome=\"sA\" )\nsl_fit$predict(new_task)\n```\n\n\n### Nesting the Super Learner for bin hazards with density Super Learner\n\nNote that `bin_estimator` can be also a Super-Learner object from `sl3`. In this case the bin hazard will be estimated by stacking several candidate estimators. For example, below, we define a single density learner `lrn`,  with the hazard estimator defined by the Super-Learner that stacks two candidates (GLM and `xgboost` GBM). Note that in contrast to the above example, this Super-Learner fit will be optimized for the logistic regression problem (estimating pooled bin hazards), but still using internal 10-fold cross-validation. \n\n```{r, eval = FALSE}\nlibrary(\"sl3\")\nlrn \u003c- Lrnr_condensier$new(nbins = 35, bin_method = \"equal.len\", pool = TRUE, bin_estimator = \n  Lrnr_sl$new(\n    learners = list(\n      Lrnr_glm_fast$new(family = \"binomial\"),\n      Lrnr_xgboost$new(nrounds = 50, objective = \"reg:logistic\")\n      ),\n    metalearner = Lrnr_glm$new()\n    ))\nbinSL_fit \u003c- lrn$train(task)\n```\n\nIn prinicple, one can nest the two of the above described types of Super Learners: the Super Learner that fits the bin hazard of each candidate density and the Super Learner that finds the optimal combination of the candidate densities. However, due to potential performance constraints, we currently advise against that. \n\n### Stacking and cross-validating candidate densities with `sl3` package\n\nOne can build a custom version of their own Super Learner by using the stacking and cross-validation procedures availabe in `sl3`. Here we define a stack of 3 learners, then train all 3 and predict for new data (likelihood):\n\n```{r, eval = FALSE}\nlearner_stack \u003c- Stack$new(lrn1, lrn2, lrn3)\nstack_fit \u003c- learner_stack$train(task)\npreds \u003c- stack_fit$predict(new_task)\n```\n\nHere we cross-validate all 3 learners in the stack, using the default 10-fold CV:\n\n```{r, eval = FALSE}\ncv_stack \u003c- Lrnr_cv$new(learner_stack)\ncv_fit \u003c- cv_stack$train(task)\n```\n\n\n### Funding\nThe development of this package was funded through an NIH grant (R01 AI074345-07).\n\n### Copyright\nThe contents of this repository are distributed under the MIT license.\n```\nThe MIT License (MIT)\n\nCopyright (c) 2017 Oleg Sofrygin \n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n### References\n\n```{r results = \"asis\", echo = FALSE}\nPrintBibliography(bib, .opts = list(check.entries = FALSE, sorting = \"ynt\"))\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosofr%2Fcondensier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosofr%2Fcondensier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosofr%2Fcondensier/lists"}