These data are a subset of the dataset Marcinkevičs et al. (2023)
Some outliers and missing values were removed.
link <-url("https://gregorkb.github.io/data/hrbc.csv")hrbc <-read.csv(link)head(hrbc)
hem rbc sex age diag
1 14.8 5.27 female 12.68 appendicitis
2 15.7 5.26 male 14.10 no appendicitis
3 11.4 3.98 female 14.14 no appendicitis
4 13.6 4.64 female 16.37 no appendicitis
5 12.6 4.44 female 11.08 appendicitis
6 12.5 4.96 male 11.05 no appendicitis
Hemoglobin level vs red blood cell count for \(n = 762\) children.
For \((x_1,Y_1),\dots,(x_n,Y_n)\), the simple linear regression model is \[
Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i, \quad i = 1,\dots,n
\] where
\(x_1,\dots,x_n\) are the covariate or predictor values.
\(Y_1,\dots,Y_n\) are the response values.
\(\beta_0\) and \(\beta_1\) are the intercept and slope parameters, respectively.
\(\varepsilon_1,\dots,\varepsilon_n\) are independent \(\text{Normal}(0,\sigma^2)\)error terms.
\(\sigma^2\) is the error term variance.
Goals in simple linear regression
We will learn how to:
Estimate the intercept and slope parameters \(\beta_0\) and \(\beta_1\).
Estimate the error term variance \(\sigma^2\).
Perform inference on \(\beta_1\).
Build a confidence interval for \(\beta_0 + \beta_1 x_{\operatorname{new}}\) at any \(x_{\operatorname{new}}\).
Build a prediction interval for \(Y\) at any \(x_{\operatorname{new}}\).
Decompose the variation in \(Y\) into sums of squares.
Check whether the model assumptions are satisfied.
Identify outliers and understand their effects.
Least-squares estimation of slope and intercept
The squared error criterion given by the sum \[
Q(b_0,b_1) = \sum_{i=1}^n(Y_i - (b_0 + b_1 x_i))^2
\] of squared vertical distances of \(Y_i\) from the line \(y = b_0 + b_1x\).
We find \(Q(b_0,b_1)\) is minimized at \((b_0,b_1) = (\hat \beta_0,\hat\beta_1)\), where
The least-squares line or fitted line is the line \(y = \hat \beta_0 + \hat \beta_1x\).
Pearson’s correlation coefficient
Given \((x_1,Y_n),\dots,(x_n,Y_n)\), the quantity \[
r_{xY} = \frac{\sum_{i=1}^n (x_i - \bar x_n)(Y_i-\bar Y_n)}{\sqrt{\sum_{i=1}^n(x_i - \bar x_n)^2 \sum_{i=1}^n(Y_i - \bar Y_n)^2}}
\] is called Pearson’s correlation coefficient.
Describes strength and direction of linear relationships.
Must satisfy \(r_{xY} \in [-1,1]\).
Values close to zero indicate a weak linear relationship.
Is related to \(\hat \beta_1\) by \[
\hat \beta_1 = r_{xY} \frac{S_{Y}}{S_{x}}.
\] where \(S_Y\) and \(S_x\) are the sample std devs of the \(Y\) and \(x\) values.
Hemoglobin versus RBC count example (cont)
Find the least-squares line on the hemoglobin data.
We most often test hypotheses about \(\beta_1\) of the form \[
\begin{array}{lclcl}
H_0\text{: } \beta_1 \geq 0 &\text{ or } & H_0\text{: } \beta_1 = 0 &\text{ or } & H_0\text{: } \beta_1 \leq 0\\
H_1\text{: } \beta_1 < 0 && H_1\text{: } \beta_1 \neq 0 && H_1\text{: } \beta_1 > 0.
\end{array}
\] Reject or fail to reject \(H_0\) based on the value of the test statistic \[
T_{\operatorname{test}} = \frac{\hat \beta_1 }{\hat\sigma / \sqrt{S_{xx}}}.
\] Rejection rules for the above at significance level \(\alpha\) are \[
\begin{array}{lclcl}
T_{\operatorname{test}} < -t_{n-2,\alpha} &\text{ or } & |T_{\operatorname{test}}| > t_{n-2,\alpha/2}&\text{ or } & T_{\operatorname{test}} > t_{n-2,\alpha}.
\end{array}
\]
The corresponding p-values are, with \(T \sim t_{n-2}\), the probabilities \[
\begin{array}{lclcl}
P(T < T_{\operatorname{test}}) &\text{ or } & 2 \times P(T > |T_{\operatorname{test}}|) & \text{ or } & P(T > T_{\operatorname{test}}).
\end{array}
\]
Hemoglobin versus RBC count example (cont)
Test the hypotheses \(H_0\): \(\beta_1 = 0\) vs \(H_1\): \(\beta_1 \neq 0\) at \(\alpha = 0.05\).
alpha <-0.05Tstat <- b1hat /sqrt(sgsqhat/Sxx)crit <-qt(1-alpha/2, df = n -2)pval <-2*(1-pt(abs(Tstat), df = n -2))
We get \(T_{\operatorname{test}} = 28.021\), \(t_{n-2,\alpha/2} = 1.963\), and \(p\)-value 0; we reject \(H_0\).
Test the hypotheses \(H_0\): \(\beta_1 \leq 2\) vs \(H_1\): \(\beta_1 > 2\).
Tstat <- (b1hat -2) /sqrt(sgsqhat/Sxx)crit <-qt(1-alpha, df = n -2)pval <-1-pt(Tstat, df = n -2)
We get \(T_{\operatorname{test}} = 2.107\), \(t_{n-2,\alpha} = 1.647\), and \(p\)-value 0.018; we reject \(H_0\).
Can obtain CI for \(\beta_0 + \beta_1x_\text{new}\) and PI for \(Y_{\text{new}}\) with predict() function.
lm_out <-lm(Y~x)xnew <-5.5predict(lm_out, newdata =data.frame(x = xnew), int ="conf")
fit lwr upr
1 14.88892 14.76725 15.01059
predict(lm_out, newdata =data.frame(x = xnew), int ="pred")
fit lwr upr
1 14.88892 13.33117 16.44667
Sums of squares in simple linear regression
We decompose the variation in \(Y_1,\dots,Y_n\) by defining the:
Total sum of squares: \(\operatorname{SS}_{\operatorname{Tot}} = \sum_{i=1}^n(Y_i - \bar Y_n)^2\)
Regression sum of squares: \(\operatorname{SS}_{\operatorname{Reg}} = \sum_{i=1}^n(\hat Y_i - \bar Y_n)^2\)
Error sum of squares: \(\operatorname{SS}_{\operatorname{Error}} = \sum_{i=1}^n(Y_i - \hat Y_i)^2\)
We have \(\operatorname{SS}_{\operatorname{Tot}} = \operatorname{SS}_{\operatorname{Reg}} + \operatorname{SS}_{\operatorname{Error}}\).
The coefficient of determination is defined as \(\displaystyle R^2 = \frac{\operatorname{SS}_{\operatorname{Reg}}}{\operatorname{SS}_{\operatorname{Tot}}}\).
\(R^2 \in [0,1]\)
Proportion of variation in \(Y\) “explained” by the covariate \(x\).
In simple linear regression we have \(R^2 = r_{xY}^2\).
The mean squares in simple linear regression
The SS, appropriately scaled, follow chi-square distributions:
\(P(F > F_{\operatorname{test}})\), where this is computed under \(F \sim F_{1,n-2}\)
These are the test statistic and p-value of the overall F test.
In simple linear regression this p-value is the same as the one for testing \(H_0\): \(\beta_1 = 0\) versus \(H_1\): \(\beta_1 \neq 0\) with the t test; moreover \(F_{\operatorname{test}} = T_{\operatorname{test}}^2\).
For what it’s worth, one can show \(\displaystyle F_{\operatorname{test}} = \frac{(n-2)r_{xY}^2}{1-r_{xY}^2}\) in SLR.
We will discuss the overall F test in greater detail later.
Building the ANOVA table
SST <-sum((Y - Ybar)^2)SSR <-sum((Yhat - Ybar)^2)SSE <-sum((Y - Yhat)^2)MSR <- SSR /1MSE <- SSE / (n-2)Fstat <- MSR / MSE # same as (n-2)*rxY^2/(1 - rxY^2)pval <-1-pf(Fstat,1,n-2)
Source
Df
SS
MS
F value
p-value
x
1
491.37
491.37
785.15
0
Error
760
475.63
0.63
Total
761
967
The lm(), summary(), and anova() functions in R
lm_out <-lm(Y~x)lm_out
Call:
lm(formula = Y ~ x)
Coefficients:
(Intercept) x
2.994 2.163
summary(lm_out)
Call:
lm(formula = Y ~ x)
Residuals:
Min 1Q Median 3Q Max
-5.9702 -0.4232 0.0074 0.4645 2.3791
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.99447 0.37065 8.079 2.56e-15 ***
x 2.16263 0.07718 28.021 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.7911 on 760 degrees of freedom
Multiple R-squared: 0.5081, Adjusted R-squared: 0.5075
F-statistic: 785.1 on 1 and 760 DF, p-value: < 2.2e-16
anova(lm_out)
Analysis of Variance Table
Response: Y
Df Sum Sq Mean Sq F value Pr(>F)
x 1 491.37 491.37 785.15 < 2.2e-16 ***
Residuals 760 475.63 0.63
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Invariance of tests to shifting/scaling of data values
Our test statistics and p-values do not depend on the units in which the data are recorded:
Hemoglobin measured in grams per deciliter. What if we convert to grams per liter?
RBC measured in millions of cells per microliter. What if we convert to thousands of cells per microliter?
And what if we center the RBC values around their mean?
Call:
lm(formula = hem2 ~ rbc2)
Residuals:
Min 1Q Median 3Q Max
-59.702 -4.232 0.074 4.645 23.791
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.335e+02 2.866e-01 465.80 <2e-16 ***
rbc2 2.163e+04 7.718e+02 28.02 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 7.911 on 760 degrees of freedom
Multiple R-squared: 0.5081, Adjusted R-squared: 0.5075
F-statistic: 785.1 on 1 and 760 DF, p-value: < 2.2e-16
Checking model assumptions
Validity of the foregoing analyses depends on these assumptions:
The responses are normally distributed around the regression line (Check QQ plot of residuals). If \(n\) is large this only matters when making prediction intervals.
The response has the same variance for all values of the covariate (Check residuals vs fitted values plot).
The covariate and the response are linearly related (Check residuals vs fitted values plot).
The response values are independent of each other (No way to check; must trust experimental design).
Generating diagnostic plots from lm() with plot()
plot(lm_out,which =2)
plot(lm_out,which =1)
The abalone
Photo on the left by Sharktopus - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=14082271
Abalone data example
Predict shucked weight of an abalone by its length.
Transforming variables to obtain a linear relationship
Take care how to interpret \(\beta_1\) after transforming the data.
Example: Log transforming \(x\) and \(Y\) gives \(\beta_1\) a %-change interpretation: \[
\log y = \beta_0 + \beta_1 \log x \iff \frac{d \log y}{dx} = \beta_1 \frac{1}{x} \iff \frac{dy}{y} = \beta_1 \frac{dx}{x}
\]
Abalone data example (cont)
We must back-transform prediction intervals if we have transformed \(Y\).
xnew <-0.5newdata <-data.frame( logx =log(xnew))pi_logY <-predict(lm3,newdata = newdata, int ="pred")pi_logY
fit lwr upr
1 -1.335636 -1.724831 -0.946441
pi <-exp(pi_logY)pi
fit lwr upr
1 0.2629909 0.1782032 0.3881199
plot(Y~x,col="gray"); logx <-seq(min(logx),max(logx),length=500)newdata <-data.frame(logx = logx)logy_hat <-predict(lm3,newdata = newdata,int ="pred")lines(exp(logy_hat[,1]) ~exp(logx), col ="red")lines(exp(logy_hat[,2]) ~exp(logx), col ="red", lty =3,lwd=1.5)lines(exp(logy_hat[,3]) ~exp(logx), col ="red", lty =3,lwd=1.5)
Outliers in simple linear regression
Outlying data points can have a large influence on the estimated regression function.
Let’s generate some data and then add an outlier:
n <-20b0 <-1b1 <--1/2sg <- .2x0 <-runif(n,0,5)e <-rnorm(n,0,sg)Y0 <- b0 + b1 * x0 + ex <-c(x0,.3)Y <-c(Y0,-1.3)
plot(Y~x);points(Y[n+1]~x[n+1], col ="red")abline(lm(Y0~x0))abline(lm(Y~x), col ="red")
The red data point appears to exert an undue influence over the fit.
Leverage
The leverage of a point \((x_i,Y_i)\) among \((x_1,Y_1),\dots,(x_n,Y_n)\) is \[
\text{lev}_i = \frac{1}{n} + \frac{(x_i - \bar x_n)^2}{S_{xx}}
\]
Leverage only shows outlying-ness in the \(x\) direction.
Least-squares line must pass through the point \((\bar x_n,\bar Y_n)\).
Greater leverage means greater influence on the least-squares line.
Cook’s distance
Cook’s Distance measures how much each data point changes the fit: \[
D_i = \frac{1}{2 \hat \sigma^2}\sum_{j = 1 }^n(\hat Y_j - \hat Y_{j(i)})^2 \quad \text{ for } i = 1,\dots,n,
\] where \(\hat Y_{j(i)}\) is the \(j\)th fitted value from the model fitted without obs \(i\).
Can also write \(\displaystyle D_i = \frac{\hat e_i^2}{2\hat \sigma^2}\frac{\text{lev}_i}{(1 - \text{lev}_i)^2}\) for \(i = 1,\dots,n\).
Let \(Y_{ij} \overset{\operatorname{ind}}{\sim}\text{Normal}(\mu_i,\sigma^2)\), \(j = 1,\dots,n_i\), \(i=1,2\) and consider \[
\text{$H_0$: $\mu_2 - \mu_1 = 0$ versus $H_1$: $\mu_2 - \mu_1 \neq 0$.}
\] The (equal-variances) two-sample t-test uses the test statistic \[
T_{\operatorname{test}}= \frac{\bar Y_2 - \bar Y_1}{S_{\operatorname{pooled}}\sqrt{\frac{1}{n_1} + \frac{1}{n_2}}},
\]
where \(\bar y_i =n_i^{-1}\sum_{j=1}^{n_i}Y_{ij}\), \(i=1,2\) and \[
S_{\operatorname{pooled}}^2 = \frac{(n_1 - 1)S_1^2 + (n_2 - 1)S_2^2}{n_1 + n_2 - 2}, \quad S_i = \frac{1}{n_i - 1}\sum_{j=1}^{n_i}(Y_{ij} - \bar Y_i)^2.
\] We reject \(H_0\) at significance level \(\alpha\) if \(|T_{\operatorname{test}}| > t_{n-2,\alpha/2}\).
Appendicitis example
Look again at the data from Marcinkevičs et al. (2023).
link <-url("https://people.stat.sc.edu/gregorkb/data/hrbc.csv")data <-read.csv(link)head(data)
hem rbc sex age diag
1 14.8 5.27 female 12.68 appendicitis
2 15.7 5.26 male 14.10 no appendicitis
3 11.4 3.98 female 14.14 no appendicitis
4 13.6 4.64 female 16.37 no appendicitis
5 12.6 4.44 female 11.08 appendicitis
6 12.5 4.96 male 11.05 no appendicitis
Is the mean hemaglobin level the same in children with and without appendicitis (ignoring rbc, age, and sex)?
Appendicitis example (cont)
boxplot(data$hem ~ data$diag)
Appendicitis example (cont)
t.test(data$hem ~ data$diag, var.equal =TRUE)
Two Sample t-test
data: data$hem by data$diag
t = -0.49212, df = 760, p-value = 0.6228
alternative hypothesis: true difference in means between group appendicitis and group no appendicitis is not equal to 0
95 percent confidence interval:
-0.2038964 0.1221585
sample estimates:
mean in group appendicitis mean in group no appendicitis
13.33229 13.37316
Appendicitis example (cont)
Let the \(Y_i\) be the hemaglobin values and define an indicator variable as \[
x_i = \left\{\begin{array}{ll}
0& \text{if no appendicitis}\\
1& \text{if appendicitis}
\end{array}\right.\quad \text{ for } i = 1,\dots,n.
\] Then in the SLR model \(Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i\) we have
The t test in the simple linear regression setup of \[
\text{$H_0$: $\beta_1 = 0$ versus $H_1$: $\beta_1 \neq 0$}
\] will give the same p value as the equal-variances two-sample t test of \[
\text{$H_0$: $\mu_{\operatorname{app}} - \mu_{\operatorname{no~app}} = 0$ versus $H_1$: $\mu_{\operatorname{app}} - \mu_{\operatorname{no~app}} \neq 0$.} \quad \text{ Cool!}
\]
Exercise: Show that in the above setup we have
\[
\frac{\bar Y_2 - \bar Y_1}{S_{\operatorname{pooled}}\sqrt{\frac{1}{n_1} + \frac{1}{n_2}}} = \frac{\hat \beta_1}{\hat \sigma /\sqrt{S_{xx}}}.
\] Do it in steps, showing:
Check the Normal quantile-quantile plot of the residuals.
plot(lm_out,which =2)
Generate a large number of such data sets and obtain \(\hat \beta_1\) for each one.
S <-300b1hat <-numeric(S)for(s in1:S){ x <-rnorm(n) e <-rgamma(n,shape =3/2, scale =2/3) -1 Y <- b0 + b1*x + e lm_out <-lm(Y~x) b1hat[s] <-coef(lm_out)[2]}
Check if the \(\hat \beta_1\) values have a Normal distribution.
qqnorm(scale(b1hat))abline(0,1)
References
Marcinkevičs, Ričards, Patricia Reis Wolfertstetter, Ugne Klimiene, Ece Ozkan, Kieran Chin-Cheong, Alyssia Paschke, Julia Zerres, et al. 2023. “Regensburg Pediatric Appendicitis Dataset.” Zenodo. https://doi.org/10.5281/zenodo.7711412.