Variable Cualitativa Nominal-Tipo de Combustible

A partir de los datos muestrales, se construyeron tablas de frecuencia y gráficos que permiten calcular probabilidades empíricas, facilitando así la realización de inferencias sobre el uso predominante de combustibles en el periodo 1984–2017.

1 Cargar datos

setwd("C:/Users/Usuario/Documents/Trabajo Estadistica/PROYECTO/")
datos <- read.csv("database.csv", header = TRUE, sep = ",", dec = ".")

Verificamos que rstudio nos lea correctamente los datos:

str(datos)
## 'data.frame':    38113 obs. of  81 variables:
##  $ Vehicle.ID                         : int  26587 27705 26561 27681 27550 28426 27549 28425 27593 28455 ...
##  $ Year                               : int  1984 1984 1984 1984 1984 1984 1984 1984 1984 1984 ...
##  $ Make                               : chr  "Alfa Romeo" "Alfa Romeo" "Alfa Romeo" "Alfa Romeo" ...
##  $ Model                              : chr  "GT V6 2.5" "GT V6 2.5" "Spider Veloce 2000" "Spider Veloce 2000" ...
##  $ Class                              : chr  "Minicompact Cars" "Minicompact Cars" "Two Seaters" "Two Seaters" ...
##  $ Drive                              : chr  "" "" "" "" ...
##  $ Transmission                       : chr  "Manual 5-Speed" "Manual 5-Speed" "Manual 5-Speed" "Manual 5-Speed" ...
##  $ Transmission.Descriptor            : chr  "" "" "" "" ...
##  $ Engine.Index                       : int  9001 9005 9002 9006 1830 1880 1831 1881 1524 1574 ...
##  $ Engine.Descriptor                  : chr  "(FFS)" "(FFS) CA model" "(FFS)" "(FFS) CA model" ...
##  $ Engine.Cylinders                   : int  6 6 4 4 4 4 6 6 6 6 ...
##  $ Engine.Displacement                : num  2.5 2.5 2 2 2.5 2.5 4.2 4.2 4.2 4.2 ...
##  $ Turbocharger                       : logi  NA NA NA NA NA NA ...
##  $ Supercharger                       : chr  "" "" "" "" ...
##  $ Fuel.Type                          : chr  "Regular" "Regular" "Regular" "Regular" ...
##  $ Fuel.Type.1                        : chr  "Regular Gasoline" "Regular Gasoline" "Regular Gasoline" "Regular Gasoline" ...
##  $ Fuel.Type.2                        : chr  "" "" "" "" ...
##  $ City.MPG..FT1.                     : int  17 17 18 18 18 18 13 13 15 15 ...
##  $ Unrounded.City.MPG..FT1.           : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ City.MPG..FT2.                     : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Unrounded.City.MPG..FT2.           : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ City.Gasoline.Consumption..CD.     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ City.Electricity.Consumption       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ City.Utility.Factor                : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Highway.MPG..FT1.                  : int  24 24 25 25 17 17 13 13 20 19 ...
##  $ Unrounded.Highway.MPG..FT1.        : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Highway.MPG..FT2.                  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Unrounded.Highway.MPG..FT2.        : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Highway.Gasoline.Consumption..CD.  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Highway.Electricity.Consumption    : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Highway.Utility.Factor             : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Unadjusted.City.MPG..FT1.          : num  21 21 23 23 22 22 16 16 19 19 ...
##  $ Unadjusted.Highway.MPG..FT1.       : num  34 34 35 35 24 24 18 18 27 26 ...
##  $ Unadjusted.City.MPG..FT2.          : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Unadjusted.Highway.MPG..FT2.       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Combined.MPG..FT1.                 : int  20 20 21 21 17 17 13 13 17 17 ...
##  $ Unrounded.Combined.MPG..FT1.       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Combined.MPG..FT2.                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Unrounded.Combined.MPG..FT2.       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Combined.Electricity.Consumption   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Combined.Gasoline.Consumption..CD. : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Combined.Utility.Factor            : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Annual.Fuel.Cost..FT1.             : int  1750 1750 1650 1650 2050 2050 2700 2700 2050 2050 ...
##  $ Annual.Fuel.Cost..FT2.             : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Gas.Guzzler.Tax                    : chr  "" "" "" "" ...
##  $ Save.or.Spend..5.Year.             : int  -2000 -2000 -1500 -1500 -3500 -3500 -6750 -6750 -3500 -3500 ...
##  $ Annual.Consumption.in.Barrels..FT1.: num  16.5 16.5 15.7 15.7 19.4 ...
##  $ Annual.Consumption.in.Barrels..FT2.: num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Tailpipe.CO2..FT1.                 : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ Tailpipe.CO2.in.Grams.Mile..FT1.   : num  444 444 423 423 523 ...
##  $ Tailpipe.CO2..FT2.                 : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ Tailpipe.CO2.in.Grams.Mile..FT2.   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Fuel.Economy.Score                 : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ GHG.Score                          : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ GHG.Score..Alt.Fuel.               : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ My.MPG.Data                        : chr  "N" "N" "N" "N" ...
##  $ X2D.Passenger.Volume               : int  74 74 0 0 0 0 0 0 0 0 ...
##  $ X2D.Luggage.Volume                 : int  7 7 0 0 0 0 0 0 0 0 ...
##  $ X4D.Passenger.Volume               : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ X4D.Luggage.Volume                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Hatchback.Passenger.Volume         : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Hatchback.Luggage.Volume           : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Start.Stop.Technology              : chr  "" "" "" "" ...
##  $ Alternative.Fuel.Technology        : chr  "" "" "" "" ...
##  $ Electric.Motor                     : chr  "" "" "" "" ...
##  $ Manufacturer.Code                  : chr  "" "" "" "" ...
##  $ Gasoline.Electricity.Blended..CD.  : chr  "False" "False" "False" "False" ...
##  $ Vehicle.Charger                    : chr  "" "" "" "" ...
##  $ Alternate.Charger                  : chr  "" "" "" "" ...
##  $ Hours.to.Charge..120V.             : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Hours.to.Charge..240V.             : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Hours.to.Charge..AC.240V.          : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Composite.City.MPG                 : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Composite.Highway.MPG              : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Composite.Combined.MPG             : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ Range..FT1.                        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ City.Range..FT1.                   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Highway.Range..FT1.                : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Range..FT2.                        : chr  "" "" "" "" ...
##  $ City.Range..FT2.                   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Highway.Range..FT2.                : num  0 0 0 0 0 0 0 0 0 0 ...

1.1 Cargar librerias

library(knitr)
## Warning: package 'knitr' was built under R version 4.4.3
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.4.3

1.2 Extraemos la variable

Tipo_Combustible <- datos$Fuel.Type

2 Tabla de Distribución De Frecuencias

TDFTipoCombustible <- table(Tipo_Combustible)
Tabla <- as.data.frame(TDFTipoCombustible)
colnames(Tabla) <- c("Tipo de Combustible", "Frecuencia Absoluta (ni)")
# Calcular frecuencia relativa
Tabla$`Frecuencia Observada (fo)` <- round(
  Tabla$`Frecuencia Absoluta (ni)` / sum(Tabla$`Frecuencia Absoluta (ni)`) * 100,
  2
)

2.1 Crear fila de totales

fila_total <- data.frame(
  `Tipo de Combustible` = "Total",
  `Frecuencia Absoluta (ni)` = sum(Tabla$`Frecuencia Absoluta (ni)`),
  `Frecuencia Observada (fo)` = 100,
  check.names = FALSE
)
Tabla <- rbind(Tabla, fila_total)

2.2 Unir fila total a la tabla

Tabla <- rbind(Tabla, fila_total)
# Mostrar la tabla formateada
kable(Tabla, format = "html", caption = "Tabla Nº1: Distribución de Frecuencias del Tipo de Combustible") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                full_width = FALSE, position = "center") %>%
  column_spec(1, bold = TRUE)
Tabla Nº1: Distribución de Frecuencias del Tipo de Combustible
Tipo de Combustible Frecuencia Absoluta (ni) Frecuencia Observada (fo)
CNG 60 0.16
Diesel 1014 2.66
Electricity 133 0.35
Gasoline or E85 1223 3.21
Gasoline or natural gas 20 0.05
Gasoline or propane 8 0.02
Midgrade 77 0.20
Premium 10133 26.59
Premium and Electricity 25 0.07
Premium Gas or Electricity 18 0.05
Premium or E85 122 0.32
Regular 25258 66.27
Regular Gas and Electricity 20 0.05
Regular Gas or Electricity 2 0.01
Total 38113 100.00
Total 38113 100.00

3 Gráficas de distribución de frecuencia

3.1 Diagrama de barras ni

Creamos el diagrama de barras con la frecuencia absoluta

# Ajustar márgenes
par(mar = c(8, 4, 3, 2))
# Frecuencia Absoluta
barplot(TDFTipoCombustible,
        main = "Gráfica No.1: \nDistribución Tipo de Combustible",
        xlab = "",
        ylab = "Frecuencia",
        col = "goldenrod1",
        las = 3, 
        cex.names = 0.8,
        cex.axis = 0.9,
        cex.main = 1.2,
        ylim = c(0, max(TDFTipoCombustible) * 1.2),
        border = "black")
mtext("Tipo de Combustible", side = 1, line = 4, cex = 1.1)
abline(h = pretty(range(TDFTipoCombustible), n = 5), col = "gray85", lty = "dotted")

3.2 Diagrama de barras fo

Creamos el diagrama de barras con la frecuencia observada

porcentajes <- TDFTipoCombustible / sum(TDFTipoCombustible) * 100
par(mar = c(8, 4, 3, 2)) 
barplot(porcentajes,
        main = "Gráfica No.2: \nDistribución Porcentual Tipo de Combustible",
        xlab = "",
        ylab = "Porcentaje",
        col = "goldenrod1",
        las = 3, 
        cex.names = 0.8,
        cex.axis = 0.9,
        cex.main = 1.2,
        ylim = c(0, 100),
        border = "black")
mtext("Tipo de Combustible", side = 1, line = 4, cex = 1.1)
abline(h = pretty(range(porcentajes), n = 5), col = "gray85", lty = "dotted")

4 Modelo de probabilidad

Tabla_combustible <- Tabla[Tabla$`Tipo de Combustible` != "Total", ]
P_Combustible <- Tabla_combustible$`Frecuencia Absoluta (ni)` / sum(Tabla_combustible$`Frecuencia Absoluta (ni)`)
nombres_combustible <- Tabla_combustible$`Tipo de Combustible`
barplot(P_Combustible,
        main = "Gráfica Nº3: \nDistribución de Probabilidad de Tipo de Combustible",
        xlab = "Tipo de Combustible",
        ylab = "Probabilidad",
        col = "goldenrod1",
        names.arg = nombres_combustible,
        las = 3,
        cex.names = 0.8)

4.1 Probabilidad

# Calcular probabilidad (en porcentaje)
combustible_objetivo <- "Regular"
probabilidad_regular <- round(
  (Tabla$`Frecuencia Absoluta (ni)`[Tabla$`Tipo de Combustible` == combustible_objetivo] /
     sum(Tabla$`Frecuencia Absoluta (ni)`[Tabla$`Tipo de Combustible` != "Total"])) * 100,
  1
)
print(paste("La probabilidad es de:", probabilidad_regular, "%"))
## [1] "La probabilidad es de: 66.3 %"

5 Conclusión

Como resultado del análisis, se determinó que el tipo de combustible más frecuente es Regular, con una participación del 66.3 %. Esta categoría representa la mayor proporción dentro del conjunto de datos, lo que sugiere una preferencia o disponibilidad predominante en el contexto estudiado.