{"id":2319,"date":"2020-03-16T03:46:07","date_gmt":"2020-03-16T00:46:07","guid":{"rendered":"http:\/\/kusuaks7\/?p=1924"},"modified":"2023-12-29T10:20:18","modified_gmt":"2023-12-29T10:20:18","slug":"how-to-measure-the-goodness-of-a-regression-model","status":"publish","type":"post","link":"https:\/\/www.experfy.com\/blog\/ai-ml\/how-to-measure-the-goodness-of-a-regression-model\/","title":{"rendered":"How To Measure The Goodness Of A Regression Model"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"2319\" class=\"elementor elementor-2319\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"has_eae_slider elementor-section elementor-top-section elementor-element elementor-element-1ce5a43e elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"1ce5a43e\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"has_eae_slider elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-7bf28176\" data-id=\"7bf28176\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-eac037e elementor-widget elementor-widget-text-editor\" data-id=\"eac037e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<section>\n<h2 style=\"color: #aaa; font-style: italic;\">A simple study on how to check the statistical goodness of a regression model.<\/h2>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-89691fa elementor-widget elementor-widget-heading\" data-id=\"89691fa\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\"><h3 style=\"color: #aaaaaa;font-style: italic;text-align: center\"><img decoding=\"async\" class=\"alignnone\" src=\"https:\/\/miro.medium.com\/max\/5184\/0*s_6m3X8IQCdCVH-i\" alt=\"How To Measure The Goodness Of A Regression Model\" \/><\/h3><\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2ea99e2 elementor-widget elementor-widget-heading\" data-id=\"2ea99e2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\"><h3 style=\"color: #aaaaaa;font-style: italic;text-align: center\">Photo by\u00a0<a style=\"font-size: 13px\" href=\"https:\/\/unsplash.com\/@antoine1003?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener\">Antoine Dautry<\/a><span style=\"color: #333333;font-size: 13px\">\u00a0on\u00a0<\/span><a style=\"font-size: 13px\" href=\"https:\/\/unsplash.com\/?utm_source=medium&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener\">Unsplash<\/a><\/h3><\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5fb07ff elementor-widget elementor-widget-text-editor\" data-id=\"5fb07ff\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"8d07\" data-selectable-paragraph=\"\"><strong>Regression models\u00a0<\/strong>are very useful and widely used in machine learning. However, they might show some problems when comes to measure the\u00a0<strong>goodness\u00a0<\/strong>of a trained model. While classification models have some\u00a0<strong>standard tools<\/strong>\u00a0that can be used to assess their performance (i.e. area under the ROC curve, confusion matrix, F-1 score etc.), regression models\u2019 performance can be measured in many different ways. In this article, I\u2019ll show you some techniques I\u2019ve used in my experience as a Data Scientist.<\/p>\n<\/section>\n<hr \/>\n<section>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f34e4b0 elementor-widget elementor-widget-heading\" data-id=\"f34e4b0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\"><h2 id=\"6248\">Example in R<\/h2><\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5d4479e elementor-widget elementor-widget-text-editor\" data-id=\"5d4479e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"4241\" data-selectable-paragraph=\"\">In this example, I\u2019ll show you how to measure the goodness of a trained model using the famous\u00a0<strong>iris dataset<\/strong>. I\u2019ll use a\u00a0<strong>linear regression model<\/strong>\u00a0to predict the value of the Sepal Length as a function of the other variables.<\/p>\n<p id=\"89b3\" data-selectable-paragraph=\"\">First, we\u2019ll load the iris dataset and split it in training and holdout.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f6699eb elementor-widget elementor-widget-text-editor\" data-id=\"f6699eb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">data(iris)<br \/>set.seed(1)training_idx = sample(1:nrow(iris),nrow(iris)*0.8,replace=FALSE)<br \/>holdout_idx = setdiff(1:nrow(iris),training_idx)training = iris[training_idx,]<br \/>holdout = iris[holdout_idx,]<\/span><\/div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2a911f0 elementor-widget elementor-widget-text-editor\" data-id=\"2a911f0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-selectable-paragraph=\"\">\u00a0<\/p>\n<p id=\"14a0\" data-selectable-paragraph=\"\">Then we can perform a simple\u00a0<strong>linear regression<\/strong>\u00a0in order to describe the variable\u00a0<strong>Sepal.Length<\/strong>\u00a0as a linear function of the others. This is the model we want to check the goodness of.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3ca3abc elementor-widget elementor-widget-text-editor\" data-id=\"3ca3abc\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre>m = lm(Sepal.Length ~ .,training)<\/pre>\n<p id=\"aeb0\" data-selectable-paragraph=\"\">All we need to do now is compare the\u00a0<strong>residuals\u00a0<\/strong>in the training set with the residuals in the holdout. Remember that the residuals are the\u00a0<strong>differences\u00a0<\/strong>between the real value and the predicted value.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-08d6d63 elementor-widget elementor-widget-text-editor\" data-id=\"08d6d63\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">training_res = training$Sepal.Length &#8211; predict(m,training)<br \/>holdout_res = holdout$Sepal.Length &#8211; predict(m,holdout)<\/span><\/div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fc81a19 elementor-widget elementor-widget-text-editor\" data-id=\"fc81a19\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-selectable-paragraph=\"\">\u00a0<\/p>\n<p id=\"f80c\" data-selectable-paragraph=\"\">If our training procedure has produced\u00a0<strong>overfitting<\/strong>, the residuals in the training set will be\u00a0<strong>very small<\/strong>\u00a0compared with the residuals in the holdout. That\u2019s a negative signal that should invite us to\u00a0<strong>simplify\u00a0<\/strong>the model or\u00a0<strong>remove\u00a0<\/strong>some variables.<\/p>\n<p id=\"ddff\" data-selectable-paragraph=\"\">Let\u2019s now perform some statistical checks.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ffc7f81 elementor-widget elementor-widget-heading\" data-id=\"ffc7f81\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\"><h3 id=\"e326\">t-test<\/h3><\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3057d26 elementor-widget elementor-widget-text-editor\" data-id=\"3057d26\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"802b\" data-selectable-paragraph=\"\">The first thing we have to check is whether the residuals are biased or not. We know from elementary statistics that the\u00a0<strong>mean value<\/strong>\u00a0of the residuals is\u00a0<strong>zero<\/strong>, so we can start checking with a\u00a0<strong>Student\u2019s t-test\u00a0<\/strong>if it\u2019s true or not for our holdout sample.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7c98e56 elementor-widget elementor-widget-text-editor\" data-id=\"7c98e56\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre>t.test(holdout_res,mu=0)<\/pre>\n<p style=\"text-align: center;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/max\/469\/1*0Gso5jlY6IlCJTtPtZ1fuQ.png\" alt=\"How To Measure The Goodness Of A Regression Model\" width=\"469\" height=\"188\" \/><\/p>\n<p id=\"b196\" data-selectable-paragraph=\"\">As we can see, the p-value is greater than 5%, so we\u00a0<strong>cannot reject<\/strong>\u00a0the null <a href=\"https:\/\/www.experfy.com\/blog\/bigdata-cloud\/hypothesis-testing-in-real-life\/\">hypothesis<\/a> and can say that the mean value of the holdout residuals is statistically similar to 0.<\/p>\n<p id=\"4bb1\" data-selectable-paragraph=\"\">Then, we can test if the holdout residuals have the\u00a0<strong>same average<\/strong>\u00a0as the training ones. This is called\u00a0<strong>Welch\u2019s t-test<\/strong>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e2f3715 elementor-widget elementor-widget-text-editor\" data-id=\"e2f3715\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre>t.test(training_res,holdout_res)<\/pre>\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"alignnone\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/max\/606\/1*zATHQwG2YvpapXdUtzytSg.png\" alt=\"How To Measure The Goodness Of A Regression Model\" width=\"606\" height=\"240\" \/><\/p>\n<p id=\"a8be\" data-selectable-paragraph=\"\">Again, a p-value higher than 5% can make us tell that\u00a0<strong>there aren\u2019t enough reasons<\/strong>\u00a0to assume that the mean values are different.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8cc8b02 elementor-widget elementor-widget-heading\" data-id=\"8cc8b02\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\"><h3 id=\"1eff\">F-test<\/h3>`<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-62d9fd5 elementor-widget elementor-widget-text-editor\" data-id=\"62d9fd5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"1409\" data-selectable-paragraph=\"\">After we have checked the mean value, there comes the\u00a0<strong>variance<\/strong>. We obviously want that the holdout residuals show a behavior\u00a0<strong>not so much different<\/strong>\u00a0from the training residuals, so we can\u00a0<strong>compare\u00a0<\/strong>the variances of the two sets and check whether the holdout variance is higher than the training variance.<\/p>\n<p id=\"ac5e\" data-selectable-paragraph=\"\">A good test to check if a variance is greater than another one is the\u00a0<strong>F-test<\/strong>, but it only works with\u00a0<strong>normally distributed residuals<\/strong>. If the distribution is not normal, the test might give wrong results.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2f1835a elementor-widget elementor-widget-text-editor\" data-id=\"2f1835a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"54d3\" data-selectable-paragraph=\"\">So, if we really want to use this test, we must check the normality of the residuals using (for example) a\u00a0<strong>Shapiro-Wilk<\/strong>\u00a0test.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-635ba65 elementor-widget elementor-widget-text-editor\" data-id=\"635ba65\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p style=\"text-align: center;\" data-selectable-paragraph=\"\"><img decoding=\"async\" class=\"alignnone\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/max\/332\/1*weqN15sgUVWOkgILhgZuZQ.png\" alt=\"How To Measure The Goodness Of A Regression Model\" width=\"332\" height=\"283\" \/><\/p>\n<p id=\"ce32\" data-selectable-paragraph=\"\">Both p-values are higher than 5%, so we can say that both sets\u00a0<strong>show normally distributed residuals<\/strong>. We can safely go on performing the F-test.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8d4de8a elementor-widget elementor-widget-text-editor\" data-id=\"8d4de8a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre>var.test(training_res,holdout_res)<\/pre>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-310ad9f elementor-widget elementor-widget-image\" data-id=\"310ad9f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/597\/1*9U7V6m7OZP3fU_0uyxy0Mg.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-30c2300 elementor-widget elementor-widget-text-editor\" data-id=\"30c2300\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"0978\" data-selectable-paragraph=\"\">The p-value is 72%, which is greater than 5% and allows us to say that the two sets have the same variance.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-af1cca0 elementor-widget elementor-widget-heading\" data-id=\"af1cca0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\"><h3 id=\"9351\">Kolmogorov-Smirnov test<\/h3><\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6328d3f elementor-widget elementor-widget-text-editor\" data-id=\"6328d3f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"84a1\" data-selectable-paragraph=\"\">KS test is very general and useful for many situations. Generally speaking, we expect that, if our model works well, the\u00a0<strong>probability distribution<\/strong>\u00a0of the holdout residuals is\u00a0<strong>similar\u00a0<\/strong>to the probability distribution of the training residuals. The KS test has been created to compare probability distributions, so it can be used for this purpose. However, it carries some approximations that can be dangerous to our analysis. Significative differences between probability distributions can be hidden in the general considerations made by the test. Last, KS distribution is known only with some kind of approximation and, consequently, the p-value; so I suggest to use this test with care.<\/p>\n<pre>ks.test(training_res,holdout_res)<\/pre>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c269201 elementor-widget elementor-widget-image\" data-id=\"c269201\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/397\/1*N6n6Z3uVthNORi95RWcDmw.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4c60632 elementor-widget elementor-widget-text-editor\" data-id=\"4c60632\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"73d0\" data-selectable-paragraph=\"\">Again, the large p-value can make us tell that the two distributions are the same.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-382a81d elementor-widget elementor-widget-heading\" data-id=\"382a81d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\"><h3 id=\"2cfc\">Plot<\/h3><\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b51af6c elementor-widget elementor-widget-text-editor\" data-id=\"b51af6c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"9c6d\" data-selectable-paragraph=\"\">A Professor of mine at the University usually said: \u201cyou have to look at data\u00a0<strong>by your eyes<\/strong>\u201d. In machine learning, it\u2019s definitely true.<\/p>\n<p id=\"b24c\" data-selectable\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d51f7eb elementor-widget elementor-widget-text-editor\" data-id=\"d51f7eb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"b24c\" data-selectable-paragraph=\"\">The best way to take a look at a regression data is by\u00a0<strong>plotting\u00a0<\/strong>the predicted values against the real values in the holdout set. In a perfect condition, we expect that the points lie on the\u00a0<strong>45 degrees line<\/strong>\u00a0passing through the origin (<em>y = x\u00a0<\/em>is the equation). The nearer the points to this line, the better the regression. If our data make a shapeless blob in the Cartesian plane, there is definitely something wrong.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2e70e7b elementor-widget elementor-widget-text-editor\" data-id=\"2e70e7b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">plot(holdout$Sepal.Length,predict(m,holdout))<br \/>abline(0,1)<\/span><\/div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-23f3c3d elementor-widget elementor-widget-image\" data-id=\"23f3c3d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1366\/1*jYnaEfRNl7pmxy_w4NlaQg.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-988d99b elementor-widget elementor-widget-text-editor\" data-id=\"988d99b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"dbcc\" data-selectable-paragraph=\"\">Well, it could have been better, but it\u2019s not completely wrong. Points lie approximatively on the straight line.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e6e496b elementor-widget elementor-widget-heading\" data-id=\"e6e496b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\"><h3 id=\"ab97\">t-test on plot<\/h3><\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9177a3d elementor-widget elementor-widget-text-editor\" data-id=\"9177a3d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"c1ec\" data-selectable-paragraph=\"\">Finally, we can calculate a linear regression line from the previous plot and check if its intercept is statistically different from zero and its slope is statistically different from 1. To perform these checks, we can use a simple linear model and the statistical theory behind the Student\u2019s t-test.<\/p>\n<p id=\"121f\" data-selectable-paragraph=\"\">Remember the definition of the\u00a0<em>t<\/em>\u00a0variable with\u00a0<em>n-1<\/em>\u00a0degrees of freedom:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0cb3005 elementor-widget elementor-widget-image\" data-id=\"0cb3005\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/150\/0*92yF9LZ7LHbyfree.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c9bbe26 elementor-widget elementor-widget-text-editor\" data-id=\"c9bbe26\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"e6cf\" data-selectable-paragraph=\"\">When we use the\u00a0<em>summarize<\/em>\u00a0function of R on a linear model, it gives us the estimates of the parameters and their standard errors (i.e. the complete denominator of the\u00a0<em>t<\/em>\u00a0definition).<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8e68b32 elementor-widget elementor-widget-text-editor\" data-id=\"8e68b32\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"db59\" data-selectable-paragraph=\"\">For the intercept, we have\u00a0<em>mu = 0<\/em>, while the slope has\u00a0<em>mu = 1<\/em>.<\/p>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">test_model = lm(real ~ predicted, data.frame(real=holdout$Sepal.Length,predicted=predict(m,holdout)))<br \/>s = summary(test_model)intercept =\u00a0 s$coefficients[&#8220;(Intercept)&#8221;,&#8221;Estimate&#8221;]<br \/>intercept_error = s$coefficients[&#8220;(Intercept)&#8221;,&#8221;Std. Error&#8221;]<br \/>slope = s$coefficients[&#8220;predicted&#8221;,&#8221;Estimate&#8221;]<br \/>slope_error = s$coefficients[&#8220;predicted&#8221;,&#8221;Std. Error&#8221;]t_intercept = intercept\/intercept_errort_slope = (slope-1)\/slope_error<\/span><\/div>\n<p data-selectable-paragraph=\"\">\u00a0<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"has_eae_slider elementor-section elementor-top-section elementor-element elementor-element-7da15c4 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"7da15c4\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"has_eae_slider elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-ddeb525\" data-id=\"ddeb525\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-1eb0a3e elementor-widget elementor-widget-text-editor\" data-id=\"1eb0a3e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"e14a\" data-selectable-paragraph=\"\">Now we have the\u00a0<em>t<\/em>\u00a0values, so we can perform a two-sided t-test in order to calculate the p-values.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bfed503 elementor-widget elementor-widget-image\" data-id=\"bfed503\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/388\/1*wy076s-nK13R3-S3IjRb0A.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bb72a75 elementor-widget elementor-widget-text-editor\" data-id=\"bb72a75\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"3c9a\" data-selectable-paragraph=\"\">They are greater than 5% but not too high in absolute value.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3ab28bc elementor-widget elementor-widget-heading\" data-id=\"3ab28bc\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\"><h2 id=\"940b\">Which method is the best one?<\/h2><\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a9ce04b elementor-widget elementor-widget-text-editor\" data-id=\"a9ce04b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"8321\" data-selectable-paragraph=\"\">As usual, it depends on the problem. If the residuals are\u00a0<strong>normally distributed<\/strong>, t-test and F-test are enough. If they are not, maybe a first plot can help us discover a\u00a0<strong>macroscopic bias<\/strong>\u00a0before using a Kolmogorov-Smirnov test.<\/p>\n<p id=\"c272\" data-selectable-paragraph=\"\">However, non-normally distributed residuals should always\u00a0<strong>raise an alarm<\/strong>\u00a0in our head and make us search for some\u00a0<strong>hidden phenomenon<\/strong>\u00a0we haven\u2019t considered yet.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-da48069 elementor-widget elementor-widget-heading\" data-id=\"da48069\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\"><h2 id=\"6c09\">Conclusions<\/h2><\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e302cd2 elementor-widget elementor-widget-text-editor\" data-id=\"e302cd2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"840c\" data-selectable-paragraph=\"\">In this short article, I\u2019ve shown you some methods to calculate the goodness of a regression model. Though there are many possible ways to measure it, these simple techniques can be very useful in many situations and easily explainable to a non-technical audience.<\/p>\n<\/section>\n\n<p>&nbsp;<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Regression models&nbsp;are very useful and widely used in machine learning. However, they might show some problems when comes to measure the&nbsp;goodness&nbsp;of a trained model. Their performance can be measured in many different ways. This article shows you some methods to calculate the goodness of a regression model. Though there are many possible ways to measure it, these simple techniques can be very useful in many situations and easily explainable to a non-technical audience.<\/p>\n","protected":false},"author":618,"featured_media":3984,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[183],"tags":[92],"ppma_author":[3328],"class_list":["post-2319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai-ml","tag-machine-learning"],"authors":[{"term_id":3328,"user_id":618,"is_guest":0,"slug":"gianluca-malato","display_name":"Gianluca Malato","avatar_url":"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2020\/04\/medium_918623b2-8f36-4110-8343-6fc9228595dd-150x150.jpg","user_url":"http:\/\/www.gianlucamalato.it\/","last_name":"Malato","first_name":"Gianluca","job_title":"","description":"Gianluca Malato is Data Scientist at Poste Italiane SPA.\u00a0 He is also a fiction author and software developer, Editor of\u00a0<a href=\"https:\/\/medium.com\/data-science-journal?source=follow_footer--------------------------follow_footer-\">Data Science Journal<\/a>,\u00a0<a href=\"https:\/\/medium.com\/the-trading-scientist?source=follow_footer--------------------------follow_footer-\">The Trading Scientist<\/a>, and\u00a0<a href=\"https:\/\/medium.com\/the-writers-notebook?source=follow_footer--------------------------follow_footer-\">The Writer\u2019s Notebook<\/a>. His books are available on <a href=\"https:\/\/www.amazon.com\/Gianluca-Malato\/e\/B076CHTG3W?ref=dbs_a_mng_rwt_scns_share\">Amazon<\/a>."}],"_links":{"self":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/2319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/users\/618"}],"replies":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/comments?post=2319"}],"version-history":[{"count":6,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/2319\/revisions"}],"predecessor-version":[{"id":35237,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/2319\/revisions\/35237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media\/3984"}],"wp:attachment":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media?parent=2319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/categories?post=2319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/tags?post=2319"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=2319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}