{"id":18430160,"url":"https://github.com/friendly/nestedlogit","last_synced_at":"2026-02-06T12:21:53.474Z","repository":{"id":154963317,"uuid":"629096343","full_name":"friendly/nestedLogit","owner":"friendly","description":"Nested Dichotomy Logistic Regression Models","archived":false,"fork":false,"pushed_at":"2026-01-29T03:29:48.000Z","size":7573,"stargazers_count":10,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-01-29T19:54:30.148Z","etag":null,"topics":["logistic-regression","multinomial-logistic-regression","polytomous-variables","r-package"],"latest_commit_sha":null,"homepage":"https://friendly.github.io/nestedLogit/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/friendly.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-17T15:57:19.000Z","updated_at":"2024-06-03T14:47:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"b7b58ce8-f154-415a-b986-7d105b821fee","html_url":"https://github.com/friendly/nestedLogit","commit_stats":{"total_commits":274,"total_committers":2,"mean_commits":137.0,"dds":0.3832116788321168,"last_synced_commit":"0fe4707575f11e0fedda45e3ce68762915fafd46"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/friendly/nestedLogit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendly%2FnestedLogit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendly%2FnestedLogit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendly%2FnestedLogit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendly%2FnestedLogit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/friendly","download_url":"https://codeload.github.com/friendly/nestedLogit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/friendly%2FnestedLogit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29160810,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T07:18:23.844Z","status":"ssl_error","status_checked_at":"2026-02-06T07:13:32.659Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["logistic-regression","multinomial-logistic-regression","polytomous-variables","r-package"],"created_at":"2024-11-06T05:19:46.634Z","updated_at":"2026-02-06T12:21:53.466Z","avatar_url":"https://github.com/friendly.png","language":"HTML","readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file.\n     When version changes, install the package before re-kniting so the deve version is up-to-date\n--\u003e\n\n```{r setup, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  warning = FALSE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  fig.height = 5,\n  fig.width = 5\n#  out.width = \"100%\"\n)\n\nlibrary(nestedLogit)\n# get package versions\ncran_version \u003c- available.packages(repos = \"https://cloud.r-project.org\")[\"nestedLogit\", \"Version\"]\ndev_version \u003c- getNamespaceVersion(\"nestedLogit\")\n\n```\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)\n[![Last Commit](https://img.shields.io/github/last-commit/friendly/nestedLogit)](https://github.com/friendly/nestedLogit)\n[![CRAN status](https://www.r-pkg.org/badges/version/nestedLogit)](https://cran.r-project.org/package=nestedLogit)\n[![Downloads](https://cranlogs.r-pkg.org/badges/nestedLogit?color=brightgreen)](https://www.r-pkg.org:443/pkg/nestedLogit)\n[![Docs](https://img.shields.io/badge/pkgdown%20site-blue)](https://friendly.github.io/nestedLogit)\n\n\u003c!-- badges: end --\u003e\n\n# nestedLogit \u003cimg src=\"man/figures/logo.png\" style=\"float:right; height:200px;\" /\u003e\n\u003c!-- **Version 0.3.4** --\u003e\n**Version `r dev_version`**; documentation built for `pkgdown` `r Sys.Date()`\n\nThe `nestedLogit` package provides functions for fitting _nested dichotomy_ logistic regression models\nfor a **polytomous** response (with $m \u003e 2$ categories), such as: \n\n* support for political party in Canada (PC, Liberal, NDP, Green, BQ), \n* preferred mode of transport (foot, bus, bike, train, plane), \n* womens' working status (not working, part-time, full-time).\n\nThe figure below shows two different ways that a $m=4$-category polytomous response $Y = \\{1, 2, 3, 4\\}$ can be decomposed as\nthree ($m-1$) nested dichotomies among the levels.\n \n* In the case shown at the left of the figure, the response categories\nare divided first as $\\{1, 2\\}$ vs. $\\{3, 4\\}$. Then these compound categories are subdivided\n as the dichotomies $\\{1\\}$ vs. $\\{2\\}$ and as $\\{3\\}$ vs. $\\{4\\}$.\n* Alternatively, as shown at the right of the figure, the response categories\nare divided progressively:\nfirst as $\\{1\\}$ vs. $\\{2, 3, 4\\}$; \nnext as $\\{2\\}$ vs. $\\{3, 4\\}$; and\nand finally $\\{3\\}$ vs. $\\{4\\}$.\n\n```{r nested}\n#| echo=FALSE,\n#| out.width=\"80%\",\n#| fig.cap = \"**Nested dichotomies**: The boxes show two different ways a four-category response can be represented as three nested dichotomies.\"\nknitr::include_graphics(\"vignettes/fig/nested.jpg\")\n```\n\n\nThe basic model for this situation is the standard **multinomial logistic model** (fit by: e.g., `nnet::multinom()`)\nwhich compares response categories to a _reference level_.\nWhen you can think of the differences among the response categories as a set nested comparisons\namong subsets of the categories, the approach of nested dichotomies is simpler, because:\n\n* Nested dichotomies are statistically independent, and hence: \n* the likelihood chi-square statistics for the sub-models are additive;\n* they provide an additive decomposition of tests for the overall polytomous response.\n* You can think of this as breaking up the overall question of \"How do the response categories differ?\" into $m-1$\n  sub-questions that answer the global one.\n\nWhen the dichotomies make\nsense substantively, this method can be a simpler alternative to the standard **multinomial logistic model**\nwhich compares response categories to a reference level.\nThis choice is similar to using **orthogonal contrasts** among factor categories in an ANOVA,\nas opposed to using the default reference-level coding.\n\n### Ordered categories\n\nNote that when the response catgegories are **ordered**, as in \neducation attained: \"HS\" \u003c \"College\" \u003c \"BA\" \u003c \"MA\" \u003c \"Phd\", another attractive model is the \n**proportional odds** model (e.g., fit by `MASS::polr()`).\nThis is a simpler model, but achieves that simplicity by\nmaking the additional assumption that the coefficients for the\npredictors are the same for all categories.\n\n\n\n## Installation\n\nYou can install the current published version (`r cran_version`) from [CRAN](https://cran.r-project.org/package=nestedLogit), \nor the development version (`r dev_version`) from\neither [R-universe](https://friendly.r-universe.dev/nestedLogit) or [Github](https://github.com/friendly/nestedLogit)\n\n+-------------------+------------------------------------------------------------------------------+\n| CRAN version      | `install.packages(\"nestedLogit\")`                                            |\n+-------------------+------------------------------------------------------------------------------+\n| R-universe        | `install.packages('nestedLogit', repos = 'https://friendly.r-universe.dev')` |\n+-------------------+------------------------------------------------------------------------------+\n|                   |                                                                              |\n| Github            | `remotes::install_github(\"friendly/nestedLogit\")`                            |\n|                   |                                                                              |\n+-------------------+------------------------------------------------------------------------------+\n\n\n## Package overview\n\nThe package provides one main function, `nestedLogit()` for fitting the set of $(m-1)$\nbinary logistic regression models for a polytomous response with $m$ levels.\nThese can be specified using helper functions,\n\n* `dichotomy()`: constructs a _single_ dichotomy among the levels of a response factor;\n* `logits()`: creates the set of dichotomies, typically using `dichotomy()` for each.\n* `continuationLogits()`: provides a convenient way to generate all dichotomies for an ordered response.\n\nFor instance, a 4-category response, with levels `r LETTERS[1:4]`, and successive binary splits\nfor the dichotomies of interest\ncould be specified as:\n\n\n```{r}\n(ABCD \u003c-\n  logits(AB.CD = dichotomy(c(\"A\", \"B\"), c(\"C\", \"D\")),\n           A.B = dichotomy(\"A\", \"B\"),\n           C.D = dichotomy(\"C\", \"D\")\n         )\n)\n```\n\nThese dichotomies are effectively a tree structure of lists, which can be displayed simply using\n`lobstr::tree()`.\n\n```{r tree}\nlobstr::tree(ABCD)\n```\n\n\nAlternatively, the nested dichotomies can be specified more compactly as a nested (i.e., recursive) list \nwith optionally named elements. For example, where people might choose a method of transportation\namong the categories `plane`, `train`, `bus`, `car`, a sensible set of three dichotomies could\nbe specified as:\n\n```{r transport}\ntransport \u003c- list(\n  air = \"plane\",\n  ground = list(\n    public = list(\"train\", \"bus\"),\n    private = \"car\"\n  ))\n\nlobstr::tree(transport)\n```\n\n\nThere are also methods including `as.matrix.dichotomies()`, `as.character.dichotomies()`\nto facilitate working with `dichotomies` objects in other representations. The `ABCD` example\nabove corresponds to the matrix below, whose rows represent the dichotomies and columns\nare the response levels:\n\n```{r}\nas.matrix(ABCD)\n\nas.character(ABCD)\n```\n\n\nThe result of `nestedLogit()` is an object of class `\"nestedLogit\"`. It contains\nthe set of $(m-1)$ `glm()` models fit to the dichotomies.\n\n### Methods\n\n```{r child=\"man/partials/methods.Rmd\"}\n```\n\n\n## Examples\n\nThis example uses data on women's labor force participation to fit a nested logit model for\nthe response, `partic`, representing categories\n`not.work`, `parttime` and `fulltime` for 263 women from a 1977\nsurvey in Canada. This dataset is explored in more detail in the\npackage vignette, `vignette(\"nestedLogits\", package = \"nestedLogit\")`.\n\nA model for the complete polytomy can be specified as two nested\ndichotomies, using helper functions `dichotomy()` and `logits()`, as shown in the example that follows:\n\n* `work`: {not.work} vs. {parttime, fulltime}\n* `full`: {parttime} vs. {fulltime}, but only for those working\n\n`nestedLogit()` effectively fits each of these dichotomies\nas logistic regression models via `glm(..., family = binomial)`\n\n```{r wlf-model}\ndata(Womenlf, package = \"carData\")\n\n# Use `logits()` and `dichotomy()` to specify the comparisons of interest\ncomparisons \u003c- logits(work=dichotomy(\"not.work\", \n                                     working=c(\"parttime\", \"fulltime\")),\n                      full=dichotomy(\"parttime\", \"fulltime\"))\n\nm \u003c- nestedLogit(partic ~ hincome + children,\n                 dichotomies = comparisons,\n                 data=Womenlf)\ncoef(m)\n```\nThe `\"nestedLogit\"` object contains the components of the fitted model. The structure can be shown nicely\nusing `lobstr::tree()`:\n\n```{r}\nm |\u003e lobstr::tree(max_depth=1)\n```\n\nThe separate models for the `work` and `full` dichotomies can be extracted via `models()`. These\nare the binomial `glm()` models.\n```{r}\nmodels(m) |\u003e lobstr::tree(max_depth = 1)\n```\n\n`Anova()` produces analysis of variance deviance tests for the terms in this model for each of the submodels, as well as for the combined responses of the polytomy. The `LR Chisq` and `df` for terms in the combined model are the sums of those for\nthe submodels.\n\n\n```{r wlf-anova}\ncar::Anova(m)\n```\n\n### Plots\nA basic plot of predicted probabilities can be produced using\nthe `plot()` method for `\"nestedLogit\"` objects.\nIt can be called several times to give multi-panel plots.\nBy default, a 95% pointwise confidence envelope is added to the plot.\nHere, they are plotted with `conf.level = 0.68` to give $\\pm 1$ std. error bounds.\n\n```{r wlf-plot}\n#| out.width = \"100%\",\n#| fig.asp = 0.55,\n#| echo = 1:3\nop \u003c- par(mfcol=c(1, 2), mar=c(4, 4, 3, 1) + 0.1)\nplot(m, \"hincome\", list(children=\"absent\"),\n     conf.level = 0.68,\n     xlab=\"Husband's Income\", legend=FALSE)\nplot(m, \"hincome\", list(children=\"present\"),\n     conf.level = 0.68,\n     xlab=\"Husband's Income\")\npar(op)\n```\n\n## Vignettes\n\n* A more general discussion of nested dichotomies logistic regression and detailed examples can be found \nin `vignette(\"nestedLogit\")`\n\n\n* A variety of other plots can be produced using `ggplot()`, as described in the vignette,\n`vignette(\"plotting-ggplot\")`.\n\n* A new vignette, \"`vignette(\"standard-errors\")`, describes the mathematics behind the calculation of\nstandard errors using the delta method.\n\n## Authors\n* John Fox\n* Michael Friendly\n\n## References\n\nS. Fienberg (1980) _The Analysis of Cross-Classified Categorical Data_, 2nd Edition, MIT Press, Section 6.6.\n\nJ. Fox (2016) _Applied Regression Analysis and Generalized Linear Models_, 3rd Edition, Sage, Section 14.2.2.\n\nM. Friendly and D. Meyers (2016) _Discrete Data Analysis with R_, CRC Press, Section 8.2.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendly%2Fnestedlogit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffriendly%2Fnestedlogit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendly%2Fnestedlogit/lists"}