Primeiramente carregamos as bibliotecas importantes como tidyverse, here, cluster e plotly, além de um código que carregará os dados do nosso Ator Adam Sandler no qua faremos nossa analise de agrupamentos

Importamos os dados e lemos para a variavel filmes que contem 36 filmes e algumas colunas como, avaliação, nota_audiencia, filme, papel, bilheteria e ano.

import_data("adam_sandler")
## Loading required package: rvest
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:readr':
## 
##     guess_encoding
## Loading required package: janitor
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
filmes = read_imported_data("adam_sandler")
filmes
## # A tibble: 36 x 6
##    avaliacao nota_audiencia filme            papel              bilheteria   ano
##        <int>          <dbl> <chr>            <chr>                   <dbl> <int>
##  1        91             52 Uncut Gems       Howard Ratner (Ch…       50    2019
##  2        62             48 Hotel Transylva… Dracula (Voice),E…      168.   2018
##  3        17             46 Pixels           Sam Brenner (Char…       78.7  2015
##  4        15             64 Blended          Jim (Character),P…       46.3  2014
##  5         8             53 Grown Ups 2      Lenny Feder (Char…      134.   2013
##  6        45             72 Hotel Transylva… Dracula (Voice),E…      168.   2012
##  7        20             51 That's My Boy    Donny (Character)…       36.9  2012
##  8         3             36 Jack and Jill    Jack/Jill (Charac…       74.2  2011
##  9        19             59 Just Go With It  Danny (Character)…      103    2011
## 10        14             41 Zookeeper        Donald the Monkey…       80.4  2011
## # … with 26 more rows

Aqui podemos ver informações sobre as colunas como minimos e maximos da colunas numericas como avaliacao, nota_audiencia, bilheteria e ano

summary(filmes)
##    avaliacao     nota_audiencia     filme              papel          
##  Min.   : 3.00   Min.   :25.00   Length:36          Length:36         
##  1st Qu.:15.00   1st Qu.:48.75   Class :character   Class :character  
##  Median :25.00   Median :59.00   Mode  :character   Mode  :character  
##  Mean   :32.89   Mean   :58.72                                        
##  3rd Qu.:45.00   3rd Qu.:66.75                                        
##  Max.   :91.00   Max.   :85.00                                        
##    bilheteria          ano      
##  Min.   :  2.30   Min.   :1994  
##  1st Qu.: 32.65   1st Qu.:2000  
##  Median : 76.45   Median :2006  
##  Mean   : 79.17   Mean   :2005  
##  3rd Qu.:128.07   3rd Qu.:2011  
##  Max.   :167.90   Max.   :2019

Em uma rápida visualização sobre os dados de bilheteria do ator, vemos que há varios filmes com pouca e alguns com muita bilheteria, será que isso se reflete na avaliação dos mesmos?

filmes %>% 
    ggplot(aes(x = bilheteria)) + 
    geom_density(fill = "green", color = "black") + 
    geom_rug(size = .5) 

Podemos ver que muitos deles estão com baixa avaliação (abaixo de 50) e poucos acima de nota 50, veremos a relação das duas variaveis em um gráfico.

filmes %>% 
    ggplot(aes(x = avaliacao)) + 
    geom_density(fill = "coral", color = "black") + 
    geom_rug(size = .5) 

Ao observar os dados abaixo não é possivel definir agrupamentos de maneira facil, então faremos uso do kmeans para identificar esses grupos:

p = filmes %>% 
    ggplot(aes(x = bilheteria, y = avaliacao, label = filme)) + 
    geom_point(size = 4, color = paleta[1]) + 
    scale_y_log10()

ggplotly(p)

Podemos ver abaixo o resultado para o agrupamento realizado pelo kmeans em 4 grupos, no qual foi feito uma transformação nos dados e aplicado o algoritmo.

dados <- filmes %>%
    select(bilheteria, avaliacao) %>%
    filter(!is.na(bilheteria + avaliacao))
row.names(dados) <- filmes$filme

dados <- scale(dados)

res <- kmeans(dados, 4)

fviz_cluster(res, data = dados)

Podemos ver que o algoritmo agrupou os filmes e podemos classifica-los da seguinte maneira

Abaixo podemos ver que os clusters possuem quantidades de 7 a 11 filmes cada

res
## K-means clustering with 4 clusters of sizes 9, 7, 9, 11
## 
## Cluster means:
##   bilheteria  avaliacao
## 1  0.5489896 -0.7737296
## 2 -0.6509811  1.5902908
## 3  1.1920446  0.3530142
## 4 -1.0102218 -0.6677814
## 
## Clustering vector:
##                            Uncut Gems Hotel Transylvania 3: Summer Vacation 
##                                     2                                     3 
##                                Pixels                               Blended 
##                                     1                                     4 
##                           Grown Ups 2                    Hotel Transylvania 
##                                     1                                     3 
##                         That's My Boy                         Jack and Jill 
##                                     4                                     1 
##                       Just Go With It                             Zookeeper 
##                                     1                                     1 
##       Bucky Larson: Born to Be a Star                             Grown Ups 
##                                     4                                     1 
##                          Funny People         You Don't Mess With the Zohan 
##                                     2                                     3 
##                       Bedtime Stories   I Now Pronounce You Chuck and Larry 
##                                     1                                     1 
##                         Reign Over Me                                 Click 
##                                     2                                     3 
##                      The Longest Yard                        50 First Dates 
##                                     3                                     3 
##                             Spanglish                      Anger Management 
##                                     2                                     3 
##     Adam Sandler's Eight Crazy Nights                             Mr. Deeds 
##                                     4                                     1 
##                         The Hot Chick                      Punch-Drunk Love 
##                                     4                                     2 
##                          Little Nicky                             Big Daddy 
##                                     4                                     3 
##                            Dirty Work                    The Wedding Singer 
##                                     4                                     2 
##                          The Waterboy                           Bulletproof 
##                                     3                                     4 
##                         Happy Gilmore                         Billy Madison 
##                                     2                                     4 
##                            Mixed Nuts                              Airheads 
##                                     4                                     4 
## 
## Within cluster sum of squares by cluster:
## [1] 2.993612 2.628983 2.795071 2.770716
##  (between_SS / total_SS =  84.0 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"