Aula_7

Correlação

É a medida de associação linear entre duas variáveis númericas.

  • Intervalo:

-1 r 1

r > 0 -> relação positiva (maior)

r < 0 -> relação negativa (menor)

r = 0 -> sem relação linear

  • Intensidade

0 a 0.3 - fraca

0.3 a 0.6 - moderada

0.6 a 0.9 - forte

> 0.9 - muito forte

Funciona do mesmo modo para numeros negativos.

Pacotes

library(tidyverse)
Warning: pacote 'dplyr' foi compilado no R versão 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(skimr)
Warning: pacote 'skimr' foi compilado no R versão 4.5.2
library(gt)
Warning: pacote 'gt' foi compilado no R versão 4.5.2
library(readr)
library(ggthemes)

Dados

churn <- read_csv("churn_clean.csv")
Rows: 7043 Columns: 21
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (18): customer_id, gender, senior_citizen, partner, dependents, phone_se...
dbl  (3): tenure, monthly_charges, total_charges

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(churn)

Correlação

Correlação simples

Função cor(), seleciona as duas variaveis e arredonda com round()

cor(churn$tenure, churn$monthly_charges) %>% 
  round(2)
[1] 0.25
  • A correlação entre tempo de uso de serviço em meses e valor pago mensalmente é fraca.

    (r = 0,25)

  • Grafico

churn %>% 
  ggplot(aes(x = tenure, y = monthly_charges)) +
  geom_point()

Outras maneiras de visualizar correlações

  • Selecionando as variáveis numéricas
library(corrplot)
corrplot 0.95 loaded
churn_num <- churn %>% 
  select(where(is.numeric))
view(churn_num)
  • Matriz de correlação
matriz_cor <- cor(churn_num, use = "complete.obs")
  • Matriz de correlação - gráfica
corrplot(
  matriz_cor,
  method = "color",
  type = "upper",
  addCoef.col = "black",
  tl.col = "black",
  tl.srt = 45
)

  • Grafico de dispersão das colunas mais correlacionadas
churn %>% 
  ggplot(aes(x = tenure, y = total_charges)) +
  geom_point(alpha = 0.4, color = "lightblue") +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'