Last active
June 23, 2018 21:15
-
-
Save bgall/74fecd314650f29b66f5fbaeb0c18a7f to your computer and use it in GitHub Desktop.
Estimate models with groups of variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create sample data | |
set.seed(1) | |
data <- data.frame(y = rnorm(10), | |
a = rnorm(10), | |
b = rnorm(10), | |
c = rnorm(10), | |
d = rnorm(10)) | |
# Function takes a string | |
# for outcome, character vector of variable | |
# names to always keep, character vector of | |
# variable names to add, and data | |
quick_lm <- function(outcome, keepers, new, data){ | |
keepers <- paste(keepers, collapse = " + ") | |
new <- paste(new, collapse = " + ") | |
rhs <- paste(keepers, new, sep = " + ") | |
model <- paste0(outcome, " ~ ", rhs) | |
fit <- lm(as.formula(model), data = data) | |
} | |
# Arbitrarily assign some variables to inputs | |
outcome <- "y" | |
keepers <- c("a","b") | |
# Fit a model, check summary | |
m1 <- quick_lm(outcome = outcome, keepers = keepers, new = "c", data = data) | |
summary(m1) | |
# Same model, shorter version | |
m1 <- quick_lm(outcome, keepers, "c", data) | |
# Try some other variable | |
m2 <- quick_lm(outcome = outcome, keepers = keepers, new = "d", data = data) | |
summary(m2) | |
# Same model, shorter version | |
m2 <- quick_lm(outcome, keepers, "d", data) | |
# Try a vector of variables | |
m3 <- quick_lm(outcome = outcome, keepers = keepers, new = c("c","d"), data = data) | |
# Same model, shorter version | |
m3 <- quick_lm(outcome, keepers, c("c","d"), data) | |
# If you really want a sparse function, just hard-code in | |
# the variables you want for your outcome and keepers: | |
my_outcome <- "y" | |
my_keepers <- c("a","b") | |
my_data <- data | |
# Hard-coded variable functions | |
quick_lm <- function(outcome = my_outcome, keepers = my_keepers, new = NULL, data = my_data){ | |
keepers <- paste(keepers, collapse = " + ") | |
new <- paste(new, collapse = " + ") | |
rhs <- paste(keepers, new, sep = " + ") | |
model <- paste0(outcome, " ~ ", rhs) | |
fit <- lm(as.formula(model), data = data) | |
} | |
# Same as m1, m2, m3, but less code due to | |
# less flexible function | |
m4 <- quick_lm(new = "c") | |
m5 <- quick_lm(new = "d") | |
m6 <- quick_lm(new = c("c","d")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment