EB

Basic Statistics

\(\newcommand{\footnotename}{footnote}\) \(\def \LWRfootnote {1}\) \(\newcommand {\footnote }[2][\LWRfootnote ]{{}^{\mathrm {#1}}}\) \(\newcommand {\footnotemark }[1][\LWRfootnote ]{{}^{\mathrm {#1}}}\) \(\let \LWRorighspace \hspace \) \(\renewcommand {\hspace }{\ifstar \LWRorighspace \LWRorighspace }\) \(\newcommand {\TextOrMath }[2]{#2}\) \(\newcommand {\mathnormal }[1]{{#1}}\) \(\newcommand \ensuremath [1]{#1}\) \(\newcommand {\LWRframebox }[2][]{\fbox {#2}} \newcommand {\framebox }[1][]{\LWRframebox } \) \(\newcommand {\setlength }[2]{}\) \(\newcommand {\addtolength }[2]{}\) \(\newcommand {\setcounter }[2]{}\) \(\newcommand {\addtocounter }[2]{}\) \(\newcommand {\arabic }[1]{}\) \(\newcommand {\number }[1]{}\) \(\newcommand {\noalign }[1]{\text {#1}\notag \\}\) \(\newcommand {\cline }[1]{}\) \(\newcommand {\directlua }[1]{\text {(directlua)}}\) \(\newcommand {\luatexdirectlua }[1]{\text {(directlua)}}\) \(\newcommand {\protect }{}\) \(\def \LWRabsorbnumber #1 {}\) \(\def \LWRabsorbquotenumber "#1 {}\) \(\newcommand {\LWRabsorboption }[1][]{}\) \(\newcommand {\LWRabsorbtwooptions }[1][]{\LWRabsorboption }\) \(\def \mathchar {\ifnextchar "\LWRabsorbquotenumber \LWRabsorbnumber }\) \(\def \mathcode #1={\mathchar }\) \(\let \delcode \mathcode \) \(\let \delimiter \mathchar \) \(\def \oe {\unicode {x0153}}\) \(\def \OE {\unicode {x0152}}\) \(\def \ae {\unicode {x00E6}}\) \(\def \AE {\unicode {x00C6}}\) \(\def \aa {\unicode {x00E5}}\) \(\def \AA {\unicode {x00C5}}\) \(\def \o {\unicode {x00F8}}\) \(\def \O {\unicode {x00D8}}\) \(\def \l {\unicode {x0142}}\) \(\def \L {\unicode {x0141}}\) \(\def \ss {\unicode {x00DF}}\) \(\def \SS {\unicode {x1E9E}}\) \(\def \dag {\unicode {x2020}}\) \(\def \ddag {\unicode {x2021}}\) \(\def \P {\unicode {x00B6}}\) \(\def \copyright {\unicode {x00A9}}\) \(\def \pounds {\unicode {x00A3}}\) \(\let \LWRref \ref \) \(\renewcommand {\ref }{\ifstar \LWRref \LWRref }\) \( \newcommand {\multicolumn }[3]{#3}\) \(\require {textcomp}\) \(\newcommand {\intertext }[1]{\text {#1}\notag \\}\) \(\let \Hat \hat \) \(\let \Check \check \) \(\let \Tilde \tilde \) \(\let \Acute \acute \) \(\let \Grave \grave \) \(\let \Dot \dot \) \(\let \Ddot \ddot \) \(\let \Breve \breve \) \(\let \Bar \bar \) \(\let \Vec \vec \) \(\require {mathtools}\) \(\newcommand {\vcentcolon }{\mathrel {\unicode {x2236}}}\) \(\newcommand {\approxcolon }{\approx \vcentcolon }\) \(\newcommand {\Approxcolon }{\approx \dblcolon }\) \(\newcommand {\simcolon }{\sim \vcentcolon }\) \(\newcommand {\Simcolon }{\sim \dblcolon }\) \(\newcommand {\dashcolon }{\mathrel {-}\vcentcolon }\) \(\newcommand {\Dashcolon }{\mathrel {-}\dblcolon }\) \(\newcommand {\colondash }{\vcentcolon \mathrel {-}}\) \(\newcommand {\Colondash }{\dblcolon \mathrel {-}}\) \(\newenvironment {crampedsubarray}[1]{}{}\) \(\newcommand {\smashoperator }[2][]{#2\limits }\) \(\newcommand {\SwapAboveDisplaySkip }{}\) \(\newcommand {\LaTeXunderbrace }[1]{\underbrace {#1}}\) \(\newcommand {\LaTeXoverbrace }[1]{\overbrace {#1}}\) \(\Newextarrow \xLongleftarrow {10,10}{0x21D0}\) \(\Newextarrow \xLongrightarrow {10,10}{0x21D2}\) \(\let \xlongleftarrow \xleftarrow \) \(\let \xlongrightarrow \xrightarrow \) \(\newcommand {\LWRmultlined }[1][]{\begin {multline*}}\) \(\newenvironment {multlined}[1][]{\LWRmultlined }{\end {multline*}}\) \(\let \LWRorigshoveleft \shoveleft \) \(\renewcommand {\shoveleft }[1][]{\LWRorigshoveleft }\) \(\let \LWRorigshoveright \shoveright \) \(\renewcommand {\shoveright }[1][]{\LWRorigshoveright }\) \(\newcommand {\shortintertext }[1]{\text {#1}\notag \\}\) \(\newcommand {\bm }[1]{\boldsymbol {#1}}\) \(\require {cancel}\) \(\require {colortbl}\) \(\let \LWRorigcolumncolor \columncolor \) \(\renewcommand {\columncolor }[2][named]{\LWRorigcolumncolor [#1]{#2}\LWRabsorbtwooptions }\) \(\let \LWRorigrowcolor \rowcolor \) \(\renewcommand {\rowcolor }[2][named]{\LWRorigrowcolor [#1]{#2}\LWRabsorbtwooptions }\) \(\let \LWRorigcellcolor \cellcolor \) \(\renewcommand {\cellcolor }[2][named]{\LWRorigcellcolor [#1]{#2}\LWRabsorbtwooptions }\) \(\newcommand {\tcbset }[1]{}\) \(\newcommand {\tcbsetforeverylayer }[1]{}\) \(\newcommand {\tcbox }[2][]{\boxed {\text {#2}}}\) \(\newcommand {\tcboxfit }[2][]{\boxed {#2}}\) \(\newcommand {\tcblower }{}\) \(\newcommand {\tcbline }{}\) \(\newcommand {\tcbtitle }{}\) \(\newcommand {\tcbsubtitle [2][]{\mathrm {#2}}}\) \(\newcommand {\tcboxmath }[2][]{\boxed {#2}}\) \(\newcommand {\tcbhighmath }[2][]{\boxed {#2}}\)

14.2 Solutions

  • 14.2.1 Initially, the main quantities necessary for the point estimation of the regression coefficients and the sums of squares and products were obtained, as presented in the sequence. The sum of products is

    \begin{align*} SPXY=& \displaystyle \sum _{i=1}^{n}X_iY_i - \dfrac {\displaystyle \sum _{i=1}^{n}X_i \sum _{i=1}^{n}Y_i}{n}\\ =& -148.859-\dfrac {-13.5\times 28.6}{8}=-100.5965, \end{align*}

    \begin{align*} SQX=&\displaystyle \sum _{i=1}^{n}X_i^2 - \dfrac {\displaystyle \left (\sum _{i=1}^{n} X_i \right )^2}{n} \\ =& 1184.21-\dfrac {(-13.5)^2}{8}=1161.429, \end{align*}

    \begin{align*} SQY=&\displaystyle \sum _{i=1}^{n}Y_i^2 - \dfrac {\displaystyle \left (\sum _{i=1}^{n} Y_i \right )^2}{n}\\ =& 111.2274 -\dfrac {28.6^2}{8}=8.9824, \end{align*}

    \begin{align*} \bar {X}_. =& -1.6875 & \textrm { and } && \bar {Y}_.=& 3.575. \end{align*}

    Therefore, the estimates of \(\beta _0\) and \(\beta _1\) are

    \begin{align*} \hat {\beta }_1 =& \dfrac {SPXY}{SQX}=\dfrac {-100.5965}{1161.429}=-0.08661444 \end{align*} and

    \begin{align*} \hat {\beta }_0 =& \bar {Y}_.-\hat {\beta }_1\bar {X}_{.}=3.575-0.08661444\times 1.6875 = 3.428838. \end{align*}

    The fitted regression model is

    \begin{align*} \hat {Y}_i=& 3.428838 -0.08661444 X_i. \end{align*}

    The increment of each unit of temperature in degrees Celsius causes an average reduction of \(0.08661444\) ml/g/h in oxygen consumption. The value \(3.428838\) corresponds to \(X=0\) degrees. In this case, the fit of the special regression model, considering passing through the origin, point \((0\), \(0)\) of the Cartesian plane, does not seem appropriate at first sight.

    The sums of squares corresponding to total, regression and residual are

    \begin{align*} SQTotal=& SQY=8.9824,\\ SQRegression=& \dfrac {SPXY^2}{SQX}=8.713109,\\ SQRes=& SQTotal-SQRegression\\ =& 8.9824-8.713109=0.2692906. \end{align*}

    The analysis of variance table is presented below:

    .
    Source of variation DF SS MS \(F_c\) \(p\)-value
    Regression 1 8.7131094 8.71310945 194.1348 \(8.5169\times 10^{-6}\)
    Residual 6 0.2692906 0.04488176
    Total 7 8.9824000 1.28320000

    It is verified that the fraction explained by the regression of the total variation is much greater than the variation attributed to the residual variation. This is an indication, at first and in principle, that the regression model is well fitted and is suitable for making predictions of oxygen consumption as a function of ambient temperature.

    The hypotheses of interest are given by

    \begin{align*} H_0:&\,\,\beta _0=0 &\textrm { vs } && H_1:&\,\,\beta _0\ne 0 \end{align*} and

    \begin{align*} H_0:&\,\,\beta _1=0 &\textrm { vs } && H_1:&\,\,\beta _1\ne 0. \end{align*}

    The statistic for testing the parameter \(\beta _0\) is given by

    \begin{align*} T_c=& \dfrac {\hat {\beta }_0-\beta ^*_0}{S\sqrt {\displaystyle \sum _{i=1}^{n} X^2_i \Big / (n SQX)}} = \dfrac {3.428838}{0.2118532\times \sqrt {1184.21 \Big / (8 \times 1161.429)}}\\ =&45.33556. \end{align*}

    Since the upper quantile \(t_{0.025;\nu =6}\) is equal to \(2.4469\), the null hypothesis that the intercept is equal to zero is rejected and, therefore, it is justified to obtain the \(95\%\) confidence interval for \(\beta _0\). Thus,

    \begin{align*} CI_{0.95}\left (\beta _0\right ):&\,\,\hat {\beta }_0 \pm t_{\alpha /2;\nu }S\sqrt { \dfrac {\displaystyle \sum _{i=1}^{n} X_i^2}{n SQX}}=3.428838\pm 2.4469\times 0.07563242\\ &\,\,=[3.2437723,\,\, 3.61390401]. \end{align*}

    For the slope, the test statistic is given by

    \begin{align*} T_c =& \dfrac {\hat {\beta }_1-\beta ^*_1}{S\Big /\sqrt {SQX}} = \dfrac {-0.08661444}{0.2118532\Big /\sqrt {1161.429}} =\dfrac {-0.08661444}{0.006216396}\\ =& -13.93322. \end{align*}

    Since the calculated value \(|-13.93322|\) is greater than the tabulated value of \(2.4469\), the hypothesis of nullity of the slope is rejected and it is concluded that oxygen consumption reduces by \(0.0866\) ml/g/h for each one degree Celsius increase in temperature \(X\). The associated \(p\)-value was \(8.516873\times 10^{-06}\), which is less than the nominal significance level of \(5\%\), which would lead us to the same decision as before, when we compared the observed value with the tabulated value of the Student’s \(t\) distribution.

    It can also be observed that the \(p\)-value for the test of the regression coefficient being null, in this case of simple linear regression, is exactly the same as the \(p\)-value of the \(F\) test of the regression analysis of variance. The value of the \(F\) statistic is also the square of the value of the \(t\) statistic, that is, the two tests, in this situation, are equivalent.

    The \(95\%\) interval is given by

    \begin{align*} CI_{0.95}\left (\beta _1\right ):&\,\,\hat {\beta }_1 \pm t_{\alpha /2;\nu }\dfrac {S}{\sqrt {SQX}} = -0.0866\pm 2.4469\times 0.006216\\ &\,\,=[-0.1018254,\,\, -0.07140347]. \end{align*}

    Since both tests rejected their respective null hypotheses, it was justified to estimate the respective linear and angular coefficients by interval. This is the general procedure, illustrated in this exercise, for applying hypothesis tests and estimating the parameters of the simple linear model.

    The estimated value of \(Y_i\) for a given value of \(X_i\) is given by

    \begin{align*} \hat {Y}_i=& \hat {\beta }_0+ \hat {\beta }_1 X_i\\ =& 3.428838 -0.08661444 X_i. \end{align*}

    The standard error for the current estimated values is given by:

    \begin{align*} S_{\hat {Y}_i}=S\sqrt {\dfrac {1}{n}+\dfrac {\left (X_i-\bar {X}_.\right )^2}{\displaystyle \sum _{i=1}^{n}\left (X_i-\bar {X}_.\right )^2}}, \end{align*} whose values are presented in the following table.

    For the second case, the standard error is similar to the previous one, although the future prediction is more imprecise and the standard error has one unit added in the radicand of the previous expression. The estimator of the standard error for the measurement of a future observation \(X_0\) is given by

    \begin{align*} S_{\hat {Y}_0}=&S\sqrt {1+\dfrac {1}{n}+\dfrac {\left (X_0-\bar {X}_.\right )^2}{ \displaystyle \sum _{i=1}^{n}\left (X_i - \bar {X}_.\right )^2}}, \end{align*} whose values are presented in the following table, considering each \(X_i\) as an \(X_{0i}\).

    The confidence interval for a predicted value of \(Y\) (\(Y_i\)), given the observed value \(X_i\), is based on the Student’s \(t\) distribution and given by

    \begin{align*} CI_{1-\alpha }(Y_i):&\,\, \hat {Y}_i \pm t_{\alpha /2;\nu }S_{\hat {Y}_i}=& \hat {\beta }_0+ \hat {\beta }_1 X_i \pm t_{\alpha /2;\nu }S_{\hat {Y}_i}. \end{align*}

    The \(1-\alpha \) confidence interval for a future predicted value of \(Y\) (\(Y_0\)), given a future observed value \(X_0\), is also based on the Student’s \(t\) distribution and given by

    \begin{align*} CI_{1-\alpha }(Y_0):& \,\, \hat {Y}_0 \pm t_{\alpha /2;\nu }S_{\hat {Y}_0}= \hat {\beta }_0+ \hat {\beta }_1 X_0 \pm t_{\alpha /2;\nu }S_{\hat {Y}_0}. \end{align*}

    The predicted values with the confidence intervals for the current value and for the future value are presented in the following table:

    .
    \(y_i\) \(\hat {y}_i\) \(S_{\hat {Y}_i}\) LI LS \(S_{\hat {Y}_0}\) LIF LSF
    5.3100 2.2752 1.9122 -2.2463 6.7967 4.1077 -7.4378 11.9883
    4.7500 1.9107 1.6058 -1.8864 5.7078 3.9743 -7.4870 11.3084
    4.0200 1.2445 1.0459 -1.2286 3.7176 3.7829 -7.7007 10.1896
    3.5900 0.6285 0.5282 -0.6205 1.8776 3.6736 -8.0582 9.3153
    3.4100 -0.0126 0.0106 -0.0376 0.0124 3.6355 -8.6091 8.5839
    3.1100 -0.6411 0.5388 -1.9151 0.6329 3.6752 -9.3314 8.0493
    2.6500 -1.2822 1.0776 -3.8302 1.2659 3.7918 -10.2483 7.6840
    1.7600 -2.4261 2.0389 -7.2474 2.3952 4.1682 -12.2822 7.4301

    The graph showing the fitted model and the intervals for the current and future values is presented in figure 14.1.

    (-tikz- diagram)

    Figure 14.1: Fitted simple linear regression model for the data of the exercise (solid line) and the respective \(95\%\) confidence intervals for the predicted mean values (dotted lines) and for the future values (dashed lines) corresponding to each value of \(x\).

    For \(X_0\) \(=\) \(18\) we have that the predicted value is \(\hat {Y}_0\) \(=\) \(1.869778\) and the \(95\%\) interval for the future observation is:

    \begin{align*} CI_{1-\alpha }(Y_0):&\,\, \hat {Y}_0 \pm t_{\alpha /2;\nu }S_{\hat {Y}_0}= \hat {\beta }_0+ \hat {\beta }_1 X_0 \pm t_{\alpha /2;\nu }S_{\hat {Y}_0}\\ :& 1.869778 \pm 2.446912 \times 0.2558713 = [1.243684,\,\,\, 2.495873]. \end{align*}

    Using the point estimator for the inverse prediction, given \(Y_i\) \(=\) \(3.8\), we have

    \begin{align*} \hat {X}_i=& \dfrac {Y_i-\hat {\beta }_0}{\hat {\beta }_1}\\ =& \dfrac {3.8-3.42883813}{-0.08661444}\\ =& -4.285219^\circ C. \end{align*}

    The \(1-\alpha \) confidence interval for the population value \(X_i\), given the observed value \(Y_i\) \(=\) \(3.8\), is

    \begin{align*} CI_{1-\alpha }\left (X_i\right ):&\,\scriptstyle \bar {X}_.+\dfrac {\hat {\beta }_1\left (Y_i- \bar {Y}_.\right )}{K}\pm \dfrac {t_{\alpha /2;\nu }}{K}\sqrt {S^2 \left [\dfrac {\left (Y_i- \bar {Y}_.\right )^2}{\displaystyle \sum _{i=1}^{n}\left (X_i-\bar {X}_.\right )^2}+K\left (1+\dfrac {1}{n} \right )\right ]}\\ =& -1.6875+ \dfrac {-0.08661\left (3.8- 3.575\right )}{0.007271}\\ & \quad \pm \dfrac {2.446}{0.007271}\sqrt {0.04488 \left [\dfrac {\left (3.8- 3.575\right )^2}{1161.429}+0.007271\times \left (1+\dfrac {1}{8} \right )\right ]}\\ =& [-10.83329,\,\,\, 2.097521]. \end{align*} where \(K\) \(=\) \(\hat {\beta }_1^2\) \(-\) \(t^2_{\alpha /2;\nu }S^2_{\hat {\beta }_1}\) and \(\nu \) \(=\) \(n-2\) \(=\) \(8\), resulting in \(K\) \(=\) \(0.007270687\).

    For each of the observed values of \(Y\) the inverse prediction, its standard error and the \(95\%\) confidence interval were obtained and presented in the following Table.

    .
    \(y_i\) \(X_i\) \(\hat {X}_i\) \(S_{\hat {X}_i}\) LI LS
    5.31 -18.1 -21.7188 3.0241 -29.7559 -14.9566
    4.75 -15.2 -15.2534 2.8203 -22.5860 -8.7841
    4.02 -9.9 -6.8252 2.6626 -13.5038 -0.4736
    3.59 -5.0 -1.8607 2.6353 -8.3145 4.5821
    3.41 0.1 0.2175 2.6390 -6.1794 6.7356
    3.11 5.1 3.6811 2.6651 -2.6693 10.3732
    2.65 10.2 8.9920 2.7514 2.5995 16.0642
    1.76 19.3 19.2674 3.0582 12.4511 27.4175
    # function to adjust the regression between x and y
    # compute the anova table and apply the F and t tests
    # compute the confidence intervals for b0 and b1,
    # predicted Y and X (actual and future)
    # y_i=b0+b1 x_i + epsilon_i
    lr_fit <- function(x, y, alpha=0.05)
    {
      if (length(x) != length(y))
        stop("Sample sizes must be equal!")
      n <- length(x)
      sum_x <- sum(x)
      sum_y <- sum(y)
      spxy <- sum(x * y) - sum_x * sum_y / n
      sqx <- sum(x * x) - sum_x^2 / n
      sqy <- sum(y * y) - sum_y^2 / n
      sqreg <- spxy^2 / sqx
      sqres <- sqy - sqreg
      nu <- n - 2
      qmres <- sqres / nu
      b1 <- spxy / sqx
      b0 <- sum_y / n - b1 * sum_x / n
    
        # R2 and R2 adjusted
        R2 <- sqreg / sqy # sqtotal = sqy
        R2_adj <- 1 - (n - 1) / (n - 2) * (1 - R2)
    
        # anova
        anova_reg <- data.frame(
          sv = character(),
          df = numeric(),
          sum_sq = numeric(),
          mean_sq = numeric(),
          f_value =numeric(),
          p_value = numeric(),
          stringsAsFactors = FALSE
        )
        fc <- sqreg / qmres
        p.value <- 1 - pf(fc, 1, nu)
        aov_result <- list("Regression", 1, sqreg, sqreg, fc, p.value)
        anova_reg[nrow(anova_reg) + 1, ] <- aov_result
        fc <- NA
        p.value <- NA
        aov_result <- list("Residuals", nu, sqres, qmres, fc, p.value)
        anova_reg[nrow(anova_reg) + 1, ] <- aov_result
        fc <- NA
        p.value <- NA
        aov_result <- list("Total", n - 1, sqy, sqy / (n - 1), fc, p.value)
        anova_reg[nrow(anova_reg) + 1, ] <- aov_result
    
        # confidence intervals for predicted values
        cint_pred <- data.frame(
          y_obs    = numeric(),
          y_pred   = numeric(),
          st_error = numeric(),
          lower = numeric(),
          upper = numeric(),
          st_err_fut = numeric(),
          lower_fut = numeric(),
          upper_fut = numeric(),
          stringsAsFactors = FALSE
        )
    
        one_div_n <- 1 / n
        one_div_sqx <- 1 / sqx
        x_mean <- sum_x / n
        ta2 <- qt(1 - alpha / 2, nu)
        for (i in 1:n)
        {
          aux <- (x[i] - x_mean)^2
          aux2 <- one_div_n + aux * one_div_sqx
          ste_yi <- sqrt(qmres) * sqrt(aux2)
          aux2 <- aux2 + 1
          ste_yf <- sqrt(qmres) * sqrt(aux2)
          y_est <- b0 + b1 * x[i]
          lower <- y_est - ta2 * ste_yi
          upper <- y_est + ta2 * ste_yi
          l_fut <- y_est - ta2 * ste_yf
          u_fut <- y_est + ta2 * ste_yf
          cint_result <- list(y[i], y_est, ste_yi, lower, upper,
                               ste_yf, l_fut, u_fut)
          # Add to dataframe
          cint_pred[nrow(cint_pred) + 1, ] <- cint_result
        }
    
        # hipothesis Tests for beta0=0 and beta1=0
        tests <- data.frame(
          parameter = character(),
          estimate = numeric(),
          st_error = numeric(),
          t_statistic = numeric(),
          p_value = numeric(),
          stringsAsFactors = FALSE
        )
    
        # beta0
        steb0 <- sqrt(qmres) * sqrt(sum(x * x) / (n * sqx))
        tc <- b0 / steb0
        p.value <- 2 * (1 - pt(abs(tc), nu))
        test_result <- list("beta_0", b0, steb0, tc, p.value)
        # Add to dataframe
        tests[nrow(tests) + 1, ] <- test_result
        # beta1
        steb1 <- sqrt(qmres) / sqrt(sqx)
        tc <- b1 / steb1
        p.value <- 2 * (1 - pt(abs(tc), nu))
        test_result <- list("beta_1", b1, steb1, tc, p.value)
        # Add to dataframe
        tests[nrow(tests) + 1, ] <- test_result
    
        # confidence intervals for beta0 and beta1
        cint <- data.frame(
          parameter = character(),
          estimate = numeric(),
          st_error = numeric(),
          lower = numeric(),
          upper = numeric(),
          stringsAsFactors = FALSE
        )
    
        # beta0
        lower <- b0 - ta2 * steb0
        upper <- b0 + ta2 * steb0
        cint_result <- list("beta_0", b0, steb0, lower, upper)
        # Add to dataframe
        cint[nrow(cint) + 1, ] <- cint_result
        # beta1
        lower <- b1 - ta2 * steb1
        upper <- b1 + ta2 * steb1
        cint_result <- list("beta_1", b1, steb1, lower, upper)
        # Add to dataframe
        cint[nrow(cint) + 1, ] <- cint_result
    
        # confidence intervals for inverse
        # predicted values
        cint_inv_pred <- data.frame(
          y_obs    = numeric(n),
          x_obs    = numeric(n),
          x_pred   = numeric(n),
          st_error = numeric(n),
          lower    = numeric(n),
          upper    = numeric(n)
        )
    
        y_mean <- sum_y / n
        K <- b1^2 - ta2^2 * steb1^2
        p2 <- K * (1 + one_div_n)
    
        for (i in seq_len(n)) {
          x_pred <- (y[i] - b0) / b1
    
            if (K <= 0) {
              ste_xpred <- NA_real_
              lower     <- -Inf
              upper     <- Inf
            } else {
              aux       <- y[i] - y_mean
              aux2      <- p2 + aux^2 * one_div_sqx
              ste_xpred <- (sqrt(qmres) * sqrt(aux2)) / K
              center    <- x_mean + (b1 * aux) / K
              lower     <- center - ta2 * ste_xpred
              upper     <- center + ta2 * ste_xpred
            }
    
            cint_inv_pred[i, ] <- list(y[i], x[i], x_pred, ste_xpred, lower, upper)
        }
    
        # residuals plot
        res <- y - (b0 + b1 * x)
        if (any(res < 0)) hmin <- 0 else
          hmin <- mean(res)
        plot(x, res,
             xlab = "x",
             ylab = "Residues",
             #main = "Residuals vs x",
             pch = 19,        # point (full circle)
             col = "blue")    # points's color
        abline(h = hmin, lty = 2, col = "red") # horizontal line in y = hmin
    
    
    
        return(list(anova_reg=anova_reg, R2=R2, R2_adj=R2_adj,
                    tests=tests, cint=cint, cint_pred=cint_pred,
                    cint_inv_pred=cint_inv_pred))
    }
    
    
    
    # Proposed example from book exercise 14.5.1
    # Temperature (°C)
    x <- c(-18.1, -15.2, -9.9, -5.0, 0.1, 5.1, 10.2, 19.3)
    # Oxigen consumption (ml/g/h)
    y <- c(5.31, 4.75, 4.02, 3.59, 3.41, 3.11, 2.65, 1.76)
    
    alpha <- 0.05
    lr_fit(x, y, alpha)
    summary(lm(y~x))
    anova(lm(y~x))
    
    # Solved example from book (14.1)
    x <- c(   75, 100, 100, 120, 130, 130, 160, 190, 200, 240, 250)
    y <- c(0.00, 0.65, 0.50, 1.00, 0.95, 1.30, 1.80, 2.80, 2.50, 4.30, 4.50)
    
  • 14.2.2 For the model passing through the origin we have the main quantities necessary for the point estimation of the regression coefficients. The uncorrected sum of products is

    \begin{align*} \displaystyle \sum _{i=1}^{n}X_iY_i =& -148.859, \end{align*}

    \begin{align*} \sum _{i=1}^{n}X_i^2 =& 1184.21, \end{align*}

    \begin{align*} \displaystyle \sum _{i=1}^{n}Y_i^2 =& 111.2274, \end{align*}

    \begin{align*} \bar {X}_. =& -1.6875 & \textrm { and } && \bar {Y}_.=& 3.575. \end{align*}

    Therefore, the estimate of \(\beta \) is

    \begin{align*} \hat {\beta } =& \dfrac {\displaystyle \sum _{i=1}^{n}X_iY_i}{\displaystyle \sum _{i=1}^{n} X_i^2} =\dfrac {-148.859}{1184.21}=-0.1257032. \end{align*}

    The fitted regression model is

    \begin{align*} \hat {Y}_i=& -0.1257032 X_i. \end{align*}

    The increment of each unit of temperature in degrees Celsius causes an average reduction of \(0.1257032\) ml/g/h in oxygen consumption. We must, however, verify whether this value differs significantly from zero from a statistical point of view.

    The sums of squares corresponding to total and residual are

    \begin{align*} SQTotal =& SQY=8.9824, \textrm { (Corrected total),}\\ SQRes=& \displaystyle \sum _{i=1}^{n} Y_i^2 - \dfrac {\left (\displaystyle \sum _{i=1}^{n}X_i Y_i\right )^2}{\displaystyle \sum _{i=1}^{n} X_i^2}\\ =& 111.2274 - \dfrac {(-148.859)^2}{1184.21}=92.51535. \end{align*}

    The \(QMRes\) is obtained by dividing this value by the degrees of freedom \(n-1\) \(=\) \(7\). The result \(S^2\) \(=\) \(QMRes\) \(=\) \(13.21648\). The hypothesis of interest is given by:

    \begin{align*} H_0:&\,\,\beta =0 &\textrm { vs } && H_1:&\,\,\beta \ne 0. \end{align*}

    For the slope, the test statistic is given by

    \begin{align*} T_c =& \dfrac {\hat {\beta }-\beta ^*}{\sqrt {\dfrac {S^2}{\displaystyle \sum _{i=1}^{n} X_i^2}}} = \dfrac {-0.1257032}{\sqrt {\dfrac {13.21648}{1184.21}}} =\dfrac {-0.1257032}{0.1056437}\\ =& -1.189879. \end{align*}

    Since the calculated value \(|-1.189879|\) is less than the tabulated value of \(2.3646\), the null hypothesis of nullity of the slope is not rejected and it is concluded that oxygen consumption is not influenced by temperature \(X\). The associated \(p\)-value was \(0.2728806\), which is greater than the nominal significance level of \(5\%\), which would lead us to the same decision as before, when we compared the observed value with the tabulated value of the Student’s \(t\) distribution.

    The \(95\%\) interval is given by

    \begin{align*} CI_{0.95}\left (\beta \right ):&\,\,\hat {\beta } \pm t_{\alpha /2;\nu }\dfrac {S}{\sqrt {\displaystyle \sum _{i=1}^{n} X_i^2}} = -0.1257032\pm 2.3646\times 0.1056437\\ &\,\,=[-0.3755108,\,\, 0.1241044]. \end{align*}

    Both the hypothesis test and the \(95\%\) confidence interval lead to the same conclusion that the linear regression coefficient in the model passing through the origin does not differ statistically from zero.

    The estimated value of \(Y_i\) for a given value of \(X_i\) is given by

    \begin{align*} \hat {Y}_i=& \hat {\beta }_1 X_i\\ =& -0.1257032 X_i. \end{align*}

    The standard error for the current estimated values is given by:

    \begin{align*} S_{\hat {Y}_i}=\dfrac {S}{\sqrt {\displaystyle \sum _{i=1}^{n} X_i^2}}, \end{align*} whose values are presented in the following table.

    For the second case, the standard error is similar to the previous one, although the future prediction is more imprecise and the standard error has one unit added in the radicand of the previous expression. The estimator of the standard error for the measurement of a future observation \(X_0\) is given by

    \begin{align*} S_{\hat {Y}_0} =& \sqrt {S^2\left (1+\dfrac {X_0^2}{\displaystyle \sum _{i=1}^{n} X_i^2}\right )}, \end{align*} whose values are presented in the following table, considering each \(X_i\) as an \(X_{0i}\).

    The confidence interval for a predicted value of \(Y\) (\(Y_i\)), given the observed value \(X_i\), is based on the Student’s \(t\) distribution and given by

    \begin{align*} CI_{1-\alpha }(Y_i):&\,\, \hat {Y}_i \pm t_{\alpha /2;\nu }S_{\hat {Y}_i}= \hat {\beta }_0+ \hat {\beta }_1 X_i \pm t_{\alpha /2;\nu }S_{\hat {Y}_i}. \end{align*}

    The \(1-\alpha \) confidence interval for a future predicted value of \(Y\) (\(Y_0\)), given a future observed value \(X_0\), is also based on the Student’s \(t\) distribution and given by

    \begin{align*} CI_{1-\alpha }(Y_0):&\,\, \hat {Y}_0 \pm t_{\alpha /2;\nu }S_{\hat {Y}_0}= \hat {\beta }_0+ \hat {\beta }_1 X_0 \pm t_{\alpha /2;\nu }S_{\hat {Y}_0}. \end{align*}

    The predicted values with the confidence intervals for the current value and for the future value are presented in the following table. We can see from these results how inadequate the fit of the model passing through the origin was. We have, in this case, an \(R^2\) \(=\) \(0.1682324\), extremely low, the estimated coefficient \(\beta _1\) did not differ significantly from zero and its confidence interval included zero.

    .
    \(y_i\) \(\hat {y}_i\) \(S_{\hat {Y}_i}\) LI LS \(S_{\hat {Y}_0}\) LIF LSF
    5.3100 2.2752 1.9122 -2.2463 6.7967 4.1077 -7.4378 11.9883
    4.7500 1.9107 1.6058 -1.8864 5.7078 3.9743 -7.4870 11.3084
    4.0200 1.2445 1.0459 -1.2286 3.7176 3.7829 -7.7007 10.1896
    3.5900 0.6285 0.5282 -0.6205 1.8776 3.6736 -8.0582 9.3153
    3.4100 -0.0126 0.0106 -0.0376 0.0124 3.6355 -8.6091 8.5839
    3.1100 -0.6411 0.5388 -1.9151 0.6329 3.6752 -9.3314 8.0493
    2.6500 -1.2822 1.0776 -3.8302 1.2659 3.7918 -10.2483 7.6840
    1.7600 -2.4261 2.0389 -7.2474 2.3952 4.1682 -12.2822 7.4301

    The graph showing the fitted model and the intervals for the current and future values is presented in figure 14.1, visually confirming the inadequacy of the model chosen for the fit.

    (-tikz- diagram)

    Figure 14.2: Fitted simple linear regression model for the data of the exercise (solid line) and the respective \(95\%\) confidence intervals for the predicted mean values (dotted lines) and for the future values (dashed lines) corresponding to each value of \(x\).

    The inverse predictions when the regression coefficient is not significantly different from zero should not be obtained, since the results are inadequate and the confidence intervals are the real line itself. This indicates that any value of \(X\) would be suitable to have the response \(Y\) used in the inverse prediction. In this case, another factor that indicates this is the value of \(K<0\), which in this case resulted in \(-0.04660254\). The following program allows all these calculations to be instantly performed.

    # function to adjust the regression between x and y
    # compute the anova table and apply the F and t tests
    # compute the confidence intervals for b1, predicted
    # Y and X (actual and future)
    # y_i= b1 x_i + epsilon_i
    lr_orig_fit <- function(x, y, alpha=0.05)
    {
      if (length(x) != length(y))
        stop("Sample sizes must be equal!")
      n <- length(x)
      sum_x <- sum(x)
      sum_y <- sum(y)
      sum_x2 <- sum(x * x)
      sum_y2 <- sum(y * y)
      x_mean <- sum_x / n
      sqy <- sum_y2 - sum_y^2 / n
      sqres <- sum_y2 - sum(x * y)^2 / sum_x2
      nu <- n - 1
      qmres <- sqres / nu
      b <- sum(x * y) / sum_x2
    
        # R2 and R2 adjusted
        R2 <- 1 - sqres / sum_y2 # uncorrected ss total
    
        # confidence intervals for predicted values
        cint_pred <- data.frame(
          y_obs    = numeric(),
          y_pred   = numeric(),
          st_error = numeric(),
          lower = numeric(),
          upper = numeric(),
          st_err_fut = numeric(),
          lower_fut = numeric(),
          upper_fut = numeric(),
          stringsAsFactors = FALSE
        )
    
        ta2 <- qt(1 - alpha / 2, nu)
        for (i in 1:n)
        {
          aux <- x[i]^2
          aux2 <- aux / sum_x2
          ste_yi <- sqrt(qmres) * sqrt(aux2)
          aux2 <- aux2 + 1
          ste_yf <- sqrt(qmres) * sqrt(aux2)
          y_est <- b * x[i]
          lower <- y_est - ta2 * ste_yi
          upper <- y_est + ta2 * ste_yi
          l_fut <- y_est - ta2 * ste_yf
          u_fut <- y_est + ta2 * ste_yf
          cint_result <- list(y[i], y_est, ste_yi, lower, upper,
                               ste_yf, l_fut, u_fut)
          # Add to dataframe
          cint_pred[nrow(cint_pred) + 1, ] <- cint_result
        }
    
        # hipothesis Tests for beta0=0 and beta1=0
        tests <- data.frame(
          parameter = character(),
          estimate = numeric(),
          st_error = numeric(),
          t_statistic = numeric(),
          p_value = numeric(),
          stringsAsFactors = FALSE
        )
    
        # beta
        steb <- sqrt(qmres) / sqrt(sum_x2)
        tc <- b / steb
        p.value <- 2 * (1 - pt(abs(tc), nu))
        test_result <- list("beta", b, steb, tc, p.value)
        # Add to dataframe
        tests[nrow(tests) + 1, ] <- test_result
    
        # confidence intervals for beta
        cint <- data.frame(
          parameter = character(),
          estimate = numeric(),
          st_error = numeric(),
          lower = numeric(),
          upper = numeric(),
          stringsAsFactors = FALSE
        )
    
        # beta
        lower <- b - ta2 * steb
        upper <- b + ta2 * steb
        cint_result <- list("beta", b, steb, lower, upper)
        # Add to dataframe
        cint[nrow(cint) + 1, ] <- cint_result
    
        # confidence intervals for inverse
        # predicted values (through the origin)
        cint_inv_pred <- data.frame(
          y_obs    = numeric(n),
          x_obs    = numeric(n),
          x_pred   = numeric(n),
          st_error = numeric(n),
          lower    = numeric(n),
          upper    = numeric(n)
        )
    
        K <- b^2 - ta2^2 * steb^2
    
        for (i in seq_len(n)) {
          x_pred <- y[i] / b
    
            if (K <= 0) {
              ste_xpred <- NA_real_
              lower     <- -Inf
              upper     <- Inf
            } else {
              aux2      <- K + y[i]^2 / sum_x2
              ste_xpred <- (sqrt(qmres) * sqrt(aux2)) / K
              center    <- (b * y[i]) / K
              lower     <- center - ta2 * ste_xpred
              upper     <- center + ta2 * ste_xpred
            }
    
            cint_inv_pred[i, ] <- list(y[i], x[i], x_pred, ste_xpred, lower, upper)
        }
    
        # residuals plot
        res <- y - b * x
        if (any(res < 0)) hmin <- 0 else
          hmin <- mean(res)
        plot(x, res,
             xlab = "x",
             ylab = "Residuals",
             #main = "Residuals vs x",
             pch = 19,        # point (full circle)
             col = "blue")    # points's color
        abline(h = hmin, lty = 2, col = "red") # horizontal line in y = hmin
    
        return(list(R2=R2, tests=tests, cint=cint,
                    cint_pred=cint_pred,
                    cint_inv_pred=cint_inv_pred))
    }
    
    # Proposed example from book exercise 14.5.1
    # Temperature (°C)
    x <- c(-18.1, -15.2, -9.9, -5.0, 0.1, 5.1, 10.2, 19.3)
    # Oxygen consumption (ml/g/h)
    y <- c(5.31, 4.75, 4.02, 3.59, 3.41, 3.11, 2.65, 1.76)
    
    alpha <- 0.05
    lr_orig_fit(x, y, alpha)
    summary(lm(y~x))
    anova(lm(y~x))
    
    # Solved example from book (14.1)
    x <- c(   75, 100, 100, 120, 130, 130, 160, 190, 200, 240, 250)
    y <- c(0.00, 0.65, 0.50, 1.00, 0.95, 1.30, 1.80, 2.80, 2.50, 4.30, 4.50)
    
  • 14.2.3 The residuals and the residual plots were obtained for both models. Only the plots of the two models will be presented in the sequence.

    • (a) For the complete model with intercept we have the following plot:

      (image)

      It is verified that the pattern is quite random around the zero line. The maximum and minimum limits revolved around 0.3 and -0.3, respectively. This indicates, at first, a well-fitted model without problems of heterogeneity.

    • (b) Model passing through the origin

      (image)

      In this case, the residuals were above zero and with a mean around 3.4. The dispersion did not show a random pattern and as the values of \(X\) increase, there is a tendency for the residuals to increase. This indicates that there is no good fit of the model or the choice of the model was inadequate. These data show a strong specification bias due to the omission of the intercept (\(\beta _{0}\)), indicating that the line should not have been forced to pass through the origin \((0,0)\). The fitted line is systematically below all observed points. The residuals grow almost linearly with \(x\), violating the assumption of independence between the error and the explanatory variable.

  • 14.2.4 The model to be fitted is:

    \begin{align*} Y_i =& \beta X_i + \epsilon _i, \end{align*}

    The sum of squares of the errors over all \(n\) pairs (\(X_i\), \(Y_i\)) is given by

    \begin{align*} Q=&\sum _{i=1}^{n}\epsilon _i^2 = \sum _{i=1}^{n}\left (Y_i - \beta X_i \right )^2. \end{align*}

    To obtain the solution one must minimize the sum of squares of the errors. For this, the derivative of this function with respect to the parameter \(\beta \) is obtained by

    \begin{align*} \dfrac {d Q}{d \beta } =& -2 \sum _{i=1}^{n} \left (Y_i-\beta X_i\right )X_i. \end{align*}

    Setting these derivatives equal to zero and replacing \(\beta \) by the estimator \(\hat {\beta }\) we have, after some simplifications, the solution for the estimator of the parameter that minimizes the sum of squares of the errors given by

    \begin{align*} \displaystyle \sum _{i=1}^{n} X_i Y_i - \hat {\beta } \sum _{i=1}^{n} X_i^2 = 0. \end{align*}

    The least squares estimator is:

    \begin{align*} \hat {\beta }=& \dfrac {\displaystyle \sum _{i=1}^{n} X_i Y_i}{\displaystyle \sum _{i=1}^{n} X_i^2}, \end{align*}

    The function \(Q\) represents the sum of squares of errors. Therefore, assuming that the residual \(e_i\) is the predictor of the error \(\epsilon _i\), then the sum of its squares is called the residual sum of squares and is given by

    \begin{align*} \sum _{i=1}^{n} e_i^2 =& SQRes =\sum _{i=1}^{n} \left (Y_i-\hat {Y}_i\right )^2\\ =& \sum _{i=1}^{n} \left (Y_i-\hat {\beta } X_i\right )^2\\ =& \sum _{i=1}^{n} \left (Y_i^2 - 2 \hat {\beta } X_iY_i + \hat {\beta }^2X_i^2\right )\\ =& \sum _{i=1}^{n}Y_i^2 - 2 \dfrac {\displaystyle \sum _{i=1}^{n} X_i Y_i}{\displaystyle \sum _{i=1}^{n} X_i^2}\displaystyle \sum _{i=1}^{n} X_i Y_i + \left (\dfrac {\displaystyle \sum _{i=1}^{n} X_i Y_i}{\displaystyle \sum _{i=1}^{n} X_i^2}\right )^2\displaystyle \sum _{i=1}^{n} X_i^2\\ =& \sum _{i=1}^{n}Y_i^2 - 2 \dfrac {\left (\displaystyle \sum _{i=1}^{n} X_i Y_i\right )^2}{\displaystyle \sum _{i=1}^{n} X_i^2} + \dfrac {\left (\displaystyle \sum _{i=1}^{n} X_i Y_i\right )^2}{\displaystyle \sum _{i=1}^{n} X_i^2}\\ =& \sum _{i=1}^{n}Y_i^2 - \dfrac {\left (\displaystyle \sum _{i=1}^{n} X_i Y_i\right )^2}{\displaystyle \sum _{i=1}^{n} X_i^2}. \end{align*} Therefore, the sum of squares of the residuals, where the residuals are the predictors of the random errors, is

    \begin{align} \label {eb14:eq:148} SQRes =& \sum _{i=1}^{n}Y_i^2 - \dfrac {\left (\displaystyle \sum _{i=1}^{n} X_i Y_i\right )^2}{\displaystyle \sum _{i=1}^{n} X_i^2}. \end{align}

    The variance of the estimator of \(\beta \), using the independence and linearity of the variance, is:

    \begin{align*} V(\hat {\beta })=& V\left (\dfrac {\displaystyle \sum _{i=1}^{n} X_i Y_i}{\displaystyle \sum _{i=1}^{n} X_i^2}\right )=\dfrac {\displaystyle \sum _{i=1}^{n} X_i^2 V( Y_i)}{\displaystyle \left (\sum _{i=1}^{n} X_i^2\right )^2}\\ =& \dfrac {\sigma ^2 \displaystyle \sum _{i=1}^{n} X_i^2 }{\displaystyle \left (\sum _{i=1}^{n} X_i^2\right )^2}\\ =& \dfrac {\sigma ^2}{\displaystyle \sum _{i=1}^{n} X_i^2}. \end{align*}

    Therefore, the estimator of the standard error is the square root of the estimator of this variance, which is given by:

    \begin{align*} S_{\hat {\beta }} =& \sqrt {\dfrac {S^2}{\displaystyle \sum _{i=1}^{n} X_i^2}}, \end{align*} where \(S^2\) is the \(QMRes\) obtained by \(SQRes/(n-1)\).