Skip to content

Instantly share code, notes, and snippets.

View gavinsimpson's full-sized avatar

Gavin Simpson gavinsimpson

View GitHub Profile
@gavinsimpson
gavinsimpson / smooth_re2.R
Created August 26, 2020 21:07 — forked from noamross/smooth_re2.R
Smooth construct, predict, and plot for random effects with numeric inputs
#' Create a random effect basis with integers rather than factors
#' @import mgcv
#' @export
smooth.construct.re2.smooth.spec <- function (object, data, knots) {
if (!is.null(object$id))
stop("random effects don't work with ids.")
if(any(sapply(data, is.numeric))) data <- lapply(data, as.factor) ## <-- All I did was this (and below)
form <- as.formula(paste("~", paste(object$term, collapse = ":"),
"-1"))
object$X <- model.matrix(form, data)