Skip to content

Commit

Permalink
use prostate data for design matrix eg and set methylation design mat…
Browse files Browse the repository at this point in the history
…rix calculations after
  • Loading branch information
mallewellyn authored Mar 28, 2024
1 parent 00e61f2 commit 46da648
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions _episodes_rmd/02-high-dimensional-regression.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,6 @@ may have seen when running linear models. Here, we define a *model matrix* or
coefficients that should be fit in each linear model. These are used in
similar ways in many different modelling libraries.

```{r design-age}
library("limma")
design_age <- model.matrix(~age)
dim(design_age)
head(design_age)
```

> ## What is a model matrix?
> When R fits a regression model, it chooses a vector of regression coefficients
> that minimises the differences between outcome values and those values
Expand All @@ -625,14 +618,20 @@ head(design_age)
> model matrix behind the scenes. Usually, it can be extracted from a model fit
> using the function `model.matrix()`. Here is an example:
>
> ```{r}
> data(cars)
> head(cars)
> mod1 <- lm(dist ~ speed, data=cars) # fit regression model using speed as a predictor
> head(model.matrix(mod1)) # the model matrix contains two columns: intercept and speed
> ```
> prostate <- readRDS(here("data/prostate.rds"))
> prostate2 <- (prostate[, c("lcavol", "lpsa")]) #select two variables for illustration
> head(prostate2)
> mod1 <- lm(lcavol ~ lpsa, data=prostate2) # fit regression model using lpsa as a predictor
> head(model.matrix(mod1)) # the model matrix contains two columns: intercept and lpsa
{: .callout}

```{r design-age}
library("limma")
design_age <- model.matrix(~age)
dim(design_age)
head(design_age)
```

As you can see, the design matrix has the same number of rows as our
methylation data has samples. It also has two columns - one for the
intercept (similar to the linear model we fit above) and one for age.
Expand Down

0 comments on commit 46da648

Please sign in to comment.