I want to retrieve values of beta for every subgroup formed by the group_by function. But when I run the following code, I get NA values in beta column for all the subgroups. Please help me fixing this issue.
My code is as follows:
fit_lm <- function(df) {
  lr <- lm(num_transactions ~ price , data = df)
  # Filter out groups where the model cannot be fitted
  if (length(lr$coefficients) < 2) {
    return(c(beta = NA, intercept = NA))
  }
  beta <- as.numeric(lr$coefficients[2])
  intercept <- as.numeric(lr$coefficients[1])
  return(c(beta = beta, intercept = intercept))
}
pred_sales_by_col <- machine_info %>%
  group_by(product_name, small_machine, column) %>%
  summarize(model_results = list(fit_lm(cur_data())), .groups = "drop") %>%
  mutate(beta = sapply(model_results, function(x) x[1]),
         intercept = sapply(model_results, function(x) x[2])) %>%
  select(-model_results)
# View the result
View(pred_sales_by_col)
I did get values for intercept but NA for beta in all subgroups.
 
    