Help I would like to request: I am trying to automate a sequence of tasks in a loop. Basically it is running a series of regressions, storing the results and exporting (writing) it into a csv file.
Issue: I have tried many times to run a “For” loop. I couldn’t figure out how to create and name new variables for each regression based on the current_date column
Description of data: Link to dataset It is a dataset of US states in a tabular format (somewhat similar to Tidy Data). Each Row or observation refers to a specific state at a specific date. Each column refers to an attribute of the state Task: Main problem: Automate the process of running a series of regressions, storing the results by date and exporting (writing) it into a csv file.
Sequence of tasks:
- Run the regression
- Use broom to tidy the regression results (coefficients, std errors and p-vale)
- Store the results by a specific date
- Repeat regression for another date
- Use broom to tidy the regression results (coefficients and the model)
- Store the results of the co-efficients (coeff, std error, p value) by a specific date
- Store the results of the regression models (R-square etc.) by a specific date
….. Finally, export all the results in a CSV file
library(dplyr)
library(broom)
for(i in 1:length(current_date)){
lmtest <- lm( outcome_var [[i]] ~ cumulative_var1_at_current_date + key_cat_var1, data = testdata)
coeff_results<-tidy(lmtest)
model_results<- glance(lmtest)
write.csv(coeff_results, “C:\\Users\\....combined_results.csv”)
}