Tabs

Análisis de componentes principales

paquetes

Dataset

Clusterización k-means

contenido del segundo tab

Análisis de regresión

packages <- c("shiny", "shinydashboard", "readr", "corrplot", "DataExplorer", "data.table", "ggplot2", 
               "dslabs", "dplyr","viridisLite","RColorBrewer","plotly","DT","corrr", "corrplot", "FactoMineR","factoextra","rworldmap","rnaturalearth","rnaturalearthdata","sf" )

install.load::install_load(packages)
## 
## Adjuntando el paquete: 'shinydashboard'
## The following object is masked from 'package:graphics':
## 
##     box
## corrplot 0.92 loaded
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## 
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## 
## Adjuntando el paquete: 'DT'
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
## Cargando paquete requerido: sp
## ### Welcome to rworldmap ###
## For a short introduction type :   vignette('rworldmap')
## 
## Adjuntando el paquete: 'rnaturalearthdata'
## The following object is masked from 'package:rnaturalearth':
## 
##     countries110
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
#Matriz de correlación
library(corrr)
library(corrplot)
#PCA
library(FactoMineR)
library(factoextra)
#Gráficos
library(ggplot2)
library(plotly)
#Manejo datos
library(dplyr)
#Mapas
library(rworldmap)
library(rnaturalearth)
library(rnaturalearthdata)
library(sf)
protein_data<-read.csv("protein.csv")
colSums(is.na(protein_data))
##              Country             Red_Meat           White_Meat 
##                    0                    0                    0 
##                 Eggs                 Milk                 Fish 
##                    0                    0                    0 
##              Cereals        Starchy_Foods Pulses_nuts_oilseeds 
##                    0                    0                    0 
##    Fruits_Vegetables                Total 
##                    0                    0
numerical_data <- protein_data[,2:10]
apply(X=numerical_data, MARGIN=2,FUN=mean)##media
##             Red_Meat           White_Meat                 Eggs 
##                 9.80                 7.92                 3.08 
##                 Milk                 Fish              Cereals 
##                17.28                 4.28                32.32 
##        Starchy_Foods Pulses_nuts_oilseeds    Fruits_Vegetables 
##                 4.36                 3.08                 4.20
apply(X=numerical_data, MARGIN=2,FUN=var) ##Varianza
##             Red_Meat           White_Meat                 Eggs 
##            11.583333            13.993333             1.243333 
##                 Milk                 Fish              Cereals 
##            50.376667            12.043333           121.226667 
##        Starchy_Foods Pulses_nuts_oilseeds    Fruits_Vegetables 
##             2.740000             4.076667             3.666667
apply(X=numerical_data, MARGIN=2,FUN=sd)## desviacion estandar
##             Red_Meat           White_Meat                 Eggs 
##             3.403430             3.740766             1.115049 
##                 Milk                 Fish              Cereals 
##             7.097652             3.470351            11.010298 
##        Starchy_Foods Pulses_nuts_oilseeds    Fruits_Vegetables 
##             1.655295             2.019076             1.914854
data_normalized <- scale(numerical_data)
mean(data_normalized[,1])
## [1] -2.192951e-16
apply(X=data_normalized, MARGIN =2,FUN = mean) #Media
##             Red_Meat           White_Meat                 Eggs 
##        -2.192951e-16         1.097646e-17        -6.438426e-17 
##                 Milk                 Fish              Cereals 
##        -1.701417e-16        -6.438426e-17        -1.774622e-17 
##        Starchy_Foods Pulses_nuts_oilseeds    Fruits_Vegetables 
##        -1.976479e-16        -1.833169e-17        -1.021492e-16
data.cpa <- princomp(data_normalized)

data.cpa$loadings
## 
## Loadings:
##                      Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8
## Red_Meat              0.311         0.355  0.597  0.397  0.377  0.228       
## White_Meat            0.316  0.215 -0.628        -0.311         0.146       
## Eggs                  0.421                0.255        -0.665         0.467
## Milk                  0.379  0.169  0.404        -0.318        -0.718 -0.102
## Fish                  0.134 -0.652  0.300 -0.235 -0.304         0.237  0.441
## Cereals              -0.430  0.254                0.185  0.194 -0.343  0.721
## Starchy_Foods         0.296 -0.389 -0.281 -0.305  0.673        -0.326       
## Pulses_nuts_oilseeds -0.422 -0.129  0.140  0.251        -0.587        -0.218
## Fruits_Vegetables    -0.122 -0.504 -0.340  0.604 -0.228  0.158 -0.359       
##                      Comp.9
## Red_Meat              0.251
## White_Meat            0.577
## Eggs                 -0.275
## Milk                  0.190
## Fish                  0.260
## Cereals               0.192
## Starchy_Foods         0.150
## Pulses_nuts_oilseeds  0.567
## Fruits_Vegetables    -0.211
## 
##                Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8 Comp.9
## SS loadings     1.000  1.000  1.000  1.000  1.000  1.000  1.000  1.000  1.000
## Proportion Var  0.111  0.111  0.111  0.111  0.111  0.111  0.111  0.111  0.111
## Cumulative Var  0.111  0.222  0.333  0.444  0.556  0.667  0.778  0.889  1.000
summary(data.cpa)
## Importance of components:
##                           Comp.1    Comp.2    Comp.3    Comp.4    Comp.5
## Standard deviation     1.9828553 1.2489623 1.0207403 0.9321032 0.6400533
## Proportion of Variance 0.4550596 0.1805448 0.1205915 0.1005574 0.0474153
## Cumulative Proportion  0.4550596 0.6356044 0.7561959 0.8567534 0.9041687
##                            Comp.6     Comp.7     Comp.8     Comp.9
## Standard deviation     0.57711577 0.50866787 0.35936288 0.32716279
## Proportion of Variance 0.03854891 0.02994711 0.01494695 0.01238837
## Cumulative Proportion  0.94271757 0.97266468 0.98761163 1.00000000
corr_matrix<-cor(data_normalized)
ggcorrplot::ggcorrplot(corr_matrix)

fviz_eig(data.cpa,addlabels = TRUE)

fviz_pca_var(data.cpa)

fviz_cos2(data.cpa,choice = "var",axes=1:2)