R/modelTrim.R

Defines functions modelTrim

Documented in modelTrim

modelTrim <- function(model, method = "summary", alpha = 0.05) {
  # version 2.1 (9 Jan 2023)

  stopifnot(
    class(model) %in% c("glm", "lm"),
    alpha > 0,
    alpha < 1
  )

  #dat <- as.data.frame(model$model)  # new
  n.vars.start <- length(model$coefficients) - 1
  names.vars.start <- names(model$coefficients)[-1]

  if (method == "summary") {
    family <- family(model)  # new after J.C.Guerrero email 30/12/2022
    p.values <- expression(summary(model)$coefficients[ , 4])
  } else if (method == "anova") {
    p.values <- expression(as.matrix(anova(model, test = "Chi"))[ , 5])
  } else stop ("'method' must be either 'summary' or 'anova'")

  while (max(eval(p.values)[-1]) > alpha) {  # excludes p-value of intercept
    exclude <- names(which.max(eval(p.values)[-1]))
    model <- update(model, as.formula(paste("~.-", exclude)), data = model$model)
    if (length(model$coefficients) == 1) {  # only intercept remains
      message("No significant variables left in the model.")
      break
    }  # end if length
  } # end while

  n.vars.trim <- length(model$coefficients) - 1
  excluded.vars <- setdiff(names.vars.start, names(model$coefficients)[-1])
  message(n.vars.start - n.vars.trim, " variable(s) excluded by 'modelTrim' function\n ", paste(excluded.vars, collapse = ", "), "\n\n")

  return(model)
}

Try the fuzzySim package in your browser

Any scripts or data that you put into this service are public.

fuzzySim documentation built on March 19, 2024, 3:09 a.m.