You can use broom::augment_columns instead of augment . The two arguments to the function that we need are x - the "model" - and data - the "source data to which the columns should be added."
library(tidyverse) library(broom) split(data, data$dataset) %>% map(., ~lm(formula = observation ~ estimate, data = .)) %>% map2(.x = ., .y = split(data, f = data$dataset), .f = ~augment_columns(x = .x, data = .y)) %>% bind_rows() %>% select(-.rownames)
The idea is to split data for the data set, fit the model to each component of the list, and then use map2 to iterate over the models and (complete) data used to build the model, i.e. split(data, f = data$dataset) in parallel.
augment_columns adds the .rownames column, so select in the last row.
change
The same solution, but hopefully easier to read.
data_split <- split(data, data$dataset) models <- map(data_split, ~lm(formula = observation ~ estimate, data = .)) map2(.x = models, .y = data_split, .f = ~augment_columns(x = .x, data = .y)) %>% bind_rows() %>% select(-.rownames)
The first code block as a function that has four arguments: df , split_var , dependend_var and explanatory_var .
augment_df <- function(df, split_var, dependend_var, explanatory_var) { require(tidyverse) require(broom) split(df, df[split_var]) %>% map(., ~lm(formula = as.formula(paste0(dependend_var, " ~ ", explanatory_var)), data = .)) %>% map2(.x = ., .y = split(df, df[split_var]), .f = ~augment_columns(x = .x, data = .y)) %>% bind_rows() %>% select(-.rownames) } augment_df(df = data, split_var = "dataset", dependend_var = "observation", explanatory_var = "estimate")