Introduction¶
Statistics is meant to impart structure to observations in nature and many times the observations that we. are interested in contain many different pieces of information. For example, when studying the relationship between patients randomized in a clinical trial we may be interested in, for each patient, their age, sex, race, body mass index, and whether or not they attain the outcome of interest. Then every observation (a patient) contains five pieces of information. We can think of this observation as living in a single, 5-dimensional space.
Mathematical objects that keep track of values in 1, 2, 3, and higher dimensional spaces are called vectors, and linear maps that take as input one vector and produce a second vector are called matrices. The rules for operating with vectors and matrices is called matrix calculus.
Because we wish to structure observations with many pieces of information it is natural that statistics rely on matrix calculus.
Vectors¶
Definition¶
Define the set of all real numbers (positive, negative, and decimal numbers) with the symbol , and further define the Cartesian product of the set of real numbers with itself as . In general the Cartesian produce of with itself times can be denoted as .
We will informally define a vector of length as a point in the space . A vector is typically represented as a lowercase letter and the values of the vector are enclosed in square brackets.
For the space we can define the vector
or the vector
For a space we could define a vector
Vectors can be characterized by their length. The length of a vector is the number of values that are needed to define it. The vector and have a length of 2 and the vector has a length of 6.
Though it is possible to discuss vectors of length one, more often we give these objects their own name and special properties. A vector of length one is called a scalar. Scalars do not need to be enclosed by square brackets.
In any of the spaces for examples of scalars are 6, -0.4, 0,1, 22, etc.
Addition and subtraction¶
There are specific rules for how two vectors can interact with one another. You can add and subtract vectors, and you can multiply vectors by a scalar. Addition, subtraction; multiplication and division by a scalar all have a geometric interpretation.
To add two vectors you add the corresponding values in the first, second, third and so on position. You cannot add vectors of different lengths.
Let
and
Then
To subtract two vectors you subtract the corresponding values in the first, second, third and so on position. You cannot subtract vectors of different lengths.
Consider , the vector can be drawn on the Cartesian coordinate plane by starting at the origin and then drawing a straight line from the origin to the point .
You can draw the addition of two vectors with a method nicknamed the "tip to tail" draw. Consider two vector labeled and . Draw the first vector starting at the origin. Draw the second vector starting, not at the origin, but at the tip of the first vector. The vector is the vector starting at the origin and ending at the tip of the second vector.
Multiplication and division by a scalar¶
A new vector can be built by multiplying or dividing a vector by a scalar , . The vector is built by multiplying each entry in by .
Geometrically, when you multiply a vector by a scalar that is greater than one the vector is stretched. When diving by that scalar (or multiplying by a scalar between 0 and one) then the vector is compressed. When you multiply a vector by negative one that vector is rotated by 180 degrees. To multiply a vector by a scalar smaller than negative one first rotate the vector 180 degrees and then stretch the vector. A vector multiplied by a scalar between negative one and zero is rotated and then compressed.
Matrices¶
Definition¶
A matrix is a list of vectors and can be thought of as a function that transforms one vector to another. A matrix is often denoted as a capital letter and the values of a matrix are enclosed in square brackets.
We can build a matrix
Entries in a matrix can be referred to by row number and column number. For example the entry in the matrix is value 0.5. We can also refer to the (2,1) entry in the matrix as
The shape of a matrix is a tuple that describes the number of rows of the matrix and the number of columns. The shape of the matrix above is .
An important operation---called the transpose---interchanges rows and columns of a matrix. Define a matrix
with shape . Then the transpose of , labeled or sometimes , is the matrix with shape and where the first row of is the first column of , the second row of is the second column of , and so on.
If the matrix has shape then the matrix has shape .
We can think of vectors as matrix where the number of rows equals the length of the vector and the number of columns equals one. This allows us to consider both a vector
and a vector transpose . A vector with several rows and one column is often called a column vector and a vector with several columns and one row is often called a row vector. The vector is an example of a column vector.
Like vectors, matrices can interact with other matrices and with vectors.
Matrix times vector¶
If the number of columns of a matrix is equal to the length of a vector then and can be multiplied together to produce another vector.
If the shape of the matrix is and the vector is of length then the new vector will be of length .
The values of each entry in are combinations of individual rows in the matrix and the vector .
Let
and
To compute the first entry in we work with the first row of and the vector .
where denotes the first entry in , denotes the entry in and denotes the entry in the matrix .
To compute the second entry in we work with the second row of and the vector , and so on.
The new vector generated by multiplying and is the vector
We could rewrite, if desired, the above vector as a scalar times a vector
In general, given a matrix with shape and a vector with length , multiplying produces a new vector where
Because a matrix times a vector produces a new vector we can think of a matrix as a function. The input to our function is a vector (such as the vector ) and the output is another vector (such as the vector above)
Matrices and vectors have special properties when interacting. A matrix can be "distributed" across two vectors Given a matrix and the vectors and , the matrix product
Scalars can be "factored" out of a matrix, vector multiplications. Let be a scalar, then
Matrix-matrix operations¶
Addition and subtraction¶
Matrices of the same shape can be added and subtracted by adding (subtracting) corresponding entries.
Given the matrix
and the matrix
then the entry of a new matrix is the sum of the entry in and entry in or
The entry of a matrix is the difference of the entry in and entry in or
Multiplication¶
A matrix can be multiplied by a matrix to produce a new matrix with the following formula
Given a matrix
and a matrix
The entry in the new matrix , in the (1,1) position is
The above operation for computing an entry in occurs frequently in matrix calculus. This operation is called an inner product. The inner product of the vector of length and the vector of length is equal to
We can think of the matrix as consisting of 2 row vectors, the vectors and , and the matrix as consisting of two column vectors
and
Then the matrix or
Important: The matrix product is not necessarily equal to the matrix product . For matrix multiplication, the order of multiplication matters.
The matrix inverse¶
There exists a special matrix called identity matrix. The identity matrix of size is a matrix with rows and columns with ones along the diagonal entries (the entries for from 1 to ) and then zeroes for all other entries.
A matrix has an inverse if (i) has the same number of rows as columns and (ii)
A matrix that satisfies the above properties is called the inverse of and is denoted .
A matrix inverse is similar to an inverse in one dimensional algebra. In 1d algebra an inverse for the variable satisfies
and the variable is denotes or . Matrix with a different number of rows and columns will never have an inverse.
Multivariate Linear regression¶
Multivariate regression models are ubiquitous in science because of their ability to relate two or more measurable quantities to a target variable.
Multivariate linear regression supposes that many different covariates may be able to describe the distribution of a random variable .
Multivariate linear regression extends single covariate regression, and we will see below that adding more covariates makes estimating, as well as communicating, parameters more difficult.
Data and model setup¶
Multivariate linear regression supposes that an experiment generates a single data point that contains one measurement of our target of interest and two or more measurements of covariates that may help explain our target.
A single observation takes the form
where is our target and are covariates that are measured at the same time as our target.
A dataset containing observation then can be written as
Expected '\right', got '&' at position 103: …{1,p}), \\
&̲(y_{2}, x_{2,1}…
\begin{aligned}
&\mathcal{D} = \left( (y_{1}, x_{1,1}, x_{1,2}, x_{1,3}, \cdots, x_{1,p}), \\
&(y_{2}, x_{2,1}, x_{2,2}, x_{2,3}, \cdots, x_{2,p}), \cdots, (y_{n}, x_{n,1}, x_{n,2}, x_{n,3}, \cdots, x_{n,p}) \right)
\end{aligned}The model for multivariate linear regression assumes
where we assume that the observation was generated from a corresponding random variable with the above Normal distribution conditioned on all covariate values.
The above model can also be expressed as
Compared to single covariate linear regression, multivariate linear regression begins with as in single covariate linear regression and then further adjusts the mean by multiplying each additional covariate with a corresponding and summing all of these quantities .
A single categorical variable¶
A natural experiment where one will need to use multivariate regression is when associating a continuous target variable to a categorical explanatory variable that can take one of possible categories.
Suppose that we wish to relate , a continuous variable, to x, a variable that can take one of the three values ‘A’, ‘B’, ‘C’ then linear regression does not---at first---make much sense.
Given the dataset
The naive model would assume that
However, we cannot multiply the value by the letters A, B, C.
One solution is to convert this single categorical variable into many numerical variables. We will need to (i) choose a reference value for that we will compare all other y values corresponding to other categories to and (ii) create two additional columns---one per category excluding the reference---that contain the value one when that category value is present in and the value zero otherwise.
For example, suppose for the above dataset we choose a reference of "B". Then we will create one column that identifies values of "A" and a second column that identifies values of "C".
Our new dataset will look like
We can update our naive single covariate linear regression with the following multivariate linear regression.
To interpret the above model, lets compute the expected value of when we observe a "A", "B", and a "C".
When we observe an "A" then the covariate is the value one and the covariate is the value zero. So then
When we observe a "B" then the covariate is the value zero and the covariate is the value zero. So then
When we observe a "C" then the covariate is the value zero and the covariate is the value one. So then
The intercept term is the expected value of when we observe a value that is equal to the reference. All other categories include this reference . The parameter is the change in the expected value of when an observation includes compared to the expected value when x=B. The parameter is the change in the expected value of when an observation includes compared to the expected value when x=B.
Two or more continuous covariates and adjustment for confounding¶
We can include several continuous valued covariates without the need to first transform them as we must do when we include a covariate that is categorical. For example, suppose that we wish to associate the annual cost of household health insurance to annual family income, number of dependents that are claimed on previous taxes by the individual who pays the insurance, and age of the oldest resident of the household.
Then we can generate a model
and estimate the parameters . Standard software can produce point estimates, confidence intervals, and hypothesis tests at a desired false positive rate, but software can not interpret the hypothesized impact of these parameters on .
One method to understand whether a covariate is associated with while adjusting for all other covariates that are considered is to build an added variable plot between and .
Suppose our experiment generates three covariates and a target . An added variable plot for the covariate follows these steps:
Fit a linear regression to
Fit a linear regression to
Plot the pairs ( , )
Fit the regression and plot the expected value.
The residuals include any relationships that are not captured by associating , and linearly. The residuals are values that linearly relating and could not capture, and so when we relate to we are asking about the association between patterns in that could not be captured by and , and patterns in that could not be explained by and .
Estimating and ¶
Homework¶
Let $$\begin{aligned} A = \begin{bmatrix} 1/2 & -2 & 0 \ -10 & -5 & -1 \end{bmatrix} ; B = \begin{bmatrix} 1 & 0 \ 0 & -1 \ 1 & 1 \end{bmatrix} ; x = \begin{bmatrix} -2 \ 1 \ 0 \end{bmatrix}
\end{aligned}$$ If possible, please compute the below. If it is not possible to compute one of the below quantities then please write "Cannot compute" and explain why.
Suppose that we collect data on the total number of confirmed influenza hospitalizations over the 2022/2023 influenza season and hypothesize that the total number of cases may be related to population density of the state and the percent of the state population that is vaccinated before October 1, 2022.
Please describe a multivariate linear regression that may be used to model this data. Please include: (i) the model, a description of each variable in the dataset, a summary of what the vector and matrix might look like.
Let , where $$\begin{aligned} \beta = \begin{bmatrix} \beta_{0}\ \beta_{1}\ \beta_{2}\ \beta_{3}\ \end{bmatrix}; ; x = \begin{bmatrix} x_{3}\ x_{2}\ x_{1}\ x_{0}\ \end{bmatrix}
\end{aligned}$\beta’x$ as a series of sums and products.