{"id":18501572,"url":"https://github.com/const-ae/coefexplainer","last_synced_at":"2025-10-10T13:13:59.713Z","repository":{"id":82391739,"uuid":"286991710","full_name":"const-ae/CoefExplainer","owner":"const-ae","description":"Understand How to Interpret the Coefficients of a Categorical Linear Model","archived":false,"fork":false,"pushed_at":"2022-04-27T14:28:33.000Z","size":2317,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-10T05:35:01.986Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/const-ae.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-12T11:05:22.000Z","updated_at":"2024-09-23T17:27:32.000Z","dependencies_parsed_at":"2023-05-13T08:15:49.035Z","dependency_job_id":null,"html_url":"https://github.com/const-ae/CoefExplainer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/const-ae/CoefExplainer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/const-ae%2FCoefExplainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/const-ae%2FCoefExplainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/const-ae%2FCoefExplainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/const-ae%2FCoefExplainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/const-ae","download_url":"https://codeload.github.com/const-ae/CoefExplainer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/const-ae%2FCoefExplainer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004070,"owners_count":26083665,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2024-11-06T13:54:19.793Z","updated_at":"2025-10-10T13:13:59.696Z","avatar_url":"https://github.com/const-ae.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, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\",\n  fig.width = 12\n)\n```\n\n# CoefExplainer\n\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\nUnderstand How to Interpret the Coefficients of a Categorical Linear Model\n\n## Installation\n\nYou can install the released version of CoefExplainer from [Github](https://CRAN.R-project.org) with:\n\n``` r\ndevtools::install_github(\"const-ae/CoefExplainer\")\n```\n\n## Example\n\nLet's demonstrate the package with the `palmerpenguins` package.\nFirst, we have to remove all `NA`'s.\n\n```{r}\npeng \u003c- palmerpenguins::penguins\n# Remove any NA's\npeng \u003c- peng[! apply(peng, 1, function(row) any(is.na(row))), ]\npeng\n```\n\nWe can now load the `CoefExplainer` package and parse a formula for a linear model with categorical covariates:\n\n```{r}\nlibrary(CoefExplainer)\ncoefExplFit \u003c- CoefExplainer(peng, flipper_length_mm ~ species + island + sex)\n```\n\n\nThere are three different ways to look at the model:\n\n1. A beeswarm plot for each group (black dots). For each group it shows how the coefficients are combined to arrive at the prediction for that group (blue line) and how that line compares against the true group mean (red line).\n\n```{r}\nplotModel(coefExplFit)\n```\n\n2. We can also look at the underlying model matrix\n\n```{r}\nplotModelMatrix(coefExplFit)\n```\n\n3. And lastly, we can look at the magnitude of each coefficient. \n\n```{r}\nplotCoef(coefExplFit)\n```\n\n# Advanced Example\n\nWhat happens if we deal with an ordered factor?\n\n```{r}\npeng2 \u003c- peng\npeng2$bill_length_fct \u003c- cut(peng2$bill_length_mm, breaks = 4, ordered_result = TRUE)\n\nplotAll(CoefExplainer(peng2, flipper_length_mm ~ bill_length_fct))\n```\n\nWe can use the `C()` function to change the contrast setting for the `bill_length_fct`. Note how the predictions (blue lines)\ndon't change, however the coefficients have very different interpretations depending on the contrast setting. Do you\nrecognize which is the default contrast for an ordered factor?\n\n```{r}\npeng2$bill_length_fct \u003c- C(peng2$bill_length_fct, contr.treatment)\nplotAll(CoefExplainer(peng2, flipper_length_mm ~ bill_length_fct), title = \"Treatment Contrast\")\npeng2$bill_length_fct \u003c- C(peng2$bill_length_fct, contr.sum)\nplotAll(CoefExplainer(peng2, flipper_length_mm ~ bill_length_fct), title = \"Sum Contrast\")\npeng2$bill_length_fct \u003c- C(peng2$bill_length_fct, contr.poly)\nplotAll(CoefExplainer(peng2, flipper_length_mm ~ bill_length_fct), title = \"Polynomial Contrast\")\npeng2$bill_length_fct \u003c- C(peng2$bill_length_fct, contr.helmert)\nplotAll(CoefExplainer(peng2, flipper_length_mm ~ bill_length_fct), title = \"Helmert Contrast\")\n```\n\nI find all the coefficients above difficult to interpret. In my opinion, for ordered factors a better choice is `contr.step()`:\n\n```{r}\ncontr.step \u003c- function(n){\n  ret \u003c- matrix(0, nrow = n, ncol = n - 1)\n  ret[lower.tri(ret)] \u003c- 1\n  ret\n}\n\npeng2$bill_length_fct \u003c- C(peng2$bill_length_fct, contr.step)\nplotAll(CoefExplainer(peng2, flipper_length_mm ~ bill_length_fct), title = \"Step Contrast\")\n```\n\nThe MASS package provides a similar function called `contr.sdif()` that produces coefficients that have the same values as the ones from the `contr.step()` function. The only difference is the intercept. In the `contr.step()`, the intercept corresponds to the mean of the first group, whereas in the `contr.sdif()` function it is the mean over all group means.\n\n```{r}\npeng2$bill_length_fct \u003c- C(peng2$bill_length_fct, MASS::contr.sdif)\nplotAll(CoefExplainer(peng2, flipper_length_mm ~ bill_length_fct), title = \"MASS Step Contrast\")\n```\n\n# Credit\n\nIf you find the package useful, also checkout the http://www.bioconductor.org/packages/ExploreModelMatrix/ by Charlotte Soneson et al. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconst-ae%2Fcoefexplainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconst-ae%2Fcoefexplainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconst-ae%2Fcoefexplainer/lists"}