Caso 1

knitr::opts_chunk$set(echo = TRUE)
library(readr)
beer2 <- read_csv("beer2.csv")
## Rows: 1586614 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (5): marca, nacionalidad, nombre_cerveza, tipo, origen
## dbl (10): id, precio, alcohol, carbohidratos, calorias, calificacion, aroma,...
## 
## ℹ 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.

# Cualitativas

nombre_cerveza <- read.csv("beer2.csv")

table(nombre_cerveza$tipo)
## 
## baja en calorías / sin alcohol        cerveza normal y helada 
##                          22143                         839088 
##                clara artesanal                lager artesanal 
##                         483464                          22725 
##                lager importada 
##                         219194
table(nombre_cerveza$origen)
## 
## importada  nacional 
##   1412449    174165
table(nombre_cerveza$nacionalidad)
## 
##        Alemania         Austria         Bélgica          Canadá  Estados Unidos 
##          121684          201941          188677          159508          174165 
##         Francia      Inglaterra         Irlanda           Japón República Checa 
##          181574          162644          120742          155273          120406
# TABLA PARA MARCA (Top 20)

library(knitr)

# Top 20 marcas, resto como "Otros"
top20_nombres <- names(head(sort(table(nombre_cerveza$marca), 
                                  decreasing = TRUE), 20))

nombre_cerveza$marca_reducida <- ifelse(
  nombre_cerveza$marca %in% top20_nombres,
  nombre_cerveza$marca,
  "Otros"
)

# Convertir a factor ORDENADO de mayor a menor
nombre_cerveza$marca_reducida <- factor(
  nombre_cerveza$marca_reducida,
  levels = c(top20_nombres, "Otros")
)

# Tabla con summarytools igual que tipo y origen
summarytools::freq(nombre_cerveza$marca_reducida, cumul = FALSE)
## Frequencies  
## nombre_cerveza$marca_reducida  
## Type: Factor  
## 
##                                               Freq   % Valid   % Total
## ---------------------------------------- --------- --------- ---------
##       Boston Beer Company (Samuel Adams)     39444      2.49      2.49
##                     Dogfish Head Brewery     33839      2.13      2.13
##                        Stone Brewing Co.     33066      2.08      2.08
##                Sierra Nevada Brewing Co.     28751      1.81      1.81
##                     Bell's Brewery, Inc.     25191      1.59      1.59
##                               Rogue Ales     24083      1.52      1.52
##                 Founders Brewing Company     20004      1.26      1.26
##                  Victory Brewing Company     19479      1.23      1.23
##                Lagunitas Brewing Company     16837      1.06      1.06
##                    Avery Brewing Company     16107      1.02      1.02
##            Southern Tier Brewing Company     15868      1.00      1.00
##                           Anheuser-Busch     15815      1.00      1.00
##             Great Divide Brewing Company     14935      0.94      0.94
##                    Goose Island Beer Co.     14534      0.92      0.92
##       Three Floyds Brewing Co. & Brewpub     14039      0.88      0.88
##                                 Unibroue     13921      0.88      0.88
##                         Brooklyn Brewery     13902      0.88      0.88
##                      New Belgium Brewing     13410      0.85      0.85
##                  Weyerbacher Brewing Co.     12248      0.77      0.77
##                   Tröegs Brewing Company     11842      0.75      0.75
##                                    Otros   1189299     74.96     74.96
##                                     <NA>         0                0.00
##                                    Total   1586614    100.00    100.00
tipo_factor <- as.factor(nombre_cerveza$tipo)
summarytools::freq(tipo_factor, cumul = FALSE)
## Frequencies  
## tipo_factor  
## Type: Factor  
## 
##                                           Freq   % Valid   % Total
## ------------------------------------ --------- --------- ---------
##       baja en calorías / sin alcohol     22143      1.40      1.40
##              cerveza normal y helada    839088     52.89     52.89
##                      clara artesanal    483464     30.47     30.47
##                      lager artesanal     22725      1.43      1.43
##                      lager importada    219194     13.82     13.82
##                                 <NA>         0                0.00
##                                Total   1586614    100.00    100.00
origen_factor <- as.factor(nombre_cerveza$origen)
summarytools::freq(origen_factor, cumul = FALSE)
## Frequencies  
## origen_factor  
## Type: Factor  
## 
##                      Freq   % Valid   % Total
## --------------- --------- --------- ---------
##       importada   1412449     89.02     89.02
##        nacional    174165     10.98     10.98
##            <NA>         0                0.00
##           Total   1586614    100.00    100.00
origen_factor <- as.factor(nombre_cerveza$nacionalidad)
summarytools::freq(origen_factor, cumul = FALSE)
## Frequencies  
## origen_factor  
## Type: Factor  
## 
##                            Freq   % Valid   % Total
## --------------------- --------- --------- ---------
##              Alemania    121684      7.67      7.67
##               Austria    201941     12.73     12.73
##               Bélgica    188677     11.89     11.89
##                Canadá    159508     10.05     10.05
##        Estados Unidos    174165     10.98     10.98
##               Francia    181574     11.44     11.44
##            Inglaterra    162644     10.25     10.25
##               Irlanda    120742      7.61      7.61
##                 Japón    155273      9.79      9.79
##       República Checa    120406      7.59      7.59
##                  <NA>         0                0.00
##                 Total   1586614    100.00    100.00
#install.packages("flextable")
library(flextable)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

##Barras tipo

barplot(sort(table(nombre_cerveza$tipo), decreasing = TRUE),
        las = 2, col = "#3498db",
        main = "Reseñas por tipo de cerveza",
        ylab = "Frecuencia")

##Barras origen

barplot(table(nombre_cerveza$origen),
        col = c("#8e44ad", "#16a085"),
        main = "Reseñas por origen",
        ylab = "Frecuencia")

##Barras nacionalidad

barplot(sort(table(nombre_cerveza$nacionalidad), decreasing = TRUE),
        las = 2, col = "#e67e22",
        main = "Reseñas por nacionalidad",
        ylab = "Frecuencia")

##Barras marca top 20

top20_nombres <- names(head(sort(table(nombre_cerveza$marca),
                                  decreasing = TRUE), 20))

nombre_cerveza$marca_reducida <- ifelse(
  nombre_cerveza$marca %in% top20_nombres,
  as.character(nombre_cerveza$marca),
  "Otros"
)

nombre_cerveza$marca_reducida <- factor(
  nombre_cerveza$marca_reducida,
  levels = c(top20_nombres, "Otros")
)

barplot(sort(table(nombre_cerveza$marca_reducida), decreasing = TRUE),
        las = 2, cex.names = 0.7, col = "#1abc9c",
        main = "Reseñas por marca (top 20 + Otros)",
        ylab = "Frecuencia")

##Barra de calificacion

barplot(table(nombre_cerveza$calificacion),
        col = "#f39c12",
        main = "Distribución de la calificación general",
        xlab = "Calificación", ylab = "Frecuencia")

##Barra de aroma

barplot(table(nombre_cerveza$aroma),
        col = "#9b59b6",
        main = "Distribución del aroma",
        xlab = "Aroma", ylab = "Frecuencia")

## Barra de aparencia

barplot(table(nombre_cerveza$apariencia),
        col = "#2ecc71",
        main = "Distribución de la apariencia",
        xlab = "Apariencia", ylab = "Frecuencia")

## Barra de paladar

barplot(table(nombre_cerveza$paladar),
        col = "#e74c3c",
        main = "Distribución del paladar",
        xlab = "Paladar", ylab = "Frecuencia")

## Barra de sabor

barplot(table(nombre_cerveza$sabor),
        col = "#3498db",
        main = "Distribución del sabor",
        xlab = "Sabor", ylab = "Frecuencia")

# VARIABLES CUNTITATIVAS

## INDICADORES DE CENTRO

summarytools::descr(nombre_cerveza$precio)
## Descriptive Statistics  
## nombre_cerveza$precio  
## N: 1586614  
## 
##                         precio
## ----------------- ------------
##              Mean         5.00
##           Std.Dev         0.89
##               Min         2.37
##                Q1         4.36
##            Median         5.00
##                Q3         5.64
##               Max         7.72
##               MAD         0.95
##               IQR         1.28
##                CV         0.18
##          Skewness         0.00
##       SE.Skewness         0.00
##          Kurtosis        -0.50
##           N.Valid   1586614.00
##                 N   1586614.00
##         Pct.Valid       100.00
summarytools::descr(nombre_cerveza$alcohol)
## Descriptive Statistics  
## nombre_cerveza$alcohol  
## N: 1586614  
## 
##                        alcohol
## ----------------- ------------
##              Mean        18.71
##           Std.Dev         5.46
##               Min         0.03
##                Q1        14.28
##            Median        17.67
##                Q3        22.63
##               Max        59.45
##               MAD         5.56
##               IQR         8.35
##                CV         0.29
##          Skewness         0.96
##       SE.Skewness         0.00
##          Kurtosis         1.31
##           N.Valid   1586614.00
##                 N   1586614.00
##         Pct.Valid       100.00
summarytools::descr(nombre_cerveza$carbohidratos)
## Descriptive Statistics  
## nombre_cerveza$carbohidratos  
## N: 1586614  
## 
##                     carbohidratos
## ----------------- ---------------
##              Mean           14.05
##           Std.Dev            2.38
##               Min            3.50
##                Q1           12.40
##            Median           13.80
##                Q3           15.60
##               Max           30.20
##               MAD            2.37
##               IQR            3.20
##                CV            0.17
##          Skewness            0.46
##       SE.Skewness            0.00
##          Kurtosis            0.87
##           N.Valid      1586614.00
##                 N      1586614.00
##         Pct.Valid          100.00
summarytools::descr(nombre_cerveza$calorias)
## Descriptive Statistics  
## nombre_cerveza$calorias  
## N: 1586614  
## 
##                       calorias
## ----------------- ------------
##              Mean       193.17
##           Std.Dev        46.57
##               Min        21.00
##                Q1       157.00
##            Median       182.00
##                Q3       224.00
##               Max       541.00
##               MAD        43.00
##               IQR        67.00
##                CV         0.24
##          Skewness         0.92
##       SE.Skewness         0.00
##          Kurtosis         1.32
##           N.Valid   1586614.00
##                 N   1586614.00
##         Pct.Valid       100.00
boxplot(nombre_cerveza$precio, las=1, horizontal = TRUE)

boxplot(nombre_cerveza$alcohol, las=1, horizontal = TRUE)

boxplot(nombre_cerveza$carbohidratos, las=1, horizontal = TRUE)

boxplot(nombre_cerveza$calorias, las=1, horizontal = TRUE)

### MEDIA TRUNCADA

mean(nombre_cerveza$precio, na.rm = TRUE, trim = 0.10)
## [1] 5.000016
mean(nombre_cerveza$alcohol, na.rm = TRUE, trim = 0.10)
## [1] 18.20988
mean(nombre_cerveza$carbohidratos, na.rm = TRUE, trim = 0.10)
## [1] 13.9487
mean(nombre_cerveza$calorias, na.rm = TRUE, trim = 0.10)
## [1] 189.1044

### RANGO MEDIO

(max(nombre_cerveza$precio,na.rm = TRUE)+min(nombre_cerveza$precio,na.rm = TRUE))/2
## [1] 5.045
(max(nombre_cerveza$alcohol,na.rm = TRUE)+min(nombre_cerveza$alcohol,na.rm = TRUE))/2
## [1] 29.74
(max(nombre_cerveza$carbohidratos,na.rm = TRUE)+min(nombre_cerveza$carbohidratos,na.rm = TRUE))/2
## [1] 16.85
(max(nombre_cerveza$calorias,na.rm = TRUE)+min(nombre_cerveza$calorias,na.rm = TRUE))/2
## [1] 281

##DISPERCION:

###RANGO

max(nombre_cerveza$precio)-min(nombre_cerveza$precio)
## [1] 5.35
max(nombre_cerveza$alcohol)-min(nombre_cerveza$alcohol)
## [1] 59.42
max(nombre_cerveza$carbohidratos)-min(nombre_cerveza$carbohidratos)
## [1] 26.7
max(nombre_cerveza$calorias)-min(nombre_cerveza$calorias)
## [1] 520

###VARIANZA

var(nombre_cerveza$precio)
## [1] 0.7839686
var(nombre_cerveza$alcohol)
## [1] 29.7684
var(nombre_cerveza$carbohidratos)
## [1] 5.644963
var(nombre_cerveza$calorias)
## [1] 2168.646

#precio ##Histograma de precio

hist(nombre_cerveza$precio,
     main = "Distribución del precio",
     xlab = "Precio", col = "#c0392b", border = "white")

##Boxplot: precio según tipo

boxplot(precio ~ tipo, data = nombre_cerveza,
        las = 2, col = "#c0392b",
        main = "Precio según tipo de cerveza",
        xlab = "", ylab = "Precio")

##Boxplot: precio según origen

boxplot(precio ~ origen, data = nombre_cerveza,
        col = c("#8e44ad", "#16a085"),
        main = "Precio según origen",
        xlab = "Origen", ylab = "Precio")

##Densidad de precio

plot(density(nombre_cerveza$precio, na.rm = TRUE),
     main = "density(x = nombre_cerveza$precio, na.rm = TRUE)",
     col = "#c0392b", lwd = 2)

##Histograma de alcohol

hist(nombre_cerveza$alcohol,
     main = "Distribución del alcohol",
     xlab = "Alcohol (g)", col = "#d68910", border = "white")

##Boxplot: Alcohol segun tipo

boxplot(alcohol ~ tipo, data = nombre_cerveza,
        las = 2, col = "#d68910",
        main = "Alcohol según tipo de cerveza",
        xlab = "", ylab = "Alcohol (g)")

##boxplo: Alcohol segun origen

boxplot(alcohol ~ origen, data = nombre_cerveza,
        col = c("#8e44ad", "#16a085"),
        main = "Alcohol según origen",
        xlab = "Origen", ylab = "Alcohol (g)")

##Densidad de Alcohol

plot(density(nombre_cerveza$alcohol, na.rm = TRUE),
     main = "density(x = nombre_cerveza$alcohol, na.rm = TRUE)",
     col = "#d68910", lwd = 2)

##histograma Carbohidratos

hist(nombre_cerveza$carbohidratos,
     main = "Distribución de carbohidratos",
     xlab = "Carbohidratos (g)", col = "#27ae60", border = "white")

## Boxplot: Carbohidratos segun tipo

boxplot(carbohidratos ~ tipo, data = nombre_cerveza,
        las = 2, col = "#27ae60",
        main = "Carbohidratos según tipo de cerveza",
        xlab = "", ylab = "Carbohidratos (g)")

##Boxplot: carbohidratos segun origen

boxplot(carbohidratos ~ origen, data = nombre_cerveza,
        col = c("#8e44ad", "#16a085"),
        main = "Carbohidratos según origen",
        xlab = "Origen", ylab = "Carbohidratos (g)")

##Densidad carbohidratos

plot(density(nombre_cerveza$carbohidratos, na.rm = TRUE),
     main = "density(x = nombre_cerveza$carbohidratos, na.rm = TRUE)",
     col = "#27ae60", lwd = 2)

##Histograma Calorias

hist(nombre_cerveza$calorias,
     main = "Distribución de calorías",
     xlab = "Calorías", col = "#2980b9", border = "white")

## Boxplot: Calorias segun tipo

boxplot(calorias ~ tipo, data = nombre_cerveza,
        las = 2, col = "#2980b9",
        main = "Calorías según tipo de cerveza",
        xlab = "", ylab = "Calorías")

##Boxplot: Calorias segun origen

boxplot(calorias ~ origen, data = nombre_cerveza,
        col = c("#8e44ad", "#16a085"),
        main = "Calorías según origen",
        xlab = "Origen", ylab = "Calorías")

##Densidad calorias

plot(density(nombre_cerveza$calorias, na.rm = TRUE),
     main = "density(x = nombre_cerveza$calorias, na.rm = TRUE)",
     col = "#2980b9", lwd = 2)

#comparaciones de Tipo y Origen

## Densidad presupuestada por origen

d_nac <- density(nombre_cerveza$precio[nombre_cerveza$origen == "nacional"], na.rm = TRUE)
d_imp <- density(nombre_cerveza$precio[nombre_cerveza$origen == "importada"], na.rm = TRUE)
plot(d_nac, col = "#16a085", lwd = 2, main = "Densidad del precio según origen",
     xlab = "Precio", ylim = range(c(d_nac$y, d_imp$y)))
lines(d_imp, col = "#8e44ad", lwd = 2)
legend("topright", legend = c("Nacional", "Importada"), col = c("#16a085", "#8e44ad"), lwd = 2, bty = "n")

##Medias por tipo con barra de +/- 1 desviación estándar

medias <- tapply(nombre_cerveza$precio, nombre_cerveza$tipo, mean, na.rm = TRUE)
sds    <- tapply(nombre_cerveza$precio, nombre_cerveza$tipo, sd, na.rm = TRUE)
posiciones <- barplot(medias, las = 2, col = "#3498db",
                       main = "Precio promedio por tipo (+/- 1 DE)", ylab = "Precio",
                       ylim = c(0, max(medias + sds) * 1.1))
arrows(posiciones, medias - sds, posiciones, medias + sds, angle = 90, code = 3, length = 0.05)

##Dispersión alcohol vs. calorías, coloreada por origen

set.seed(123)
muestra <- nombre_cerveza[sample(nrow(nombre_cerveza), 20000), ]
colores <- ifelse(muestra$origen == "nacional", "#16a085", "#8e44ad")
plot(muestra$alcohol, muestra$calorias, col = colores, pch = 16, cex = 0.5,
     main = "Alcohol vs. calorías según origen", xlab = "Alcohol (g)", ylab = "Calorías")
legend("topleft", legend = c("Nacional", "Importada"), col = c("#16a085", "#8e44ad"), pch = 16, bty = "n")

Interpretación: las diferencias por tipo son notorias: la cerveza normal y helada tiene el mayor alcohol (20,1 g) y calorías (204 kcal), mientras que la baja en calorías/sin alcohol tiene casi la mitad de ambas. El precio casi no varía entre tipos, coherente con ser una variable simulada. Por origen, las diferencias son mínimas (alcohol y calorías prácticamente iguales entre nacional e importada), por lo que el tipo explica mejor las diferencias que el origen. Al ser un análisis descriptivo, estas diferencias indican asociación, no causalidad.