Learn R Programming

wrapr (version 2.1.0)

clean_fit_lm: Fit a stats::lm without carying back large structures.

Description

Please see https://win-vector.com/2014/05/30/trimming-the-fat-from-glm-models-in-r/ for discussion.

Usage

clean_fit_lm(
  outcome,
  variables,
  data,
  ...,
  intercept = TRUE,
  weights = NULL,
  env = baseenv()
)

Value

list(model=model, summary=summary)

Arguments

outcome

character, name of outcome column.

variables

character, names of varaible columns.

data

data.frame, training data.

...

not used, force later arguments to be used by name

intercept

logical, if TRUE allow an intercept term.

weights

passed to stats::glm()

env

environment to work in.

Examples

Run this code

mk_data_example <- function(k) {
  data.frame(
    x1 = rep(c("a", "a", "b", "b"), k),
    x2 = rep(c(0, 0, 0, 1), k),
    y = rep(1:4, k),
    yC = rep(c(FALSE, TRUE, TRUE, TRUE), k),
    stringsAsFactors = FALSE)
}

res_lm <- clean_fit_lm("y", c("x1", "x2"),
                       mk_data_example(1))
length(serialize(res_lm$model, NULL))

res_lm <- clean_fit_lm("y", c("x1", "x2"),
                       mk_data_example(10000))
length(serialize(res_lm$model, NULL))

predict(res_lm$model,
        newdata = mk_data_example(1))

Run the code above in your browser using DataLab