# genomicsclass/labs

Switch branches/tags
Nothing to show
e321b04 Mar 28, 2017
3 contributors

### Users who have contributed to this file

280 lines (209 sloc) 6.59 KB
title author date output layout
Linear Algebra Examples
Rafa
February 18, 2015
html_document
page
library(knitr)
opts_chunk$set(fig.path=paste0("figure/", sub("(.*).Rmd","\\1",basename(knitr:::knit_concord$get('infile'))), "-"))


## Examples

Now we are ready to see how matrix algebra can be useful when analyzing data. We start with some simple examples and eventually arrive at the main one: how to write linear models with matrix algebra notation and solve the least squares problem.

#### The average

To compute the sample average and variance of our data, we use these formulas $\bar{Y}=\frac{1}{N} Y_i$ and $\mbox{var}(Y)=\frac{1}{N} \sum_{i=1}^N (Y_i - \bar{Y})^2$. We can represent these with matrix multiplication. First, define this $N \times 1$ matrix made just of 1s:

$$A=\begin{pmatrix} 1\ 1\ \vdots\ 1 \end{pmatrix}$$

This implies that:

$$\frac{1}{N} \mathbf{A}^\top Y = \frac{1}{N} \begin{pmatrix}1&1&\dots&1\end{pmatrix} \begin{pmatrix} Y_1\ Y_2\ \vdots\ Y_N \end{pmatrix}= \frac{1}{N} \sum_{i=1}^N Y_i = \bar{Y}$$

Note that we are multiplying by the scalar $1/N$. In R, we multiply matrix using %*%:

data(father.son,package="UsingR")