Análisis exploratorio

Column

ibex

59,724

brent

1,754,150

column

column

Análisis cluster (PCA)

column

PCA

Silhouette

column

Análisis cluster (RPCA)

column

RPCA

Silhouette

column

---
title: "Propagación del covid 19"
author: "Sebastián Sosa Pérez"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source: embed
    theme: cerulean
---



```{r setup, include=FALSE}

# PACKAGES / LIBRARIES:

library(flexdashboard)
library(rpca)
library(tidyverse)
library(highcharter)
library(quantmod)
library(lubridate)
library(sf)
library(purrr)
library(tidyverse)
library(ggplot2)
library(ggrepel)
library(maptools)  
library(RColorBrewer) 
library(readxl)
library(leaflet)
library(broom)
library(dplyr)
library(ggplot2)
library(classInt) 
library(sp)
library(rgdal)
library(spdep) 
library(ggpubr)
library(faraway)
library(mapproj)
library(rgeos)
library(plotly)
library(rpca)
```

```{r,include=FALSE}
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse","cluster", "factoextra","NbClust","tidyr")
ipak(packages)
```

```{r,include=FALSE}
setwd("C:/Users/atita/Desktop/departamentos")

peru_d <- st_read("dp.shp")

```

```{r,include=FALSE}
setwd("C:/Users/atita/Desktop/departamentos")
data<- read_delim("positivos_covid.csv",";",escape_double = FALSE, 
                trim_ws = TRUE)

fallecidos <- read_delim("fallecidos_covid.csv",";",escape_double = F,
                         trim_ws = T)

muertes<-fallecidos %>% select(DEPARTAMENTO) %>% 
  group_by(DEPARTAMENTO) %>% 
  count()

contagios<- data %>% select(DEPARTAMENTO) %>% 
  group_by(DEPARTAMENTO) %>% 
  count()
```


Análisis exploratorio
==================================================================================


Column {data-width=150}
-----------------------------------------------------------------------


### ibex{.value-box}
  
```{r}

total_muertes<- sum(muertes$n)

valueBox(value = format(total_muertes,big.mark=","), 
         caption = "Número de fallecidos",
         color = "salmon")

```

### brent{.value-box}

```{r}

total_contagios<- sum(contagios$n)

valueBox(value = format(total_contagios, big.mark= ","), 
         caption = "Total de personas contagiadas",
         color = "orange")

```

column 
-----------------------------------------------------------------------

```{r}
fallecidos$FECHA_FALLECIMIENTO<- 
  fallecidos$FECHA_FALLECIMIENTO %>% ymd()



fallecidos %>% select(FECHA_FALLECIMIENTO) %>% 
    group_by(FECHA_FALLECIMIENTO) %>% count() %>% 
    ggplot(aes(FECHA_FALLECIMIENTO,n))+geom_col(color="purple",
                                          alpha=0.5)+
    geom_line(mapping = aes(y=rollmean(n,7,fill = NA)),
              size=1,color="blue")+theme_minimal()+labs(x=" ",
                                                        y="Total de fallecidos",
                                                        title = "Crecimiento del número de muertos en el Perú")

```

```{r}
names(muertes)[1]<- "DEPARTAMEN"
peru_datos <- peru_d %>% #Juntamos ambas bases de datos
  left_join(muertes)
ggplot(peru_datos) +
  geom_sf(aes(fill = n))+
  labs(title = "Número de fallecidos por Departamento",
       caption = "Fuente: MINSA
       Elaboración propia",
       x=" ",
       y=" ")+
  scale_fill_continuous(guide_legend(title = "Cantidad de fallecidos"))
```

column 
-----------------------------------------------------------------------

```{r}
data$FECHA_RESULTADO<- 
  data$FECHA_RESULTADO %>% ymd()



data %>% select(FECHA_RESULTADO) %>% 
    group_by(FECHA_RESULTADO) %>% count() %>% 
    ggplot(aes(FECHA_RESULTADO,n))+geom_col(color="purple",
                                          alpha=0.5)+
    geom_line(mapping = aes(y=rollmean(n,7,fill = NA)),
              size=1,color="blue")+theme_minimal()+labs(x=" ",
                                                        y="Total de fallecidos",
                                                        title = "Crecimiento del número de contagiados en el Perú")
```

```{r}
names(contagios)[1]<- "DEPARTAMEN"
peru_datos <- peru_d %>% #Juntamos ambas bases de datos
  left_join(contagios)

ggplot(peru_datos) +
  geom_sf(aes(fill = n))+
  labs(title = "Número de contagiados por Departamento",
       caption = "Fuente: MINSA
       Elaboración propia",
       x=" ",
       y=" ")+
  scale_fill_continuous(guide_legend(title = "Cantidad de fallecidos"))
```

Análisis cluster (PCA)
==================================================================================

column 
-----------------------------------------------------------------------

### PCA 



```{r}
names(muertes)[2]<- "n_muertes"
names(contagios)[2]<- "n_contagios"

df1<- contagios %>% #Juntamos ambas bases de datos
  left_join(muertes)
df=df1[-16,-1]
k2 <- kmeans(df, centers = 3, nstart = 25)
a<-fviz_cluster(k2, data = df, ellipse.type = "euclid",repel = TRUE,star.plot = TRUE)+
  theme_minimal()
ggplotly(a)
```

### Silhouette {.no-mobile}

```{r,include=FALSE}
PCA_KM3 = eclust(df, "kmeans", 3, nboot = 20, graph = F, seed = 123)
```


```{r }
b<-fviz_silhouette(PCA_KM3,print.summary = F)+theme_minimal()
ggplotly(b)
```

column 
-----------------------------------------------------------------------

```{r fig.height=10, fig.width=10}
df<- cbind(df1[-16,],k2$cluster)
names(df)[1]<- "DEPARTAMEN"
names(df)[4]<- "cluster"
peru_datos <- peru_d %>% #Juntamos ambas bases de datos
  left_join(df[,c(1,4)])
pal <- colorFactor(
  palette = c("blue","red","purple"),
  domain = peru_datos$cluster)

leaflet(peru_datos) %>%
  addTiles() %>%
  addPolygons(weight = 1,
              stroke = TRUE,
              color = "white",
              fillColor = ~pal(cluster),
              fillOpacity = 0.7,
              dashArray = "3",
              popup = ~paste(
                "Departamento:", DEPARTAMEN),
              highlight = highlightOptions(
                weight = 2,
                dashArray = "",
                color = "grey",
                bringToFront = TRUE
              ))
```


Análisis cluster (RPCA)
==================================================================================

column 
-----------------------------------------------------------------------

### RPCA 



```{r}
names(muertes)[2]<- "n_muertes"
names(contagios)[2]<- "n_contagios"

df1<- contagios %>% #Juntamos ambas bases de datos
  left_join(muertes)

df=df1[-16,-1]

RPCA_res3 = rpca(df %>% as.matrix(),trace=F)
L_matrix3 = data.frame(RPCA_res3$L)
names(L_matrix3)<- names(df)
RPCA_KM3 = eclust(L_matrix3, "kmeans", 2 ,nboot = 20, graph = F, seed = 123)
k<-fviz_cluster(RPCA_KM3, data = df, ellipse.type = "euclid",repel = TRUE,star.plot = TRUE)+theme_minimal()
ggplotly(k)
```

### Silhouette {.no-mobile}



```{r }
z<-fviz_silhouette(RPCA_KM3,print.summary = F)+theme_minimal()
ggplotly(z)
```

column 
-----------------------------------------------------------------------

```{r fig.height=10, fig.width=10}
df<- cbind(df1[-16,],RPCA_KM3$cluster)
names(df)[1]<- "DEPARTAMEN"
names(df)[4]<- "cluster"
peru_datos <- peru_d %>% #Juntamos ambas bases de datos
  left_join(df[,c(1,4)])


pal <- colorFactor(
  palette = c("blue","purple","red"),
  domain = peru_datos$cluster)

leaflet(peru_datos) %>%
  addTiles() %>%
  addPolygons(weight = 1,
              stroke = TRUE,
              color = "white",
              fillColor = ~pal(cluster),
              fillOpacity = 0.7,
              dashArray = "3",
              popup = ~paste(
                "Departamento:", DEPARTAMEN),
              highlight = highlightOptions(
                weight = 2,
                dashArray = "",
                color = "grey",
                bringToFront = TRUE
              ))
```