getR2: Calculate R squared from a matrix

View source: R/getR2.R

getR2R Documentation

Calculate R squared from a matrix

Description

Using limma, calculate the R squared from a matrix using nested models

Usage

getR2(p, mod, mod0 = NULL)

Arguments

p

A matrix that will be passed to lmFit object.

mod

A model matrix for the alternative model (the larger one).

mod0

A model matrix for the null model (the smaller one). If NULL then p will be used to calculate the residual sum of squares of the null model.

Value

A data.frame with the R squared and the adjusted R squared.

Author(s)

Andrew E Jaffe, Leonardo Collado-Torres (examples)

Examples


## Define a model generating function for 30 'samples'
set.seed(20190827)
model_fun <- function(x) {
    ## Baseline + a group effect (2 groups) + a second covariate effect
    rnorm(30) +
        c(rnorm(15, mean = 3), rnorm(15, mean = 1)) +
        c(
            rnorm(5, sd = 0.5), rnorm(5, sd = 0.2, mean = 0.5),
            rnorm(5, sd = 0.2, mean = 0.9)
        )
}

## Generate the data for 20 'genes'
p <- t(sapply(seq_len(20), model_fun))

## Define the phenotype data for these 30 'samples'
pheno <- data.frame(
    group = rep(c("A", "B"), each = 15),
    batch = rep(seq_len(3), each = 5)
)

## Define a full model
mod <- with(pheno, model.matrix(~ group + batch))
## and compute the R2 for each 'gene'
getR2(p, mod)

## Define a smaller model
mod0 <- with(pheno, model.matrix(~group))
## And now compute the new R2 for each 'gene'
getR2(p, mod, mod0)

LieberInstitute/jaffelab documentation built on Dec. 15, 2024, 10:44 p.m.