Suppose you have a data frame like this:
dat1 <- data.frame(Loc = rep(c("NY","MA","FL","GA"), each = 1000),
Region = rep(c("a","b","c","d"),each = 1000),
ID = rep(c(1:10), each=200),
var1 = rnorm(1000),
var2=rnorm(1000),
var3=rnorm(1000))
You start off by creating some empty lists to store things you are interested in, something like this:
models <- list()
fitvals <- list()
summaries <- list()
plots <- ()
Now for each combination of var# and ID you want to create a separate model, each named according to the Loc, Reg, ID, and var# combination it belongs to (e.g., the model name for Var1 / ID == 1 who is from NY, which is in Region1 would be something like: NYa1var1mod). Each of these models will be stored in the models list. Similarly, you want to store the fit values, standard errors, and confidence intervals for each model in a data frame with a similar naming convention and store these data frames in the list fitvals. Further, you want to produce a standard ggplot of some kind upon each (say, just a line plot), which has a title that follows the naming convention, and y-axis name that corresponds with the variable being plotted.
Obviously there are tons of ways to go about doing this in a programmatic fashion, I am just curious about the different approaches to tackling this situation and what the most computationally efficient methods would be.