{"id":3069,"date":"2023-05-15T05:49:08","date_gmt":"2023-05-15T05:49:08","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=3069"},"modified":"2023-05-15T12:02:51","modified_gmt":"2023-05-15T12:02:51","slug":"different-ways-of-calculating-intervals-of-uncertainty","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/different-ways-of-calculating-intervals-of-uncertainty\/","title":{"rendered":"Different ways of calculating intervals of uncertainty"},"content":{"rendered":"<p>I&#8217;ve talked a lot in this blog about making predictions (see <span style=\"color: #0000ff;\"><strong><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/approximating-a-bayesian-posterior-prediction\/\">HERE<\/a><\/strong><\/span>, <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>, and <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/making-predictions-from-a-mixed-model-using-r\/\">HERE<\/a><\/span><\/strong>) as well as the difference between confidence intervals and prediction intervals and why you&#8217;d use one over the other (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><span style=\"color: #000000;\">). Tonight I was having a discussion with a colleague about some models he was working on and he was building some confidence intervals around his predictions. That got me to thinking about the various ways we can code confidence intervals, quantile intervals, and prediction intervals in R. So, I decided to put together this quick tutorial to provide a few different ways of constructing these values (after all, unless we can calculate the uncertainty in our predictions, point estimate predictions are largely useless on their own).<\/span><\/p>\n<p>The full code is available on my <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/R-Tips-Tricks\/blob\/master\/Calculating%20Intervals%20of%20Uncertainty.R\">GITHUB page<\/a><\/span><\/strong>.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Load packages, get data, and fit regression model<\/strong><\/span><\/p>\n<p>The only package we will need is {<strong>tidyverse<\/strong>}, the data will be the <strong>mtcars<\/strong> dataset and the model will be a linear regression which attempts to predict mpg from wr and carb.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Load packages\r\nlibrary(tidyverse)\r\n\r\ntheme_set(theme_classic())\r\n\r\n## Get data\r\nd &lt;- mtcars d %&gt;%\r\n  head()\r\n\r\n## fit model\r\nfit_lm &lt;- lm(mpg ~ wt + carb, data = d)\r\nsummary(fit_lm)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.22-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3070\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.22-PM.png\" alt=\"\" width=\"515\" height=\"357\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.22-PM.png 938w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.22-PM-300x208.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.22-PM-768x532.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.22-PM-624x432.png 624w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Get some data to make predictions on<\/strong><\/span><\/p>\n<p>We will just grab a random sample of 5 rows from the original data set and use that to make some predictions on.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Get a few rows to make predictions on\r\nset.seed(1234)\r\nd_sample &lt;- d %&gt;%\r\n  sample_n(size = 5) %&gt;%\r\n  select(mpg, wt, carb)\r\n\r\nd_sample\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.54-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3071\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.54-PM.png\" alt=\"\" width=\"392\" height=\"200\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.54-PM.png 502w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.10.54-PM-300x153.png 300w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Confidence Intervals with the predict() function<\/strong><\/span><\/p>\n<p>Using <strong>preidct() <\/strong>we calculate the predicted value with 95% Confidence Intervals.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## 95% Confidence Intervals\r\nd_sample %&gt;%\r\n  bind_cols(\r\n    predict(fit_lm, newdata = d_sample, interval = &quot;confidence&quot;, level = 0.95)\r\n  )\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.12.49-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3072\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.12.49-PM.png\" alt=\"\" width=\"543\" height=\"126\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.12.49-PM.png 894w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.12.49-PM-300x70.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.12.49-PM-768x179.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.12.49-PM-624x145.png 624w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/a><br \/>\n<span style=\"text-decoration: underline;\"><strong>Calculate confidence intervals by hand<\/strong><\/span><\/p>\n<p>Instead of using the R function, we can calculate the confidence intervals by hand (and obtain the same result).<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Calculate the 95% confidence interval by hand\r\nlevel &lt;- 0.95\r\nalpha &lt;- 1 - (1 - level) \/ 2\r\nt_crit &lt;- qt(p = alpha, df = fit_lm$df.residual) \r\n\r\nd_sample %&gt;%\r\n  mutate(pred = predict(fit_lm, newdata = .),\r\n         se_pred = predict(fit_lm, newdata = ., se = TRUE)$se.fit,\r\n         cl95 = t_crit * se_pred,\r\n         lwr = pred - cl95,\r\n         upr = pred + cl95)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.15.30-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3073\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.15.30-PM-1024x190.png\" alt=\"\" width=\"625\" height=\"116\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.15.30-PM-1024x190.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.15.30-PM-300x56.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.15.30-PM-768x143.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.15.30-PM-624x116.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.15.30-PM.png 1172w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><br \/>\n<span style=\"text-decoration: underline;\"><strong>Calculate confidence intervals with the qnorm() function<\/strong><\/span><\/p>\n<p>Above, we calculated a 95% t-critical value for the degrees of freedom of our model. Alternatively, we could calculate 95% confidence intervals using the standard z-critical value for 95%, 1.96, which we obtain with the <strong>qnorm()<\/strong> function.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nd_sample %&gt;%\r\n  mutate(pred = predict(fit_lm, newdata = .),\r\n         se_pred = predict(fit_lm, newdata = ., se = TRUE)$se.fit,\r\n         lwr = pred + qnorm(p = 0.025, mean = 0, sd = 1) * se_pred,\r\n         upr = pred + qnorm(p = 0.975, mean = 0, sd = 1) * se_pred)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.21.10-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3074\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.21.10-PM.png\" alt=\"\" width=\"1014\" height=\"204\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.21.10-PM.png 1014w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.21.10-PM-300x60.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.21.10-PM-768x155.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.21.10-PM-624x126.png 624w\" sizes=\"auto, (max-width: 1014px) 100vw, 1014px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Calculate quantile intervals via simulation<\/strong><\/span><\/p>\n<p>Finally, we can calculate quantile intervals by simulating predictions using the predicted value and standard error for each of the observations. We simulate 1000 times from a normal distribution and then use the <strong>quantile()<\/strong> function to get our quantile intervals.<\/p>\n<p>If all we care about is a predicted value and the lower and upper intervals, we can use the <strong>rowwise()<\/strong> function to indicate that we are going to do a simulation for each row and then store the end result (our lower and upper quantile intervals) in a new column.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## 95% Quantile Intervals via Simulation\r\nd_sample %&gt;%\r\n  mutate(pred = predict(fit_lm, newdata = .),\r\n         se_pred = predict(fit_lm, newdata = ., se = TRUE)$se.fit) %&gt;%\r\n  rowwise() %&gt;%\r\n  mutate(lwr = quantile(rnorm(n = 1000, mean = pred, sd = se_pred), probs = 0.025),\r\n         upr = quantile(rnorm(n = 1000, mean = pred, sd = se_pred), probs = 0.975))\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.23.52-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3075\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.23.52-PM.png\" alt=\"\" width=\"507\" height=\"190\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.23.52-PM.png 710w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.23.52-PM-300x112.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.23.52-PM-624x234.png 624w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/a><\/p>\n<p>While that is useful, there might be times where we want to extract the full simulated distribution. We can create a simulated distribution (1000 simulations) for each of the 5 observations using a <strong>for()<\/strong> loop.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## 95% quantile intervals via Simulation with full distribution\r\nN &lt;- 1000\r\npred_sim &lt;- list()\r\n\r\nset.seed(8945)\r\nfor(i in 1:nrow(d_sample)){\r\n  \r\n  pred &lt;- predict(fit_lm, newdata = d_sample&#x5B;i, ])\r\n  se_pred &lt;- predict(fit_lm, newdata = d_sample&#x5B;i, ], se = TRUE)$se.fit\r\n  \r\n  pred_sim&#x5B;&#x5B;i]] &lt;- rnorm(n = N, mean = pred, sd = se_pred)\r\n  \r\n}\r\n\r\nsim_df &lt;- tibble( sample_row = rep(1:5, each = N), pred_sim = unlist(pred_sim) ) \r\n\r\nsim_df %&gt;%\r\n  head()\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.25.37-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3076\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.25.37-PM.png\" alt=\"\" width=\"256\" height=\"216\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.25.37-PM.png 334w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.25.37-PM-300x253.png 300w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/a><br \/>\nNext we summarize the simulation for each observation.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# get predictions and quantile intervals\r\nsim_df %&gt;%\r\n  group_by(sample_row) %&gt;%\r\n  summarize(pred = mean(pred_sim),\r\n         lwr = quantile(pred_sim, probs = 0.025),\r\n         upr = quantile(pred_sim, probs = 0.975)) %&gt;%\r\n  mutate(sample_row = rownames(d_sample))\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.27.38-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3077\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.27.38-PM.png\" alt=\"\" width=\"426\" height=\"200\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.27.38-PM.png 528w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.27.38-PM-300x141.png 300w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/a><br \/>\nWe can then plot the entire posterior distribution for each observation.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# plot the predicted distributions\r\nsim_df %&gt;%\r\n  mutate(actual_value = rep(d_sample$mpg, each = N),\r\n         sample_row = case_when(sample_row == 1 ~ &quot;Hornet 4 Drive&quot;,\r\n                                sample_row == 2 ~ &quot;Toyota Corolla&quot;,\r\n                                sample_row == 3 ~ &quot;Honda Civic&quot;,\r\n                                sample_row == 4 ~ &quot;Ferrari Dino&quot;,\r\n                                sample_row == 5 ~ &quot;Pontiac Firebird&quot;)) %&gt;%\r\n  ggplot(aes(x = pred_sim)) +\r\n  geom_histogram(color = &quot;white&quot;,\r\n                 fill = &quot;light grey&quot;) +\r\n  geom_vline(aes(xintercept = actual_value),\r\n             color = &quot;red&quot;,\r\n             size = 1.2,\r\n             linetype = &quot;dashed&quot;) +\r\n  facet_wrap(~sample_row, scale = &quot;free_x&quot;) +\r\n  labs(x = &quot;Predicted Simulation&quot;,\r\n       y = &quot;count&quot;,\r\n       title = &quot;Predicted Simulation with actual observation (red line)&quot;,\r\n       subtitle = &quot;Note that the x-axis are specific to that simulation and not the same&quot;)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.33.00-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3078\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.33.00-PM-1024x1013.png\" alt=\"\" width=\"625\" height=\"618\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.33.00-PM-1024x1013.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.33.00-PM-300x297.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.33.00-PM-768x760.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.33.00-PM-624x617.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.33.00-PM.png 1464w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Prediction Intervals with the predict() function<\/strong><\/span><\/p>\n<p>Next we turn attention to prediction intervals, which will be wider than the confidence intervals because they are incorporating additional uncertainty.<\/p>\n<p>The <strong>predict()<\/strong> function makes calculating prediction intervals very convenient.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## 95% Prediction Intervals\r\nd_sample %&gt;%\r\n  bind_cols(\r\n    predict(fit_lm, newdata = d_sample, interval = &quot;predict&quot;, level = 0.95)\r\n  )\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.35.14-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3079\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.35.14-PM.png\" alt=\"\" width=\"564\" height=\"139\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.35.14-PM.png 908w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.35.14-PM-300x74.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.35.14-PM-768x189.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.35.14-PM-624x154.png 624w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Prediction Intervals from a simulated distribution<\/strong><\/span><\/p>\n<p>Similar to how we simulated a distribution for calculating quantile intervals, above, we will perform the same procedure here. The difference is that we need to get the residual standard error (RSE) from our model as we need to add this additional piece of uncertainty (on top of the predicted standard error) to each of the simulated predictions.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## 95% prediction intervals from a simulated distribution \r\n# store the model residual standard error\r\nsigma &lt;- summary(fit_lm)$sigma\r\n\r\n# run simulation\r\nN &lt;- 1000\r\npred_sim2 &lt;- list()\r\n\r\nset.seed(85)\r\nfor(i in 1:nrow(d_sample)){\r\n  \r\n  pred &lt;- predict(fit_lm, newdata = d_sample&#x5B;i, ])\r\n  se_pred &lt;- predict(fit_lm, newdata = d_sample&#x5B;i, ], se = TRUE)$se.fit\r\n  \r\n  pred_sim2&#x5B;&#x5B;i]] &lt;- rnorm(n = N, mean = pred, sd = se_pred) + rnorm(n = N, mean = 0, sd = sigma)\r\n  \r\n}\r\n\r\n# put results in a data frame\r\nsim_df2 &lt;- tibble( sample_row = rep(1:5, each = N), pred_sim2 = unlist(pred_sim2) ) \r\n\r\nsim_df2 %&gt;%\r\n  head()\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.38.11-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3080\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.38.11-PM.png\" alt=\"\" width=\"267\" height=\"220\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.38.11-PM.png 354w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.38.11-PM-300x247.png 300w\" sizes=\"auto, (max-width: 267px) 100vw, 267px\" \/><\/a><\/p>\n<p>We summarize our predictions and their intervals.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# get predictions and intervals\r\nsim_df2 %&gt;%\r\n  group_by(sample_row) %&gt;%\r\n  summarize(pred = mean(pred_sim2),\r\n            lwr = quantile(pred_sim2, probs = 0.025),\r\n            upr = quantile(pred_sim2, probs = 0.975)) %&gt;%\r\n  mutate(sample_row = rownames(d_sample))\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.39.16-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3081\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.39.16-PM.png\" alt=\"\" width=\"407\" height=\"205\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.39.16-PM.png 528w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.39.16-PM-300x151.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/a><\/p>\n<p>Finally, we plot the simulated distributions for each of the observations.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.41.02-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3082\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.41.02-PM-1024x941.png\" alt=\"\" width=\"625\" height=\"574\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.41.02-PM-1024x941.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.41.02-PM-300x276.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.41.02-PM-768x706.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.41.02-PM-624x573.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-14-at-10.41.02-PM.png 1576w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Wrapping Up<\/strong><\/span><\/p>\n<p>Uncertainty is important to be aware of and convey whenever you share your predictions. The point estimate prediction is one a single value of many plausible values given the data generating process. This article provided a few different approaches for calculating uncertainty intervals. The full code is available on my <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/R-Tips-Tricks\/blob\/master\/Calculating%20Intervals%20of%20Uncertainty.R\">GITHUB page<\/a><\/span><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve talked a lot in this blog about making predictions (see HERE, HERE, and HERE) as well as the difference between confidence intervals and prediction intervals and why you&#8217;d use one over the other (see HERE). Tonight I was having a discussion with a colleague about some models he was working on and he was [&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],"tags":[],"class_list":["post-3069","post","type-post","status-publish","format-standard","hentry","category-model-building-in-r","category-sports-analytics"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3069","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=3069"}],"version-history":[{"count":1,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3069\/revisions"}],"predecessor-version":[{"id":3083,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3069\/revisions\/3083"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=3069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=3069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=3069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}