{"id":32581437,"url":"https://github.com/jaytimm/mds-for-linguists","last_synced_at":"2026-02-26T19:05:33.591Z","repository":{"id":124112790,"uuid":"192790185","full_name":"jaytimm/mds-for-linguists","owner":"jaytimm","description":"Using R \u0026 VoteView mutlidimensional scaling (MDS) methods for the analysis \u0026 visualization of complex patterns of crosslinguistic variation.","archived":false,"fork":false,"pushed_at":"2022-02-07T17:22:08.000Z","size":1662,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-10-29T16:58:11.839Z","etag":null,"topics":["linguistic-typology","multidimensional-scaling","nominate","semantic-maps","voteview"],"latest_commit_sha":null,"homepage":"","language":null,"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/jaytimm.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2019-06-19T19:13:43.000Z","updated_at":"2023-07-18T20:05:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"fe23184d-5920-4f36-aaf7-53617a4b4456","html_url":"https://github.com/jaytimm/mds-for-linguists","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jaytimm/mds-for-linguists","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaytimm%2Fmds-for-linguists","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaytimm%2Fmds-for-linguists/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaytimm%2Fmds-for-linguists/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaytimm%2Fmds-for-linguists/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jaytimm","download_url":"https://codeload.github.com/jaytimm/mds-for-linguists/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaytimm%2Fmds-for-linguists/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29868052,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T18:42:30.764Z","status":"ssl_error","status_checked_at":"2026-02-26T18:41:47.936Z","response_time":89,"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":["linguistic-typology","multidimensional-scaling","nominate","semantic-maps","voteview"],"created_at":"2025-10-29T16:56:21.377Z","updated_at":"2026-02-26T19:05:33.583Z","avatar_url":"https://github.com/jaytimm.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: \"MDS for Linguists\"\noutput:\n  md_document:\n    variant: markdown_github\n    toc: TRUE\n    toc_depth: 2\n---\n\n  \n## MDS for Linguists\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(echo = TRUE)\nsource(\"/home/jtimm/pCloudDrive/GitHub/git-projects/render_toc.r\")\n```\n\n**An R-based guide for linguistic typologists** interested in applying [NOMINATE](https://voteview.com/about) multidimensional scaling (MDS) techniques to linguistic data as presented in [Croft](http://www.unm.edu/~wcroft/) and [Poole](https://polisci.ucsd.edu/about-our-people/faculty/faculty-directory/emeriti-faculty/poole-profile.html), \"Inferring universals from grammatical variation: multidimensional scaling for typological analysis\" (*Theoretical Linguistics* 34.1-37, 2008).\" [[Abstract]](https://www.degruyter.com/view/j/thli.2008.34.issue-1/thli.2008.001/thli.2008.001.xml)\n\n\nThis guide provides a brief summary of an R-based workflow for model implementation and the visualization of model results within the `ggplot` data visualization framework.  A cross-linguistic data set of indefinite pronouns from Haspelmath (1997) is utilized (and made available) here for demonstration purposes. For more thoughtful discussions regarding theory, scaling procedures \u0026 model interpretation, see reference section.\n\n\n```{r echo=FALSE}\nrender_toc(\"/home/jtimm/pCloudDrive/GitHub/git-projects/mds_for_linguists/README.Rmd\", \n           toc_header_name = 'MDS for Linguists',\n           toc_depth = 2)\n```\n\n\n\n## Getting started\n\n### Install and load required packages\n\n```{r message=FALSE, warning=FALSE}\nif (!require(\"pacman\")) install.packages(\"pacman\")\npacman::p_load(# anominate, -- no longer maintained -- \n               wnominate, \n               pscl, \n               ggplot2, \n               knitr, \n               devtools, \n               ggrepel, \n               data.table)\n```\n\n\n```{r eval=FALSE}\ndevtools::install_github(\"jaytimm/wnomadds\")\nlibrary(wnomadds)\n```\n\n\n### Load data\n\nData set:  A 9 x 140 data frame: Nine indefinite pronominal meanings, using data from 140 pronouns in 40 languages.  Data are made available [here](https://github.com/jaytimm/mds_for_linguists_using_R/blob/master/resources/Indefprn13.txt).\n\n\n```{r include=FALSE}\nlocal_data \u003c- '/home/jtimm/pCloudDrive/GitHub/git-projects/mds_for_linguists/resources'\n```\n\n\n```{r eval=FALSE}\n## File paths will look differently for Windows/Mac\nlocal_data \u003c- '/home/jtimm/Desktop/data/'\n```\n\n\nLoad data set:\n\n```{r message=FALSE, warning=FALSE}\nsetwd(local_data)\nraw_data \u003c- read.csv(\"Indefprn13.txt\",\n            sep=\"\\t\", \n            stringsAsFactors = FALSE)\n```\n\n\n\nA portion of the data frame is presented below.  Rows contain functions/meanings, and are analagous to legislators in the NOMINATE model.  Columns contain language-specific grammatical forms, and are analagous to roll calls (ie, votes) in the NOMINATE model.  \n\nA value of 1 in the table below means that a given form expresses a particular meaning; a value of 6 means that a given form does not express that particular meaning.  Missing data are specified with the value 9.\n\n\n```{r message=FALSE, warning=FALSE}\nknitr::kable(raw_data[,1:9]) \n```\n\n\n---\n\n\n## Using the wnominate and pscl packages\n\n### Building MDS models\n\n#### Rollcall object\n\nThe first step is to transform the original data structure into a `rollcall` object using the `pscl` package.\n\n\n```{r}\nroll_obj \u003c- pscl::rollcall(raw_data [,-1], \n                           yea=1, \n                           nay=6, \n                           missing=9,\n                           notInLegis=8,\n                           vote.names = colnames(raw_data)[2:ncol(raw_data)], \n                           legis.names = raw_data[,1])\n```\n\n\n\n#### Ideal points estimation \n\nThen we fit three models using the `wnominate` function -- one-, two- \u0026 three-dimensional solutions.\n\n```{r message=FALSE, warning=FALSE, results = 'hide'}\nideal_points_1D \u003c- wnominate::wnominate (roll_obj, dims = 1, polarity=c(1)) \nideal_points_2D \u003c- wnominate::wnominate (roll_obj, dims = 2, polarity=c(1,2)) \nideal_points_3D \u003c- wnominate::wnominate (roll_obj, dims = 3, polarity=c(1,2,3)) \n```\n\n\nThe resulting data structures are each comprised of seven elements:\n\n```{r}\nnames(ideal_points_1D)\n```\n\n\n\n#### Model comparison and fitness statistics\n\nCorrect classification and fitness statistics for each model are extracted from the `fits` element, and summarized below: \n\n```{r message=FALSE, warning=FALSE}\nlist('1D' = ideal_points_1D$fits, \n     '2D' = ideal_points_2D$fits, \n     '3D' = ideal_points_3D$fits)\n```\n\n\n\n### Visualizing model results\n\n\n#### A one-dimensional solution \n\nExtract legislator coordinates (ie, ideal points) from one-dimensional model results.\n\n```{r}\nd1 \u003c- cbind(label=rownames(ideal_points_1D$legislators), \n            ideal_points_1D$legislators)\nd1 \u003c- d1[order(d1$coord1D),]\n```\n\n\n\nPlot legislators (ie, grammatical functions) in one-dimensional space by rank.\n\n```{r fig.height=6, fig.width=6}\nggplot()  +\n  geom_text(data = d1,\n            aes(x=reorder(label, coord1D), \n                y=coord1D, \n                label=label), \n            size=4, \n            color = 'blue') +\n  \n#  theme_classic() +\n   theme_minimal() +\n  \n  labs(title=\"1D W-NOMINATE Plot\") + \n  theme(axis.text.y=element_blank(),\n        axis.ticks.y=element_blank())+ \n  xlab('') + ylab('First Dimension')+\n  ylim(-1.1, 1.1)+\n  coord_flip()\n```\n\n\n\n\n#### A two-dimensional solution \n\nWe first build a simple \"base\" plot using legislator coordinates from two-dimensional model results.  Per `wnominate` convention, we add a unit circle to specify model constraints.  All subsequent plots are built on this simple base plot. \n\n\n```{r fig.height=6, fig.width=6}\nbase_2D \u003c- ggplot(data = ideal_points_2D$legislators,\n       aes(x=coord1D, \n           y=coord2D)) +\n  \n  geom_point(size= 1.5,\n             color = 'blue') +\n  \n  annotate(\"path\",\n           x=cos(seq(0,2*pi,length.out=300)),\n           y=sin(seq(0,2*pi,length.out=300)),\n           color='gray',\n           size = .25) +\n  \n  xlab('First Dimension') + \n  ylab('Second Dimension') \n\nbase_2D + ggtitle('Two-dimensional base plot')\n```\n\n\n\n**Add** labels, a title, and change the theme.  \n\n```{r fig.height=6, fig.width=6}\nbase_2D +\n\n  ggrepel::geom_text_repel(\n    data  = ideal_points_2D$legislators,\n    aes(label = rownames(ideal_points_2D$legislators)),\n    direction = \"y\",\n    hjust = 0, \n    size = 4,\n    color = 'blue') +\n  \n  theme_classic() +\n# theme_minimal() +\n  \n  ggtitle(\"W-NOMINATE Coordinates\") \n```\n\n\n---\n\n\n### Cutting lines and roll call polarity via the wnomadds package\n\nI have developed a simple R package, `wnomadds`, that facilitates the plotting of roll call cutting lines and roll call polarities using `ggplot`.  While `wnominate` provides functionality for plotting cutting lines, only plotting in base R is supported.  The `wnm_get_cutlines` function extracts cutting line coordinates from `wnominate` model results, along with coordinates specifying the direction of majority Yea votes for a given roll call (ie, vote polarity). Addtional details about the package are available [here](https://github.com/jaytimm/wnomadds).\n\n```{r message=FALSE, warning=FALSE}\nwith_cuts \u003c- wnomadds::wnm_get_cutlines(ideal_points_2D, \n                                        rollcall_obj = roll_obj, \n                                        arrow_length = 0.05)\n```\n\n\nA sample of the resulting data frame:\n\n```{r}\nhead(with_cuts)\n```\n\n\n\n#### Cutting lines \u0026 legislator coordinates\n\n```{r fig.height=6, fig.width=6}\nbase_2D +\n\n  ggrepel::geom_text_repel(\n    data  = ideal_points_2D$legislators,\n    aes(label = rownames(ideal_points_2D$legislators)),\n    direction = \"y\",\n    hjust = 0, \n    size = 4,\n    color = 'blue') +\n  \n  geom_segment(data = with_cuts, \n               aes(x = x_1, y = y_1, \n                   xend = x_2, yend = y_2),\n               size = .25) + #cutting start to end\n  \n  theme_minimal() +\n  labs(title=\"Cutting lines \u0026 W-NOMINATE Coordinates\")\n```\n\n\n\n#### Cutting lines, roll call polarity \u0026 legislator coordinates\n\n```{r fig.height=6, fig.width=6}\nbase_2D +\n  \n  geom_segment(data=with_cuts, \n               aes(x = x_1, y = y_1, \n                   xend = x_2, yend = y_2),\n               size = .25) + #cutting start to end\n  \n  ##ARROWS -- \n  geom_segment(data=with_cuts, \n               aes(x = x_2, y = y_2, \n                   xend = x_2a, yend = y_2a), \n               #cutting end to opposite arrow\n               color = 'red',\n               arrow = arrow(length = unit(0.2,\"cm\"))) +\n  \n  geom_segment(data=with_cuts, \n               aes(x = x_1, y = y_1, \n                   xend = x_1a, yend = y_1a), \n               #cutting start to opposite arrow\n               color = 'red',\n               arrow = arrow(length = unit(0.2,\"cm\")))+ \n  ##END ARROWS.\n  \n  geom_text(data=with_cuts, \n               aes(x = x_1a, y = y_1a, \n                   label = Bill_Code), \n               size=2.5, \n               nudge_y = 0.03,\n               check_overlap = TRUE) +\n\n  theme_minimal() +\n  labs(title = \"W-NOMINATE Coordinates, cutting lines \u0026 roll call polarity\")\n```\n\n\n\n\n#### Selected cutting lines and legislator coordinates\n\n```{r fig.height=6, fig.width=6}\nselected \u003c- c('X01e', 'X01j', 'X01jd', 'X01n')\n\nsubset_cuts \u003c- subset(with_cuts, Bill_Code %in% selected)\n\nbase_2D +\n  \n  ggrepel::geom_text_repel(\n    data  = ideal_points_2D$legislators,\n    aes(label = rownames(ideal_points_2D$legislators)),\n    direction = \"y\",\n    hjust = 0, \n    size = 4,\n    color = 'blue') +\n  \n  geom_segment(data=subset_cuts, \n               aes(x = x_1, y = y_1, \n                   xend = x_2, yend = y_2),\n               size = .25) + #cutting start to end\n  \n  ##ARROWS -- \n  geom_segment(data=subset_cuts, \n               aes(x = x_2, y = y_2, \n                   xend = x_2a, yend = y_2a), \n               #cutting end to opposite arrow\n               color = 'red',\n               arrow = arrow(length = unit(0.2,\"cm\"))) +\n  \n  geom_segment(data=subset_cuts, \n               aes(x = x_1, y = y_1, \n                   xend = x_1a, yend = y_1a), \n               #cutting start to opposite arrow\n               color = 'red',\n               arrow = arrow(length = unit(0.2,\"cm\")))+ \n  ##END ARROWS.\n  \n  geom_text(data=subset_cuts, \n               aes(x = x_1a, y = y_1a, \n                   label = Bill_Code), \n               size=2.5, \n               nudge_y = 0.03,\n               check_overlap = TRUE) +\n\n  theme_minimal() +\n  labs(title = \"W-NOMINATE Coordinates \u0026 selected cutting lines\")\n```\n\n\n\n### Facet cutting lines by language\n\n```{r fig.height=6}\n#Extract language code from language-specific grammatical forms\nwith_cuts$lang \u003c- gsub('[A-Za-z]', '', with_cuts$Bill_Code)\n\n#Filter cutting line data set to first six language codes.\nfacet_cuts \u003c- subset(with_cuts, lang %in% c('01', '02', '03', '04', '05', '06'))\n\nbase_2D +\n  \n  geom_segment(data=facet_cuts, \n               aes(x = x_1, y = y_1, \n                   xend = x_2, yend = y_2),\n               size = .25) + #cutting start to end\n  \n  ##ARROWS -- \n  geom_segment(data=facet_cuts, \n               aes(x = x_2, y = y_2, \n                   xend = x_2a, yend = y_2a), \n               #cutting end to opposite arrow\n               color = 'red',\n               arrow = arrow(length = unit(0.2,\"cm\"))) +\n  \n  geom_segment(data=facet_cuts, \n               aes(x = x_1, y = y_1, \n                   xend = x_1a, yend = y_1a), \n               #cutting start to opposite arrow\n               color = 'red',\n               arrow = arrow(length = unit(0.2,\"cm\")))+ \n  ##END ARROWS.\n\n  theme_minimal() +\n  facet_wrap(~lang) +\n  coord_fixed()+\n  labs(title = \"W-NOMINATE Coordinates \u0026 language-specific cutting lines\")\n```\n\n\n\n\n---\n\n\n## References\n\nRoyce Carroll, Christopher Hare, Jeffrey B. Lewis, James Lo, Keith T. Poole and Howard Rosenthal (2017). Alpha-NOMINATE: Ideal Point Estimator. R package version 0.6. URL http://k7moa.c\nom/alphanominate.htm\n\nCroft, W., \u0026 Poole, K. T. (2008). Inferring universals from grammatical variation: Multidimensional scaling for typological analysis. *Theoretical linguistics*, 34(1), 1-37.\n\nHaspelmath, M. (1997). *Indefinite pronouns*. Oxford: Clarendon Press.\n\nPoole, K. T. (2005). *Spatial models of parliamentary voting*. Cambridge University Press.\n\nKeith Poole, Jeffrey Lewis, James Lo, Royce Carroll (2011). Scaling Roll Call Votes with wnominate in R. *Journal of Statistical Software*, 42(14), 1-21. URL http://www.jstatsoft.org/v42/i14/.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaytimm%2Fmds-for-linguists","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaytimm%2Fmds-for-linguists","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaytimm%2Fmds-for-linguists/lists"}