$residuals2 <- residuals(M2)
@@ -735,7 +735,7 @@ df
24.3.2 Spread-location plot
-
The M2
and lo
models do a good job in eliminating any dependence between residual and x-value. Next, we will check that the residuals do not show a dependence with fitted y-values. This is analogous to univariate analysis where we checked if residuals increased or decreased with increasing medians across categories. Here we will compare residuals to the fitted cp.ratio
values (for a univariate analogy, think of the fitted line as representing a level across different segments along the x-axis). We’ll generate a spread-level plot of model M2
’s residuals (note that in the realm of regression analysis, such plot is often referred to as a scale-location plot). We’ll also add a loess curve to help visualize any patterns in the plot.
+The M2
and lo
models do a good job in eliminating any dependence between residual and x-value. Next, we will check that the residuals do not show a dependence with fitted y-values. This is analogous to univariate analysis where we checked if residuals increased or decreased with increasing medians across categories. Here we will compare residuals to the fitted cp.ratio
values (for a univariate analogy, think of the fitted line as representing a level across different segments along the x-axis). We’ll generate a spread-level plot of model M2
’s residuals (note that in the realm of regression analysis, such plot is often referred to as a scale-location plot). We’ll also add a loess curve to help visualize any patterns in the plot.
<- data.frame( std.res = sqrt(abs(residuals(M2))),
sl2 fit = predict(M2))
@@ -747,10 +747,10 @@
![](bivariate_files/figure-html/unnamed-chunk-24-1.png)
-The function predict()
extracts the y-values from the fitted model M2
and is plotted along the x-axis. It’s clear from this plot that the residuals are not homogeneous; they increase as a function of increasing fitted CP ratio. The “bend” observed in the loess curve is most likely due to a single point at the far (right) end of the fitted range. Given that we have a small batch of numbers, a loess can be easily influenced by an outlier. We may want to increase the loess span.
-
+The function predict()
extracts the y-values from the fitted model M2
and is plotted along the x-axis. It’s clear from this plot that the residuals are not homogeneous; they increase as a function of increasing fitted CP ratio. The “bend” observed in the loess curve is most likely due to a single point at the far (right) end of the fitted range. Given that we have a small batch of numbers, a loess can be easily influenced by an outlier. We may therefore want to increase the loess span by setting span = 2
.
+
ggplot(sl2, aes(x = fit, y = std.res)) + geom_point() +
-stat_smooth(method = "loess", se = FALSE, span = 1.5,
+ stat_smooth(method = "loess", se = FALSE, span = 2,
method.args = list(degree = 1) )
![](bivariate_files/figure-html/unnamed-chunk-25-1.png)
diff --git a/docs/bivariate_files/figure-html/unnamed-chunk-25-1.png b/docs/bivariate_files/figure-html/unnamed-chunk-25-1.png
index feb65c5beb4044ddd975dd4fd23967e89b784560..e498ba6d975355cbe911591f2ab84a322aabd235 100644
GIT binary patch
delta 4669
zcmYjVdpy(o|KEj8uF3V}*6lwJ5R{)F?ab=-*FiJ)D!waaJPdmO8i_Q};=ZorT))KHC8RH--A6csy6`OnMMfnS~k
zw(yd3sW;*JfX=wIE8}g4dO2_$7IRla>Q}#mqXM17!?+wXma&Vgou|~yBj^yKmkISJ
zrpk^T<0g+9pv^dvZ37oKDR{hQYCdnyS9wXru!
zusd51{{$?ki#bKS%dr&ut~>e*}YmA9%`UeEoFIKPXj^V;^y65kM{4Nd)MR}pA1TfPXiaZ=UY6H*=i`yFYhV2}6PevfcO{MJgeHfgXjisU=LC=J@qow
zl@zuPRtK8XLZw0|`ky|x_FxxW>EWn29bLmqy_0IcUu#j~zRKEVw2~2tq4r!~8Uel=
z#7%g%Y$bbz@ZfZ?%}J&Ux1lu
zJ20_Z@5p%tth~v_g3A<59MB3?qiCfcm8qPMy~|Q)+X?d+J^x<`!QF9c>TEVhySN)^vuRkZejbp(=K3(&NCbIKmJE`^wPD>CJ|_-0f9Nvi+tuV2(mY+zHoyK}PJ
z^(fKv@@AZVqQd53LJ6)BrLSkO-Y^l&%dZ@8Za%wx&4JO~{K?jwO=*n#8DDd7{p8{s
zi7_A7&II1I4Ax>_p2P>%D}*Fj9_ssJnQdU?eT~(b^=nrLX}w;=w$r7cf@V(P?76K&
zMigaAq*0?3LaIy8M_E&me%Ra*XTt3nQl>PKJMfKCuV6ko!#j*s`SV}ZD8NlJYOnql
zc`59n)zdg_?wd3@sU_&)L{!m5MoyZ8)DpyFyz}p94dj*sdLHAWkc&p5Z5~#YUC6i(
zzHGhi{0X2zjMC++xO>gR}7pNNQU=
zIxRK1L+CZT`EB1hL19h>>9fC)i!mI*+~>#xpJog^CD&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2Sflpr2Uh)L*SFy=DX7{}U|Y^5aRDc+!=}sk%W+l~3ppl5N57gfaoWcTZ11dR)Tu
zHdJCe
z{G6`L@w!
zYM-C^ZcX>lJZ*CKxEzMnIR5h&WkNzxb&RB>HbC8G1W^~Fz{C`|*}1xx7-Onh~g>XM>OS{vwlh_CU1!5dv2LJ#7
diff --git a/docs/search.json b/docs/search.json
index f7aeb6ba..7bec3135 100755
--- a/docs/search.json
+++ b/docs/search.json
@@ -865,14 +865,14 @@
"href": "bivariate.html#fitting-the-data",
"title": "24 Fits and residuals",
"section": "24.2 Fitting the data",
- "text": "24.2 Fitting the data\nScatter plots are a good first start in visualizing bivariate data, but this is sometimes not enough. Our eyes need “guidance” to help perceive patterns. Another visual aid involves fitting the data with a line. We will explore two fitting strategies: the parametric fit and the non-parametric fit.\n\n24.2.1 Parametric fit\nA parametric fit is one where we impose a structure to the data–an example of which is a straight line (also referred to as a 1st order polynomial fit). The parametric fit is by far the most popular fitting strategy used in the realm of data analysis and statistics.\n\n24.2.1.1 Fitting a straight line\nA straight line is the simplest fit one can make to bivariate data. A popular method for fitting a straight line is the least-squares method. We’ll use R’s lm() function which provides us with a slope and intercept for the best fit line.\nThis can be implemented in the base plotting environment as follows:\n\nM <- lm(cp.ratio ~ area, dat = df)\nplot(cp.ratio ~ area, dat = df)\nabline(M, col = \"red\")\n\n\n\n\nIn the ggplot2 plotting environment, we can make use of the stat_smooth function to generate the regression line.\n\nlibrary(ggplot2)\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method =\"lm\", se = FALSE)\n\n\n\n\nThe se = FALSE option prevents R from drawing a confidence envelope around the regression line. Confidence envelopes are used in inferential statistics (a topic not covered in this course).\nThe straight line is a first order polynomial with two parameters, \\(a\\) and \\(b\\), that define an equation that best describes the relationship between the two variables:\n\\[\ny = a + b (x)\n\\]\nwhere \\(a\\) and \\(b\\) can be extracted from the regression model object M as follows:\n\ncoef(M)\n\n(Intercept) area \n 0.01399056 0.10733436 \n\n\nThus \\(a\\) = 0.014 and \\(b\\) = 0.11.\n\n\n24.2.1.2 Fitting a 2nd order polynomial\nA second order polynomial is a three parameter function (\\(a\\), \\(b\\) and \\(c\\)) whose equation \\(y = a + bx + cx^2\\) defines a curve that best fits the data. We define such a relationship in R using the formula cp.ratio ~ area + I(area^2). The identity function I() preserves the arithmetic interpretation of area^2 as part of the model. Our new lm expression and resulting coefficients follow:\n\nM2 <- lm(cp.ratio ~ area + I(area^2) , dat = df)\ncoef(M2)\n\n (Intercept) area I(area^2) \n 2.8684792029 -0.0118691702 0.0008393243 \n\n\nThe quadratic fit is thus,\n\\[\ny = 2.87 - 0.012 x + 0.000839 x^2\n\\]\nIn using the base plot environment, we cannot use abline to plot the predicted 2nd order polynomial curve since abline only draws straight lines. We will need to construct the line manually using the predict and lines functions.\n\nplot(cp.ratio ~ area, dat=df)\nx.pred <- data.frame( area = seq(min(df$area), max(df$area), length.out = 50) )\ny.pred <- predict(M2, x.pred)\nlines(x.pred$area, y.pred, col = \"red\")\n\n\n\n\nTo generate the same plot in ggplot2, simply pass the formula as an argument to stat_smooth:\n\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method = \"lm\", se = FALSE, formula = y ~ x + I(x^2) )\n\n\n\n\n\n\n\n24.2.2 Non-parametric fits\nNon-parametric fit applies to the family of fitting strategies that do not impose a structure on the data. Instead, they are designed to let the dataset reveal its inherent structure. One explored in this course is the loess fit.\n\n24.2.2.1 Loess\nA flexible curve fitting option is the loess curve (short for local regression; also known as the local weighted regression). Unlike the parametric approach to fitting a curve, the loess does not impose a structure on the data. The loess curve fits small segments of a regression lines across the range of x-values, then links the mid-points of these regression lines to generate the smooth curve. The range of x-values that contribute to each localized regression lines is defined by the \\(\\alpha\\) parameter which usually ranges from 0.2 to 1. The larger the \\(\\alpha\\) value, the smoother the curve. The other parameter that defines a loess curve is \\(\\lambda\\): it defines the polynomial order of the localized regression line. This is usually set to 1 (though ggplot2’s implementation of the loess defaults to a 2nd order polynomial).\n\n\n24.2.2.2 How a loess is constructed\nBehind the scenes, each point (xi,yi) that defines the loess curve is constructed as follows:\n\nA subset of data points closest to point xi are identified (xi i shown as the vertical dashed line in the figures below). The number of points in the subset is computed by multiplying the bandwidth \\(\\alpha\\) by the total number of observations. In our current example, \\(\\alpha\\) is set to 0.5. The number of points defining the subset is thus 0.5 * 14 = 7. The points are identified in the light blue area of the plot in panel (a) of the figure below.\nThe points in the subset are assigned weights. Greater weight is assigned to points closest to xi and vice versa. The weights define the points’ influence on the fitted line. Different weighting techniques can be implemented in a loess with the gaussian weight being the most common. Another weighting strategy we will also explore later in this course is the symmetric weight.\nA regression line is fit to the subset of points. Points with smaller weights will have less leverage on the fitted line than points with larger weights. The fitted line can be either a first order polynomial fit or a second order polynomial fit.\nNext, the value yi from the regression line is computed. This is shown as the red dot in panel (d). This is one of the points that will define the shape of the loess.\n\n\n\n\n\n\nThe above steps are repeated for as many xi values practically possible. Note that when xi approaches an upper or lower x limit, the subset of points becomes skewed to one side of xi. For example, when estimating x10, the seven closest points to the right of x10 are selected. Likewise, for the upper bound x140, the seven closest points to the left of x140 are selected.\n\n\n\n\n\nIn the following example, just under 30 loess points are computed at equal intervals. This defines the shape of the loess.\n\n\n\n\n\nIt’s more conventional to plot the line segments than it is to plot the points.\n\n\n\n\n\n\n\n\n24.2.2.3 Plotting a loess in R\nThe loess fit can be computed in R using the loess() function. It takes as arguments span (\\(\\alpha\\)), and degree (\\(\\lambda\\)).\n\n# Fit loess function\nlo <- loess(cp.ratio ~ area, df, span = 0.5, degree = 1)\n\n# Predict loess values for a range of x-values\nlo.x <- seq(min(df$area), max(df$area), length.out = 50)\nlo.y <- predict(lo, lo.x)\n\nThe modeled loess curve can be added to the scatter plot using the lines function.\n\nplot(cp.ratio ~ area, dat = df)\nlines(lo.x, lo.y, col = \"red\")\n\n\n\n\nIn ggplot2 simply pass the method=\"loess\" parameter to the stat_smooth function.\n\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method = \"loess\", se = FALSE, span = 0.5)\n\nHowever, ggplot defaults to a second degree loess (i.e. the small regression line elements that define the loess are modeled using a 2nd order polynomial and not a 1st order polynomial). If a first order polynomial (degree=1) is desired, you need to include an argument list in the form of method.args=list(degree=1) to the stat_smooth function.\n\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method = \"loess\", se = FALSE, span = 0.5, \n method.args = list(degree = 1) )"
+ "text": "24.2 Fitting the data\nScatter plots are a good first start in visualizing bivariate data, but this is sometimes not enough. Our eyes need “guidance” to help perceive patterns. Another visual aid involves fitting the data with a line. We will explore two fitting strategies: the parametric fit and the non-parametric fit.\n\n24.2.1 Parametric fit\nA parametric fit is one where we impose a structure to the data–an example of which is a straight line (also referred to as a 1st order polynomial fit). The parametric fit is by far the most popular fitting strategy used in the realm of data analysis and statistics.\n\n24.2.1.1 Fitting a straight line\nA straight line is the simplest fit one can make to bivariate data. A popular method for fitting a straight line is the least-squares method. We’ll use R’s lm() function which provides us with a slope and intercept for the best fit line.\nThis can be implemented in the base plotting environment as follows:\n\nM <- lm(cp.ratio ~ area, dat = df)\nplot(cp.ratio ~ area, dat = df)\nabline(M, col = \"red\")\n\n\n\n\nIn the ggplot2 plotting environment, we can make use of the stat_smooth function to generate the regression line.\n\nlibrary(ggplot2)\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method =\"lm\", se = FALSE)\n\n\n\n\nThe se = FALSE option prevents R from drawing a confidence envelope around the regression line. Confidence envelopes are used in inferential statistics (a topic not covered in this course).\nThe straight line is a first order polynomial with two parameters, \\(a\\) and \\(b\\), that define an equation that best describes the relationship between the two variables:\n\\[\ny = a + b (x)\n\\]\nwhere \\(a\\) and \\(b\\) can be extracted from the regression model object M as follows:\n\ncoef(M)\n\n(Intercept) area \n 0.01399056 0.10733436 \n\n\nThus \\(a\\) = 0.014 and \\(b\\) = 0.11.\n\n\n24.2.1.2 Fitting a 2nd order polynomial\nA second order polynomial is a three parameter function (\\(a\\), \\(b\\) and \\(c\\)) whose equation \\(y = a + bx + cx^2\\) defines a curve that best fits the data. We define such a relationship in R using the formula cp.ratio ~ area + I(area^2). The identity function I() preserves the arithmetic interpretation of area^2 as part of the model. Our new lm expression and resulting coefficients follow:\n\nM2 <- lm(cp.ratio ~ area + I(area^2) , dat = df)\ncoef(M2)\n\n (Intercept) area I(area^2) \n 2.8684792029 -0.0118691702 0.0008393243 \n\n\nThe quadratic fit is thus,\n\\[\ny = 2.87 - 0.012 x + 0.000839 x^2\n\\]\nIn using the base plot environment, we cannot use abline to plot the predicted 2nd order polynomial curve since abline only draws straight lines. We will need to construct the line manually using the predict and lines functions.\n\nplot(cp.ratio ~ area, dat=df)\nx.pred <- data.frame( area = seq(min(df$area), max(df$area), length.out = 50) )\ny.pred <- predict(M2, x.pred)\nlines(x.pred$area, y.pred, col = \"red\")\n\n\n\n\nTo generate the same plot in ggplot2, simply pass the formula as an argument to stat_smooth:\n\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method = \"lm\", se = FALSE, formula = y ~ x + I(x^2) )\n\n\n\n\n\n\n\n24.2.2 Non-parametric fits\nNon-parametric fit applies to the family of fitting strategies that do not impose a structure on the data. Instead, they are designed to let the dataset reveal its inherent structure. One explored in this course is the loess fit.\n\n24.2.2.1 Loess\nA flexible curve fitting option is the loess curve (short for local regression; also known as the local weighted regression). Unlike the parametric approach to fitting a curve, the loess does not impose a structure on the data. The loess curve fits small segments of a regression lines across the range of x-values, then links the mid-points of these regression lines to generate the smooth curve. The range of x-values that contribute to each localized regression lines is defined by the span parameter, \\(\\alpha\\), which usually ranges from 0.2 to 1 (but, it can be greater than 1 for smaller datasets). The larger the \\(\\alpha\\) value, the smoother the curve. The other parameter that defines a loess curve is \\(\\lambda\\): it defines the polynomial order of the localized regression line. This is usually set to 1 (though ggplot2’s implementation of the loess defaults to a 2nd order polynomial).\n\n\n24.2.2.2 How a loess is constructed\nBehind the scenes, each point (xi,yi) that defines the loess curve is constructed as follows:\n\nA subset of data points closest to point xi are identified (xi i shown as the vertical dashed line in the figures below). The number of points in the subset is computed by multiplying the bandwidth \\(\\alpha\\) by the total number of observations. In our current example, \\(\\alpha\\) is set to 0.5. The number of points defining the subset is thus 0.5 * 14 = 7. The points are identified in the light blue area of the plot in panel (a) of the figure below.\nThe points in the subset are assigned weights. Greater weight is assigned to points closest to xi and vice versa. The weights define the points’ influence on the fitted line. Different weighting techniques can be implemented in a loess with the gaussian weight being the most common. Another weighting strategy we will also explore later in this course is the symmetric weight.\nA regression line is fit to the subset of points. Points with smaller weights will have less leverage on the fitted line than points with larger weights. The fitted line can be either a first order polynomial fit or a second order polynomial fit.\nNext, the value yi from the regression line is computed. This is shown as the red dot in panel (d). This is one of the points that will define the shape of the loess.\n\n\n\n\n\n\nThe above steps are repeated for as many xi values practically possible. Note that when xi approaches an upper or lower x limit, the subset of points becomes skewed to one side of xi. For example, when estimating x10, the seven closest points to the right of x10 are selected. Likewise, for the upper bound x140, the seven closest points to the left of x140 are selected.\n\n\n\n\n\nIn the following example, just under 30 loess points are computed at equal intervals. This defines the shape of the loess.\n\n\n\n\n\nIt’s more conventional to plot the line segments than it is to plot the points.\n\n\n\n\n\n\n\n\n24.2.2.3 Plotting a loess in R\nThe loess fit can be computed in R using the loess() function. It takes as arguments span (\\(\\alpha\\)), and degree (\\(\\lambda\\)).\n\n# Fit loess function\nlo <- loess(cp.ratio ~ area, df, span = 0.5, degree = 1)\n\n# Predict loess values for a range of x-values\nlo.x <- seq(min(df$area), max(df$area), length.out = 50)\nlo.y <- predict(lo, lo.x)\n\nThe modeled loess curve can be added to the scatter plot using the lines function.\n\nplot(cp.ratio ~ area, dat = df)\nlines(lo.x, lo.y, col = \"red\")\n\n\n\n\nIn ggplot2 simply pass the method=\"loess\" parameter to the stat_smooth function.\n\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method = \"loess\", se = FALSE, span = 0.5)\n\nHowever, ggplot defaults to a second degree loess (i.e. the small regression line elements that define the loess are modeled using a 2nd order polynomial and not a 1st order polynomial). If a first order polynomial (degree=1) is desired, you need to include an argument list in the form of method.args=list(degree=1) to the stat_smooth function.\n\nggplot(df, aes(x = area, y = cp.ratio)) + geom_point() + \n stat_smooth(method = \"loess\", se = FALSE, span = 0.5, \n method.args = list(degree = 1) )"
},
{
"objectID": "bivariate.html#residuals",
"href": "bivariate.html#residuals",
"title": "24 Fits and residuals",
"section": "24.3 Residuals",
- "text": "24.3 Residuals\nFitting the data with a line is just the first step in EDA. Your next step should be to explore the residuals. The residuals are the distances (parallel to the y-axis) between the observed points and the fitted line. The closer the points are to the line (i.e. the smaller the residuals) the better the fit.\nThe residuals can be computed using the residuals() function. It takes as argument the model object. For example, to extract the residuals from the linear model M computed earlier type,\n\nresiduals(M)\n\n 1 2 3 4 5 6 7 8 9 10 \n 1.3596154 1.3146525 0.5267426 -0.1133131 -0.1421694 0.3998247 -1.2188692 -2.0509148 -1.8622085 -0.1704418 \n 11 12 13 14 \n-0.5788861 0.4484394 -1.0727624 3.1602905 \n\n\n\n24.3.1 Residual-dependence plot\nOne way to visualize the residuals is to create a residual-dependence plot–this plots the residuals as a function of the x-values. We’ll do this using ggplot so that we can also fit a loess curve to help discern any pattern in the residuals.\n\ndf$residuals <- residuals(M)\nggplot(df, aes(x = area, y = residuals)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nWe are interested in identifying any pattern in the residuals. If the model does a good job in fitting the data, the points should be uniformly distributed across the plot and the loess fit should approximate a horizontal line. With the linear model M, we observe a convex pattern in the residuals suggesting that the linear model is not a good fit. We say that the residuals show dependence on the x values.\nNext, we’ll look at the residuals from the second order polynomial model M2.\n\ndf$residuals2 <- residuals(M2)\nggplot(df, aes(x = area, y = residuals2)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nThere is no indication of dependency between the residual and the area values. The second order polynomial is a large improvement over the first order polynomial. Let’s look at the loess model.\n\ndf$residuals3 <- residuals(lo)\nggplot(df, aes(x = area, y = residuals3)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nNo surprise here. The loess model does a good job in capturing any overall pattern in the data. But note that this is to be expected given that the loess fit is a non-parametric model–it lets the data define its shape!\n\nYou may ask “if the loess model does such a good job in fitting the data, why bother with polynomial fits?” If you are seeking to generate a predictive model that explains the relationship between the y and x variables, then a mathematically tractable model (like a polynomial model) should be sought. If the interest is simply in identifying a pattern in the data, then a loess fit is a good choice.\n\nA model is deemed “well defined” when its residuals are constant over the full range of \\(x\\). In essence, we expect a formula of the form:\n\\[\nY = a + bx + \\varepsilon\n\\] where \\(\\varepsilon\\) is a constant that does not vary as a function of varying \\(x\\). This should sound quite familiar to you given that we’ve spent a good part of the univariate analysis section seeking a homogeneous spread in the data (i.e. a spread that did not change as a function of fitted group values). So what other diagnostic plots can we (and should we) generate from the fitted model? We explore such plots next.\n\n\n24.3.2 Spread-location plot\nThe M2 and lo models do a good job in eliminating any dependence between residual and x-value. Next, we will check that the residuals do not show a dependence with fitted y-values. This is analogous to univariate analysis where we checked if residuals increased or decreased with increasing medians across categories. Here we will compare residuals to the fitted cp.ratio values (for a univariate analogy, think of the fitted line as representing a level across different segments along the x-axis). We’ll generate a spread-level plot of model M2’s residuals (note that in the realm of regression analysis, such plot is often referred to as a scale-location plot). We’ll also add a loess curve to help visualize any patterns in the plot.\n\nsl2 <- data.frame( std.res = sqrt(abs(residuals(M2))), \n fit = predict(M2))\n\nggplot(sl2, aes(x = fit, y =std.res)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nThe function predict() extracts the y-values from the fitted model M2 and is plotted along the x-axis. It’s clear from this plot that the residuals are not homogeneous; they increase as a function of increasing fitted CP ratio. The “bend” observed in the loess curve is most likely due to a single point at the far (right) end of the fitted range. Given that we have a small batch of numbers, a loess can be easily influenced by an outlier. We may want to increase the loess span.\n\nggplot(sl2, aes(x = fit, y = std.res)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1.5, \n method.args = list(degree = 1) )\n\n\n\n\nThe point’s influence is reduced enough to convince us that the observed monotonic increase is real.\nWe learned with the univariate data that re-expressing values was one way to correct for any increasing or decreasing spreads across the range of fitted values. At this point, we may want to look into re-expressing the data. But, before we do, we’ll explore another diagnostic plot: the normal q-q plot.\n\n\n24.3.3 Checking residuals for normality\nIf you are interested in conducting a hypothesis test (i.e. addressing the question “is the slope significantly different from 0”) you will likely want to check the residuals for normality since this is an assumption made when computing a confidence interval and a p-value.\nIn ggplot, we learned that a normal q-q plot could be generated using the stat_qq and stat_qq_line function.\n\nggplot(df, aes(sample = residuals2)) + \n stat_qq(distribution = qnorm) +\n stat_qq_line(distribution = qnorm, col = \"blue\")\n\n\n\n\nHere, the residuals seem to stray a little from a normal distribution."
+ "text": "24.3 Residuals\nFitting the data with a line is just the first step in EDA. Your next step should be to explore the residuals. The residuals are the distances (parallel to the y-axis) between the observed points and the fitted line. The closer the points are to the line (i.e. the smaller the residuals) the better the fit.\nThe residuals can be computed using the residuals() function. It takes as argument the model object. For example, to extract the residuals from the linear model M computed earlier type,\n\nresiduals(M)\n\n 1 2 3 4 5 6 7 8 9 10 \n 1.3596154 1.3146525 0.5267426 -0.1133131 -0.1421694 0.3998247 -1.2188692 -2.0509148 -1.8622085 -0.1704418 \n 11 12 13 14 \n-0.5788861 0.4484394 -1.0727624 3.1602905 \n\n\n\n24.3.1 Residual-dependence plot\nOne way to visualize the residuals is to create a residual-dependence plot–this plots the residuals as a function of the x-values. We’ll do this using ggplot so that we can also fit a loess curve to help discern any pattern in the residuals.\n\ndf$residuals <- residuals(M)\nggplot(df, aes(x = area, y = residuals)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nWe are interested in identifying any pattern in the residuals. If the model does a good job in fitting the data, the points should be uniformly distributed across the plot and the loess fit should approximate a horizontal line. With the linear model M, we observe a convex pattern in the residuals suggesting that the linear model is not a good fit. We say that the residuals show dependence on the x values.\nNext, we’ll look at the residuals from the second order polynomial model M2.\n\ndf$residuals2 <- residuals(M2)\nggplot(df, aes(x = area, y = residuals2)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nThere is no indication of dependency between the residual and the area values. The second order polynomial is a large improvement over the first order polynomial. Let’s look at the loess model.\n\ndf$residuals3 <- residuals(lo)\nggplot(df, aes(x = area, y = residuals3)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nNo surprise here. The loess model does a good job in capturing any overall pattern in the data. But note that this is to be expected given that the loess fit is a non-parametric model–it lets the data define its shape!\n\nYou may ask “if the loess model does such a good job in fitting the data, why bother with polynomial fits?” If you are seeking to generate a predictive model that explains the relationship between the y and x variables, then a mathematically tractable model (like a polynomial model) should be sought. If the interest is simply in identifying a pattern in the data, then a loess fit is a good choice.\n\nA model is deemed “well defined” when its residuals are constant over the full range of \\(x\\). In essence, we expect a formula of the form:\n\\[\nY = a + bx + \\varepsilon\n\\] where \\(\\varepsilon\\) is a constant that does not vary as a function of varying \\(x\\). This should sound quite familiar to you given that we’ve spent a good part of the univariate analysis section seeking a homogeneous spread in the data (i.e. a spread that did not change as a function of fitted group values). So what other diagnostic plots can we (and should we) generate from the fitted model? We explore such plots next.\n\n\n24.3.2 Spread-location plot\nThe M2 and lo models do a good job in eliminating any dependence between residual and x-value. Next, we will check that the residuals do not show a dependence with fitted y-values. This is analogous to univariate analysis where we checked if residuals increased or decreased with increasing medians across categories. Here we will compare residuals to the fitted cp.ratio values (for a univariate analogy, think of the fitted line as representing a level across different segments along the x-axis). We’ll generate a spread-level plot of model M2’s residuals (note that in the realm of regression analysis, such plot is often referred to as a scale-location plot). We’ll also add a loess curve to help visualize any patterns in the plot.\n\nsl2 <- data.frame( std.res = sqrt(abs(residuals(M2))), \n fit = predict(M2))\n\nggplot(sl2, aes(x = fit, y =std.res)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 1, \n method.args = list(degree = 1) )\n\n\n\n\nThe function predict() extracts the y-values from the fitted model M2 and is plotted along the x-axis. It’s clear from this plot that the residuals are not homogeneous; they increase as a function of increasing fitted CP ratio. The “bend” observed in the loess curve is most likely due to a single point at the far (right) end of the fitted range. Given that we have a small batch of numbers, a loess can be easily influenced by an outlier. We may therefore want to increase the loess span by setting span = 2.\n\nggplot(sl2, aes(x = fit, y = std.res)) + geom_point() +\n stat_smooth(method = \"loess\", se = FALSE, span = 2, \n method.args = list(degree = 1) )\n\n\n\n\nThe point’s influence is reduced enough to convince us that the observed monotonic increase is real.\nWe learned with the univariate data that re-expressing values was one way to correct for any increasing or decreasing spreads across the range of fitted values. At this point, we may want to look into re-expressing the data. But, before we do, we’ll explore another diagnostic plot: the normal q-q plot.\n\n\n24.3.3 Checking residuals for normality\nIf you are interested in conducting a hypothesis test (i.e. addressing the question “is the slope significantly different from 0”) you will likely want to check the residuals for normality since this is an assumption made when computing a confidence interval and a p-value.\nIn ggplot, we learned that a normal q-q plot could be generated using the stat_qq and stat_qq_line function.\n\nggplot(df, aes(sample = residuals2)) + \n stat_qq(distribution = qnorm) +\n stat_qq_line(distribution = qnorm, col = \"blue\")\n\n\n\n\nHere, the residuals seem to stray a little from a normal distribution."
},
{
"objectID": "bivariate.html#re-expressing-the-data",