|
| 1 | +#' Auto-detect GAM terms |
| 2 | +#' |
| 3 | +#' @description This is a useful function that returns a table of the terms in a GAM, whether they are a one dimensional (linear) term, a two dimensional smooth, or a factor. Also detects the offset. |
| 4 | +#' @param model A fitted GAM model object |
| 5 | +#' @param hgam character describing how to handle hgams; use "d" for the density model, "p" for the prob model, or "b" or "all" for both |
| 6 | +#' |
| 7 | +#' @return returns a data frame with columns describing the name and type of all model components and relevant smoother parameters |
| 8 | +#' @export |
| 9 | +#' |
| 10 | +#' @examples |
| 11 | +AutodetectGAMTerms <- function(model, hgam = "all") { |
| 12 | + n.formulas <- 1 |
| 13 | + if (model$family$family == "ziplss" & hgam %in% c("b", "both", "all")) { |
| 14 | + n.formulas <- 2 |
| 15 | + } |
| 16 | + |
| 17 | + for (f in 1:n.formulas) { |
| 18 | + form.index <- 3 |
| 19 | + |
| 20 | + # a lot of special handling for ziplss models |
| 21 | + if (model$family$family == "ziplss") { |
| 22 | + if (hgam %in% c("b", "both", "all")) { |
| 23 | + form1 <- stats::formula(model)[[f]] |
| 24 | + } |
| 25 | + if (hgam %in% c("d", "dens", "density")) { |
| 26 | + form1 <- stats::formula(model)[[1]] |
| 27 | + } |
| 28 | + if (hgam %in% c("p", "prob", "probability")) { |
| 29 | + form1 <- stats::formula(model)[[2]] |
| 30 | + } |
| 31 | + } else { |
| 32 | + form1 <- stats::formula(model) |
| 33 | + } |
| 34 | + |
| 35 | + terms <- trimws(strsplit(as.character(form1[[length(form1)]]), split = "[+]")[[2]]) |
| 36 | + |
| 37 | + # loop through and figure out the information for the table |
| 38 | + type.dat <- data.frame(type = rep(NA, length(terms)), dims = 1, term = NA, term2 = NA, bs = NA, k = NA, m = NA, m2 = NA) |
| 39 | + for (t in 1:length(terms)) { |
| 40 | + x <- terms[t] |
| 41 | + x2 <- strsplit(x, split = "[(=)]")[[1]] |
| 42 | + # linear terms |
| 43 | + if (length(x2) == 1) { |
| 44 | + type.dat$type[t] <- "linear" |
| 45 | + type.dat$term[t] <- x2 |
| 46 | + } else { |
| 47 | + # smoothed terms |
| 48 | + if (x2[1] %in% c("s", "te")) { |
| 49 | + dims <- length(strsplit(x2[2], split = ", ")[[1]]) - 1 |
| 50 | + for (n in 1:dims) { |
| 51 | + type.dat[t, 2 + n] <- strsplit(x2[2], split = ", ")[[1]][n] |
| 52 | + } |
| 53 | + type.dat$type[t] <- "smooth" |
| 54 | + type.dat$dims[t] <- dims |
| 55 | + |
| 56 | + # find smoother basis |
| 57 | + formula.options <- strsplit(x, split = ",")[[1]] |
| 58 | + bs.spot <- which(unlist(lapply(strsplit(formula.options, "="), FUN = function(x) { |
| 59 | + return(trimws(x[1])) |
| 60 | + })) == "bs") |
| 61 | + if (length(bs.spot) > 0) { |
| 62 | + type.dat$bs[t] <- strsplit(formula.options[bs.spot], split = "\"")[[1]][2] |
| 63 | + } |
| 64 | + |
| 65 | + # find smoother k |
| 66 | + k.spot <- which(unlist(lapply(strsplit(formula.options, "="), FUN = function(x) { |
| 67 | + return(trimws(x[1])) |
| 68 | + })) == "k") |
| 69 | + if (length(k.spot) > 0) { |
| 70 | + type.dat$k[t] <- trimws(strsplit(strsplit(formula.options[k.spot], split = "=")[[1]][2], split = "[)]")[[1]]) |
| 71 | + } |
| 72 | + |
| 73 | + # find penalty m, which can be complicated |
| 74 | + m.spot <- which(unlist(lapply(strsplit(formula.options, "="), FUN = function(x) { |
| 75 | + return(trimws(x[1])) |
| 76 | + })) == "m") |
| 77 | + if (length(m.spot) > 0) { |
| 78 | + if ("c" %in% strsplit(formula.options[m.spot], split = "")[[1]]) { |
| 79 | + m.spot <- c(m.spot, m.spot + 1) |
| 80 | + } |
| 81 | + for (n in 1:length(m.spot)) { |
| 82 | + m1 <- trimws(formula.options[m.spot][n]) |
| 83 | + if (n == 1) { |
| 84 | + m2 <- trimws(strsplit(m1, split = "=")[[1]][2]) |
| 85 | + } else { |
| 86 | + m2 <- m1 |
| 87 | + } |
| 88 | + m3 <- trimws(strsplit(m2, split = "[()]")[[1]]) |
| 89 | + type.dat[t, 6 + n] <- suppressWarnings(stats::na.omit(as.numeric(m3))[1]) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + # factor terms |
| 94 | + if (x2[1] == "as.factor") { |
| 95 | + type.dat$type[t] <- "factor" |
| 96 | + type.dat$term[t] <- x2[2] |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + # Make the table |
| 101 | + terms2 <- unlist(strsplit(x = names(model$model), split = "[()]")) |
| 102 | + off.term <- which(terms2 == "offset") + 1 |
| 103 | + if (length(off.term) > 0) { |
| 104 | + type.dat <- rbind(type.dat, data.frame(type = "offset", dims = 1, term = terms2[off.term], term2 = NA, bs = NA, k = NA, m = NA, m2 = NA)) |
| 105 | + } |
| 106 | + # if multiple formulas, need to make a list |
| 107 | + if (n.formulas > 1) { |
| 108 | + if (f == 1) { |
| 109 | + out.dat <- list(type.dat) |
| 110 | + } else { |
| 111 | + out.dat[[f]] <- type.dat |
| 112 | + } |
| 113 | + } else { |
| 114 | + out.dat <- type.dat |
| 115 | + } |
| 116 | + } |
| 117 | + return(out.dat) |
| 118 | +} |
0 commit comments