{"id":2811,"date":"2023-01-09T05:46:00","date_gmt":"2023-01-09T05:46:00","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=2811"},"modified":"2023-01-14T02:42:48","modified_gmt":"2023-01-14T02:42:48","slug":"making-predictions-from-a-mixed-model-using-r","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/making-predictions-from-a-mixed-model-using-r\/","title":{"rendered":"Making predictions from a mixed model using R"},"content":{"rendered":"<p>Models are built for different reasons. Some models are built to make inferences and explore an underlying phenomenon while other models are built for making predictions and forecasting future outcomes. Often, in the applied sport science setting, the latter is a key goal as we are interested in future predictions that can assist practitioners in making decisions about training or treatment interventions.<\/p>\n<p>I&#8217;ve previously discussed building and interpreting mixed models using R (see <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/mixed-models-in-sport-science-frequentist-bayesian\/\">HERE<\/a> <\/span><\/strong><span style=\"color: #000000;\">and<\/span><strong><span style=\"color: #0000ff;\"> <a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/tidyx-29-custom-color-palette-stacked-bar-blot-and-mixed-models\/\">HERE<\/a><\/span><\/strong>). Therefore, today, I wanted to talk about making predictions with mixed models. We will do this in R using the mixed model packages {<strong>lme4<\/strong>} and walk through how to use the <strong>predict() <\/strong>function and other helper functions to extract confidence intervals and predictions intervals. First, I&#8217;ll briefly cover the <strong>predict()<\/strong> function using a simple linear regression so that we have a general understanding of how it works and what it does before we get into the mixed models (as a side note, I&#8217;ve previously talked about making predictions using Bayesian models <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/making-predictions-with-a-bayesian-regression-model\/\">here<\/a><\/span><\/strong>).<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Data<\/strong><\/span><\/p>\n<p>As with my <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/mixed-models-in-sport-science-frequentist-bayesian\/\">previous article on mixed models<\/a><\/span><\/strong>, I&#8217;ll be using the <strong><em>sleepstudy<\/em><\/strong> data set, which is freely available in the {<strong>lme4<\/strong>} package. It&#8217;s a convenient data set to use for this project because it has repeated measurements of reaction time during increasing days of sleep deprivation for multiple subjects. Additionally, the role that sleep plays in sport performance is an important topic that is frequently discussed on social media. Therefore, practitioners might be handling similar wearable tracking data for the groups of athletes they work with.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Load Packages &amp; Data\r\nlibrary(tidyverse)\r\n\r\ndat &lt;- lme4::sleepstudy\r\n<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Simple Linear Regression<\/strong><\/span><\/p>\n<p>For the purposes of building the model from the ground up, we will create a simple linear regression that acts as if we don&#8217;t have repeated measurements on individuals. This model will regress the dependent variable, <strong>Reaction<\/strong>, on the independent variable, <strong>Days<\/strong>.<\/p>\n<p>Below we see a plot of the data as well as the output of the regression model.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# plot of the data\r\ndat %&gt;%\r\n  ggplot(aes(x = Days, y = Reaction)) +\r\n  geom_point(size = 3,\r\n              shape = 21,\r\n              fill = &quot;grey&quot;,\r\n              color = &quot;black&quot;) +\r\n  geom_smooth(method = &quot;lm&quot;,\r\n              color = &quot;red&quot;,\r\n              size = 1.2)\r\n\r\n# regression\r\nfit_ols &lt;- lm(Reaction ~ Days, data = dat)\r\nsummary(fit_ols)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2815\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM-1014x1024.png\" alt=\"\" width=\"505\" height=\"510\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM-1014x1024.png 1014w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM-150x150.png 150w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM-297x300.png 297w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM-768x776.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM-624x630.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.14.58-PM.png 1360w\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" \/><\/a> <a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.15.14-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2816\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.15.14-PM.png\" alt=\"\" width=\"516\" height=\"333\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.15.14-PM.png 944w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.15.14-PM-300x193.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.15.14-PM-768x495.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.15.14-PM-624x402.png 624w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/a><\/p>\n<p>Visually we can see a linear relationship indicating that for every additional day of sleep deprivation there is a corresponding increase in Reaction time (IE, the people react slower and become more impaired). The model output tells us that this increase is on the magnitude of roughly 10.5 milliseconds (the beta coefficient for the Days variable). Thus, for every one day increase in sleep deprivation we see a 10.5 millisecond increase in Reaction time, on average (for this population that was tested).<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Making a prediction with the linear model<\/strong><\/span><\/p>\n<p>What if we were interested in predicting the expected reaction time following 5 days of sleep deprivation? We could do this with the <strong>predict() <\/strong>function and we find that we expect the reaction time to be about 304 milliseconds, on average.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.25.49-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2817\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.25.49-PM.png\" alt=\"\" width=\"474\" height=\"72\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.25.49-PM.png 754w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.25.49-PM-300x45.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.25.49-PM-624x94.png 624w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>This is the same as if we used the coefficients from the above model and multiplied the Days coefficient (10.5) by 5 days:<\/p>\n<p style=\"text-align: center;\"><em><strong>251.4 + 10.5 * 5<\/strong><\/em><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.28.09-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2818\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.28.09-PM.png\" alt=\"\" width=\"214\" height=\"61\" \/><\/a><\/p>\n<p>While this may be the average value of reaction time after 5 days of sleep deprivation we can clearly see from our plot above that there is a considerable amount of dispersion around the regression line. If we are interested in understanding the dispersion around this average estimate at the population level, we might use a confidence interval. Alternatively, if we&#8217;d like to understand this dispersion at an individual participant level, we might instead use a prediction interval. I&#8217;ve discussed the differences between these two in a prior blog article (see <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/confidence-intervals-vs-prediction-intervals-a-frequentist-bayesian-example\/\">here<\/a><\/span><\/strong>).<\/p>\n<p>Within the predict function we can pass the <strong>interval<\/strong> argument to indicate whether we want confidence or prediction intervals. Additionally, the <strong>level<\/strong> argument specifies what level of interval we are interested (e.g., 90%, 95%, 99%, etc).<\/p>\n<p>Below we see the output for a 90% Confidence Interval and a 90% Prediction Interval. Notice that the average value (<strong>fit<\/strong>) is the same for both. However, the prediction interval has a much larger 90% width than the confidence interval. For all the details about why this is and the gory calculations behind them, <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/confidence-intervals-vs-prediction-intervals-a-frequentist-bayesian-example\/\">see my previous blog post<\/a><\/span><\/strong>.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.38.53-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2819\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.38.53-PM-1024x254.png\" alt=\"\" width=\"625\" height=\"155\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.38.53-PM-1024x254.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.38.53-PM-300x75.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.38.53-PM-768x191.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.38.53-PM-624x155.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.38.53-PM.png 1360w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><br \/>\nFinally, if we use the <strong>predict()<\/strong> function and set the argument <strong>se.fit<\/strong> to TRUE, we obtain the fitted value and 90% confidence interval (as seen above) along with the standard error and the model degrees of freedom.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.00.20-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2827\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.00.20-PM-1024x306.png\" alt=\"\" width=\"625\" height=\"187\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.00.20-PM-1024x306.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.00.20-PM-300x90.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.00.20-PM-768x230.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.00.20-PM-624x187.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.00.20-PM.png 1504w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>The results are returned in a named list, thus we can extract the standard error and calculate the 90% confidence interval directly. We determine the upper and lower\u00a0 90% confidence limits by multiplying the t-critical value to the standard error. The t-critical value is determined using a t-distribution with 178 degrees of freedom (180 observations minus 2, since our model has 2 parameters: an intercept and a slope). We then add\/subtract the confidence limits from our average predicted value.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Build the 90% confidence interval with the standard error\r\npredict(fit_ols, newdata = data.frame(Days = 5)) + qt(p = c(0.05, 0.95), df = nrow(dat) - 2) * predict(fit_ols, newdata = data.frame(Days =5), interval = 'confidence', level = 0.90, se.fit = TRUE)$se.fit\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.06.07-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2828\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.06.07-PM.png\" alt=\"\" width=\"864\" height=\"152\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.06.07-PM.png 864w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.06.07-PM-300x53.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.06.07-PM-768x135.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.06.07-PM-624x110.png 624w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><br \/>\nWe find that we obtain the exact same results. Fortunately, R makes it relatively easy to build a model and make predictions, so we don&#8217;t need to go through all those steps every time. I was mainly just providing them in the instance that you might need to extract values specifically from the model predictions.<\/p>\n<p>Now that we have the basics down, let&#8217;s extend this to a mixed model and see what happens when we account for repeated measurements on each participant and what that does to our model predictions.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Mixed Model<br \/>\n<\/strong><\/span><\/p>\n<p>To being, we can plot the data for each individual to show that there are a bunch of individual responses to days of sleep deprivation. Then, we construct a mixed model (<strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/mixed-models-in-sport-science-frequentist-bayesian\/\">the same one built in my previous article<\/a><\/span><\/strong>) where we extend the linear regression above to incorporate random intercepts for each individual. Basically, what this means is that we retain the same linear relationship from the model above; however, the random effect now understands that there are several observations for each participant, meaning that those observations have some correlation to each other (within individual). So, we allow the intercept of the linear regression model to vary by the participant based on the information we can glean from their data.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.46.41-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2820\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.46.41-PM-1024x878.png\" alt=\"\" width=\"471\" height=\"404\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.46.41-PM-1024x878.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.46.41-PM-300x257.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.46.41-PM-768x658.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.46.41-PM-624x535.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.46.41-PM.png 1608w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/a> <a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.47.36-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2821\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.47.36-PM.png\" alt=\"\" width=\"421\" height=\"523\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.47.36-PM.png 662w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.47.36-PM-242x300.png 242w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.47.36-PM-624x775.png 624w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/a><\/p>\n<p>Similar to the linear regression model, for every 1 day increase in sleep deprivation we see, on average, a 10.5 millisecond increase in reaction time. However, notice the standard error around the <strong>Days<\/strong> coefficient! In the linear regression model it was 1.24 and we have now shrunk it to 0.804. This is due to accounting for the repeated measures of the data. We can see in the <strong>Random Effects<\/strong> portion of the model output how much of the variance is contained between subjects, around the <strong>(Intercept<\/strong>), and how much of the variance is left unexplained (<strong>Residual<\/strong>).<\/p>\n<p>We can look explicitly at how each individual behaves within the model with the <strong>ranef()<\/strong> function, which gives us the difference between the individual and the model intercept (<strong>251.4<\/strong>). Alternatively, we can use the <strong>coef() <\/strong>function to indicate the linear equation for each participant. This is the model intercept plus the random effect, which we found with the <strong>ranef()<\/strong> function. Also notice that since this is not a random slope model, the <strong>Days<\/strong> coefficient is the same for each participant.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.59.32-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2824\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.59.32-PM.png\" alt=\"\" width=\"456\" height=\"469\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.59.32-PM.png 456w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-6.59.32-PM-292x300.png 292w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Making Predictions with a Mixed Model<\/strong><\/span><\/p>\n<p>Making predictions with the mixed model can be a little tricky. We don&#8217;t only have the linear regression component of the model (often referred to as the <em>fixed effects<\/em>), but we also have a component that references specific individuals. Additionally, it&#8217;s not as easy as just extracting confidence or predictions intervals, as we did before.<\/p>\n<p><strong><em>Making point estimate <\/em><em>predictions<\/em><\/strong><\/p>\n<p>The easiest thing to do is to simply disregard the random effects in the model by passing the <strong>predict()<\/strong> function the argument <strong>re.form = NA<\/strong>. Doing so indicates that at five days of sleep deprivation we see, on average, a 304 millisecond increase in Reaction time (similar to the linear model above).<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-7.08.00-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2825\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-7.08.00-PM.png\" alt=\"\" width=\"523\" height=\"103\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-7.08.00-PM.png 752w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-7.08.00-PM-300x59.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-12-at-7.08.00-PM-624x123.png 624w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/a><br \/>\nIf we are making a prediction for one of the specific participants in our data, for example Subject 308, we can include this in the <strong>predict()<\/strong> function by specifying that we want to use the random effects that were previously set. We do this with the <strong>re.form<\/strong> argument and pass it the exact random effect that we specified when we built the model.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.13.04-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2829\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.13.04-PM.png\" alt=\"\" width=\"960\" height=\"158\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.13.04-PM.png 960w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.13.04-PM-300x49.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.13.04-PM-768x126.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.13.04-PM-624x103.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><br \/>\nNow we see the point estimate prediction is different than the one above it because the prediction contains some information that is known about Subject 308.<\/p>\n<p><em>What if we have new participant that the model has no information on?<\/em><\/p>\n<p>Using the <strong>allow.new.levels<\/strong> argument we can tell the model that we want to allow new random effect levels. In doing so, since the model has no information about the new participant, it will simply return the fixed effects point estimate, just as we got when we specified no random effects in the prediction. This is because the model&#8217;s best guess for a new participant is the population average for 5 days of sleep deprivation.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.16.40-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2830\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.16.40-PM.png\" alt=\"\" width=\"948\" height=\"178\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.16.40-PM.png 948w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.16.40-PM-300x56.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.16.40-PM-768x144.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.16.40-PM-624x117.png 624w\" sizes=\"auto, (max-width: 948px) 100vw, 948px\" \/><\/a><br \/>\nFinally, we can take these point estimate predictions and predict with only the fixed effects and then with the random effects for every participant in our data and then plot the results.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Make predictions using fixed effect only and then random effects and plot the results\r\ndat %&gt;%\r\n  mutate(pred_fixef = predict(fit_lme, newdata = ., re.form = NA),\r\n         pred_ranef = predict(fit_lme, newdata = ., re.form = ~(1|Subject))) %&gt;%\r\n  ggplot(aes(x = Days, y = Reaction)) +\r\n  geom_point(shape = 21,\r\n             size = 2,\r\n             color = &quot;black&quot;,\r\n             fill = &quot;grey&quot;) +\r\n  geom_line(aes(y = pred_fixef),\r\n            color = &quot;blue&quot;,\r\n            size = 1.2) +\r\n  geom_line(aes(y = pred_ranef),\r\n            color = &quot;green&quot;,\r\n            size = 1.2) +\r\n  facet_wrap(~Subject) +\r\n  scale_x_continuous(labels = seq(from = 0, to = 9, by = 3),\r\n                     breaks = seq(from = 0, to = 9, by = 3)) +\r\n  labs(x = &quot;Days of Sleep Deprivation&quot;,\r\n       y = &quot;Average Reaction Time&quot;,\r\n       title = &quot;Reaction Time ~ Days of Sleep Deprivation&quot;,\r\n       subtitle = &quot;Blue = Fixed Effects Prediction | Green = Random Effects Prediction&quot;)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.18.48-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2831\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.18.48-PM-1024x749.png\" alt=\"\" width=\"625\" height=\"457\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.18.48-PM-1024x749.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.18.48-PM-300x219.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.18.48-PM-768x562.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.18.48-PM-624x456.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.18.48-PM.png 1868w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><br \/>\nWe see our fixed effects predictions in blue (which are the same for every participant) and our predictions incorporating random effects in green, which move up and down based on the participant. Also notice that the slope is exactly the same for each participant and each prediction. This is because we only specified a random intercept for this model, which is why the green line moves up and down relative to the fixed effects blue line, given that some participants are higher or lower than the population.<\/p>\n<p><em><strong>Confidence Intervals<\/strong><\/em><\/p>\n<p>To obtain confidence intervals, we can&#8217;t simply specify the\u00a0<strong>interval<\/strong> argument in the <strong>predict() <\/strong>function as we did with linear regression. Instead, we need to bootstrap the predictions using the <strong>bootMer()<\/strong> function. (<strong>NOTE:<\/strong> If you want to always be able to replicate your confidence interval results be sure to <strong>set.seed() <\/strong>prior to running the function).<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nboot_ci &lt;- bootMer(fit_lme,\r\n                   nsim = 100,\r\n                   FUN = function(x) { predict(x, newdata = data.frame(Days = 5), re.form = NA) })\r\n\r\nboot_ci\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.23.43-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2832\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.23.43-PM.png\" alt=\"\" width=\"437\" height=\"214\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.23.43-PM.png 890w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.23.43-PM-300x147.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.23.43-PM-768x376.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.23.43-PM-624x306.png 624w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/a><\/p>\n<p>The <strong>boot_ci<\/strong> element that we created has a number of different parameters contained within it. The &#8216;<strong>t<\/strong>&#8216; parameter contains the 100 bootstrapped resamples that we created.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.25.15-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2833\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.25.15-PM.png\" alt=\"\" width=\"204\" height=\"221\" \/><\/a><\/p>\n<p>With these bootstrapped resamples we can do a number of things such as plotting a histogram.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.26.07-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2834\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.26.07-PM-1024x827.png\" alt=\"\" width=\"510\" height=\"412\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.26.07-PM-1024x827.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.26.07-PM-300x242.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.26.07-PM-768x620.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.26.07-PM-624x504.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.26.07-PM.png 1666w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/a><\/p>\n<p>Or extracting information such as the quantiles, quantile intervals, the standard deviation of the bootstrapped resamples, and using the mean and standard deviation of the reamples to build 90% confidence intervals around the point estimate prediction.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.27.25-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2835\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.27.25-PM.png\" alt=\"\" width=\"499\" height=\"270\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.27.25-PM.png 900w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.27.25-PM-300x162.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.27.25-PM-768x415.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.27.25-PM-624x337.png 624w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/a><\/p>\n<p><em><strong>Prediction Intervals<\/strong><\/em><\/p>\n<p>We can create prediction intervals around our point estimate predictions using the <strong>merTools<\/strong> package and the <strong>predictInterval()<\/strong> function.<\/p>\n<p>With the <strong>predictInterval() <\/strong>function we need to specify a Subject, since that is what the original mixed model also had in its equation (as a random effect). Here I&#8217;ll specify a new subject that the model has never seen. The model will return prediction intervals for the point estimate prediction using only fixed effects given that it doesn&#8217;t have data on this subject (it will also let me know this in the warning).<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.30.45-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2836\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.30.45-PM.png\" alt=\"\" width=\"553\" height=\"256\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.30.45-PM.png 998w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.30.45-PM-300x139.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.30.45-PM-768x356.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.30.45-PM-624x289.png 624w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/a><br \/>\nOf course, if we are making a prediction on a subject that the model is familiar with, we can use that information. Here I make a prediction on Subject 308 and we see that the results differ from above.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.32.07-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2837\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.32.07-PM.png\" alt=\"\" width=\"972\" height=\"274\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.32.07-PM.png 972w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.32.07-PM-300x85.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.32.07-PM-768x216.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.32.07-PM-624x176.png 624w\" sizes=\"auto, (max-width: 972px) 100vw, 972px\" \/><\/a><br \/>\nFinally, just for fun and to show we can do this at scale, we will make a prediction on every single participant in our data across all of the days of sleep deprivation. We will bind those predictions and their corresponding 90% prediction intervals to the original data and then plot the results.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\npred_ints &lt;- predictInterval(fit_lme, \r\n                newdata = dat, \r\n                n.sims = 100,\r\n                returnSims = TRUE, \r\n                seed = 123, \r\n                level = 0.90)\r\n\r\ndat_new &lt;- cbind(dat, pred_ints) dat_new %&gt;%\r\n  head()\r\n\r\n# plot predictions\r\ndat_new %&gt;%\r\n  ggplot(aes(x = Days, y = Reaction)) +\r\n  geom_point(shape = 21,\r\n             size = 2,\r\n             color = &quot;black&quot;,\r\n             fill = &quot;grey&quot;) +\r\n  geom_ribbon(aes(ymin = lwr, ymax = upr),\r\n            fill = 'light grey',\r\n            alpha = 0.4) +\r\n  geom_line(aes(y = fit),\r\n            color = 'red',\r\n            size = 1.2) +\r\n  facet_wrap(~Subject) +\r\n  scale_x_continuous(labels = seq(from = 0, to = 9, by = 3),\r\n                     breaks = seq(from = 0, to = 9, by = 3)) +\r\n  labs(x = &quot;Days of Sleep Deprivation&quot;,\r\n       y = &quot;Average Reaction Time&quot;,\r\n       title = &quot;Reaction Time ~ Days of Sleep Deprivation&quot;)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.34.39-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2838\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.34.39-PM-1024x747.png\" alt=\"\" width=\"625\" height=\"456\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.34.39-PM-1024x747.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.34.39-PM-300x219.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.34.39-PM-768x560.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.34.39-PM-624x455.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/01\/Screen-Shot-2023-01-13-at-6.34.39-PM.png 1868w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Wrapping Up<\/strong><\/span><\/p>\n<p>Mixed models are an incredibly valuable analysis tools. In sport medicine and sport science, models are often helpful for allowing practitioners to make forecasts about how an athlete is progressing or to understand how an athlete is performing relative to what might be expected. Hopefully this article was useful in walking through how to make point estimate predictions and obtain confidence and prediction intervals from mixed models. If you notice any errors, feel free to reach out!<\/p>\n<p>As always, all of the code if available on my <span style=\"color: #0000ff;\"><strong><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/Mixed-Models\/blob\/main\/Making%20Predicitons%20with%20a%20Mixed%20Model%20using%20R.R\">GITHUB page<\/a><\/strong><\/span>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Models are built for different reasons. Some models are built to make inferences and explore an underlying phenomenon while other models are built for making predictions and forecasting future outcomes. Often, in the applied sport science setting, the latter is a key goal as we are interested in future predictions that can assist practitioners in [&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],"tags":[],"class_list":["post-2811","post","type-post","status-publish","format-standard","hentry","category-model-building-in-r"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2811","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=2811"}],"version-history":[{"count":5,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2811\/revisions"}],"predecessor-version":[{"id":2840,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2811\/revisions\/2840"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=2811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=2811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=2811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}