Below is a reprex where I use the gapminder data to fit a simple model by continent.

I am curious how people could solve the problem of "shinking" coefficients to one or more group level means. I have heard that `brms`

and `lme4`

are good libraries for this kind of analysis. Also, what is the difference between some of these shrinkage algorithms and simply doing some kind of weighted average?

```
library(gapminder)
library(tidyverse)
library(ggrepel)
#> Warning: package 'ggrepel' was built under R version 3.5.2
# simple lifeExp model
lifeExp_model <- function(df) {
lm(lifeExp ~ year, data = df) %>%
broom::tidy() %>%
select(term, estimate)
}
# change factors to char
gapminder$country <- as.character(gapminder$country)
gapminder$continent <- as.character(gapminder$continent)
# continent model (grouped)
continent_fit <- gapminder %>%
group_by(continent) %>%
nest() %>%
mutate(model = map(data, lifeExp_model)) %>%
unnest(model) %>%
rename(continent_term = term, continent_estimate = estimate)
# pooled
pooled_fit <- gapminder %>%
nest() %>%
mutate(model = map(data, lifeExp_model)) %>%
unnest(model) %>%
rename(pooled_term = term, pooled_estimate = estimate)
# number of observation per continent
n_obs <- gapminder %>%
group_by(continent) %>%
summarise(n_country = n_distinct(country))
# gouped and pooled fit
gnp_fit <- continent_fit %>%
inner_join(n_obs) %>%
inner_join(pooled_fit, by = c( "continent_term" = "pooled_term"))
#> Joining, by = "continent"
gnp_fit %>%
filter(continent_term == "year") %>%
ggplot(aes(x = n_country, y = continent_estimate )) +
geom_hline(aes(yintercept = pooled_estimate)) +
geom_point() +
ggrepel::geom_label_repel(aes(label = continent))+
theme_classic()
```

^{Created on 2019-02-02 by the reprex package (v0.2.1)}