Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Single covariate linear regression and conditional expected value and variance

Introduction

Regression is likely one of the most important concepts in modern statistical data analysis. Thousands if not tens of thousands scientific analyses depend on regression to understand the relationship between one or more variables and an outcome of interest and to make predictions about future outcomes given a new, not yet seen observation in nature. The regression approach to model building begins with a univariate distribution---often a familiar distribution---and then allows the parameters of that distribution to depend on observed data.

A new data, sampling setup

Previous to this chapter our typical statistical setup assumed that our data was a set of the form D={x1,x2,x3,⋯ ,xn}\mathcal{D} = \{ x_{1},x_{2},x_{3}, \cdots, x_{n} \} where we collected a single piece of information from each observation. Our statistical model then assumed that these observations were generated from corresponding random variables X1,X2,⋯ ,XnX_{1}, X_{2}, \cdots, X_{n} that were independent and identically distributed.

Our new setup will consider collecting one additional piece of information from each observation

D={(x1,y1),(x2,y2),(x3,y3),⋯(xn,yn)}\mathcal{D} = \{ (x_{1},y_{1}), (x_{2},y_{2}), (x_{3},y_{3}), \cdots (x_{n},y_{n}) \}

Our statistical setup will assume that the yy data in the second position of each tuple above is the outcome of interest and that this data was sampled from a set of corresponding random variables. We will not consider the xx data as being generated from corresponding random variables. The xx data is thought of as fixed. Our statistical setup is

(x1,Y1),(x2,Y2),(x3,Y3),⋯ ,(xn,Yn)(x_{1}, Y_{1}),(x_{2}, Y_{2}),(x_{3}, Y_{3}), \cdots, (x_{n}, Y_{n})

where we assume that YiY_{i} are independent and identically distributed. We will assign to the random variables YY a probabilistic distribution.

We are asked to collect data on patients who are admitted to the hospital to test the hypothesis that those who are hospitalized with influenza are more likely to be under the age of 3 or over the age of 65. A single observation will then contain two pieces of information: (i) whether the patient admitted to the hospital has confirmed influenza and (ii) whether the patient is younger than 3 or older than 65 or in between these ages. Our dataset will look like D={(r1,i1),(r2,i2),(r3,i3),⋯ ,(rn,in)}\mathcal{D} = \{(r_{1},i_{1}),(r_{2},i_{2}),(r_{3},i_{3}),\cdots,(r_{n},i_{n}) \} where the variable rr is the value one when a patient is younger than 3 or older than 65 and the value zero otherwise, and the variable ii is the value one when the patient was infected with influenza and the value zero otherwise.

Conditional distributions

We must make concise what we mean when we suppose that values of xx impact our outcome of interest YY. Let us define the distribution of the random YY, conditional on the observation xx, or Y∣xY | x, as a probability mass function or density function that produces different probabilities over the support of YY for different values of the observation xx.

Assume that YY has a geometric distribution with the following modification

P(Y=y∣x)=g(x)(1−g(x))y−1\begin{aligned} P(Y=y | x) = g(x) ( 1-g(x) )^{y-1} \end{aligned}

where the function g(x)g(x) must be between the values 0 and 1 (Why?). We could write the above as Y∣x∼Geom(g(x))Y | x \sim Geom( g(x) ) and say that the distribution of YY is conditional on the value of xx. When g(x)=0.2g(x) = 0.2 then P(Y=5)=0.08P(Y=5) = 0.08 and when g(x)=0.9g(x) = 0.9 then P(Y=5)=0.00009P(Y=5) = 0.00009.

Conditional expectation and variance

For a discrete random variable, the conditional expectation of a random variable Y∣xY|x is defined as

E(Y∣x)=∑y∈supp(Y)yp(y∣x)\begin{aligned} \mathbb{E}(Y|x) = \sum_{ y \in supp(Y) } y p(y |x) \end{aligned}

and for a continuous random variable is defined as

E(Y∣x)=∫y∈supp(Y)yf(y∣x)  dy\begin{aligned} \mathbb{E}(Y|x) = \int_{y \in supp(Y)} y f(y | x) \; dy \end{aligned}

where ff is the conditional probability density corresponding to Y∣xY|x.

Because the distribution of YY depends on xx then the expected value too will depend on the observed xx value. However, we assume that the observation xx is not random, the xx value is a fixed observation. This assumption makes computing the conditional expectation easier.

Suppose that supp(Y)={−1,0,1}supp(Y) = \{-1,0,1\} and that xx can take the values 0 or 1. Further assume the conditional probability distribution

      x=0    x=1

y=-1 0.10 0.40 y=0 0.50 0.30 y=1 0.40 0.30

: Conditional distribution of Y∣xY|x

Then

E(Y∣x=0)=∑y∈{−1,0,1}yp(y∣x=0)=(−1)0.1+(0)0.50+(1)0.40=−0.1+0.40=0.30\begin{aligned} \mathbb{E}(Y|x=0) &= \sum_{y \in \{-1,0,1\}} y p(y|x=0) \\ &= (-1) 0.1 + (0) 0.50 + (1) 0.40 \\ &= -0.1 + 0.40 = 0.30 \end{aligned}

and

E(Y∣x=1)=∑y∈{−1,0,1}yp(y∣x=1)=(−1)0.4+(0)0.30+(1)0.30=−0.4+0.30=−0.10\begin{aligned} \mathbb{E}(Y|x=1) &= \sum_{y \in \{-1,0,1\}} y p(y|x=1) \\ &= (-1) 0.4 + (0) 0.30 + (1) 0.30 \\ &= -0.4 + 0.30 = -0.10 \end{aligned}

For a discrete random variable, the conditional variance of a random variable Y∣xY|x is defined as

V(Y∣x)=∑y∈supp(Y)[y−E(Y∣x)]2p(y∣x)\begin{aligned} \mathbb{V}(Y|x) = \sum_{ y \in supp(Y) } \left[y - \mathbb{E}(Y | x)\right]^{2} p(y |x) \end{aligned}

and for a continuous random variable is defined as

V(Y∣x)=∫y∈supp(Y)[y−E(Y∣x)]2f(y∣x)  dy\begin{aligned} \mathbb{V}(Y|x) = \int_{y \in supp(Y)} \left[y - \mathbb{E}(Y | x)\right]^{2} f(y | x) \; dy \end{aligned}

where ff is the conditional probability density corresponding to Y∣xY|x.

Lets continue with the above example and compute V(Y∣x)V(Y|x).

V(Y∣x=0)=∑y∈{−1,0,1}[y−E(Y∣x=0)]2p(y∣x=0)=(−1−0.30)20.1+(0−0.30)20.50+(1−0.30)20.40=0.41\begin{aligned} \mathbb{V}(Y|x=0) &= \sum_{y \in \{-1,0,1\}} [ y - \mathbb{E}(Y|x=0) ]^{2} p(y|x=0) \\ &= (-1 - 0.30)^{2} 0.1 + (0 - 0.30)^{2} 0.50 + (1-0.30)^{2} 0.40 \\ &= 0.41 \end{aligned}

Parameters as a function of observations

Single covariate linear regression assumes that given a sample (Y1,x1),(Y2,x2),(Y3,x3),⋯ ,(Yn,xn)(Y_{1}, x_{1}), (Y_{2}, x_{2}), (Y_{3}, x_{3}), \cdots, (Y_{n}, x_{n}) that the conditional distribution of YY given xx follows a normal distribution

Y∣x∼N(μ(x),σ2)\begin{aligned} Y | x \sim \mathcal{N}\left( \mu(x), \sigma^{2} \right) \end{aligned}

where μ(x)=β0+β1x\mu(x) = \beta_{0} + \beta_{1} x, and β0\beta_{0} and β1\beta_{1} are parameters that take values in R\mathcal{R}. The above is sometimes called the probability form of linear regression. In probability form we emphasize that our target of interest is modeled as a random variable YY and make explicit that this random variable is expected to be distributed normal.

Because β0\beta_{0} and β1\beta_{1} are constant, and because xx is a fixed, constant observation then μ(x)=β0+β1x\mu(x) = \beta_{0} + \beta_{1}x is a constant. Remember that it is easy to translate normal distributions by a constant. Lets choose to add and subtract the constant (μ(x))(\mu(x))

The conditional expectation for single covariate regression can be computed by recognizing that μ(x)\mu(x) is a constant,

E(Y∣x)=μ(x)=β0+β1x,\begin{aligned} \mathbb{E}(Y|x) = \mu(x) = \beta_{0} + \beta_{1}x, \end{aligned}

and the conditional variance for single covariate regression can also be computed as

V(Y∣x)=σ2\begin{aligned} \mathbb{V}(Y|x) = \sigma^{2} \end{aligned}

We see that the variance does not depend on xx and remains constant.

Y∣x∼N(μ(x),σ2)Y∣x=Y∣x+μ(x)−μ(x)\begin{aligned} Y |x \sim \mathcal{N}(\mu(x), \sigma^{2}) \\ Y |x = Y |x + \mu(x) - \mu(x) \end{aligned}

Lets define a new random variable ϵ=Y∣x−μ(x)\epsilon = Y |x - \mu(x). Then

Y∣x=μ(x)+ϵ\begin{aligned} Y |x = \mu(x) + \epsilon\\ \end{aligned}

We can look closer at the distribution for ϵ\epsilon The random variable ϵ\epsilon is a translation of Y∣xY|x.

ϵ∼N(μ(x),σ2)−μ(x)ϵ∼N(μ(x)−μ(x),σ2)ϵ∼N(0,σ2)\begin{aligned} \epsilon \sim \mathcal{N}(\mu(x), \sigma^{2}) - \mu(x)\\ \epsilon \sim \mathcal{N}(\mu(x)-\mu(x), \sigma^{2}) \\ \epsilon \sim \mathcal{N}(0, \sigma^{2}) \end{aligned}

Finally we see that we can rewrite our linear regression as

Y∣x=μ(x)+ϵϵ∼N(0,σ2)\begin{aligned} Y |x = \mu(x) + \epsilon\\ \epsilon \sim \mathcal{N}(0, \sigma^{2}) \end{aligned}

This form for linear regression---the conditional expected value plus an error term (ϵ\epsilon)---is often called the model form for linear regression. Model form emphasizes, μ(x)\mu(x), often thought of as how you think xx and YY are associated with one another.

Linear regression supposes that there exists a linear relationship between the random variable YY and the observations xx. One way to investigate whether or not a linear relationship between YY and xx appears reasonable is to draw a scatter plot.

Given a dataset D={(x1,y1),(x2,y2),⋯(xn,yn)}\mathcal{D} = \{ (x_{1}, y_{1}),(x_{2}, y_{2}), \cdots (x_{n}, y_{n}) \} A scatter plot first draws a x and y axis on the cartesian coordinate plane. The second step iterates through each data point (xi,yi)(x_{i},y_{i}) and places a dot on the plane at (xi,yi)(x_{i},y_{i}). If it appears that a straight line may well explain the association between xx and E(Y∣x)\mathbb{E}(Y|x) then a linear regression model could be fit to this data.

The residual, diagnostics, and LINE assumptions

Single covariate linear regression is a model to help us understand the relationship between two covariates that we call, yy and xx. Rarely, if ever, will our model perfectly describe our observed data. In this case, we will need to evaluate how well our model fits our observed data and if single covariate regression is a reasonable, or un-reasonable, model. Evaluating how well our statistical model describes an observed dataset is called diagnostics.

Assume that we can find optimal estimates for our parameters β0,β1,\beta_{0}, \beta_{1}, and σ2\sigma^{2}.

Residuals, fitted values, and Normality

The random variable ϵ\epsilon that we defined for the model form of single covariate linear regression is exact, assuming that we know all of our parameter values. However, in most cases we will not be given the exact parameter values. Instead we are handed a dataset and asked to estimate our parameters. In this case, we will never know the true distribution of our error term ϵi\epsilon_{i} that corresponds to a specific xix_{i} value. We can estimate the error term ϵ\epsilon at the value xx given: (i) estimates for our parameters and (ii) a datapoint (x,y)(x, y) from our dataset. Given a datapoint d=(x,y)d = (x,y), a residual is the difference between the truly observed yy and an estimate of yy, y^\hat{y}, or ϵ^=y−y^\hat{\epsilon} = y - \hat{y}.

Given estimates for our parameters (β0,β1,σ2)(\beta_{0}, \beta_{1}, \sigma^{2}), a fitted value to a datapoint d=(x,y)d = (x,y) is y^=β0^+β1^x\hat{y} = \hat{\beta_{0}} + \hat{\beta_{1}} x.

Residuals and fitted values can be used to test the assumptions of our regression model. If our regression captures the variability in our dataset well then the residuals ϵ^i\hat{\epsilon}_{i} should behave similarly to the true error terms ϵi∼N(0,σ2)\epsilon_{i} \sim \mathcal{N}(0,\sigma^{2}).

Linearity

One assumption about single covariate linear regression to evaluate is that the relationship between xx and the conditional expected value of YY, E(Y∣x)\mathbb{E}(Y|x) is linear. Single covariate regression assumes that E(Y∣x)=β0+β1x\mathbb{E}(Y|x) = \beta_{0} + \beta_{1} x and that E(ϵ)=0\mathbb{E}(\epsilon) = 0 for any choice of xx.

To test for linearity we can plot for each xx value the corresponding residual ϵ^=y−(β0+β1x)\hat{\epsilon} = y - (\beta_{0} + \beta_{1} x). If single covariate regression sufficiently explains the relationship between our random variables YY and fixed data xx then we could expect that this plot has residuals values centered around zero for all choice of xx. If single covariate regression does not fit the data well then our residuals will not be centered around zero.

Suppose we collect the following dataset

x      y

0.44 1.23 0.50 1.19 0.83 1.26 0.41 1.26 0.03 0.87 0.20 0.87 0.98 1.45 0.37 1.28 0.80 1.31 0.99 1.69

: Data simulated from the model y∼N(1+0.5x,0.15)y \sim \mathcal{N}(1+0.5x, 0.15)

and we compute estimates for our parameters β0^=0.88\hat{\beta_{0}} = 0.88, β1^=0.64\hat{\beta_{1}} = 0.64, and σ2^=0.12\hat{\sigma^{2}} = 0.12.

We can compute 10 fitted values and 10 residuals corresponding to our 10 collected data points

x      y     $\hat{y}$   $\hat{\epsilon}$

0.44 1.23 1.16 0.07 0.50 1.19 1.20 -0.01 0.83 1.26 1.41 -0.15 0.41 1.26 1.14 0.12 0.03 0.87 0.90 -0.03 0.20 0.87 1.01 -0.14 0.98 1.45 1.51 -0.06 0.37 1.28 1.12 0.16 0.80 1.31 1.39 -0.08 0.99 1.69 1.51 0.18

: Data simulated from the model y∼N(1+0.5x,0.15)y \sim \mathcal{N}(1+0.5x, 0.15)

By plotting our fitted values y^\hat{y}, which depend on xx, against our residuals we can visualize whether or not our residuals ϵ^\hat{\epsilon} are centered around zero. If the residuals show a pattern that does not appear to be centered around zero than the relationship between xx and YY may not be linear.

(Left) Scatter plot of (x,y) pairs from the above dataset. (Right) Scatterplot of residuals versus fitted values. []{#fig.residualsDiag label="fig.residualsDiag"}{#fig.residualsDiag width=“\textwidth”}

We see from the plot of xx vs yy that the relationship between xx and yy looks linear. In addition, the scatter plot of the fitted values versus residuals also looks like the residuals show not particular pattern and are centered around zero for all fitted values.

Equal variance

A plot of fitted values versus residuals can also be used to assess whether the assumption that the error (ϵ)(\epsilon) is distributed N(0,σ2)\mathcal{N}(0,\sigma^{2}) according to a normal distribution with the same variance for all values of xx. This assumption is often called the equal variances assumption.

Below we generated 100 (x,y)(x,y) pairs of observed data points. We estimated our parameter values (more on this later) and then computed fitted values and residuals. Again we plotted a scatterplot of xx vs yy and a plot of fitted values vs residuals. We see from the residuals vs fitted values that it appears that the residuals move further away from zero as xx values increase. This indicates that the equal variances assumption may not best describe our data.

(Left) Scatter plot of 100 (x,y) pairs from the above dataset with the line (red) \hat{\mathbb{E}(Y|x)} = \hat{\beta_{0}} + \hat{\beta_{1}}x. (Right) Scatterplot of residuals versus fitted values. []{#fig.residualsDiag label="fig.residualsDiag"}{#fig.residualsDiag width=“\textwidth”}

Independence

The independence between data points di=(xi,yi)d_{i} = (x_{i}, y_{i}) and dj=(xj,yj)d_{j} = (x_{j}, y_{j}) for any (i,j)(i,j) can be tested with a sequence vs residual plot. For this plot we graph residuals versus a variable that indicates when in time the data was collected. For example, we may decide to plot the time the data point was collected vs residual, or we can rank the data by the time it was collected and plot the rank vs residual. For single covariate linear regression, we expect that there does not exist a relationship between when the data was collected and the residuals. We assume that errors ϵ\epsilon are independent from one another. If there is a relationship between when the data was collected and residuals then this may indicate the data points are not independent.

(Left) Scatter plot of 100 (order,\hat{\epsilon}) pairs from a dataset where data was generated independently. (Right) Scatterplot of the order that the data was collected versus residuals for data that was generated dependently. []{#fig.residualsDiag label="fig.residualsDiag"}{#fig.residualsDiag width=“\textwidth”}

Sums of squares

Up until this point we assumed that we can compute estimates for β0\beta_{0} and for β1\beta_{1}, but we have not yet discussed methods to compute estimates for these parameters. The first method we will explore is called minimizing the sums of squares or is often also called least squares.

Given a dataset D={(x1,y1),(x2,y2),⋯(xn,yn)}\mathcal{D} = \{ (x_{1},y_{1}), (x_{2},y_{2}), \cdots (x_{n},y_{n}) \} and a single covariate regression model Yi=μ(x)+ϵY_{i} = \mu(x) + \epsilon where ϵ∼N(0,σ2)\epsilon \sim \mathcal{N}(0,\sigma^{2}), the least squares estimates for β0\beta_{0} and β1\beta_{1} are the values that minimize

SS(β0,β1)=∑i=1N[yi−(β0+β1xi)]2\begin{aligned} SS(\beta_{0}, \beta_{1}) = \sum_{i=1}^{N} \left[ y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right]^{2} \end{aligned}

The function SS(β0,β1)SS(\beta_{0}, \beta_{1}) is called the "sums of squares" function. To minimize SS(β0,β1)SS(\beta_{0}, \beta_{1}) we will

  1. Treat β1\beta_{1} as a constant, treat β0\beta_{0} as a variable, take the derivative and set that equation to zero.

  2. Treat β0\beta_{0} as a constant, treat β1\beta_{1} as a variable, take the derivative and set that equation to zero.

  3. Solve the above two equations for β0\beta_{0} and β1\beta_{1}.

β0\beta_{0} as the variable and β1\beta_{1} as a constant

Because the SS function is a sum of similar terms, we can compute the derivative for one term and then add each term together.

SS(β0)′={∑i=1N[yi−(β0+β1xi)]2}′=∑i=1N{[yi−(β0+β1xi)]2}\begin{aligned} SS(\beta_{0})' &= \left\{ \sum_{i=1}^{N} \left[ y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right]^{2} \right \}' \\ &= \sum_{i=1}^{N} \left\{ \left[ y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right]^{2}\right \} \end{aligned}

The goal then is to compute the derivative of

SS(β0)=[yi−(β0+β1xi)]2\begin{aligned} SS(\beta_{0}) = \left[ y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right]^{2} \end{aligned}

We will need to use the chain rule

SS(β0)=[h(β0)]2h(β0)=yi−(β0+β1xi)\begin{aligned} SS(\beta_{0}) &= \left[ h(\beta_{0}) \right]^{2} \\ h(\beta_{0}) &= y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \\ \end{aligned}

The derivative of

SS(β0)=[h(β0)]2\begin{aligned} SS(\beta_{0}) &= \left[ h(\beta_{0}) \right]^{2} \\ \end{aligned}

is

SS(β0)=2[h(β0)]\begin{aligned} SS(\beta_{0}) &= 2\left[ h(\beta_{0}) \right] \\ \end{aligned}

and the derivative of

h(β0)=yi−(β0+β1xi)\begin{aligned} h(\beta_{0}) &= y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \\ \end{aligned}

is

h(β0)′=−1,\begin{aligned} h(\beta_{0})' &= - 1, \end{aligned}

and so the derivative of SS(β0)SS(\beta_{0}) is

SS(β0)′=∑i=1N2[h(β0)]×(−1)=∑i=1N−2[yi−(β0+β1xi)]\begin{aligned} SS(\beta_{0})' &= \sum_{i=1}^{N} 2\left[ h(\beta_{0}) \right] \times (-1) \\ &= \sum_{i=1}^{N} -2\left[y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right] \end{aligned}

We can set this equation equal to zero and solve for β0\beta_{0} to find the least squares estimate for β0\beta_{0}

∑i=1N−2[yi−(β0+β1xi)]=0∑i=1N[yi−(β0+β1xi)]=0∑i=1Nyi−∑i=1Nβ0−∑i=1Nβ1xi=0∑i=1Nyi−Nβ0−∑i=1Nβ1xi=0∑i=1Nyi−∑i=1Nβ1xi=Nβ0∑i=1Nyi−β1∑i=1Nxi=Nβ0∑i=1NyiN−β1∑i=1NxiN=β0yˉ−β1xˉ=β0\begin{aligned} \sum_{i=1}^{N} -2\left[y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right] &=0 \\ \sum_{i=1}^{N} \left[y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right] &=0 \\ \sum_{i=1}^{N} y_{i} - \sum_{i=1}^{N} \beta_{0} -\sum_{i=1}^{N} \beta_{1}x_{i} &=0 \\ \sum_{i=1}^{N} y_{i} - N \beta_{0} -\sum_{i=1}^{N} \beta_{1}x_{i} &=0 \\ \sum_{i=1}^{N} y_{i} -\sum_{i=1}^{N} \beta_{1}x_{i} &= N \beta_{0} \\ \sum_{i=1}^{N} y_{i} - \beta_{1}\sum_{i=1}^{N} x_{i} &= N \beta_{0} \\ \frac{\sum_{i=1}^{N} y_{i}}{N} - \beta_{1}\frac{\sum_{i=1}^{N}x_{i}}{N} &= \beta_{0}\\ \bar{y} - \beta_{1}\bar{x} &= \beta_{0} \end{aligned}

The least squares estimate for β0\beta_{0} is

β0^SS=yˉ−β1xˉ\begin{aligned} \hat{\beta_{0}}_{SS} = \bar{y} - \beta_{1} \bar{x} \end{aligned}

β1\beta_{1} as the variable and β0\beta_{0} as a constant

Lets take the same approach for computing an estimate for β1\beta_{1}.

SS(β1)′={∑i=1N[yi−(β0+β1xi)]2}′=∑i=1N{[yi−(β0+β1xi)]2}\begin{aligned} SS(\beta_{1})' &= \left\{ \sum_{i=1}^{N} \left[ y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right]^{2} \right \}' \\ &= \sum_{i=1}^{N} \left\{ \left[ y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right]^{2}\right \} \end{aligned}

The goal then is to compute the derivative of

SS(β1)=[yi−(β0+β1xi)]2\begin{aligned} SS(\beta_{1}) = \left[ y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right]^{2} \end{aligned}

We will need to use the chain rule

SS(β1)=[h(β1)]2h(β1)=yi−(β0+β1xi)\begin{aligned} SS(\beta_{1}) &= \left[ h(\beta_{1}) \right]^{2} \\ h(\beta_{1}) &= y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \end{aligned}

The derivative of

SS(β1)=[h(β1)]2\begin{aligned} SS(\beta_{1}) &= \left[ h(\beta_{1}) \right]^{2} \end{aligned}

is

SS(β1)=2[h(β1)]\begin{aligned} SS(\beta_{1}) &= 2\left[ h(\beta_{1}) \right] \end{aligned}

and the derivative of

h(β1)=yi−(β0+β1xi)\begin{aligned} h(\beta_{1}) &= y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \end{aligned}

is

h(β0)′=−xi,\begin{aligned} h(\beta_{0})' &= -x_{i}, \end{aligned}

and so the derivative of SS(β1)SS(\beta_{1}) is

SS(β1)′=∑i=1N2[h(β1)]×(−xi)=∑i=1N−2xi[yi−(β0+β1xi)]\begin{aligned} SS(\beta_{1})' &= \sum_{i=1}^{N} 2\left[ h(\beta_{1}) \right] \times (-x_{i}) \\ &= \sum_{i=1}^{N} -2x_{i}\left[y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right] \end{aligned}

We can set this equation equal to zero and solve for β1\beta_{1} to find the least squares estimate for β1\beta_{1}

∑i=1N−2xi[yi−(β0+β1xi)]=0∑i=1N−xi[yi−(β0+β1xi)]=0∑i=1N−xiyi+xiβ0+β1xi2=0∑i=1N−xiyi+β0∑i=1Nxi+β1∑i=1Nxi2=0β1∑i=1Nxi2=∑i=1Nxiyi−β0∑i=1Nxi\begin{aligned} \sum_{i=1}^{N} -2x_{i}\left[y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right] &= 0\\ \sum_{i=1}^{N} -x_{i}\left[y_{i} - \left(\beta_{0} + \beta_{1}x_{i} \right) \right] &= 0\\ \sum_{i=1}^{N} -x_{i} y_{i} + x_{i}\beta_{0} + \beta_{1}x^{2}_{i} &= 0\\ \sum_{i=1}^{N} -x_{i} y_{i} + \beta_{0} \sum_{i=1}^{N} x_{i} + \beta_{1} \sum_{i=1}^{N} x^{2}_{i} &= 0\\ \beta_{1} \sum_{i=1}^{N} x^{2}_{i} &= \sum_{i=1}^{N} x_{i} y_{i} - \beta_{0} \sum_{i=1}^{N} x_{i} \\ \end{aligned}

Solving for β0\beta_{0} and β1\beta_{1}

At this point we have two equations and two unknowns:

β0=yˉ−β1xˉβ1∑i=1Nxi2=∑i=1Nxiyi−β0∑i=1Nxi\begin{aligned} \beta_{0} &= \bar{y} - \beta_{1} \bar{x} \\ \beta_{1} \sum_{i=1}^{N} x^{2}_{i} &= \sum_{i=1}^{N} x_{i} y_{i} - \beta_{0} \sum_{i=1}^{N} x_{i} \\ \end{aligned}

We can solve the above equations and find that the least squares estimates for β0\beta_{0} and for β1\beta_{1} are

β1^=∑i=1N(xi−xˉ)(yi−yˉ)∑i=1N(xi−xˉ)2β0^=yˉ−β1^xˉ\begin{aligned} \hat{\beta_{1}} &= \frac{ \sum_{i=1}^{N} (x_{i} - \bar{x})(y_{i} - \bar{y}) }{ \sum_{i=1}^{N} (x_{i} - \bar{x})^{2} }\\ \hat{\beta_{0}} &= \bar{y} - \hat{\beta_{1}} \bar{x} \\ \end{aligned}

Suppose we collect the dataset

x      y

0.38 0.72 0.04 1.00 0.79 0.19 0.48 0.69 0.68 0.56

We can compute the least squares estimates for β0\beta_{0} and β1\beta_{1} by following these steps:

Compute xˉ\bar{x} and yˉ\bar{y}

xˉ=0.38+0.04+0.79+0.48+0.685=0.47yˉ=0.72+1.00+0.19+0.69+0.565=0.63\begin{aligned} \bar{x} = \frac{0.38 + 0.04 + 0.79 + 0.48 + 0.68}{5} = 0.47\\ \bar{y} = \frac{0.72 + 1.00 + 0.19 + 0.69 + 0.56}{5} = 0.63\\ \end{aligned}

Augment the above table with a column that computes xi−xˉx_{i} - \bar{x} and yi−yˉy_{i} - \bar{y}

x      y     $(x_{i} - \bar{x})$   $(y_{i} - \bar{y})$

0.38 0.72 -0.09 0.09 0.04 1.00 -0.43 0.37 0.79 0.19 0.31 -0.44 0.48 0.69 0.01 0.05 0.68 0.56 0.21. -0.07

Add a column that computes (xi−xˉ)(yi−yˉ)(x_{i} - \bar{x})(y_{i} - \bar{y}) and that computes (xi−xˉ)2(x_{i} - \bar{x})^{2}

x      y     $(x_{i} - \bar{x})$   $(y_{i} - \bar{y})$   $(x_{i} - \bar{x})(y_{i} - \bar{y})$   $(x_{i} - \bar{x})^{2}$

0.38 0.72 -0.09 0.09 -0.01 0.01 0.04 1.00 -0.43 0.37 -0.16 0.18 0.79 0.19 0.31 -0.44 -0.14 0.10 0.48 0.69 0.01 0.05 0.00 0.0 0.68 0.56 0.21. -0.07 -0.02 0.04

Compute the sums of the last two columns and divide the second to last column by the last column to estimate β1\beta_{1}

∑i=1N(xi−xˉ)(yi−yˉ)=−0.01−0.16−0.14+0.00−0.02=−0.33∑i=1N(xi−xˉ)2=0.01+0.18+0.10+0.0+0.04=0.33β1^=−0.33/0.33=−1\begin{aligned} \sum_{i=1}^{N} (x_{i} - \bar{x})(y_{i} - \bar{y}) &= -0.01 -0.16 - 0.14 + 0.00 - 0.02 = - 0.33 \\ \sum_{i=1}^{N} (x_{i} - \bar{x})^{2} &= 0.01 + 0.18 + 0.10 + 0.0 + 0.04 = 0.33 \\ \hat{\beta_{1}} &= -0.33 / 0.33 = -1 \end{aligned}

Use your estimate for β1\beta_{1} to estimate β0\beta_{0}

β0^=yˉ−β1^xˉβ0^=0.63−(−1)(0.47)=1.1\begin{aligned} \hat{\beta_{0}} &= \bar{y} - \hat{\beta_{1}} \bar{x} \\ \hat{\beta_{0}} &= 0.63 - (-1)(0.47) = 1.1 \end{aligned}

Maximum likelihood estimates for β0\beta_{0}, β1\beta_{1}, and σ2\sigma^{2}

We must estimate three parameters for single covariate linear regression: β0,β1,σ2\beta_{0}, \beta_{1}, \sigma^{2}. We will take a maximum likelihood approach to estimate these three covariates in two steps. Step one: compute the log-liklihod for a typical univariate normal distribution. Step two: convert this loglikelihood to single covariate linear regression.

Give a sample D={(x1,y1),(x2,y2),(x3,y3),⋯ ,(xn,yn)}\mathcal{D} = \{ (x_{1},y_{1}), (x_{2},y_{2}), (x_{3},y_{3}), \cdots, (x_{n},y_{n})\} assume that we model YY as Yi∼N(μ,σ2)Y_{i} \sim \mathcal{N}\left(\mu, \sigma^{2} \right) where μ\mu does not depend on xx observations.

Then the likelihood can be computed as

ℓ(μ,σ2)=f(y1)⋅f(y2)⋅f(y3)⋯f(yn)=12πσ2exp⁡[12σ2(y1−μ)2]⋅12πσ2exp⁡[12σ2(y2−μ)2]⋅12πσ2exp⁡[12σ2(y3−μ)2]⋯12πσ2exp⁡[12σ2(yn−μ)2]\begin{aligned} \ell ( \mu, \sigma^{2}) &= f(y_{1}) \cdot f(y_{2}) \cdot f(y_{3}) \cdots f(y_{n}) \\ &= \frac{1}{\sqrt{2 \pi \sigma^{2} } } \exp \left[ \frac{1}{2 \sigma^{2}} \left (y_{1} - \mu \right)^{2} \right] \cdot \frac{1}{\sqrt{2 \pi \sigma^{2} } } \exp \left[ \frac{1}{2 \sigma^{2}} \left (y_{2} - \mu \right)^{2} \right] \\ & \cdot \frac{1}{\sqrt{2 \pi \sigma^{2} } } \exp \left[ \frac{1}{2 \sigma^{2}} \left (y_{3} - \mu \right)^{2} \right] \cdots \frac{1}{\sqrt{2 \pi \sigma^{2} } } \exp \left[ \frac{1}{2 \sigma^{2}} \left (y_{n} - \mu \right)^{2} \right] \end{aligned}

Lets simplify this equation by computing the log likelihood.

ℓℓ(μ,σ2)=−12log⁡(2πσ2)−[12σ2(y1−μ)2]−12log⁡(2πσ2)−[12σ2(y2−μ)2]−12log⁡(2πσ2)−[12σ2(y3−μ)2]⋯−12log⁡(2πσ2)−[12σ2(yn−μ)2]=∑i=1n−12log⁡(2πσ2)−[12σ2(yi−μ)2]=−N2log⁡(2πσ2)−∑i=1n[12σ2(yi−μ)2]=−N2log⁡(2πσ2)−12σ2∑i=1n[(yi−μ)2]\begin{aligned} \ell \ell ( \mu, \sigma^{2}) &= -\frac{1}{2} \log \left(2 \pi \sigma^{2}\right) - \left[ \frac{1}{2 \sigma^{2}} \left (y_{1} - \mu \right)^{2} \right] -\frac{1}{2} \log \left(2 \pi \sigma^{2}\right) - \left[ \frac{1}{2 \sigma^{2}} \left (y_{2} - \mu \right)^{2} \right]\\ &-\frac{1}{2} \log \left(2 \pi \sigma^{2}\right) - \left[ \frac{1}{2 \sigma^{2}} \left (y_{3} - \mu \right)^{2} \right] \cdots -\frac{1}{2} \log \left(2 \pi \sigma^{2}\right) - \left[ \frac{1}{2 \sigma^{2}} \left (y_{n} - \mu \right)^{2} \right] \\ &= \sum_{i=1}^{n} -\frac{1}{2} \log \left(2 \pi \sigma^{2}\right) - \left[ \frac{1}{2 \sigma^{2}} \left (y_{i} - \mu \right)^{2} \right] \\ &= - \frac{N}{2} \log \left(2 \pi \sigma^{2}\right) - \sum_{i=1}^{n} \left[ \frac{1}{2 \sigma^{2}} \left (y_{i} - \mu \right)^{2} \right] \\ &= - \frac{N}{2} \log \left(2 \pi \sigma^{2}\right) - \frac{1}{2 \sigma^{2}} \sum_{i=1}^{n} \left[ \left (y_{i} - \mu \right)^{2} \right] \\ \end{aligned}

Our log likelihood is a function of two parameters. To maximize for μ\mu, we pretend that our log likelihood is a function of just μ\mu and treat σ2\sigma^{2} as a constant. To maximize for σ2\sigma^{2}, we pretend that our log likelihood is a function of σ2\sigma^{2} and treat μ\mu as a constant.

The maximum likelihood estimate (mle) for μ\mu is

μ^mle=∑i=1Nyi/N\begin{aligned} \hat{\mu}_{mle} = \sum_{i=1}^{N} y_{i} / N \end{aligned}

and the mle for σ2\sigma^{2} is

σ2^mle=∑i=1N(yi−μ^mle)2N\begin{aligned} \hat{\sigma^{2}}_{mle} = \sum_{i=1}^{N} \frac{\left(y_{i} - \hat{\mu}_{mle} \right)^{2}}{N} \end{aligned}

Maximum likelihood estimate for σ2\sigma^{2} and the MSE

Homework

  1. Are the following two datasets equal to one another? Why or why not? $$\begin{aligned} \mathcal{D}{1} = { (3,1),(0.1,10),(-1,1),(1,0) }\ \mathcal{D}{2} = {(0.1,10), (1,0),(3,1),(-1,1) }

    \end{aligned}$$

  2. Suppose that you are asked to explore in a group of patients who have a family history of heard disease the relationship between the number of packs of cigarettes smoked by the patient and Creatine Kinase-MB (CK-MB), an enzyme found in heart muscle that when elevated indicates damage to the heart. CK-MB is a measurement that is at minimum 0.
    You’re first thought is to fit a linear regression model of the form $$\begin{aligned} Y_{i}| x_{i} \sim \mathcal{N}\left( \beta_{0} + \beta_{1} x_{i}, \sigma^{2} \right)

    \end{aligned}$where where Y_{i}isarandomvariablethatweassumegeneratesCK−MBlevelsforpatient is a random variable that we assume generates CK-MB levels for patient iand and x_{i}$ is the reported number of packs smoked per day.

    1. What is the support of a Normal distribution? Is this assumption reasonable to model CK-MB?

    2. Suppose we collect data and estimate β0\beta_{0} as β0^=200\hat{\beta_{0}} = 200 and β1^=10\hat{\beta_{1}} = 10. How would you interpret β0^\hat{\beta_{0}}?

  3. Assume that we decide to model a dataset D=((x1,y1),(x2,y2),⋯ ,(xn,yn))\mathcal{D} = ( (x_{1},y_{1}),(x_{2},y_{2}),\cdots,(x_{n},y_{n}) ) as $$\begin{aligned} Y_{i} | x_{i} \sim \mathcal{N}\left( \beta_{0} + \beta_{1} x_{i}, \sigma^{2} \right)

    \end{aligned}$$

    1. Let E=Yi∣xi−(β0+β1xi)E = Y_{i} | x_{i} - (\beta_{0} + \beta_{1} x_{i}). What is the distribution of EE?

    2. Let Z=Yi−(β0+β1xi)σZ = \frac{Y_{i} - (\beta_{0} + \beta_{1} x_{i})}{\sigma}. What is the distribution of ZZ?

  4. We assume that in linear regression $$\begin{aligned} Y_{i} | x_{i} \sim \mathcal{N}\left( \beta_{0} + \beta_{1} x_{i}, \sigma^{2} \right)

    \end{aligned}$thevariable the variable x_{i}$ is generated randomly. Why or why not?

  5. Suppose that we decide to model D=(y1,y2,y3,⋯ ,yn)\mathcal{D} = ( y_{1}, y_{2}, y_{3}, \cdots, y_{n}) as Yi∼N(μ,σ2)Y_{i} \sim \mathcal{N}\left(\mu,\sigma^{2} \right). The loglikelihood function for the normal distribution is $$\begin{aligned} \ell \ell(\mu, \sigma^{2}) = - \frac{N}{2} \log \left(2 \pi \sigma^{2}\right) - \frac{1}{2 \sigma^{2}} \sum_{i=1}^{n} \left[ \left (y_{i} - \mu \right)^{2} \right]

    \end{aligned}$$

    1. Please compute the mle for μ\mu by computing the derivative, treating μ\mu as a variable and σ2\sigma^{2} as a constant

    2. Please compute the mle for σ2\sigma^{2} by computing the derivative, treating σ2\sigma^{2} as a variable and μ\mu as a constant

  6. Please compute for all values of qq the quantities E(R∣q)\mathbb{E}(R|q) and V(R∣q)\mathbb{V}(R|q)

          q= -1   q=0    q=1

    r=-2 0.10 0.90 0.25 r=-1 0.57 0.05 0.25 r= 0 0.13 0.05 0.25 r= 1 0.20 0.00 0.25

  7. For the above table, why do the columns sum to the value one but the rows do not?

  8. Suppose we collected the following dataset

    x y


    -2 -2.83 0 -0.78 2 2.83 1 0.74 0 0.67 1 2.48 2 2.52 3 3.75 -1 -0.91 -2 -1.42

    1. Please compute all fitted values

    2. Please compute all residuals

    3. Plot residuals vs fitted values

    4. Plot residuals vs the order that the data was collected where the top row was the first collected data point and the last row in the table was the 10th data point collected.

    5. Does the linearity assumption appear to hold for this dataset? Why or why not?

    6. Does the equal variances assumption appear to hold? Why or why not?

  9. Suppose we build a scatter plot of the relationship between observations of a variable xx and variable yy. We decide to fit a linear regression and add to the plot E(Y∣x)\mathbb{E}(Y|x).

    image

    Please sketch what you would expect the residual vs fitted value plot to look like.

  10. Suppose that you fit a linear regression model and compute the residuals. As the residuals move closer to the line E(Y∣x)\mathbb{E}(Y|x) will your estimate of σ2\sigma^{2} be smaller or larger? Why?

  11. A linear regression is fit to a dataset where we collected the number of lbs of sugar an undergraduate student consumes during a week in the Fall semester and their H1Ac level. We estimate our parameters and find that β0^=2\hat{\beta_{0}} = 2 and β1^=10\hat{\beta_{1}} = 10. It may (or may not) be more reasonable to express the change in H1Ac not in increments of 1lb but instead in increments of 1 ounce. Please reexpress the estimated change in H1Ac in ounces of sugar consumed.

  12. Will different parameter estimates lead to different diagnostic plots? Why or why not?

  13. The loglikelihood for single covariate regression is $$\begin{aligned} \ell \ell (\beta_{0}, \beta_{1}, \sigma^{2}) = - \frac{N}{2} \log \left(2 \pi \sigma^{2}\right) - \frac{1}{2 \sigma^{2}} \sum_{i=1}^{n} \left{ \left \left[y_{i} - (\beta_{0} + \beta_{1}x_{i}) \right]^{2} \right}

    \end{aligned}$Wewillcomputethemaximumlikelihoodestimatefor We will compute the maximum likelihood estimate for \beta_{1}$ in a series of steps.

    1. Are only variable of interest is β1\beta_{1}. The paramters β0\beta_{0} and σ2\sigma^{2} can be thought of as constants. Remove any terms in the loglikelihood that are constants. Call this new function gg

    2. Compute the derivative of g(β1)g(\beta_{1}), treating β1\beta_{1} as a variable.

    3. Set g′(β1)=0g'(\beta_{1}) = 0 and solve for β1\beta_{1}

  14. We saw that we can compute estimates for β0\beta_{0} and β1\beta_{1} by minimizing the sums of squares or by maximum likelihood. Please describe how the sums of squares estimates and maximum likelihood estimates are related and why they are related.

  15. Why can minimizing the sums of squares function SS(β0,β1)SS(\beta_{0}, \beta_{1}) be interpreted as finding the values (β0),β1(\beta_{0}), \beta_{1} that minimize the sum of squared residuals? Why can this minimizatoin be interpreted as minimizing the mean of squared residuals?

  16. Suppose we collect the dataset

     x       y

    -0.72 -3.33 -0.78 -2.15 -0.69 -1.94 -0.12 -0.68 0.70 0.20 -0.30 -0.47 -0.15 -2.20 -0.71 -1.37 -0.49 -1.16 0.92 1.77

    1. Compute the maximum likelihood estimate for β0\beta_{0}

    2. Compute the maximum likelihood estimate for β1\beta_{1}

    3. Compute the maximum likelihood estimate for σ2\sigma^{2}

  17. Suppose we collect the dataset

     x       y

    -0.49 -2.20 -0.55 -1.01 -0.46 -0.80 0.11 0.46 0.93 1.34 -0.07 0.66 0.08 -1.07 -0.48 -0.24 -0.26 -0.03 1.15 2.90

    1. Compute the maximum likelihood estimate for β0\beta_{0}

    2. Compute the maximum likelihood estimate for β1\beta_{1}

    3. Compute the maximum likelihood estimate for σ2\sigma^{2}