Skip to content

Instantly share code, notes, and snippets.

View hturner's full-sized avatar

Heather Turner hturner

View GitHub Profile

Log-likelihood Computation for (Weighted) Linear Regression in R (stats::logLik.lm)

The likelihood for a linear model is given by:

$$L = \prod_{i = 1}^N \frac{1}{\sqrt{2\pi\sigma^2}} \exp \left( \frac{-(y_i - \mu)^2}{2\sigma^2} \right) $$

To derive the computation of the log-likelihood used in stats::logLik.lm we first need to incorporate weights.

From ?lm

@hturner
hturner / integer_typing.R
Created January 22, 2020 16:19
Some examples of where explicitly using an integer scalar/vector is more efficient in R. Often the difference in a single operation is quite small, but if repeatedly using integer values in a script using explicit typing can be an easy way to gain speed.
library(microbenchmark)
# Examples where explictly typing with "L" is more efficient
## create a reasonably large matrix
r <- 5000
c <- 100
ints <- sample(0:10, r*c, replace = TRUE)
M <- matrix(ints, nr = r, nc = c)
@hturner
hturner / README.md
Last active November 6, 2017 15:10
Modify pkgdown to use bookdown::htmldocument2 and use custom css to style captions and abstract (suitable for Biocstyle vignettes)

This gist provides a workaround proposed by Andrzej K. Oleś (@aoles) to use pkgdown with vignettes formatted with Biocstyle::html_document2 or bookdown::html_document2, see r-lib/pkgdown#323.

  1. Copy extra.css to pkgdown/extra.css in your package sources (i.e. pkgdown is a top-level directory alongside R, man, etc) or merge with your existing extra.css.

  2. Source build_rmarkdown_format.R before calling build_site() or build_articles(). Note build_site must be called to copy extra.css to docs, so must be used the first time extra.css is added or any time it is modified for the changes to take effect.

@hturner
hturner / fix_biocstyle_articles.R
Created November 4, 2017 10:36
Post-process pkgdown version of Biocstyle vignette
#' Post-process pkgdown Version of Biocstyle Vignette
#'
#' Takes HTML vignette rendered by [pkgdown::build_articles()] from R markdown
#' using [BiocStyle::html_document2()] and post-processes it to fix
#' features not supported by [rmarkdown::render()] (used by `build_articles`
#' under the hood).
#'
#' In order to match the table and figure references to the correct table or
#' figure, the function requires both the HTML rendered by
#' [pkgdown::build_articles()] and the version rendered by