library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readxl)
library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(skimr)

Actividad

Analizar evaluaciones agrícolas para el cultivo de Café en el departamento de Antioquia desde el año 1990 hasta 2020. Se pretende conocer si la variabilidad en el rendimiento en toneladas por hectárea está influenciada por la zona (subregión) y el período.

Exploratoria de Datos

datos <- read_csv("Agricola_Depurada.csv") %>% 
  clean_names()
## 
## -- Column specification --------------------------------------------------------
## cols(
##   subregion = col_character(),
##   periodo = col_double(),
##   municipio = col_character(),
##   area_total = col_double(),
##   area_produccion = col_double(),
##   volumen_produccion = col_double()
## )
datos %>% 
  head()
## # A tibble: 6 x 6
##   subregion periodo municipio   area_total area_produccion volumen_produccion
##   <chr>       <dbl> <chr>            <dbl>           <dbl>              <dbl>
## 1 Oriente      1990 Alejandría         377             350               525 
## 2 Suroeste     1990 Amagá             2296            2266              2298.
## 3 Nordeste     1990 Amalfi            2470            2247              5056.
## 4 Suroeste     1990 Andes            14360           12900             29025 
## 5 Suroeste     1990 Angelópolis        967             967               772.
## 6 Norte        1990 Angostura         1267            1250              1625
skim(datos)
Data summary
Name datos
Number of rows 2610
Number of columns 6
_______________________
Column type frequency:
character 2
numeric 4
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
subregion 0 1 5 15 0 7 0
municipio 0 1 4 22 0 93 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
periodo 0 1 2005.65 8.81 1990 1998.00 2006.00 2013.00 2020.0 ▇▇▇▇▇
area_total 0 1 1678.95 2056.24 2 480.00 1078.00 1977.75 15136.5 ▇▁▁▁▁
area_produccion 0 1 1488.08 1779.69 0 434.25 962.00 1803.75 14373.9 ▇▁▁▁▁
volumen_produccion 0 1 1976.45 3037.95 0 425.00 1019.21 2114.80 29025.0 ▇▁▁▁▁
datos %>% 
  names
## [1] "subregion"          "periodo"            "municipio"         
## [4] "area_total"         "area_produccion"    "volumen_produccion"
datos %>% 
  glimpse()
## Rows: 2,610
## Columns: 6
## $ subregion          <chr> "Oriente", "Suroeste", "Nordeste", "Suroeste", "Sur~
## $ periodo            <dbl> 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 199~
## $ municipio          <chr> "Alejandría", "Amagá", "Amalfi", "Andes", "Angelópo~
## $ area_total         <dbl> 377, 2296, 2470, 14360, 967, 1267, 300, 1430, 2450,~
## $ area_produccion    <dbl> 350, 2266, 2247, 12900, 967, 1250, 260, 1406, 2350,~
## $ volumen_produccion <dbl> 525.00, 2297.72, 5055.75, 29025.00, 771.67, 1625.00~

Crear Variable Respuesta:

datos_rendimiento <- datos %>%  
  select(subregion, periodo, area_produccion, volumen_produccion) %>% 
mutate (rendimiento_t_h = volumen_produccion/area_produccion)
datos_rendimiento
## # A tibble: 2,610 x 5
##    subregion periodo area_produccion volumen_produccion rendimiento_t_h
##    <chr>       <dbl>           <dbl>              <dbl>           <dbl>
##  1 Oriente      1990             350               525            1.5  
##  2 Suroeste     1990            2266              2298.           1.01 
##  3 Nordeste     1990            2247              5056.           2.25 
##  4 Suroeste     1990           12900             29025            2.25 
##  5 Suroeste     1990             967               772.           0.798
##  6 Norte        1990            1250              1625            1.3  
##  7 Nordeste     1990             260               520            2    
##  8 Occidente    1990            1406              2250.           1.6  
##  9 Oriente      1990            2350              2350            1    
## 10 Occidente    1990            1887              1087.           0.576
## # ... with 2,600 more rows

Gráfica 1 - Densidad de datos General:

datos_rendimiento %>% 
  ggplot(aes(x = rendimiento_t_h, fill = subregion)) +
  geom_density(alpha = 0.5)
## Warning: Removed 2 rows containing non-finite values (stat_density).

Gráfica 2 -Agrupación con relación a la variable rendimiento_t_h

promedio_rendimiento <- datos_rendimiento %>%
  pull (rendimiento_t_h) %>% 
  mean (na.rm = TRUE) 
promedio_rendimiento
## [1] 1.187241
datos_rendimiento %>% 
  group_by(subregion) %>%
  summarise( 
    pro_rendimiento_t_h = mean(rendimiento_t_h),
    des_rendimiento_t_h = sd(rendimiento_t_h)
  ) %>% 
  
  ungroup() %>% 
  ggplot (aes(
    x = subregion,
    y = pro_rendimiento_t_h,
    ymin = pro_rendimiento_t_h - des_rendimiento_t_h,
    ymax = pro_rendimiento_t_h + des_rendimiento_t_h
   )) + 
  geom_point() +
  geom_errorbar(width = 0.2) +
  geom_hline (yintercept = promedio_rendimiento, lty = 2, color = "pink")
## Warning: Removed 2 rows containing missing values (geom_point).

Promedio y desviación subregión:

datos_subregion <- datos_rendimiento %>% 
  select(c(subregion, periodo, rendimiento_t_h))

datos_subregion
## # A tibble: 2,610 x 3
##    subregion periodo rendimiento_t_h
##    <chr>       <dbl>           <dbl>
##  1 Oriente      1990           1.5  
##  2 Suroeste     1990           1.01 
##  3 Nordeste     1990           2.25 
##  4 Suroeste     1990           2.25 
##  5 Suroeste     1990           0.798
##  6 Norte        1990           1.3  
##  7 Nordeste     1990           2    
##  8 Occidente    1990           1.6  
##  9 Oriente      1990           1    
## 10 Occidente    1990           0.576
## # ... with 2,600 more rows

Suroeste:

datos_suroeste <- filter(datos_subregion, subregion == "Suroeste")
datos_suroeste
## # A tibble: 680 x 3
##    subregion periodo rendimiento_t_h
##    <chr>       <dbl>           <dbl>
##  1 Suroeste     1990           1.01 
##  2 Suroeste     1990           2.25 
##  3 Suroeste     1990           0.798
##  4 Suroeste     1990           1.5  
##  5 Suroeste     1990           1.5  
##  6 Suroeste     1990           1.25 
##  7 Suroeste     1990           1.4  
##  8 Suroeste     1990           2.5  
##  9 Suroeste     1990           1.33 
## 10 Suroeste     1990           1    
## # ... with 670 more rows
promedio_suroeste <- datos_suroeste %>%
  pull (rendimiento_t_h) %>% 
  mean()
promedio_suroeste
## [1] 1.413393
desviacion_suroeste <- datos_suroeste %>%
  pull (rendimiento_t_h) %>% 
  sd()
desviacion_suroeste
## [1] 0.4492108

Magdalena Medio:

datos_MagdalenaMedio <- filter(datos_rendimiento, subregion == "Magdalena Medio")
datos_MagdalenaMedio
## # A tibble: 62 x 5
##    subregion       periodo area_produccion volumen_produccion rendimiento_t_h
##    <chr>             <dbl>           <dbl>              <dbl>           <dbl>
##  1 Magdalena Medio    1990            200               180             0.9  
##  2 Magdalena Medio    1990           1050               394.            0.375
##  3 Magdalena Medio    1991            200               150             0.75 
##  4 Magdalena Medio    1991            886.              665.            0.750
##  5 Magdalena Medio    1992            293                58.6           0.2  
##  6 Magdalena Medio    1992            472               236             0.5  
##  7 Magdalena Medio    1993            200                80             0.4  
##  8 Magdalena Medio    1993            450               225             0.5  
##  9 Magdalena Medio    1994            220               110             0.5  
## 10 Magdalena Medio    1994            310               225.            0.725
## # ... with 52 more rows
promedio_MagdalenaMedio <- datos_MagdalenaMedio %>%
  pull (rendimiento_t_h) %>% 
  mean()
promedio_MagdalenaMedio
## [1] 0.5472092
desviacion_MagdalenaMedio <- datos_MagdalenaMedio %>%
  pull (rendimiento_t_h) %>% 
  sd()
desviacion_MagdalenaMedio
## [1] 0.2269963

Occidente:

datos_Occidente <- filter(datos_rendimiento, subregion == "Occidente")
datos_Occidente
## # A tibble: 579 x 5
##    subregion periodo area_produccion volumen_produccion rendimiento_t_h
##    <chr>       <dbl>           <dbl>              <dbl>           <dbl>
##  1 Occidente    1990            1406              2250.           1.6  
##  2 Occidente    1990            1887              1087.           0.576
##  3 Occidente    1990            1013              1418.           1.4  
##  4 Occidente    1990            3814              3787.           0.993
##  5 Occidente    1990            2200              2750            1.25 
##  6 Occidente    1990            3200              4640            1.45 
##  7 Occidente    1990             870              2030.           2.33 
##  8 Occidente    1990             643              1286            2    
##  9 Occidente    1990            2314              2314            1    
## 10 Occidente    1990            1474              1769.           1.2  
## # ... with 569 more rows
promedio_Occidente <- datos_Occidente %>%
  pull (rendimiento_t_h) %>% 
  mean()
promedio_Occidente
## [1] 1.185922
desviacion_Occidente <- datos_Occidente %>%
  pull (rendimiento_t_h) %>% 
  sd()
desviacion_Occidente
## [1] 0.5522866

Gráfica 3 - Densidad datos Suroeste y Magdalena Medio:

datos_suroeste %>% 
  ggplot(aes(x = rendimiento_t_h, fill = subregion)) +
  geom_density(alpha = 0.5)

datos_MagdalenaMedio %>% 
  ggplot(aes(x = rendimiento_t_h, fill = subregion)) +
  geom_density(color = "red", alpha = 0.9)

Gráfica 4 - Agrupado variables volumen_producción y periodo:

datos_subregion %>%
  group_by(subregion, periodo) %>%
  summarise(promedio_rendimiento_t_h = mean(rendimiento_t_h, na.rm = TRUE)) %>%
  ggplot(aes(x = periodo, y = promedio_rendimiento_t_h, color = subregion)) +
  geom_point() +
  geom_line(aes(group = subregion))
## `summarise()` has grouped output by 'subregion'. You can override using the `.groups` argument.