Deduplicate a list of lm objects in R -
i have list
of lm
models objects possible repeated, i'd find way of checking if of these lm
objects equal, if them delete it. in words, want "deduplicate" list
.
i'd appreciate help.
an example of problem:
## creates outcome , predictors outcome <- c(names(mtcars)[1:3]) predictors <- c(names(mtcars)[4:11]) dataset <- mtcars ## creates model list model_list <- lapply(seq_along((predictors)), function(n) { left_hand_side <- outcome[1] right_hand_side <- apply(x = combn(predictors, n), margin = 2, paste, collapse = " + ") paste(left_hand_side, right_hand_side, sep = " ~ ") }) ## convert model list verctor model_vector <- unlist(model_list) ## fit linear models itens vector of models list_of_fit <- lapply(model_vector, function(x) { formula <- as.formula(x) fit <- step(lm(formula, data = dataset)) fit }) # exclude possible missing list_of_fit <- filter(negate(function(x) is.null(unlist(x))), list_of_fit) # these models same in list lm253 <- list_of_fit[[253]];lm253 lm254 <- list_of_fit[[254]];lm254 lm255 <- list_of_fit[[255]];lm255
i want exclude duplicated entries in list_of_fit
.
it seems wasteful fit many models , throw away of them. object names make code hard read me, seems models can distinguished based on formula. maybe helps:
lista_de_ajustes[!duplicated(vapply(lista_de_ajustes, function(m) deparse(m$call), fun.value = "a"))]
Comments
Post a Comment