<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by moxú on Medium]]></title>
        <description><![CDATA[Stories by moxú on Medium]]></description>
        <link>https://medium.com/@m-oxu?source=rss-dd351899da39------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uGZE3izDYwf746WJd06D8A.jpeg</url>
            <title>Stories by moxú on Medium</title>
            <link>https://medium.com/@m-oxu?source=rss-dd351899da39------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 05:20:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@m-oxu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Linear Regression by Hand]]></title>
            <link>https://medium.com/data-science/building-a-linear-regression-by-hand-b930e63bf0ae?source=rss-dd351899da39------2</link>
            <guid isPermaLink="false">https://medium.com/p/b930e63bf0ae</guid>
            <category><![CDATA[deep-dives]]></category>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[getting-started]]></category>
            <dc:creator><![CDATA[moxú]]></dc:creator>
            <pubDate>Thu, 07 Oct 2021 07:14:31 GMT</pubDate>
            <atom:updated>2021-10-08T13:02:04.018Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*b6jMAo3meAa0gPFe" /><figcaption>Image by <a href="https://unsplash.com/@michalmatlon">Unsplash</a></figcaption></figure><h4><a href="https://towardsdatascience.com/tagged/getting-started">Getting Started</a></h4><h4>Let’s use Python to create all of the equations required to estimate our own line and validate our results without relying on libraries to train our model!</h4><p>W e employ linear regression to forecast the value of Y based on the value(s) of X. Because we need to know Y, it is a supervised learning approach. Linear regression is classified into two types: basic and multiple. Let’s start with the easy one. The notebook with all the codes is <a href="https://colab.research.google.com/drive/1vuSLuMxZcFaXu0Z4KVFkJCuWP4mHheyJ?usp=sharing">here</a>. All the equations were made <a href="https://snip.mathpix.com/dairenkonmajime/notes/note-1">with LaTeX</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b881931808ff2f681853b971176b8dd/href">https://medium.com/media/1b881931808ff2f681853b971176b8dd/href</a></iframe><p>Before we begin, it is critical to understand that linear regression is a parametric approach.</p><p><strong>Parametric method</strong>:</p><p>1. It assumes the function’s shape in advance.</p><p>2. It simplifies the issue of estimating f (X) to estimating a collection of parameters. This assumption simplifies the task since estimating the collection of parameters is easier than with a completely arbitrary function.</p><p>3. The most difficult aspect of these approaches is making the proper estimate. We can only make guesses about the correct form of the curve, resulting in erroneous forecasts.</p><p>As a result, we must estimate parameters that will allow us to create a line that best approximates the values of Y, whether known or unknown.</p><h3>Simple Linear Regression</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*CtAntI12L3Y54wzH6Crszg.png" /><figcaption>Image by Author</figcaption></figure><p>This is the function of a simple linear regression. Variations of this equation can be found in statistical literature, but they all have the same substance. It may be W0 and W1, alpha and beta, and so on.</p><p>However, I’d want to bring your attention to something. Take note of the sign ≈. When we estimate values, our major goal is to get our predictions close to the true values of Y through X. That is, we aim to minimize the difference between the true and estimated values as much as feasible.</p><p>As a result, when dealing with estimations of f(X) = Y, or more specifically, parameters of f(X) where f (X) = Y or Y = f (X), we do not aim to define f (X) perfectly, but rather estimate parameters that best represent f (X). However, because Y = ^f (X) + <strong><em>e</em></strong>, our estimate of f(X) will equal Y when added with <strong><em>e</em></strong>, the residual.</p><p>Where Y = ^f (X) + <strong><em>e</em></strong>, then ^f (X) = ^B0 + ^B1X = ^Y. That is, in order to estimate ^Y, we must first estimate the parameters of ^f(X), ^B0 and ^B1. Y = ^Y + <strong><em>e</em></strong> since Y = ^f (X) + <strong><em>e </em></strong>and<strong><em> </em></strong>^f (X) = ^Y.</p><p>With that in mind, the formula we’ll use is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*YFtVdXApjWTUXPl9nDkm2g.png" /><figcaption>Image by Author</figcaption></figure><p>No more distractions.</p><h4><strong>What are B0 and B1</strong>?</h4><p>BO denotes the intercept, which is the Y value at X = 0, or the Y value at which the linear regression line begins. Here’s an example. We want to estimate the number of sales conversions based on the amount of investment made in a campaign. How much can I sell if I don’t put up any money?</p><p>And what about the B1? The slope is represented by the value of B1. It basically determines the trend line. How does this work in practice? Consider the example of calculating height based on weight. Height is measured in centimeters, and weight is measured in kilograms. We discover a slope of 2.5. It indicates that for every kilogram gained, a person’s height increases by 2.5 millimeters.</p><p>How do we estimate B0 and B1 given that we know what they are? These parameters can be estimated in a variety of methods. To begin, let’s look at the Ordinary Least Squares (OLS), which is one of the most frequent and straightforward. To estimate B0, we must first estimate B1.</p><blockquote>In statistics, <strong>ordinary least squares</strong> (<strong>OLS</strong>) is a type of linear least squares method for estimating the unknown parameters in a linear regression model. OLS chooses the parameters of a linear function of a set of explanatory variables by the principle of least squares: minimizing the sum of the squares of the differences between the observed dependent variable (values of the variable being observed) in the given dataset and those predicted by the linear function of the independent variable. — Source: <a href="https://en.wikipedia.org/wiki/Ordinary_least_squares">Wikipedia</a>.</blockquote><p>Let’s have a look at what this implies. What do we want to achieve with this method? Reduce something. What should be minimized? The sum of the squares of the differences between the observed and estimated data. Let’s put this equation together.</p><p>Backwards, we have the difference between the observed and estimated values. The values we estimate as a result of our regression are referred to as estimated values. The observed values are the true values. Therefore,</p><pre>observedValue - estimatedValue</pre><p>Now, <em>square</em> the difference between the estimated and observed values. Hence:</p><pre>square (observedValue - estimatedValue)</pre><p>Finally, the sum of the squares of the differences between the observed and estimated data.:</p><pre>sum (square (observedValue - estimatedValue))</pre><p>Okay, but what is the significance of the sum? observedValue and estimatedValue are valueS. Consider this: we have two columns, Y and Y. The first is the real value, the observed value, and second is the estimated value. As I previously mentioned, we construct a model and utilize X to estimate Y, resulting in Y. Assuming that each column has three values, Y = [5, 9, 2] and Y = [6, 7, 2]. We have the following results using our formula:</p><pre>(5–6)²<br>(9–7)²<br>(2–2)²</pre><p>The total is the sum of these three operations. As a result, (5–6)² + (9–7)² + (2–2) ². We have the Residual Sum of Squares (RSS) as a result of these procedures. RSS = 5 is used in this case. We aim to reduce RSS using OLS.</p><p>The general formula for RSS is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*pNSzREKesxSIgfOLHX4nJQ.png" /><figcaption>Image by Author</figcaption></figure><p>How we code this using Python?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f11a19d3a85fd094ffd94f76cec3e8d/href">https://medium.com/media/0f11a19d3a85fd094ffd94f76cec3e8d/href</a></iframe><p>OLS is ONE of numerous methods for reducing this RSS. Another approach is, I don’t know, guessing various parameters of B0 and B1, calculating the error of all, and selecting the smallest one, but the smallest mistake discovered may not be so tiny. We’ll finish with an approach like this. When we have a small number of features and columns, OLS is the quickest and easiest approach to use.</p><p>We will utilize fictitious data in our article.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/373a8128246bb9ee200978664fdbc08e/href">https://medium.com/media/373a8128246bb9ee200978664fdbc08e/href</a></iframe><p>In this case, I made two arrays to represent X and Y. I created 1000 values in order to provide a more intriguing perspective. I split X and Y into four separate halves. The training data will contain 80% of the X and Y values (X train and y train), whereas 20% of the data will be utilized for testing (X test and y test).</p><p>Let’s discuss about assumptions before we estimate B0 and B1. What are the fundamental assumptions of linear regression? We utilize linear regressions to forecast new values or to make inferences. There are numerous methods for estimating parameters in a regression. And many methods, such as OLS, require specific assumptions in order for their conclusions to be as accurate and impartial as feasible. These premises do not have to be followed exactly in every linear regression!</p><h4>The dependent and independent variables must have a linear relationship.</h4><p>It is only logical that this requirement be satisfied when employing linear regression for prediction. In essence, linear regression will draw a straight line through the data. If the data does not have a linear connection, the model cannot handle the data’s complexity. How can we put this concept to the test?</p><p>There are two forms of confirmation under most assumptions. There is graphical confirmation as well as mathematical confirmation. I’ll cover both whenever feasible. When things get too complicated, I’ll just cover visuals.</p><p>So we may plot the X data against the Y data and look for trends. Let’s execute this command and see what happens.</p><pre>plt.figure(figsize=(10,10))<br>plt.scatter(X_train, y_train)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C-CL8aiXOhEux6bUIvmvcA.png" /><figcaption>Image by Author</figcaption></figure><p>To me, it appears to be quite linear! There’s even a straight line I can see! But what’s the point? I’m not sure about you, but visual confirmation does not completely satisfy me. Of course, your perspective is determined by your time, money, and education. However, we have all the time in the world here, it is free, and we are knowledgeable enough to use alternative methods.</p><p>And, other than graphs, how do we assess a linear relationship? The correlation coefficient between X and Y is computed. But what exactly is the correlation?</p><blockquote>In <a href="https://en.wikipedia.org/wiki/Statistics">statistics</a>, <strong>correlation </strong>or <strong>dependence </strong>is any statistical relationship, whether <a href="https://en.wikipedia.org/wiki/Causality">causal</a> or not, between two <a href="https://en.wikipedia.org/wiki/Random_variable">random variables</a> or <a href="https://en.wikipedia.org/wiki/Bivariate_data">bivariate data</a>. In the broadest sense <strong>correlation</strong> is any statistical association, though it commonly refers to the degree to which a pair of variables are <a href="https://en.wikipedia.org/wiki/Line_(geometry)">linearly</a> related. Familiar examples of dependent phenomena include the correlation between the <a href="https://en.wikipedia.org/wiki/Human_height">height</a> of parents and their offspring, and the correlation between the price of a good and the quantity the consumers are willing to purchase, as it is depicted in the so-called <a href="https://en.wikipedia.org/wiki/Demand_curve">demand curve</a>.. —Source: Wikipedia.</blockquote><p>Following this quick explanation, I shall reiterate a statistician’s mantra: correlation does not indicate causation! Before we can get a clear judgment on causality, we need to run a number of statistical tests. But it is a topic for another day!</p><p>In our data, we wish to calculate the degree of correlation between X and Y. There are various approaches to this problem. Pearson’s coefficient is the most commonly used. Its general formula is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*J7wKZPfmiTwtKsJfcpRSRw.png" /><figcaption>Image by Author</figcaption></figure><p>That is, the division of two variables’ standard deviations divided by their covariance. Let’s break things down so you can comprehend it better. We’ll look at two sections. The covariance of X with respect to Y, as well as the variance of X and Y. I’ll leave this information, which includes some graphic explanations, for the idea of variance. It’s definitely worth a look. In summary, covariance denotes the degree to which two variables fluctuate in tandem.</p><p>How do we compute the covariance of two variables in Python? Let’s use the formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*CBReY1vWWfk0feotMcBnsg.png" /><figcaption>Image by Author</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e08ac3c65a5299bfc9aa1f8672d7d2c7/href">https://medium.com/media/e08ac3c65a5299bfc9aa1f8672d7d2c7/href</a></iframe><p>That’s all! And what is the standard deviation of the sample? The standard deviation of a random variable is a measure of its dispersion around the population mean. A low standard deviation implies that the data points are frequently near to the mean or anticipated value. A large standard deviation implies that the data points are distributed throughout a broad range of values (Wikipedia). The square root of the variance is also the standard deviation. Let’s use Python to create our standard deviation formula by utilizing the variance formula and calculating its root:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*Pge-gS03l4b9xymtyyc9rQ.png" /><figcaption>Image by Author</figcaption></figure><p>Let’s take the standard deviations first.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f661b2fc229929e517464822be0e438f/href">https://medium.com/media/f661b2fc229929e517464822be0e438f/href</a></iframe><p>As you can see, we utilize the variance formula to find the root of the problem. We may now use our Pearson coefficient. Let’s start with the original formula and create our function from there:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*J7wKZPfmiTwtKsJfcpRSRw.png" /><figcaption>Image by Author</figcaption></figure><p>Given that we’ve already gone more than halfway, let’s simplify this formula even further:</p><pre>covarianceOfXY / StdX * StdY</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/458591cccc07c926407298e42571b542/href">https://medium.com/media/458591cccc07c926407298e42571b542/href</a></iframe><p>Let’s see how the outcome with our data turned out.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b48507fd82b9ccefd06ee41f68f255b8/href">https://medium.com/media/b48507fd82b9ccefd06ee41f68f255b8/href</a></iframe><pre>The Pearson Correlation Coefficient between X and Y is [0.98478235].</pre><p>And what is the significance of Pearson’s coefficient? Pearson’s coefficient is a number between -1 and 1, with 1 and -1 representing perfect linear correlations and 0 representing a non-existent relationship. A perfect positive correlation is 1 while a perfect negative correlation is -1.</p><p>What should the ideal values be? It is conditional. Sometimes a correlation of 0.7 is optimum, and other times 0.9 is insufficient. It will be determined by the nature of your problem. In a multiple linear regression, you may use Pearson’s coefficient to find the most significant factors in your model or to exclude co-dependent variables.</p><p>Now that we’ve identified one of the assumptions, let’s get started on estimating the parameters.</p><p>B0 and B1 will be estimated using the OLS formula. The following are the equations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*lfXkSJgB0tkBdGqHW6NHPg.png" /><figcaption>Image by Author</figcaption></figure><p>Looking at it that way, it’s a bit difficult, lol. But, in reality, it’s rather easy. Let’s begin with B1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*7ZAeRqAQo7XkrCgxipLH7w.png" /><figcaption>Image by Author</figcaption></figure><p>As previously stated, B1 is the covariance between X and Y divided by X’s variation. B0 is simply the value of Y when X = 0, taking into account the difference between the median of Y and the product of the angular coefficient and the median of X.</p><p>Let’s use Python to estimate B0 and B1?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10e50c3b53a40f4a24c6eab3cebb13a6/href">https://medium.com/media/10e50c3b53a40f4a24c6eab3cebb13a6/href</a></iframe><p>Because we divide our data in training and testing, our X and Y will be our training X and Y. It’s time to estimate!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/503e95d5c8e0d3852d0dd38ebb5dc217/href">https://medium.com/media/503e95d5c8e0d3852d0dd38ebb5dc217/href</a></iframe><pre>Intercept: -4.959319577525191,        <br>Slope: [2.00341869].</pre><p>To put it another way, our line begins at -4.9593 and progresses at a rate of 2.003 for each increment of X. Now we’ll apply these parameters to new data and see how our predictions fare! Now is the time to build our linear regression!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c845662a5bd26504e14a1f26e3e592cf/href">https://medium.com/media/c845662a5bd26504e14a1f26e3e592cf/href</a></iframe><p>Because we separated our data before, we can now test and evaluate our model!</p><pre>y_pred = predict_function(b0_coeficient, b1_coeficient, X_test)</pre><p>After that? Now it’s time to design our return route!</p><pre>plt.figure(figsize=(8,6))</pre><pre>plt.scatter(X_test, y_test,  color=&#39;blue&#39;)</pre><pre>plt.plot(X_test, y_pred, color=&#39;red&#39;, linewidth=2)</pre><pre>plt.title(&quot;Linear Regression Demonstration&quot;, fontweight=&quot;bold&quot;, size=15)</pre><pre>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*p77n_qMWpaXdp1fOBSdtEQ.png" /><figcaption>Image by Author</figcaption></figure><p>It appears that our return went well!</p><p>Naturally, linear regression lines will never reach all points. Or, at the very least, if they are correct, it will not be a good thing if the model’s intention is to generalize it to new data. This is due to the error that every linear equation has.</p><p>In this moment, we will add the final component of our equation, about which we have already spoken. Error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*Y2qAo3YjzwT6ZCSaAeMaFQ.png" /><figcaption>Image by Author</figcaption></figure><p>The error is the difference between the predicted and actual value of Y. The cost of an error might be the result of a variety of factors. Sometimes it’s a single variable that you’re not considering, and other times you’ve assumed a linear relationship that doesn’t exist in reality.</p><p>Another reason might be a data shortage. With so limited data, it is possible that your estimates will fail to reveal the true nature of the problem.<br>To begin, there are several paths to take in order to reduce error. You may look for more data, choose a non-parametric or linear model, and add more variables. The strategies differ and will be determined by your assessment. How much does it cost to look for more data? Is it worthwhile to look for further information? How long will it take to test another model? When working professionally, you should ask all of these questions.</p><p>Is there a limit? Yes. Call it an irreversible error. The irreversible error is aleatory and, by definition, cannot be measured or observed. We’ll bring you our new equation, which I’ll explain to you.</p><blockquote>Y = ^f(X) + e</blockquote><p>Sure, we’ve already completed the first stage and estimated the parameters of f(X). To aid comprehension, we are employing a linear regression to determine or predict the average level of heart beats throughout an hour given a certain quantity of Pfizer vaccine applied in mouses.</p><p>However, the error has not yet been committed and will not be committed. Why is this so? We’re guessing you came home late last night, tired and with a heavy night’s sleep. On that same night, one of your mouses, Pitu, bridled by Marquinhos. Pitu, like the other rats in the gaiola, became extremely stressed. Fortunately, no one was injured. Pitu tried to tell you about the injustice the next day, but because you don’t talk to animals, you don’t know what happened.</p><p>The stress levels of Pitu were altered by what happened and by the fact that you didn’t understand what happened. Stress levels were an important variable in your results that you ignored for N reasons, one of which was that you don’t speak “mouseguese”.</p><p>Magdalena, another little mouse, is lonely and has no one to talk to or play with. When you go out, the others have a party, but Magdalena stays in her corner, mulling over her sorrows. You will most likely be unable to identify Magdalene’s sadness because you did not major in mouse psychology. This variable, which you couldn’t measure or even see, will be crucial in evaluating Magdalene’s results!</p><p>Did you get my point? There are factors that are just not observable or collectable for your analysis.</p><p>You should be wondering, but what error is this, and why do I believe I heard it somewhere? You saw it up there, but it had a different name: RSS. Will we compute the model’s RSS?</p><pre>print(f&#39;The RSS is {get_rss(y_pred, y_test)}.&#39;)</pre><pre># The RSS is 191.16066429.</pre><p>With this number, we may look for ways to reduce it. But it isn’t the issue today! I’m mentioning the error for a reason. The second premise of Linear Regression.</p><h4>The residues must be distributed in a regular manner.</h4><p>Why is this so? There are several disputes over why this assumption should be confirmed and what happens when it is not satisfied, even if it is necessary. As these are quite technical issues, I will not address them in this article.</p><p>And how can we know if a data distribution is normal? There are two types of graphs: graphic and mathematical. I’ll be using graphics in this section. If we want to examine the behavior of all the leftovers, we can’t use RSS since RSS looks at the sum of the leftovers, but I’m not interested in the sum of the leftovers, but rather in the individual ones.</p><p>Remember that registration at the start? We’ll look at the formula Y = ^Y + e. To calculate the residual, just switch Y from left to right, resulting in: Y − ^Y = <strong><em>e</em></strong>.</p><blockquote>e = observedValue— estimatedValue</blockquote><p>It’s simple, in Python:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac52b25e1c99d9497996138f8d5bd8c9/href">https://medium.com/media/ac52b25e1c99d9497996138f8d5bd8c9/href</a></iframe><pre>residual_lr = get_residual(y_test, y_pred)</pre><p>As a result, we will have an array with our leftovers. Will we examine their distribution using a histogram?</p><pre>plt.subplots(figsize=(12, 6))</pre><pre>plt.title(&#39;Distribution of Residuals&#39;)</pre><pre>sns.distplot(residual_lr)</pre><pre>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DjUucsrnYMxxgzmQPGMV-A.png" /><figcaption>Image by Author</figcaption></figure><p>Well, it appears to be a normal distribution, albeit slightly asymmetric! Is there another way to confirm that this is a normal distribution graphically? Yes, there is! It’s known as the QQPlot graph!</p><pre>lst = []<br>for i in residual_lr:<br>    for n in i:<br>        lst.append(n)</pre><pre>sm.qqplot(np.array(lst), line=&#39;45&#39;, fit=True)<br>plt.show()</pre><p>In this code, I extracted the values from an array of arrays and placed them in a list for our QQPlot to validate! There you have it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*8KBOEtNRnInJfBG5LL2K2Q.png" /><figcaption>Image by Author</figcaption></figure><p>What exactly is this stuff I’ve never seen before? You may be curious. I will not spend too much time conceptualizing the QQPlot, but rather interpreting it. <a href="https://pt.wikipedia.org/wiki/Gr%C3%A1fico_Q-Q">In case you’re interested in learning more</a>.</p><p>What can we conclude from a QQ plot? We have a red straight line and a blue dispersion of residuals. We may visualize the distribution of our data using the QQ plot. We have a perfect normal distribution if they perfectly follow the red line. The further our data deviates from this straight line, the less of a normal distribution character remains. For a better understanding, you may find an explanation with visualizations <a href="https://qastack.com.br/stats/101274/how-to-interpret-a-qq-plot#:~:text=Se%20os%20valores,menores%20e%20maiores.">here</a>.</p><p>Based on this visualization, we concluded that our waste had a typical distribution, despite some points being further away from the center.</p><p>There are several statistical tests that may determine if a distribution is normal or not, such as the Shapiro-Wilk test. But for now, we’ll just stick with the visualization! We’re heading to our next stop.</p><h4>The variance of residuals should be constant</h4><p>Linear regression models rely heavily on the premise of homoscedasticity (which means “constant variance”). Homoscedasticity defines a situation in which the error term (the “noise” or random disturbance in the relationship between the independent variables and the dependent variable) is the same for all independent variable values.</p><p>Heteroscedasticity (the violation of homoscedasticity) occurs when the size of the error term varies across independent variable values. The impact of a breach of the homoscedasticity assumption is proportional to the degree of heteroscedasticity.</p><p>What is the significance of this to us? When we use MMQ, we give all X-values the same weight. We have an issue if some X values have a greater influence than others.</p><p>Consider an estimate of luxury goods purchases based on household income. We have impoverished families who do not or only buy a few luxury items. If the purchase of luxury goods grew with every increase in household income, we would have a constant variance. But assume that just a portion of the wealthy families buy luxury items, while the others buy relatively little: we have a problem with heteroscedasticity.</p><p>Do you remember the B1 formula? The variance of the values of X is calculated in the divisor section. Because the OLS provides the same weight to all variables of X, very big variations can get in the way!</p><p>How can we see this? Let’s put Python to use! Let’s start with a visual representation. We will plot our residuals against the anticipated values to observe the variation of the errors.</p><pre>plt.scatter(residual_lr, y_pred)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*K40HfSiDlXmUous5B4TYww.png" /><figcaption>Image by Author</figcaption></figure><p>To be honest, I couldn’t draw any conclusions from this image! There doesn’t appear to be any pattern in the dots. So, shall we put it to the test?</p><pre>H0 = homocedasticity<br>H1 != homocedasticity</pre><pre>alpha = 0.05</pre><p>Let’s use the <em>statsmodels.stats</em> library for this!</p><pre>print(sm.diagnostics.het_goldfeldquandt(residual_lr, y_pred))</pre><pre>Test: 1.0206718127917291<br>p-value: 0.45956295321355667</pre><p>We cannot reject the null hypothesis because of alpha &lt; p-value, hence we cannot deny the presence of homoscedasticity!</p><p>Whew! Our model confirmed and passed all of the assumptions! You are most likely thinking “Can we leave now? The model is amazing! “. You can relax if you wish to utilize the model to analyze the variables. You can start examining it tomorrow to see if you can get anything out of it. We’re just halfway there if you want to utilize the model to make predictions!</p><p>Now it is time to evaluate our model’s generalizability! As you can see, our model did a good job of staring at the test data. But how well? How do we assess our model? Using metrics!</p><p>There are a number of measurements for this. One of these is the R-squared, often known as the Coefficient of Determination. R2 is a measure that expresses how much of the variation in the data is explained by the model. In other words, this metric computes the proportion of variance that the regression model might predict, and so shows us how “closer” the actual measures are to our model. It denotes the proportion of the variability in the response variable that may be explained by the predictor or explanatory variable.</p><p>Its R-squared value varies from 0 to 1, with 0 being the poorest possible estimate and 1 representing the best possible estimate. It is typically expressed as a percentage. For example, an R2 = 73 % indicates that the model can explain 73% of the volatility in our data, while the remaining 27% is theoretically a residual variance.</p><p>Is it possible to have negative R-squares? Yes, when your model is capable of being worse than kicking the mean of Y for all Y values.</p><p>What exactly is the R2 formula? Let’s go over our RSS formula again. We will change it based on it. We have the sum of the squares of the residuals in RSS. However, we wish to calculate the <strong>total</strong> sum of squares of the residuals.</p><p>In RSS, we take the difference between each observed and estimated value of Y, or Y, square it, and add the findings. In TSS, we compute the difference between the Y values and the Y squared mean. That is to say:</p><blockquote>TSS = sum ( ( valuesX — meanY ) ²)</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2edd9877c39b5365499441032a0c6b9f/href">https://medium.com/media/2edd9877c39b5365499441032a0c6b9f/href</a></iframe><p>R2 is basically:</p><blockquote>1 — (RSS / TSS)</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50cc3dd414b766131057ed75af489f65/href">https://medium.com/media/50cc3dd414b766131057ed75af489f65/href</a></iframe><pre>rss = get_rss(y_pred, y_test)<br>rst = rst_metric(y_test)</pre><pre>print(f&#39;The R2 of the model is {get_r2(rss, rst)[0]*100}%.&#39;)</pre><blockquote>The R2 of the model is 97.17569151561773%</blockquote><p>That means, our model was able to explain nearly 97.18 % of our data! The model appears to be generalizable to fresh data!</p><p>R-squared can alternatively be calculated by squaring Pearson’s Coefficient:</p><pre>print(0.98478235 ** 2)</pre><pre># 0.969796276871522</pre><p>Do you want to know what R-squared would be if you just took the average? Let’s have a look together! Let’s make a list the same size as Y, but with the mean of Y instead.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/378a4c2eeebad4ceca80168efeb06d80/href">https://medium.com/media/378a4c2eeebad4ceca80168efeb06d80/href</a></iframe><pre>plt.figure(figsize=(8,6))<br>plt.scatter(X_test, y_test,  color=&#39;blue&#39;)<br>plt.plot(X_test, lst, color=&#39;red&#39;, linewidth=2)<br>plt.title(&quot;Linear Regression Demonstration&quot;, size=15)</pre><pre>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*ad0KClTB6Vj1D5P7PyMh9Q.png" /></figure><p>Now, let’s calculate the R-squared:</p><pre>rst = tss_metric(y_test)</pre><pre>rss = get_rss(lst, y_test)</pre><pre>print(f&#39;The R2 of the model is {get_r2(rss, rst)[0]*100}%.&#39;)</pre><blockquote>The R2 of the model is 0.0%.</blockquote><p>Remember when I said that R-squared could be less than 0? The greater the RSS is above RST, the lower the R-squared will be.</p><p>However, the question persists. What’s the other method for estimating a linear regression? Okay, I’ll swiftly approach a reasonable strategy that I had in mind.</p><p>The explanation behind this is as follows. Let’s pick 100 intercept and 100 slope values, then test 10,000 linear regressions, compare them, and pick the combination with the smallest RSS. Let’s get started!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/436158210020472d6c39c9d4cdd044be/href">https://medium.com/media/436158210020472d6c39c9d4cdd044be/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/280e2cfe8df2a227e34d8528326c1006/href">https://medium.com/media/280e2cfe8df2a227e34d8528326c1006/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/922a932c513356f108442c7b4f6fad88/href">https://medium.com/media/922a932c513356f108442c7b4f6fad88/href</a></iframe><p>Here we run 10,000 regressions. Shall we calculate and get the smallest RSS?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee2d4a6d1a6bbcbcc7223730b540f88b/href">https://medium.com/media/ee2d4a6d1a6bbcbcc7223730b540f88b/href</a></iframe><pre>min_index = rss_list.index(np.min(rss_list))</pre><pre>print(f&#39;The lowest RSS is: {np.min(rss_list)} with index {min_index}.&#39;)</pre><pre># The lowest RSS is: 190.52186974065032 with index 4552.</pre><p>We get a little smaller RSS when we use this workaround than when we use OLS! Take this index and use it to plot our regression line on the test data.</p><pre>plt.figure(figsize=(8,6))</pre><pre>plt.scatter(X_test, y_test,  color=&#39;blue&#39;)</pre><pre>plt.plot(X_test, linear_reg[min_index], color=&#39;red&#39;, linewidth=2)</pre><pre>plt.title(&quot;Linear Regression Demonstration&quot;, size=15)</pre><pre>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*-S_nROXjw0TahUNnlXxjvg.png" /><figcaption>Image by Author</figcaption></figure><p>And the R-squared?</p><pre>rst = tss_metric(y_test)</pre><pre>rss = get_rss(linear_reg[min_index], y_test)</pre><pre>print(f&#39;The R2 of the model is {get_r2(rss, rst)[0]*100}%.&#39;)<br># The R2 of the model is 97.18512940329813%.</pre><p>As you can see, the difference is negligible, almost non-existent. While using the MMQ method we got 97.175%, here we get 97.185%.</p><p>With that, we’ll conclude our article! I attempted to address the most important aspects of creating a linear regression by understanding what lies behind the five or six lines of code that are sufficient to do everything we’ve discussed here.</p><p>The remaining questions are: Is it required to check all of this? Is there another way to achieve consistent results without going through all of this? All I want to do now is finish Kaggle’s Titanic linear regression. Is it really necessary for me to do so? This is a heated debate! <a href="https://qastack.com.br/stats/130775/why-do-we-care-so-much-about-normally-distributed-error-terms-and-homoskedastic">Some splashes can be found here</a>. But keep in mind that as a data scientist, you must solve the problem and demonstrate your findings to your peers.</p><p>When you deal with real data, it won’t be cute like the ones here, but you need to solve the problem!</p><p>References:</p><p><strong>Homoscedasticidade — CENTRO DE ESTATÍSTICA APLICADA</strong>. Disponível em: &lt;<a href="https://estatistica.pt/homoscedasticidade/">https://estatistica.pt/homoscedasticidade/</a>&gt;. Acesso em: 24 set. 2021.</p><p>‌<strong>Teste para normalidade e homocedasticidade</strong>. Disponível em: &lt;<a href="https://biostatistics-uem.github.io/Bio/aula8/teste_normalidade_homocedasticidade.html">https://biostatistics-uem.github.io/Bio/aula8/teste_normalidade_homocedasticidade.html</a>&gt;. Acesso em: 24 set. 2021.</p><p>‌HOW WOULD YOU EXPLAIN COVARIANCE TO SOMEONE WHO UNDERSTANDS ONLY THE MEAN. <strong>How would you explain covariance to someone who understands only the mean?</strong> Disponível em: &lt;<a href="https://stats.stackexchange.com/questions/18058/how-would-you-explain-covariance-to-someone-who-understands-only-the-mean">https://stats.stackexchange.com/questions/18058/how-would-you-explain-covariance-to-someone-who-understands-only-the-mean</a>&gt;. Acesso em: 24 set. 2021.</p><p>‌DAMACENO, L. <strong>Entendendo Regressão Linear: as suposições por trás de tudo!</strong> Disponível em: &lt;<a href="https://medium.com/@lauradamaceno/entendendo-regress%C3%A3o-linear-as-suposi%C3%A7%C3%B5es-por-tr%C3%A1s-de-tudo-d0e29004c7f8">https://medium.com/@lauradamaceno/entendendo-regress%C3%A3o-linear-as-suposi%C3%A7%C3%B5es-por-tr%C3%A1s-de-tudo-d0e29004c7f8</a>&gt;. Acesso em: 24 set. 2021.</p><p>‌SHRUTIMECHLEARN. <strong>Step by Step Assumptions — Linear Regression</strong>. Disponível em: &lt;<a href="https://www.kaggle.com/shrutimechlearn/step-by-step-assumptions-linear-regression">https://www.kaggle.com/shrutimechlearn/step-by-step-assumptions-linear-regression</a>&gt;. Acesso em: 24 set. 2021.</p><p>‌NATHÁLIA TITO. <strong>Scikit-learn ou statsmodels? Avaliando meu modelo de regressão</strong>. Disponível em: &lt;<a href="https://nathaliatito.medium.com/scikit-learn-ou-statsmodels-avaliando-meu-modelo-de-regress%C3%A3o-f4c04b361fa7">https://nathaliatito.medium.com/scikit-learn-ou-statsmodels-avaliando-meu-modelo-de-regress%C3%A3o-f4c04b361fa7</a>&gt;. Acesso em: 24 set. 2021.</p><p>‌<strong>Regressão multilinear usando Python</strong>. Disponível em: &lt;<a href="https://ichi.pro/pt/regressao-multilinear-usando-python-75578758662189">https://ichi.pro/pt/regressao-multilinear-usando-python-75578758662189</a>&gt;. Acesso em: 24 set. 2021.</p><p>‌SERGIO MIRANDA FREIRE. <strong>19 Regressão Linear | Bioestatística Básica</strong>. Disponível em: &lt;<a href="http://www.lampada.uerj.br/arquivosdb/_book/regress%C3%A3o-linear.html#eqReta">http://www.lampada.uerj.br/arquivosdb/_book/regress%C3%A3o-linear.html#eqReta</a>&gt;. Acesso em: 24 set. 2021.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b930e63bf0ae" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/building-a-linear-regression-by-hand-b930e63bf0ae">Building a Linear Regression by Hand</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>