I have a list of lists like out. In each list I have a dataframe (with the same structure, i.e. same dimensions and variable names (id/period/pred_dif):
id <- c(1,2,3,4,5,1,2,3,4,5)
period <- c(01,09,12,01,08, 02,08,12,11,12)
pred_dif <- c(0.5,0.1,0.15,0.23,0.75,0.6,0.49,0.81,0.37,0.14)
list_1 <- data.frame(id, period, pred_dif)
pred_dif <- c(0.45,0.18,0.35,0.63,0.25,0.63,0.29,0.11,0.17,0.24)
list_2 <- data.frame(id, period, pred_dif)
pred_dif <- c(0.58,0.13,0.55,0.13,0.76,0.3,0.29,0.81,0.27,0.04)
list_3 <- data.frame(id, period, pred_dif)
pred_dif <- c(0.3,0.61,0.18,0.29,0.85,0.76,0.56,0.91,0.48,0.91)
list_4 <- data.frame(id, period, pred_dif)
out <- list(list_1, list_2, list_3, list_4)
I want to:
- Merge list out with dataframe df of same structure
pred_second <- c(0.4,0.71,0.28,0.39,0.95,0.86,0.66,0.81,0.58,0.81)
df <- data.frame(id, period, pred_second)
I would proceed (in a dplyr environment) as follows:
out <- merge(out, df, by = c("id", "period"), all.x = T)
- Create a list containing an OLS (lm) regression capturing the effect of variable "period" on "pred_dif". In a dataframe environment would be something like:
ols <- summary(lm(formula = pred_dif ~ as.factor(period) - 1, data = out))
- Create a list or dataframe (preferred) registering the estimates and standard errors of the regressions of point 2 (it is ok if points 2/3 happen together)
Any idea on how to solve this in an iterative and fast way for all lists?