running function on different dataframe and with different outcome

hi below is my code. I am wondering is there a way to shorten it to be more efficient? create_model_function is a function I created. my concern here is just this is too messy and too long and maybe can become more generalizable. thank u so much!

# happiness as outcome for models. use regular and log rate
dragon_model_happiness  <- c(create_model_function(dragons_only, "happiness_rate"),
                       create_model_function(dragons_only,  "log_happiness_rate"))
                       
all_animals_models_happiness <- c(create_model_function(all_animals,"happiness_rate"),
                       create_model_function(all_animals,"log_happiness_rate"))

# agility as outcome for models. use regular and log rate
dragon_model_agility <- c(create_model_function(dragons_only, "agility_rate"),
                       create_model_function(dragons_only, "log_agility_rate"))
                       
all_animals_models_agility  <- c(create_model_function(all_animals, "agility_rate"),
                       create_model_function(all_animals, "log_agility_rate"))

See the FAQ: How to do a minimal reproducible example reprex for beginners to include what's needed for a testable answer.

In outline, long variable names are useful only when dealing with a multitude of objects. In a short script with a handful, use short names with explanatory comments. Refactor the equation to derive the log version within the function, rather than the data frame. This cuts down on the number of parameters required in the function.

This is only a sketch, not tested.

# shorten names

# data frames
dragons
others

cms <- function(x) do_something(x)

# if the log_variable in the data frame is 
# just the log of the variable, do it in
# the function: cms(x) where cms() will
# run a model on x and log(x)

happy <- "happy"
agililty <- "agility"

# these return a list with the two models
# not checked, for lack of data
dmh <- mapply(happy,cms)
dma <- mapply(agility,cms)
amh <- mapply(happy,cms)
ama <- mapply(agility,cms)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.