setwd("~/UCE/III/estadistica")
#Carga de Datos
datos <- read.csv("Conjunto_Datos_Minerales.csv", header = TRUE, sep = ";", 
                  dec = ".")


UltimoAnio <- na.omit(datos$yr_lst_prd)

# Paso 1: Preparación de datos
UltimoAnio <- datos$yr_lst_prd
TDFUltimoAnio <- table(UltimoAnio)
TablaUltimoAnio <- as.data.frame(TDFUltimoAnio)

# Paso 2: Frecuencia relativa (porcentaje)
hi <- (TablaUltimoAnio$Freq / sum(TablaUltimoAnio$Freq)) * 100
TablaUltimoAnio <- data.frame(TablaUltimoAnio, hi = round(hi, 2))

# Paso 3: Frecuencia acumulada ascendente y descendente
Niasc <- cumsum(TablaUltimoAnio$Freq)
Hiasc <- cumsum(TablaUltimoAnio$hi)
Nidsc <- rev(cumsum(rev(TablaUltimoAnio$Freq)))
Hidsc <- rev(cumsum(rev(TablaUltimoAnio$hi)))

# Paso 4: Tabla final
TablaUltimoAnioFinal <- data.frame(
  Año = TablaUltimoAnio$UltimoAnio,
  Frecuencia = TablaUltimoAnio$Freq,
  Porcentaje = TablaUltimoAnio$hi,
  Niasc,
  Hiasc = round(Hiasc, 2),
  Nidsc,
  Hidsc = round(Hidsc, 2)
)

# Mostrar tabla final
print(TablaUltimoAnioFinal)
##      Año Frecuencia Porcentaje Niasc  Hiasc Nidsc  Hidsc
## 1   1589          1       0.02     1   0.02  5483 100.03
## 2   1760          2       0.04     3   0.06  5482 100.01
## 3   1774          1       0.02     4   0.08  5480  99.97
## 4   1780          1       0.02     5   0.10  5479  99.95
## 5   1800          2       0.04     7   0.14  5478  99.93
## 6   1810          1       0.02     8   0.16  5476  99.89
## 7   1815          4       0.07    12   0.23  5475  99.87
## 8   1820          1       0.02    13   0.25  5471  99.80
## 9   1823          1       0.02    14   0.27  5470  99.78
## 10  1835          2       0.04    16   0.31  5469  99.76
## 11  1840          1       0.02    17   0.33  5467  99.72
## 12  1845          9       0.16    26   0.49  5466  99.70
## 13  1846          2       0.04    28   0.53  5457  99.54
## 14  1847          2       0.04    30   0.57  5455  99.50
## 15  1848          2       0.04    32   0.61  5453  99.46
## 16  1849          1       0.02    33   0.63  5451  99.42
## 17  1850         18       0.33    51   0.96  5450  99.40
## 18  1851          1       0.02    52   0.98  5432  99.07
## 19  1852          2       0.04    54   1.02  5431  99.05
## 20  1853          1       0.02    55   1.04  5429  99.01
## 21  1854          4       0.07    59   1.11  5428  98.99
## 22  1855         10       0.18    69   1.29  5424  98.92
## 23  1856          5       0.09    74   1.38  5414  98.74
## 24  1857          5       0.09    79   1.47  5409  98.65
## 25  1858          3       0.05    82   1.52  5404  98.56
## 26  1859          1       0.02    83   1.54  5401  98.51
## 27  1860          6       0.11    89   1.65  5400  98.49
## 28  1861          1       0.02    90   1.67  5394  98.38
## 29  1862          1       0.02    91   1.69  5393  98.36
## 30  1863          1       0.02    92   1.71  5392  98.34
## 31  1864          4       0.07    96   1.78  5391  98.32
## 32  1865         15       0.27   111   2.05  5387  98.25
## 33  1866          4       0.07   115   2.12  5372  97.98
## 34  1867          4       0.07   119   2.19  5368  97.91
## 35  1868         20       0.36   139   2.55  5364  97.84
## 36  1869          4       0.07   143   2.62  5344  97.48
## 37  1870          5       0.09   148   2.71  5340  97.41
## 38  1871          2       0.04   150   2.75  5335  97.32
## 39  1872          7       0.13   157   2.88  5333  97.28
## 40  1873         34       0.62   191   3.50  5326  97.15
## 41  1874         10       0.18   201   3.68  5292  96.53
## 42  1875         31       0.57   232   4.25  5282  96.35
## 43  1876          4       0.07   236   4.32  5251  95.78
## 44  1877          5       0.09   241   4.41  5247  95.71
## 45  1878          4       0.07   245   4.48  5242  95.62
## 46  1879         16       0.29   261   4.77  5238  95.55
## 47  1880         70       1.28   331   6.05  5222  95.26
## 48  1881         21       0.38   352   6.43  5152  93.98
## 49  1882         28       0.51   380   6.94  5131  93.60
## 50  1883         39       0.71   419   7.65  5103  93.09
## 51  1884         17       0.31   436   7.96  5064  92.38
## 52  1885         40       0.73   476   8.69  5047  92.07
## 53  1886         19       0.35   495   9.04  5007  91.34
## 54  1887         20       0.36   515   9.40  4988  90.99
## 55  1888         15       0.27   530   9.67  4968  90.63
## 56  1889          8       0.15   538   9.82  4953  90.36
## 57  1890         25       0.46   563  10.28  4945  90.21
## 58  1891         13       0.24   576  10.52  4920  89.75
## 59  1892         18       0.33   594  10.85  4907  89.51
## 60  1893         15       0.27   609  11.12  4889  89.18
## 61  1894          8       0.15   617  11.27  4874  88.91
## 62  1895         25       0.46   642  11.73  4866  88.76
## 63  1896         16       0.29   658  12.02  4841  88.30
## 64  1897         19       0.35   677  12.37  4825  88.01
## 65  1898         10       0.18   687  12.55  4806  87.66
## 66  1899          9       0.16   696  12.71  4796  87.48
## 67  1900         48       0.88   744  13.59  4787  87.32
## 68  1901         10       0.18   754  13.77  4739  86.44
## 69  1902         23       0.42   777  14.19  4729  86.26
## 70  1903         31       0.57   808  14.76  4706  85.84
## 71  1904         29       0.53   837  15.29  4675  85.27
## 72  1905         27       0.49   864  15.78  4646  84.74
## 73  1906         53       0.97   917  16.75  4619  84.25
## 74  1907         43       0.78   960  17.53  4566  83.28
## 75  1908         45       0.82  1005  18.35  4523  82.50
## 76  1909         17       0.31  1022  18.66  4478  81.68
## 77  1910         32       0.58  1054  19.24  4461  81.37
## 78  1911         25       0.46  1079  19.70  4429  80.79
## 79  1912         22       0.40  1101  20.10  4404  80.33
## 80  1913         27       0.49  1128  20.59  4382  79.93
## 81  1914         33       0.60  1161  21.19  4355  79.44
## 82  1915         42       0.77  1203  21.96  4322  78.84
## 83  1916         42       0.77  1245  22.73  4280  78.07
## 84  1917         62       1.13  1307  23.86  4238  77.30
## 85  1918         97       1.77  1404  25.63  4176  76.17
## 86  1919         46       0.84  1450  26.47  4079  74.40
## 87  1920         56       1.02  1506  27.49  4033  73.56
## 88  1921         26       0.47  1532  27.96  3977  72.54
## 89  1922         37       0.67  1569  28.63  3951  72.07
## 90  1923         32       0.58  1601  29.21  3914  71.40
## 91  1924         18       0.33  1619  29.54  3882  70.82
## 92  1925        119       2.17  1738  31.71  3864  70.49
## 93  1926         17       0.31  1755  32.02  3745  68.32
## 94  1927         24       0.44  1779  32.46  3728  68.01
## 95  1928         31       0.57  1810  33.03  3704  67.57
## 96  1929         37       0.67  1847  33.70  3673  67.00
## 97  1930         55       1.00  1902  34.70  3636  66.33
## 98  1931         26       0.47  1928  35.17  3581  65.33
## 99  1932         27       0.49  1955  35.66  3555  64.86
## 100 1933         24       0.44  1979  36.10  3528  64.37
## 101 1934         43       0.78  2022  36.88  3504  63.93
## 102 1935         88       1.60  2110  38.48  3461  63.15
## 103 1936         48       0.88  2158  39.36  3373  61.55
## 104 1937         73       1.33  2231  40.69  3325  60.67
## 105 1938         40       0.73  2271  41.42  3252  59.34
## 106 1939         79       1.44  2350  42.86  3212  58.61
## 107 1940        136       2.48  2486  45.34  3133  57.17
## 108 1941        103       1.88  2589  47.22  2997  54.69
## 109 1942        120       2.19  2709  49.41  2894  52.81
## 110 1943         92       1.68  2801  51.09  2774  50.62
## 111 1944         55       1.00  2856  52.09  2682  48.94
## 112 1945         93       1.70  2949  53.79  2627  47.94
## 113 1946         38       0.69  2987  54.48  2534  46.24
## 114 1947         60       1.09  3047  55.57  2496  45.55
## 115 1948         55       1.00  3102  56.57  2436  44.46
## 116 1949         79       1.44  3181  58.01  2381  43.46
## 117 1950         84       1.53  3265  59.54  2302  42.02
## 118 1951         65       1.19  3330  60.73  2218  40.49
## 119 1952         71       1.29  3401  62.02  2153  39.30
## 120 1953         56       1.02  3457  63.04  2082  38.01
## 121 1954         65       1.19  3522  64.23  2026  36.99
## 122 1955        116       2.12  3638  66.35  1961  35.80
## 123 1956         90       1.64  3728  67.99  1845  33.68
## 124 1957         95       1.73  3823  69.72  1755  32.04
## 125 1958        109       1.99  3932  71.71  1660  30.31
## 126 1959         47       0.86  3979  72.57  1551  28.32
## 127 1960         57       1.04  4036  73.61  1504  27.46
## 128 1961         51       0.93  4087  74.54  1447  26.42
## 129 1962         36       0.66  4123  75.20  1396  25.49
## 130 1963         41       0.75  4164  75.95  1360  24.83
## 131 1964         31       0.57  4195  76.52  1319  24.08
## 132 1965         54       0.98  4249  77.50  1288  23.51
## 133 1966         35       0.64  4284  78.14  1234  22.53
## 134 1967         36       0.66  4320  78.80  1199  21.89
## 135 1968         44       0.80  4364  79.60  1163  21.23
## 136 1969         40       0.73  4404  80.33  1119  20.43
## 137 1970         56       1.02  4460  81.35  1079  19.70
## 138 1971         50       0.91  4510  82.26  1023  18.68
## 139 1972         34       0.62  4544  82.88   973  17.77
## 140 1973         28       0.51  4572  83.39   939  17.15
## 141 1974         30       0.55  4602  83.94   911  16.64
## 142 1975         50       0.91  4652  84.85   881  16.09
## 143 1976         29       0.53  4681  85.38   831  15.18
## 144 1977         20       0.36  4701  85.74   802  14.65
## 145 1978         31       0.57  4732  86.31   782  14.29
## 146 1979         34       0.62  4766  86.93   751  13.72
## 147 1980         62       1.13  4828  88.06   717  13.10
## 148 1981         74       1.35  4902  89.41   655  11.97
## 149 1982        116       2.12  5018  91.53   581  10.62
## 150 1983         50       0.91  5068  92.44   465   8.50
## 151 1984         45       0.82  5113  93.26   415   7.59
## 152 1985         97       1.77  5210  95.03   370   6.77
## 153 1986         25       0.46  5235  95.49   273   5.00
## 154 1987         31       0.57  5266  96.06   248   4.54
## 155 1988          7       0.13  5273  96.19   217   3.97
## 156 1989         23       0.42  5296  96.61   210   3.84
## 157 1990         59       1.08  5355  97.69   187   3.42
## 158 1991         21       0.38  5376  98.07   128   2.34
## 159 1992         24       0.44  5400  98.51   107   1.96
## 160 1993          8       0.15  5408  98.66    83   1.52
## 161 1994          6       0.11  5414  98.77    75   1.37
## 162 1995          5       0.09  5419  98.86    69   1.26
## 163 1996          6       0.11  5425  98.97    64   1.17
## 164 1997          4       0.07  5429  99.04    58   1.06
## 165 1998          4       0.07  5433  99.11    54   0.99
## 166 1999          8       0.15  5441  99.26    50   0.92
## 167 2000          5       0.09  5446  99.35    42   0.77
## 168 2001          5       0.09  5451  99.44    37   0.68
## 169 2002          2       0.04  5453  99.48    32   0.59
## 170 2003          1       0.02  5454  99.50    30   0.55
## 171 2004         15       0.27  5469  99.77    29   0.53
## 172 2005          1       0.02  5470  99.79    14   0.26
## 173 2006          6       0.11  5476  99.90    13   0.24
## 174 2007          1       0.02  5477  99.92     7   0.13
## 175 2008          2       0.04  5479  99.96     6   0.11
## 176 2009          3       0.05  5482 100.01     4   0.07
## 177 2013          1       0.02  5483 100.03     1   0.02
# Paso 5: Gráficos de barras

# Frecuencia Local
barplot(TablaUltimoAnioFinal$Frecuencia,
        main = "Gráfico No.1: Distribución de cantidad del ultimo año de producción",
        xlab = "Año", ylab = "Cantidad",
        col = "orange",
        names.arg = TablaUltimoAnioFinal$Año,
        las = 2,
        cex.names = 0.5,
        cex.axis = 0.5)

# Frecuencia Global
barplot(TablaUltimoAnioFinal$Frecuencia,
        main = "Gráfico No.2: Distribución de cantidad del ultimo año de producción",
        xlab = "Año", ylab = "Cantidad",
        col = "red",
        names.arg = TablaUltimoAnioFinal$Año,
        ylim = c(0, length(UltimoAnio)),
        las = 2,
        cex.names = 0.5,
        cex.axis = 0.5)

# Porcentaje Local
barplot(TablaUltimoAnioFinal$Porcentaje,
        main = "Gráfico No.3: Distribución de porcentaje del ultimo año de producción",
        xlab = "Año", ylab = "Porcentaje",
        col = "skyblue",
        names.arg = TablaUltimoAnioFinal$Año,
        las = 2,
        cex.names = 0.5,
        cex.axis = 0.5)

# Porcentaje Global
barplot(TablaUltimoAnioFinal$Porcentaje,
        main = "Gráfico No.4: Distribución de porcentaje del ultimo año de producción",
        xlab = "Año", ylab = "Porcentaje",
        col = "blue",
        names.arg = TablaUltimoAnioFinal$Año,
        ylim = c(0, 100),
        las = 2,
        cex.names = 0.5,
        cex.axis = 0.5)

# Paso 6: Boxplot
boxplot(UltimoAnio, horizontal = TRUE, col = "red",
        main = "Gráfico No.5: Distribución del último año de producción")

# Paso 7: Ojiva Ascendente (Ni)
x <- as.numeric(as.character(TablaUltimoAnioFinal$Año))
y <- TablaUltimoAnioFinal$Niasc
plot(x, y, type = "p", main = "Gráfico No.6: Ojiva ascendente: Último año de producción",
     xlab = "Año", ylab = "Frecuencia acumulada", col = "blue")

# Paso 8: Ojiva Descendente (Ni)
y <- TablaUltimoAnioFinal$Nidsc
plot(x, y, type = "p", main = "Gráfico No.7: Ojiva descendente: Último año de producción",
     xlab = "Año", ylab = "Frecuencia acumulada", col = "blue")

# Paso 9: Ojiva Ascendente (Hi %)
y <- TablaUltimoAnioFinal$Hiasc
plot(x, y, type = "p", main = "Gráfico No.8: Ojiva ascendente relativa (%): Último año",
     xlab = "Año", ylab = "% acumulado", col = "blue")

# Paso 10: Ojiva Descendente (Hi %)
y <- TablaUltimoAnioFinal$Hidsc
plot(x, y, type = "p", main = "Gráfico No.9: Ojiva descendente relativa (%): Último año",
     xlab = "Año", ylab = "% acumulado", col = "blue")

# Paso 11: Intersección Ni (Ascendente vs Descendente)
plot(x, TablaUltimoAnioFinal$Niasc, type = "p",
     main = "Gráfico No.10: Ojivas Ni: ascendente vs descendente",
     xlab = "Año", ylab = "Frecuencia acumulada",
     col = "green")
lines(x, TablaUltimoAnioFinal$Nidsc, type = "p", col = "blue")

# Paso 12: Intersección Hi (Ascendente vs Descendente)
plot(x, TablaUltimoAnioFinal$Hiasc, type = "p",
     main = "Gráfico No.11: Ojivas Hi (%): ascendente vs descendente",
     xlab = "Año", ylab = "% acumulado",
     col = "green")
lines(x, TablaUltimoAnioFinal$Hidsc, type = "p", col = "blue")