{"id":3498,"date":"2025-04-01T04:18:07","date_gmt":"2025-04-01T04:18:07","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=3498"},"modified":"2025-04-01T12:20:21","modified_gmt":"2025-04-01T12:20:21","slug":"crossed-vs-nested-random-effects","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/crossed-vs-nested-random-effects\/","title":{"rendered":"Crossed vs Nested Random Effects"},"content":{"rendered":"<p>I was reading a paper this weekend and the researchers used a mixed model to for their data given that they were dealing with repeated measures of individuals and teams. Mixed models have become common in sport science research and I\u2019ve written about them a few times in this blog:<\/p>\n<ul>\n<li><span style=\"color: #0000ff;\"><strong><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/mixed-models-in-sport-science-frequentist-bayesian\/\">Mixed Models in Sports Science: Frequentist &amp; Bayesian<\/a><\/strong><\/span><\/li>\n<li><span style=\"color: #0000ff;\"><strong><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/plotting-mixed-model-outputs\/\">Plotting Mixed Model Outputs<\/a><\/strong><\/span><\/li>\n<li><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/making-predictions-from-a-mixed-model-using-r\/\"><strong><span style=\"color: #0000ff;\">Making Predictions from a Mixed Model using<\/span><span style=\"color: #0000ff;\"> R<\/span><\/strong><\/a><\/span><\/li>\n<\/ul>\n<p>As I was reading the paper I was wondering, because it wasn\u2019t specified in the methods section, if the researchers had given any thought to whether their random effects were crossed or nested. Then I thought, maybe people aren\u2019t thinking about this and perhaps a short blog article with an example would be useful. So here we are\u2026<\/p>\n<p><em>For brevity, I&#8217;ll only present short snippets of code in the blog article. The entire code is available on my <span style=\"color: #0000ff;\"><strong><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/crossed_vs_nested_effects\">Github Page<\/a><\/strong><\/span>.\u00a0<\/em><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Data<\/strong><\/span><\/p>\n<p>We need to simulate some data to work with. To keep things simple, we will create a data set of a league that has 6 teams and 60 players. Each player will have a player value metric, the outcome of interest, for every team they played on during the season.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nsuppressPackageStartupMessages({\r\n  suppressMessages({\r\n    suppressWarnings({\r\n    library(tidyverse)\r\n    library(lme4)\r\n    library(arm)\r\n    })\r\n  })\r\n})\r\n\r\n\r\ntheme_set(theme_bw())\r\n\r\nset.seed(225544)\r\nteams &lt;- sample(c(&quot;Sharks&quot;, &quot;Bears&quot;, &quot;Turtles&quot;, &quot;Bisons&quot;, &quot;Jaguars&quot;, &quot;Pythons&quot;), size = 60, replace = TRUE)\r\nplayers &lt;- as.factor(sample(1:60, size = 60, replace = TRUE))\r\nintercept &lt;- 50\r\nteam_effect &lt;- case_when(teams == &quot;Sharks&quot; ~ rnorm(1, mean = 0, sd = 7), \r\n                         teams == &quot;Bears&quot; ~ rnorm(1, mean = 0, sd = 2),\r\n                         teams == &quot;Turtles&quot; ~ rnorm(1, mean = 0, sd = 1),\r\n                         teams == &quot;Bisons&quot; ~ rnorm(1, mean = 0, sd = 13),\r\n                         teams == &quot;Jaguars&quot; ~ rnorm(1, mean = 0, sd = 5),\r\n                         teams == &quot;Pythons&quot; ~ rnorm(1, mean = 0, sd = 3))\r\nplayer_effect &lt;- rnorm(length(levels(players)), mean = 0, sd = 10)\r\nrandom_noise &lt;- 2\r\n\r\n\r\n\r\ndat &lt;- data.frame( team = teams, player_id = players ) %&gt;%\r\n  mutate(\r\n    mu = intercept + team_effect + player_effect&#x5B;players],\r\n    player_value = round(mu + rnorm(n(), mean = 0, sd = random_noise), 1)\r\n  ) %&gt;%\r\n  dplyr::select(-mu)\r\n\r\ndat %&gt;%\r\n  head()\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.44.15\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3499\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.44.15\u202fPM.png\" alt=\"\" width=\"337\" height=\"217\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.44.15\u202fPM.png 540w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.44.15\u202fPM-300x193.png 300w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>EDA<\/strong><\/span><\/p>\n<p>How many unique players were on each team during the season?<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.45.22\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3500\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.45.22\u202fPM.png\" alt=\"\" width=\"236\" height=\"166\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.45.22\u202fPM.png 542w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.45.22\u202fPM-300x211.png 300w\" sizes=\"auto, (max-width: 236px) 100vw, 236px\" \/><\/a><\/p>\n<p>Which players played on multiple teams?<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.46.07\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3501\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.46.07\u202fPM.png\" alt=\"\" width=\"313\" height=\"413\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.46.07\u202fPM.png 606w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.46.07\u202fPM-227x300.png 227w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/a><\/p>\n<p>Team Summary Stats<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.47.13\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3502\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.47.13\u202fPM.png\" alt=\"\" width=\"370\" height=\"250\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.47.13\u202fPM.png 744w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.47.13\u202fPM-300x202.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-8.47.13\u202fPM-624x421.png 624w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Crossed vs Nested Effects<\/strong><\/span><\/p>\n<p>We have 14 players that played on multiple teams (Player 11 ended up playing for all 6 teams!) and, as a consequence, different teams had different numbers of individual players. For example, the Bears had 13 different players while the Pythons had 6 different players. This small detail is important to consider with respect to our random effects because crossed and nested effects behave differently with respect to how they partition variance.<\/p>\n<p>When constructing mixed models we usually have a data hierarchy where observations are grouped or nested within specific categories. When the observations are nested the observations are exclusive to specific groups. Some examples:<\/p>\n<ul>\n<li>Classes are nested within schools but classes in one school cannot also be grouped within a different school.<\/li>\n<li>Athletes competing in the Olympics are nested within their respective countries but an individual cannot compete for two different countries in the same Olympic Games.<\/li>\n<\/ul>\n<p>By contrast, crossed effects can occur when observations move between groups during the same study\/observation period. A few examples of crossed effects are:<\/p>\n<ul>\n<li>A student might start the year at one school and then move half way through the semester and attend a different school.<\/li>\n<li>Players in team sport might be traded mid-season and play for different teams.<\/li>\n<li>In a study where the experimental and placebo groups contain both male and female participants, we would have crossed random effects to account for sex being in both groups.<\/li>\n<li>Looking at sports teams, we may be analyzing the behavior of position groups on different teams. The position groups are the same for each team (e.g., Forwards, Guards, and Centers on Basketball teams) so the model has crossed effects.<\/li>\n<\/ul>\n<p>To help solidify this concept a bit more, I&#8217;ve created the below drawing.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/crossed_nested_image.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3503\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/crossed_nested_image-1024x608.png\" alt=\"\" width=\"556\" height=\"330\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/crossed_nested_image-1024x608.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/crossed_nested_image-300x178.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/crossed_nested_image-768x456.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/crossed_nested_image-624x370.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/crossed_nested_image.png 1668w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/a><\/p>\n<p>We see that in the <strong>Completely Nested<\/strong> example there are individual players that only player for their respective teams. Contrast that to the <strong>Completely Crossed<\/strong> example, where every player ends up playing on each team. This would, obviously, be an incredibly weird scenario and not one we&#8217;d see in sport but probably one that you&#8217;d see in researched where there is a crossover of treatments. Finally, the <strong>Partially Nested &amp; Crossed<\/strong> example has a few players playing for multiple teams (for example, Player 1 played for both Team 1 and Team 2) and some players that stay with a single team during the season. This version is the one that we most frequently encounter in team sport.<\/p>\n<p>Now that we have a general understanding of the difference between crossed and random effects, let&#8217;s see how this looks when we build a model on our simulated data and (most importantly) let&#8217;s see how specifying the model to have crossed or nested random effects changes the model output.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Model Examples<\/strong><\/span><\/p>\n<p><em>Crossed<\/em><\/p>\n<p>Since we commonly deal with crossed (or partially nested\/crossed) data in team sport, I&#8217;ll start with modeling the data to have crossed random effects. As a side note, most of the time when I look at people&#8217;s models in sport science they are creating the model structure like this, by default.<\/p>\n<p>We will build random intercept model (since we didn&#8217;t simulate any fixed effects) and allow a separate intercept variance for both <em><strong>team<\/strong><\/em> and <em><strong>player_id<\/strong><\/em>, keeping in mind that this specifies that there are players who are potentially playing on different teams (crossed observations) across the season.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nlme_crossed &lt;- lmer(player_value ~ 1 + (1|team) + (1|player_id), data = dat)\r\n<\/pre>\n<p><em>Nested<\/em><\/p>\n<p>Next, we fit the random intercept model but this time we treat our random effects of <em><strong>player_id<\/strong><\/em> and <em><strong>team<\/strong><\/em> as nested. That is to say that we are allowing the players to be nested within their respective teams. To review, this means that we are treating this data as if each player only played on a single team during the season and never changed teams.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlme_nested &lt;- lmer(player_value ~ 1 + (1|team) + (1|team:player_id), data = dat)\r\n<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Comparing the two models<\/strong><\/span><\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.01.58\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3504\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.01.58\u202fPM-1024x336.png\" alt=\"\" width=\"686\" height=\"225\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.01.58\u202fPM-1024x336.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.01.58\u202fPM-300x98.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.01.58\u202fPM-768x252.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.01.58\u202fPM-624x205.png 624w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/a><\/p>\n<p>First, we notice that the fixed effects are slightly different between the two models, but the difference in small in this simulated data.<\/p>\n<p>The random effects look very different between the two model structures. In the crossed effects model we get two different random effects &#8212; one for the <em><strong>player_id<\/strong><\/em> and one for the <em><strong>team<\/strong><\/em>. However, in the nested effects model we have a random effect for <em><strong>team<\/strong><\/em> but then we have a random effect for <em><strong>player_id<\/strong><\/em> nested within each <em><strong>team<\/strong><\/em>. So, in the nested model a player that played for multiple teams will have different random intercept for each of the teams they were nested in. Both models, of course, also have a residual in the random effects, however the values are very different.<\/p>\n<p>Let&#8217;s compile the random effects side-by-side to get a better appreciation for the differences. We will start with the `team` random effect since it is present in both models.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.07.05\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3506\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.07.05\u202fPM.png\" alt=\"\" width=\"318\" height=\"166\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.07.05\u202fPM.png 518w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.07.05\u202fPM-300x156.png 300w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/a><\/p>\n<p>There are pretty stark differences between the random intercepts of the nested and crossed models! Recall from our EDA that the Bears had the highest average value of all the teams and they also had the fewest number of different players (5).<\/p>\n<p>The player random effects get a little wonky because players who played for multiple teams will end up having different random effects depending on which team we nested them in. Conversely, in the crossed model we get one random effect estimate per player.<\/p>\n<p>To join the random effects from both models we have to do a bit of work on the nested model to extract the info. <strong>(NOTE: <\/strong><em>Since the table is really long I&#8217;ll only show the first 10 rows here but you can got to the <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/crossed_vs_nested_effects\">Github page<\/a><\/span><\/strong> to see the full output.<\/em>)<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.08.16\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3507\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.08.16\u202fPM.png\" alt=\"\" width=\"372\" height=\"212\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.08.16\u202fPM.png 372w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.08.16\u202fPM-300x171.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/a><\/p>\n<p>Joining the two model random effects on <em><strong>player_id<\/strong><\/em> we see that player&#8217;s have some very different random effects between the two models. In particular, players that played on different teams can differ greatly between the models.<\/p>\n<p>For example, let&#8217;s look at Player 21.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.09.53\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3508\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.09.53\u202fPM.png\" alt=\"\" width=\"411\" height=\"131\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.09.53\u202fPM.png 736w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.09.53\u202fPM-300x95.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-03-31-at-9.09.53\u202fPM-624x198.png 624w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/a><\/p>\n<p>In the crossed model we find that Player 21 has a random intercept that is 1.6 points below league average (the fixed effect intercept). However, in the nested model, when Player 21 was on the Bears he was 1.15 points above the population intercept and 0.74 points below the population intercept when he was on the Turtles. This is partially due to the team effect, where the the Bears were the best team in the league (highest average value) and the Turtles were the worst team in the league (lowest average value).<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Wrapping Up<\/strong><\/span><\/p>\n<p>Hopefully this tutorial provided a brief, but useful, overview of crossed versus random effects during mixed model construction. While most people seem to gravitate towards crossed effects by default there may be times where nested effects are indeed required for the structure of the data. As the example shows, how we specify the model can dramatically change the outputs and thus the inference that we can derive from these outputs.<\/p>\n<p>To access the full code please see the <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/crossed_vs_nested_effects\">Github page<\/a><\/span><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was reading a paper this weekend and the researchers used a mixed model to for their data given that they were dealing with repeated measures of individuals and teams. Mixed models have become common in sport science research and I\u2019ve written about them a few times in this blog: Mixed Models in Sports Science: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47,43,42],"tags":[],"class_list":["post-3498","post","type-post","status-publish","format-standard","hentry","category-model-building-in-r","category-sports-analytics","category-sports-science"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/comments?post=3498"}],"version-history":[{"count":1,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3498\/revisions"}],"predecessor-version":[{"id":3509,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3498\/revisions\/3509"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=3498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=3498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=3498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}