Hello, in SAS there is a very efficient operator, the BY statement.
proc reg data=modeltable; by panel_id;
model height = weight age;
run;
This runs a unique model by panel_id, estimates regression coefficients by panel, independent model.
What I want to do is run a model by panel ID. May have 1000 panel ID's. Gather all the statistics by panel. Can't seem to find an equivalent in tidymodels? Not just LM, but any model in tidymodels.
I'm not sure you need tidymodels, just tidyverse. Check out this example on StackOverflow: https://stackoverflow.com/a/50005191 Showing reprex below but it's just running code from that post:
library(tidyverse)
d <- data.frame(state=rep(c('NY', 'CA'), c(10, 10)),
year=rep(1:10, 2),
response=c(rnorm(10), rnorm(10)))
fitted_models = d %>%
group_by(state) %>%
nest() %>%
mutate(model = map(data, ~lm(response ~ year, data = .)))
fitted_models
#> # A tibble: 2 x 3
#> # Groups: state [2]
#> state data model
#> <fct> <list<df[,2]>> <list>
#> 1 NY [10 x 2] <lm>
#> 2 CA [10 x 2] <lm>
fitted_models$model
#> [[1]]
#>
#> Call:
#> lm(formula = response ~ year, data = .)
#>
#> Coefficients:
#> (Intercept) year
#> -0.42980 0.07558
#>
#>
#> [[2]]
#>
#> Call:
#> lm(formula = response ~ year, data = .)
#>
#> Coefficients:
#> (Intercept) year
#> 0.07846 0.06735