I currently have a difference in difference model and would like to measure different metrics for the same model in an efficient manner.
For example, I have a data frame with columns for Miles Driven, Hours Worked, State, Group, Time.
Currently, have code where I copy and paste the model for each metric:
# Create DID models
model_miles <- lm(df$miles_driven ~ df$Group 
                        + df$Time 
                        + df$Group * df$Time, data = df)
model_hours <- lm(df$hours_worked ~ df$Group 
           + df$Time 
           + df$Group * df$Time, data = df)
# Select results using clustered standard errors. The purpose of this is to 
# avoid making distributional assumptions about the errors in the models. 
results_miles <- clubSandwich::coef_test(model_miles, 
                                            vcov = "CR2", 
                                            cluster = df$state, 
                                            test = "Satterthwaite")
results_hours <- clubSandwich::coef_test(model_hours, 
                               vcov = "CR2", 
                               cluster = df$state, 
                               test = "Satterthwaite")
results <- data.table::rbindlist(list(results_miles, results_hours))
View(results)
I would like to somehow create a list of my metric names, and loop over this list using a user defined function, in order to make this process faster and more automated, but I haven't been able to get this to work correctly:
#list of metrics
metrics <- c("miles_driven", "hours_worked")
udf <- function(metric, dataframe){
     # Create DID model
     model <- lm(dataframe$metric ~ df$Group 
                + dataframe$Time 
                + dataframe$Group * df$Time, data = dataframe)
     # Select results using clustered standard errors. The purpose of this 
     is to 
     # avoid making distributional assumptions about the errors in the 
     models. 
     results_miles <- clubSandwich::coef_test(model_miles, 
                                       vcov = "CR2", 
                                       cluster = dataframe$state, 
                                       test = "Satterthwaite")[4,]
     View(results)
}
lapply(metrics, udf)
Any insight would be appreciated. Thanks!
 
    