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 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 that were independent and identically distributed.
Our new setup will consider collecting one additional piece of information from each observation
Our statistical setup will assume that the 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 data as being generated from corresponding random variables. The data is thought of as fixed. Our statistical setup is
where we assume that are independent and identically distributed. We will assign to the random variables 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 where the variable is the value one when a patient is younger than 3 or older than 65 and the value zero otherwise, and the variable 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 impact our outcome of interest . Let us define the distribution of the random , conditional on the observation , or , as a probability mass function or density function that produces different probabilities over the support of for different values of the observation .
Assume that has a geometric distribution with the following modification
where the function must be between the values 0 and 1 (Why?). We could write the above as and say that the distribution of is conditional on the value of . When then and when then .
Conditional expectation and variance¶
For a discrete random variable, the conditional expectation of a random variable is defined as
and for a continuous random variable is defined as
where is the conditional probability density corresponding to .
Because the distribution of depends on then the expected value too will depend on the observed value. However, we assume that the observation is not random, the value is a fixed observation. This assumption makes computing the conditional expectation easier.
Suppose that and that can take the values 0 or 1. Further assume the conditional probability distribution
x=0 x=1y=-1 0.10 0.40 y=0 0.50 0.30 y=1 0.40 0.30
: Conditional distribution of
Then
and
For a discrete random variable, the conditional variance of a random variable is defined as
and for a continuous random variable is defined as
where is the conditional probability density corresponding to .
Lets continue with the above example and compute .
Parameters as a function of observations¶
Single covariate linear regression assumes that given a sample that the conditional distribution of given follows a normal distribution
where , and and are parameters that take values in . 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 and make explicit that this random variable is expected to be distributed normal.
Because and are constant, and because is a fixed, constant observation then is a constant. Remember that it is easy to translate normal distributions by a constant. Lets choose to add and subtract the constant
The conditional expectation for single covariate regression can be computed by recognizing that is a constant,
and the conditional variance for single covariate regression can also be computed as
We see that the variance does not depend on and remains constant.
Lets define a new random variable . Then
We can look closer at the distribution for The random variable is a translation of .
Finally we see that we can rewrite our linear regression as
This form for linear regression---the conditional expected value plus an error term ()---is often called the model form for linear regression. Model form emphasizes, , often thought of as how you think and are associated with one another.
Linear regression supposes that there exists a linear relationship between the random variable and the observations . One way to investigate whether or not a linear relationship between and appears reasonable is to draw a scatter plot.
Given a dataset A scatter plot first draws a x and y axis on the cartesian coordinate plane. The second step iterates through each data point and places a dot on the plane at . If it appears that a straight line may well explain the association between and 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, and . 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 and .
Residuals, fitted values, and Normality¶
The random variable 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 that corresponds to a specific value. We can estimate the error term at the value given: (i) estimates for our parameters and (ii) a datapoint from our dataset. Given a datapoint , a residual is the difference between the truly observed and an estimate of , , or .
Given estimates for our parameters , a fitted value to a datapoint is .
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 should behave similarly to the true error terms .
Linearity¶
One assumption about single covariate linear regression to evaluate is that the relationship between and the conditional expected value of , is linear. Single covariate regression assumes that and that for any choice of .
To test for linearity we can plot for each value the corresponding residual . If single covariate regression sufficiently explains the relationship between our random variables and fixed data then we could expect that this plot has residuals values centered around zero for all choice of . 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 y0.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
and we compute estimates for our parameters , , and .
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
By plotting our fitted values , which depend on , against our residuals we can visualize whether or not our residuals are centered around zero. If the residuals show a pattern that does not appear to be centered around zero than the relationship between and may not be linear.
{#fig.residualsDiag width=“\textwidth”}
We see from the plot of vs that the relationship between and 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 is distributed according to a normal distribution with the same variance for all values of . This assumption is often called the equal variances assumption.
Below we generated 100 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 vs 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 values increase. This indicates that the equal variances assumption may not best describe our data.
{#fig.residualsDiag width=“\textwidth”}
Independence¶
The independence between data points and for any 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 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.
{#fig.residualsDiag width=“\textwidth”}
Sums of squares¶
Up until this point we assumed that we can compute estimates for and for , 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 and a single covariate regression model where , the least squares estimates for and are the values that minimize
The function is called the "sums of squares" function. To minimize we will
Treat as a constant, treat as a variable, take the derivative and set that equation to zero.
Treat as a constant, treat as a variable, take the derivative and set that equation to zero.
Solve the above two equations for and .
as the variable and 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.
The goal then is to compute the derivative of
We will need to use the chain rule
The derivative of
is
and the derivative of
is
and so the derivative of is
We can set this equation equal to zero and solve for to find the least squares estimate for
The least squares estimate for is
as the variable and as a constant¶
Lets take the same approach for computing an estimate for .
The goal then is to compute the derivative of
We will need to use the chain rule
The derivative of
is
and the derivative of
is
and so the derivative of is
We can set this equation equal to zero and solve for to find the least squares estimate for
Solving for and ¶
At this point we have two equations and two unknowns:
We can solve the above equations and find that the least squares estimates for and for are
Suppose we collect the dataset
x y0.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 and by following these steps:
Compute and
Augment the above table with a column that computes and
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 and that computes
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
Use your estimate for to estimate
Maximum likelihood estimates for , , and ¶
We must estimate three parameters for single covariate linear regression: . 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 assume that we model as where does not depend on observations.
Then the likelihood can be computed as
Lets simplify this equation by computing the log likelihood.
Our log likelihood is a function of two parameters. To maximize for , we pretend that our log likelihood is a function of just and treat as a constant. To maximize for , we pretend that our log likelihood is a function of and treat as a constant.
The maximum likelihood estimate (mle) for is
and the mle for is
Maximum likelihood estimate for and the MSE¶
Homework¶
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}$$
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}$Y_{i}ix_{i}$ is the reported number of packs smoked per day.
What is the support of a Normal distribution? Is this assumption reasonable to model CK-MB?
Suppose we collect data and estimate as and . How would you interpret ?
Assume that we decide to model a dataset as $$\begin{aligned} Y_{i} | x_{i} \sim \mathcal{N}\left( \beta_{0} + \beta_{1} x_{i}, \sigma^{2} \right)
\end{aligned}$$
Let . What is the distribution of ?
Let . What is the distribution of ?
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}$x_{i}$ is generated randomly. Why or why not?
Suppose that we decide to model as . 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}$$
Please compute the mle for by computing the derivative, treating as a variable and as a constant
Please compute the mle for by computing the derivative, treating as a variable and as a constant
Please compute for all values of the quantities and
q= -1 q=0 q=1r=-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
For the above table, why do the columns sum to the value one but the rows do not?
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
Please compute all fitted values
Please compute all residuals
Plot residuals vs fitted values
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.
Does the linearity assumption appear to hold for this dataset? Why or why not?
Does the equal variances assumption appear to hold? Why or why not?
Suppose we build a scatter plot of the relationship between observations of a variable and variable . We decide to fit a linear regression and add to the plot .

Please sketch what you would expect the residual vs fitted value plot to look like.
Suppose that you fit a linear regression model and compute the residuals. As the residuals move closer to the line will your estimate of be smaller or larger? Why?
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 and . 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.
Will different parameter estimates lead to different diagnostic plots? Why or why not?
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}$\beta_{1}$ in a series of steps.
Are only variable of interest is . The paramters and can be thought of as constants. Remove any terms in the loglikelihood that are constants. Call this new function
Compute the derivative of , treating as a variable.
Set and solve for
We saw that we can compute estimates for and 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.
Why can minimizing the sums of squares function be interpreted as finding the values that minimize the sum of squared residuals? Why can this minimizatoin be interpreted as minimizing the mean of squared residuals?
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
Compute the maximum likelihood estimate for
Compute the maximum likelihood estimate for
Compute the maximum likelihood estimate for
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
Compute the maximum likelihood estimate for
Compute the maximum likelihood estimate for
Compute the maximum likelihood estimate for