{"id":380240,"date":"2023-11-21T23:00:00","date_gmt":"2023-11-22T05:00:00","guid":{"rendered":"https:\/\/www.spsanderson.com\/steveondata\/posts\/2023-11-22\/index.html"},"modified":"2023-11-21T23:00:00","modified_gmt":"2023-11-22T05:00:00","slug":"unveiling-the-magic-of-loess-regression-in-r-a-step-by-step-guide-with-mtcars","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2023\/11\/unveiling-the-magic-of-loess-regression-in-r-a-step-by-step-guide-with-mtcars\/","title":{"rendered":"Unveiling the Magic of LOESS Regression in R: A Step-by-Step Guide with mtcars"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"https:\/\/www.spsanderson.com\/steveondata\/posts\/2023-11-22\/index.html\"> Steve&#039;s Data Tips and Tricks<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n \n\n\n\n\n<section id=\"introduction\" class=\"level1\">\n<h1>Introduction<\/h1>\n<p>If you\u2019ve ever found yourself grappling with noisy data and yearning for a smoother representation, LOESS regression might be the enchanting solution you\u2019re seeking. In this blog post, we\u2019ll unravel the mysteries of LOESS regression using the power of R, and walk through a practical example using the iconic <code>mtcars<\/code> dataset.<\/p>\n<\/section>\n<section id=\"what-is-loess-regression\" class=\"level1\">\n<h1>What is LOESS Regression?<\/h1>\n<p>LOESS, which stands for LOcal regrESSion, is a versatile and powerful technique for fitting a curve to a set of data points. Unlike traditional linear regression, LOESS adapts to the local behavior of the data, making it perfect for capturing intricate patterns in noisy datasets.<\/p>\n<\/section>\n<section id=\"getting-started-loading-the-mtcars-dataset\" class=\"level1\">\n<h1>Getting Started: Loading the mtcars Dataset<\/h1>\n<p>Let\u2019s kick off our journey by loading the <code>mtcars<\/code> dataset. This dataset, featuring various car specifications, will serve as our canvas for the LOESS magic.<\/p>\n<pre># Load the mtcars dataset\ndata(mtcars)<\/pre>\n<section id=\"understanding-loess-the-basics\" class=\"level2\">\n<h2 class=\"anchored\" data-anchor-id=\"understanding-loess-the-basics\">Understanding LOESS: The Basics<\/h2>\n<p>Now, let\u2019s delve into the heart of LOESS regression. In R, the magic happens with the <code>loess()<\/code> function. This function fits a smooth curve through your data, adjusting to the local characteristics.<\/p>\n<div class=\"cell\">\n<pre># Fit a LOESS model\nloess_model &lt;- loess(mpg ~ wt, data = mtcars)<\/pre>\n<\/div>\n<p>Congratulations, you\u2019ve just cast the LOESS spell on the fuel efficiency and weight relationship of these iconic cars!<\/p>\n<\/section>\n<section id=\"visualizing-the-enchantment\" class=\"level2\">\n<h2 class=\"anchored\" data-anchor-id=\"visualizing-the-enchantment\">Visualizing the Enchantment<\/h2>\n<p>What good is magic if you can\u2019t see it? Let\u2019s visualize the results with a compelling plot.<\/p>\n<div class=\"cell\">\n<pre># Generate predictions from the LOESS model\npredictions &lt;- predict(loess_model, newdata = mtcars)\npredictions &lt;- cbind(mtcars, predictions)\npredictions &lt;- predictions[order(predictions$wt), ]\n\n# Create a scatter plot of the original data\nplot(\n  predictions$wt,\n  predictions$mpg, \n  col = &quot;blue&quot;, \n  main = &quot;LOESS Regression: Unveiling the Magic with mtcars&quot;, \n  xlab = &quot;Weight (1000 lbs)&quot;, \n  ylab = &quot;Miles Per Gallon&quot;\n)\n\n# Add the LOESS curve to the plot\nlines(predictions$predictions, col = &quot;red&quot;, lwd = 2)<\/pre>\n<div class=\"cell-output-display\">\n<p><img src=\"https:\/\/i2.wp.com\/www.spsanderson.com\/steveondata\/posts\/2023-11-22\/index_files\/figure-html\/unnamed-chunk-2-1.png?w=450&#038;ssl=1\" class=\"img-fluid\"  data-recalc-dims=\"1\"><\/p>\n<\/div>\n<\/div>\n<p>Behold, as the red curve gracefully dances through the blue points, smoothing out the rough edges and revealing the underlying trends in the relationship between weight and fuel efficiency.<\/p>\n<p>Now, we did not specify any parameters for the <code>loess()<\/code> function, so it used the default values. Let\u2019s take a look at the default parameters.<\/p>\n<pre>loess(formula, data, weights, subset, na.action, model = FALSE,\n      span = 0.75, enp.target, degree = 2,\n      parametric = FALSE, drop.square = FALSE, normalize = TRUE,\n      family = c(&quot;gaussian&quot;, &quot;symmetric&quot;),\n      method = c(&quot;loess&quot;, &quot;model.frame&quot;),\n      control = loess.control(...), ...)<\/pre>\n<p>If you want to see the documentation in R you can use <code>?loess<\/code> or <code>help(loess)<\/code>. I have it here for you anyways but it is good to know how to check it on the fly:<\/p>\n<p><em>Arguments<\/em> <code>formula<\/code> - a formula specifying the numeric response and one to four numeric predictors (best specified via an interaction, but can also be specified additively). Will be coerced to a formula if necessary.<\/p>\n<p><code>data<\/code> - an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which loess is called.<\/p>\n<p><code>weights<\/code> - optional weights for each case.<\/p>\n<p><code>subset<\/code> - an optional specification of a subset of the data to be used.<\/p>\n<p><code>na.action<\/code> - the action to be taken with missing values in the response or predictors. The default is given by getOption(\u201cna.action\u201d).<\/p>\n<p><code>model<\/code> - should the model frame be returned?<\/p>\n<p><code>span<\/code> - the parameter \u03b1 which controls the degree of smoothing.<\/p>\n<p><code>enp.target<\/code> - an alternative way to specify span, as the approximate equivalent number of parameters to be used.<\/p>\n<p><code>degree<\/code> - the degree of the polynomials to be used, normally 1 or 2. (Degree 0 is also allowed, but see the \u2018Note\u2019.)<\/p>\n<p><code>parametric<\/code> - should any terms be fitted globally rather than locally? Terms can be specified by name, number or as a logical vector of the same length as the number of predictors.<\/p>\n<p><code>drop.square<\/code> - for fits with more than one predictor and degree = 2, should the quadratic term be dropped for particular predictors? Terms are specified in the same way as for parametric.<\/p>\n<p><code>normalize<\/code> - should the predictors be normalized to a common scale if there is more than one? The normalization used is to set the 10% trimmed standard deviation to one. Set to false for spatial coordinate predictors and others known to be on a common scale.<\/p>\n<p><code>family<\/code> - if \u201cgaussian\u201d fitting is by least-squares, and if \u201csymmetric\u201d a re-descending M estimator is used with Tukey\u2019s biweight function. Can be abbreviated.<\/p>\n<p><code>method<\/code> - fit the model or just extract the model frame. Can be abbreviated.<\/p>\n<p><code>control<\/code> - control parameters: see loess.control.<\/p>\n<p><code>...<\/code> - control parameters can also be supplied directly (if control is not specified).<\/p>\n<p>Now that we see we can set things like <code>span<\/code> and <code>degree<\/code> let\u2019s try it out.<\/p>\n<div class=\"cell\">\n<pre># Create the data frame\ndf &lt;- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), \n                 y=c(1, 4, 7, 13, 19, 24, 20, 15, 13, 11, 15, 18, 22, 27))\n\n# Fit LOESS regression models\nloess50 &lt;- loess(y ~ x, data=df, span=0.5)\nsmooth50 &lt;- predict(loess50)\nloess75 &lt;- loess(y ~ x, data=df, span=0.75)\nsmooth75 &lt;- predict(loess75)\nloess90 &lt;- loess(y ~ x, data=df, span=0.9)\nsmooth90 &lt;- predict(loess90)\nloess50_degree1 &lt;- loess(y ~ x, data=df, span=0.5, degree=1)\nsmooth50_degree1 &lt;- predict(loess50_degree1)\nloess50_degree2 &lt;- loess(y ~ x, data=df, span=0.5, degree=2)\nsmooth50_degree2 &lt;- predict(loess50_degree2)\n\n# Create scatterplot with each regression line overlaid\nplot(df$x, df$y, pch=19, main='Loess Regression Models')\nlines(smooth50, x=df$x, col='red')\nlines(smooth75, x=df$x, col='purple')\nlines(smooth90, x=df$x, col='blue')\nlines(smooth50_degree1, x=df$x, col='green')\nlines(smooth50_degree2, x=df$x, col='orange')<\/pre>\n<div class=\"cell-output-display\">\n<p><img src=\"https:\/\/i2.wp.com\/www.spsanderson.com\/steveondata\/posts\/2023-11-22\/index_files\/figure-html\/unnamed-chunk-3-1.png?w=450&#038;ssl=1\" class=\"img-fluid\"  data-recalc-dims=\"1\"><\/p>\n<\/div>\n<\/div>\n<\/section>\n<section id=\"empowering-you-try-it-yourself\" class=\"level2\">\n<h2 class=\"anchored\" data-anchor-id=\"empowering-you-try-it-yourself\">Empowering You: Try It Yourself!<\/h2>\n<p>Now comes the most exciting part \u2013 empowering you to wield the magic wand with the <code>mtcars<\/code> dataset or any other dataset of your choice. Encourage your readers to try the code on their own datasets, and witness the transformative power of LOESS regression.<\/p>\n<pre># Your readers can replace this with their own dataset\nuser_data &lt;- read.csv(&quot;user_dataset.csv&quot;)\n\n# Fit a LOESS model on their data\nuser_loess_model &lt;- loess(Y ~ X, data = user_data)\n\n# Visualize the results\nuser_predictions &lt;- predict(user_loess_model, newdata = user_data)\nplot(user_data$X, user_data$Y, col = &quot;green&quot;, main = &quot;Your Turn: Unleash LOESS Magic&quot;, xlab = &quot;X&quot;, ylab = &quot;Y&quot;)\nlines(user_data$X, user_predictions, col = &quot;purple&quot;, lwd = 2)<\/pre>\n<\/section>\n<\/section>\n<section id=\"conclusion\" class=\"level1\">\n<h1>Conclusion<\/h1>\n<p>In this journey, we\u2019ve walked through the fundamentals of LOESS regression in R, witnessed its magic in action using the iconic <code>mtcars<\/code> dataset, and now it\u2019s your turn to wield the wand. As you embark on your own adventures with LOESS, remember that this enchanting technique adapts to the nuances of your data, revealing hidden patterns and smoothing the way for clearer insights.<\/p>\n<p>Happy coding, and may the LOESS magic be with you! \ud83d\ude97\u2728<\/p>\n\n\n<\/section>\n\n \n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/www.spsanderson.com\/steveondata\/posts\/2023-11-22\/index.html\"> Steve&#039;s Data Tips and Tricks<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<div style = \"width:60%; display: inline-block; float:left; \">\n<p>Introduction<br \/>\nIf you\u2019ve ever found yourself grappling with noisy data and yearning for a smoother representation, LOESS regression might be the enchanting solution you\u2019re seeking. In this blog post, we\u2019ll unravel the mysteries of LOESS regression&#8230;<\/p><\/div>\n<div style = \"width: 40%; display: inline-block; float:right;\"><\/div>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"author":2847,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/380240"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/2847"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=380240"}],"version-history":[{"count":1,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/380240\/revisions"}],"predecessor-version":[{"id":380241,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/380240\/revisions\/380241"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=380240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=380240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=380240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}