Suavização

Author

Ricardo Accioly

Published

August 20, 2024

Bibliotecas

Suavização

(locally estimated scatterplot smoothing/Local Polynomial Regression Fitting)

gap_07 <- filter(gapminder, year == 2007)
ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth()
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Fazendo o suavizador mais nervoso

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth(span = 0.2)

Fazendo o suavizador menos nervoso

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth(span = 0.9)

Removendo intervalos de confiança

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth(se = FALSE)

Usando IC de 90%

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth(level = 0.90)

Usando um modelo linear ao invés do loess

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth(method = "lm")

Usando basic splines para melhorar o ajuste

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ splines::bs(x, df = 3))

Usando o gam (general addtive models) com regressão spline

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_smooth(method = "gam", formula = y ~ s(x))

Começando a construir um gráfico do tipo facet com suavizações

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

Dividindo por continente

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  facet_wrap(~ continent)

Adicionando suavizadores

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  facet_wrap(~ continent) +
  geom_smooth()

Colorindo por continente

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  facet_wrap(~ continent) +
  geom_smooth()

Colorindo somente a curva

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  facet_wrap(~ continent) +
  geom_smooth(aes(color = continent))