Importar librerias

library(mice)
## Warning: package 'mice' was built under R version 4.3.3
## Warning in check_dep_version(): ABI version mismatch: 
## lme4 was built with Matrix ABI version 1
## Current Matrix ABI version is 0
## Please re-install lme4 from source or restore original 'Matrix' package
## 
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
## 
##     filter
## The following objects are masked from 'package:base':
## 
##     cbind, rbind
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks mice::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(plotly)
## 
## Attaching package: '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
library(lattice)
## Warning: package 'lattice' was built under R version 4.3.3
library(caret)
## Warning: package 'caret' was built under R version 4.3.3
## 
## Attaching package: 'caret'
## 
## The following object is masked from 'package:purrr':
## 
##     lift
library(ggplot2)
library(Rcpp)
## Warning: package 'Rcpp' was built under R version 4.3.3
library(GGally)
## Warning: package 'GGally' was built under R version 4.3.3
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.3.3
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
library(lmtest)
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.3
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(zoo)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.3.3
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(nortest)
library(Metrics)
## Warning: package 'Metrics' was built under R version 4.3.3
## 
## Attaching package: 'Metrics'
## 
## The following objects are masked from 'package:caret':
## 
##     precision, recall
library(olsrr)
## Warning: package 'olsrr' was built under R version 4.3.3
## 
## Attaching package: 'olsrr'
## 
## The following object is masked from 'package:datasets':
## 
##     rivers
library(stargazer)
## 
## Please cite as: 
## 
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.3

Importar base

library(readxl)
Base <- read_excel("E:/One_Drive_PUJ/OneDrive - PUJ Cali/PERSONAL/TESIS_MAS/Base.xlsx", 
    col_types = c("text", "numeric", "numeric", 
        "numeric", "numeric", "numeric"))
View(Base)
Base$Gasto_Social_USD <- as.double(Base$Gasto_Social_USD)
Base$Porcentaje_PIB <- as.double (Base$Porcentaje_PIB)
Base$Gasto_Social_USD <- as.double (Base$Gasto_Social_USD)
Base$Gasto_Social_porcentaje_PIB <- as.double (Base$Gasto_Social_porcentaje_PIB)
Base$Esperanza_vida <- as.double (Base$Esperanza_vida)

ANÁLISIS DESCRIPTIVO

data(Base)
Chile=subset(Base, Base$Pais=="Chile")
Colombia=subset(Base, Base$Pais=="Colombia")
Mexico=subset(Base, Base$Pais=="México")
Peru = subset(Base, Base$Pais=="Perú")
Latam =subset(Base, Base$Pais=="América Latina y el Caribe")
summarytools::descr(Base)
## Non-numerical variable(s) ignored: Pais
## Descriptive Statistics  
## Base  
## N: 115  
## 
##                         Año   Esperanza_vida   Gasto_Social_porcentaje_PIB   Gasto_Social_USD
## ----------------- --------- ---------------- ----------------------------- ------------------
##              Mean   2011.00            74.84                          2.21             204.18
##           Std.Dev      6.66             2.42                          1.15             199.21
##               Min   2000.00            70.10                          0.53              40.33
##                Q1   2005.00            73.20                          1.40              88.17
##            Median   2011.00            74.40                          1.82             113.28
##                Q3   2017.00            76.20                          2.79             222.26
##               Max   2022.00            80.40                          6.01             934.24
##               MAD      8.90             2.08                          0.96              69.29
##               IQR     12.00             2.85                          1.36             133.27
##                CV      0.00             0.03                          0.52               0.98
##          Skewness      0.00             0.54                          1.13               2.08
##       SE.Skewness      0.23             0.23                          0.23               0.23
##          Kurtosis     -1.24            -0.30                          1.08               3.63
##           N.Valid    115.00           115.00                        115.00             115.00
##         Pct.Valid    100.00           100.00                        100.00             100.00
## 
## Table: Table continues below
## 
##  
## 
##                     Porcentaje_PIB
## ----------------- ----------------
##              Mean             6.28
##           Std.Dev             1.19
##               Min             4.41
##                Q1             5.52
##            Median             6.07
##                Q3             7.02
##               Max             9.75
##               MAD             1.28
##               IQR             1.50
##                CV             0.19
##          Skewness             0.56
##       SE.Skewness             0.24
##          Kurtosis             0.08
##           N.Valid           105.00
##         Pct.Valid            91.30
library(ggplot2)

ggplot(Base, aes(x = Año, y = Esperanza_vida, color = Pais, group = Pais)) +
  geom_line(size = 1) +
  scale_y_continuous(limits = c(68, NA)) +  # eje Y empieza en 68, NA deja que R calcule el máximo
  labs(
    title = "Evolución de la Esperanza de Vida por País",
    x = "Año",
    y = "Esperanza de Vida (años)"
  ) +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot(Base, aes(x = Año, y = Porcentaje_PIB, color = Pais, group = Pais)) +
  geom_line(size = 1) +
  geom_point(size = 2) +  # opcional: puntos en cada año
  labs(
    title = "Evolución del Porcentaje del PIB por País",
    x = "Año",
    y = "Porcentaje del PIB",
    color = "País"
  ) +
  theme_minimal()
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_point()`).

p <- ggplot(Base, aes(x = Año, y = Porcentaje_PIB, color = Pais, group = Pais)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  labs(
    title = "Evolución del Porcentaje del PIB por País",
    x = "Año",
    y = "Porcentaje del PIB",
    color = "País"
  ) +
  theme_minimal()

ggplotly(p)
p1 <- ggplot(Base, aes(x = Año, y = Gasto_Social_USD, color = Pais, group = Pais)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  labs(
    title = "Evolución del Gasto social en USD por País",
    x = "Año",
    y = "Gasto social en USD por hab",
    color = "País"
  ) +
  theme_minimal()

ggplotly(p1)
p2 <- ggplot(Base, aes(x = Año, y = Gasto_Social_porcentaje_PIB, color = Pais, group = Pais)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  labs(
    title = "Evolución del Gasto social por porcentaje del PIB según País",
    x = "Año",
    y = "Gasto social por porcentaje del PIB",
    color = "País"
  ) +
  theme_minimal()

ggplotly(p2)
p3 <- ggplot(Base, aes(x = Año, y = Gasto_Social_USD, color = Pais, group = Pais)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  labs(
    title = "Evolución del Gasto social por porcentaje del PIB según País",
    x = "Año",
    y = "Gasto social por porcentaje del PIB",
    color = "País"
  ) +
  theme_minimal()

ggplotly(p3)

Descriptivo por subconjuntos paises

summarytools::descr(Latam)
## Non-numerical variable(s) ignored: Pais
## Descriptive Statistics  
## Latam  
## N: 23  
## 
##                         Año   Esperanza_vida   Gasto_Social_porcentaje_PIB   Gasto_Social_USD
## ----------------- --------- ---------------- ----------------------------- ------------------
##              Mean   2011.00            73.36                          1.93             142.32
##           Std.Dev      6.78             1.19                          0.44              54.39
##               Min   2000.00            71.10                          1.45              77.98
##                Q1   2005.00            72.30                          1.56              84.42
##            Median   2011.00            73.30                          1.81             132.56
##                Q3   2017.00            74.60                          2.20             191.33
##               Max   2022.00            75.10                          2.97             242.76
##               MAD      8.90             1.63                          0.45              72.38
##               IQR     11.00             2.00                          0.63              98.89
##                CV      0.00             0.02                          0.23               0.38
##          Skewness      0.00            -0.25                          0.84               0.30
##       SE.Skewness      0.48             0.48                          0.48               0.48
##          Kurtosis     -1.36            -1.21                         -0.29              -1.41
##           N.Valid     23.00            23.00                         23.00              23.00
##         Pct.Valid    100.00           100.00                        100.00             100.00
## 
## Table: Table continues below
## 
##  
## 
##                     Porcentaje_PIB
## ----------------- ----------------
##              Mean             6.56
##           Std.Dev             0.54
##               Min             5.79
##                Q1             6.06
##            Median             6.71
##                Q3             6.94
##               Max             7.96
##               MAD             0.47
##               IQR             0.88
##                CV             0.08
##          Skewness             0.50
##       SE.Skewness             0.50
##          Kurtosis            -0.06
##           N.Valid            21.00
##         Pct.Valid            91.30
summarytools::descr(Chile)
## Non-numerical variable(s) ignored: Pais
## Descriptive Statistics  
## Chile  
## N: 23  
## 
##                         Año   Esperanza_vida   Gasto_Social_porcentaje_PIB   Gasto_Social_USD
## ----------------- --------- ---------------- ----------------------------- ------------------
##              Mean   2011.00            78.75                          3.82             535.14
##           Std.Dev      6.78             1.08                          1.09             223.00
##               Min   2000.00            76.80                          2.59             259.85
##                Q1   2005.00            77.80                          2.79             306.13
##            Median   2011.00            78.90                          3.67             493.21
##                Q3   2017.00            79.50                          4.73             727.56
##               Max   2022.00            80.40                          6.01             934.24
##               MAD      8.90             1.19                          1.31             292.89
##               IQR     11.00             1.60                          1.82             399.04
##                CV      0.00             0.01                          0.28               0.42
##          Skewness      0.00            -0.21                          0.50               0.30
##       SE.Skewness      0.48             0.48                          0.48               0.48
##          Kurtosis     -1.36            -1.18                         -1.12              -1.41
##           N.Valid     23.00            23.00                         23.00              23.00
##         Pct.Valid    100.00           100.00                        100.00             100.00
## 
## Table: Table continues below
## 
##  
## 
##                     Porcentaje_PIB
## ----------------- ----------------
##              Mean             7.54
##           Std.Dev             1.07
##               Min             6.04
##                Q1             6.77
##            Median             7.21
##                Q3             8.30
##               Max             9.75
##               MAD             0.70
##               IQR             1.53
##                CV             0.14
##          Skewness             0.66
##       SE.Skewness             0.50
##          Kurtosis            -0.86
##           N.Valid            21.00
##         Pct.Valid            91.30
summarytools::descr(Colombia)
## Non-numerical variable(s) ignored: Pais
## Descriptive Statistics  
## Colombia  
## N: 23  
## 
##                         Año   Esperanza_vida   Gasto_Social_porcentaje_PIB   Gasto_Social_USD
## ----------------- --------- ---------------- ----------------------------- ------------------
##              Mean   2011.00            74.40                          2.30             137.87
##           Std.Dev      6.78             1.76                          0.76              66.19
##               Min   2000.00            71.30                          1.38              58.89
##                Q1   2005.00            72.80                          1.78              81.39
##            Median   2011.00            74.70                          1.85             104.99
##                Q3   2017.00            76.00                          3.00             201.97
##               Max   2022.00            76.80                          3.99             279.57
##               MAD      8.90             2.37                          0.40              55.95
##               IQR     11.00             2.95                          1.10             110.09
##                CV      0.00             0.02                          0.33               0.48
##          Skewness      0.00            -0.21                          0.80               0.61
##       SE.Skewness      0.48             0.48                          0.48               0.48
##          Kurtosis     -1.36            -1.35                         -0.71              -1.11
##           N.Valid     23.00            23.00                         23.00              23.00
##         Pct.Valid    100.00           100.00                        100.00             100.00
## 
## Table: Table continues below
## 
##  
## 
##                     Porcentaje_PIB
## ----------------- ----------------
##              Mean             6.87
##           Std.Dev             0.88
##               Min             5.64
##                Q1             6.07
##            Median             6.84
##                Q3             7.52
##               Max             8.99
##               MAD             1.02
##               IQR             1.45
##                CV             0.13
##          Skewness             0.41
##       SE.Skewness             0.50
##          Kurtosis            -0.51
##           N.Valid            21.00
##         Pct.Valid            91.30
summarytools::descr(Mexico)
## Non-numerical variable(s) ignored: Pais
## Descriptive Statistics  
## Mexico  
## N: 23  
## 
##                         Año   Esperanza_vida   Gasto_Social_porcentaje_PIB   Gasto_Social_USD
## ----------------- --------- ---------------- ----------------------------- ------------------
##              Mean   2011.00            73.94                          0.97              90.47
##           Std.Dev      6.78             1.23                          0.23              22.64
##               Min   2000.00            70.10                          0.53              48.28
##                Q1   2005.00            74.10                          0.79              71.18
##            Median   2011.00            74.20                          1.08              99.38
##                Q3   2017.00            74.40                          1.15             108.05
##               Max   2022.00            74.80                          1.20             113.28
##               MAD      8.90             0.30                          0.15              17.81
##               IQR     11.00             0.30                          0.34              34.06
##                CV      0.00             0.02                          0.23               0.25
##          Skewness      0.00            -2.47                         -0.66              -0.69
##       SE.Skewness      0.48             0.48                          0.48               0.48
##          Kurtosis     -1.36             4.87                         -1.18              -1.18
##           N.Valid     23.00            23.00                         23.00              23.00
##         Pct.Valid    100.00           100.00                        100.00             100.00
## 
## Table: Table continues below
## 
##  
## 
##                     Porcentaje_PIB
## ----------------- ----------------
##              Mean             5.59
##           Std.Dev             0.41
##               Min             4.45
##                Q1             5.46
##            Median             5.67
##                Q3             5.81
##               Max             6.24
##               MAD             0.22
##               IQR             0.35
##                CV             0.07
##          Skewness            -1.05
##       SE.Skewness             0.50
##          Kurtosis             1.04
##           N.Valid            21.00
##         Pct.Valid            91.30
summarytools::descr(Peru)
## Non-numerical variable(s) ignored: Pais
## Descriptive Statistics  
## Peru  
## N: 23  
## 
##                         Año   Esperanza_vida   Gasto_Social_porcentaje_PIB   Gasto_Social_USD
## ----------------- --------- ---------------- ----------------------------- ------------------
##              Mean   2011.00            73.73                          2.01             115.10
##           Std.Dev      6.78             1.62                          0.61              55.90
##               Min   2000.00            70.40                          1.19              40.33
##                Q1   2005.00            72.70                          1.43              60.48
##            Median   2011.00            73.50                          1.80              94.87
##                Q3   2017.00            75.30                          2.39             157.54
##               Max   2022.00            76.20                          3.35             210.96
##               MAD      8.90             1.63                          0.67              68.44
##               IQR     11.00             2.30                          0.90              93.43
##                CV      0.00             0.02                          0.30               0.49
##          Skewness      0.00            -0.16                          0.55               0.30
##       SE.Skewness      0.48             0.48                          0.48               0.48
##          Kurtosis     -1.36            -0.91                         -0.79              -1.34
##           N.Valid     23.00            23.00                         23.00              23.00
##         Pct.Valid    100.00           100.00                        100.00             100.00
## 
## Table: Table continues below
## 
##  
## 
##                     Porcentaje_PIB
## ----------------- ----------------
##              Mean             4.83
##           Std.Dev             0.42
##               Min             4.41
##                Q1             4.54
##            Median             4.74
##                Q3             4.98
##               Max             6.30
##               MAD             0.34
##               IQR             0.44
##                CV             0.09
##          Skewness             1.90
##       SE.Skewness             0.50
##          Kurtosis             4.46
##           N.Valid            21.00
##         Pct.Valid            91.30
fig <- plot_ly(data = Base, x = ~Gasto_Social_porcentaje_PIB, y = ~Esperanza_vida, color = ~Pais)
fig <- fig %>% layout(title = "Esperanza de vida (años) y gasto social en porcentaje del PIB")
fig
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig2 <- plot_ly(data = Base, x = ~Gasto_Social_USD, y = ~Esperanza_vida, color = ~Pais)
fig2 <- fig2 %>% layout(title = "Esperanza de vida (años) y gasto social en USD")
fig2
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig3 <- plot_ly(data = Base, x = ~Porcentaje_PIB, y = ~Esperanza_vida, color = ~Pais)
fig3 <- fig3 %>% layout(title = "Esperanza de vida (años) y porcentaje del PIB")
fig3
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 10 observations
fig4 <- plot_ly(data = Chile, x = ~Gasto_Social_porcentaje_PIB, y = ~Esperanza_vida)
fig4 <- fig4 %>% layout(title = "Esperanza de vida (años) y gasto social (%) del PIB en Chile")
fig4
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig5 <- plot_ly(data = Chile, x = ~Gasto_Social_USD, y = ~Esperanza_vida)
fig5 <- fig5 %>% layout(title = "Esperanza de vida (años) y gasto social (USD) en Chile")
fig5
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig6 <- plot_ly(data = Chile, x = ~Porcentaje_PIB, y = ~Esperanza_vida)
fig6 <- fig6 %>% layout(title = "Esperanza de vida (años) y gasto corriente total en salud en Chile")
fig6
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 2 observations
fig7 <- plot_ly(data = Colombia, x = ~Porcentaje_PIB, y = ~Esperanza_vida, marker = list(color = 'orchid') )
fig7 <- fig7 %>% layout(title = "Esperanza de vida (años) y gasto social (%) del PIB en Colombia")
fig7
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 2 observations
fig8 <- plot_ly(data = Colombia, x = ~Gasto_Social_USD, y = ~Esperanza_vida, marker = list(color = 'orchid') )
fig8 <- fig8 %>% layout(title = "Esperanza de vida (años) y gasto social (USD) en Colombia")
fig8
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig9 <- plot_ly(data = Colombia, x = ~Gasto_Social_porcentaje_PIB, y = ~Esperanza_vida, marker = list(color = 'orchid') )
fig9 <- fig9 %>% layout(title = "Esperanza de vida (años) y gasto corriente total en Salud en Colombia" )
fig9
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig10 <- plot_ly(data = Mexico, x = ~Porcentaje_PIB, y = ~Esperanza_vida, marker = list(color = 'hotpink') )
fig10 <- fig10 %>% layout(title = "Esperanza de vida (años) y gasto social (%) del PIB en Mexico" )
fig10
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 2 observations
fig11 <- plot_ly(data = Mexico, x = ~Gasto_Social_USD, y = ~Esperanza_vida, marker = list(color = 'hotpink') )
fig11 <- fig11 %>% layout(title = "Esperanza de vida (años) y gasto social (USD) en Mexico")
fig11
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig12 <- plot_ly(data = Mexico, x = ~Gasto_Social_porcentaje_PIB, y = ~Esperanza_vida, marker = list(color = 'hotpink') )
fig12 <- fig12 %>% layout(title = "Esperanza de vida (años) y gasto corriente total en Salud en Mexico" )
fig12
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig13 <- plot_ly(data = Peru, x = ~Porcentaje_PIB, y = ~Esperanza_vida, marker = list(color = '#32CD32') )
fig13 <- fig13 %>% layout(title = "Esperanza de vida (años) y gasto social (%) del PIB en Peru" )
fig13
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 2 observations
fig14 <- plot_ly(data = Peru, x = ~Gasto_Social_USD, y = ~Esperanza_vida, marker = list(color = '#32CD32') )
fig14 <- fig14 %>% layout(title = "Esperanza de vida (años) y gasto social (USD) en Peru")
fig14
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
fig15 <- plot_ly(data = Peru, x = ~Gasto_Social_porcentaje_PIB, y = ~Esperanza_vida, marker = list(color = '#32CD32') )
fig15 <- fig15 %>% layout(title = "Esperanza de vida (años) y gasto corriente total en Salud en Peru" )
fig15
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

ANALISIS DE CORRELACION

#Chile$Gasto_Social_porcentaje_PIB <- as.numeric(Chile$Gasto_Social_porcentaje_PIB)

NORMALIDAD CHILE

shapiro.test(Chile$Esperanza_vida)
## 
##  Shapiro-Wilk normality test
## 
## data:  Chile$Esperanza_vida
## W = 0.95986, p-value = 0.4605
shapiro.test(Chile$Gasto_Social_USD)
## 
##  Shapiro-Wilk normality test
## 
## data:  Chile$Gasto_Social_USD
## W = 0.9131, p-value = 0.0475
shapiro.test(Chile$Gasto_Social_porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Chile$Gasto_Social_porcentaje_PIB
## W = 0.90345, p-value = 0.02979
shapiro.test(Chile$Porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Chile$Porcentaje_PIB
## W = 0.90745, p-value = 0.04892

NORMALIDAD COLOMBIA

shapiro.test(Colombia$Esperanza_vida)
## 
##  Shapiro-Wilk normality test
## 
## data:  Colombia$Esperanza_vida
## W = 0.94107, p-value = 0.1895
shapiro.test(Colombia$Gasto_Social_porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Colombia$Gasto_Social_porcentaje_PIB
## W = 0.85549, p-value = 0.003426
shapiro.test(Colombia$Gasto_Social_USD)
## 
##  Shapiro-Wilk normality test
## 
## data:  Colombia$Gasto_Social_USD
## W = 0.885, p-value = 0.01257
shapiro.test(Colombia$Porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Colombia$Porcentaje_PIB
## W = 0.95333, p-value = 0.393

NORMALIDAD MEXICO

shapiro.test(Mexico$Esperanza_vida)
## 
##  Shapiro-Wilk normality test
## 
## data:  Mexico$Esperanza_vida
## W = 0.54609, p-value = 2.436e-07
shapiro.test(Mexico$Gasto_Social_porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Mexico$Gasto_Social_porcentaje_PIB
## W = 0.85115, p-value = 0.002855
shapiro.test(Mexico$Gasto_Social_USD)
## 
##  Shapiro-Wilk normality test
## 
## data:  Mexico$Gasto_Social_USD
## W = 0.8375, p-value = 0.001628
shapiro.test(Mexico$Porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Mexico$Porcentaje_PIB
## W = 0.90228, p-value = 0.03875

NORMALIDAD PERU

shapiro.test(Peru$Esperanza_vida)
## 
##  Shapiro-Wilk normality test
## 
## data:  Peru$Esperanza_vida
## W = 0.96015, p-value = 0.4664
shapiro.test(Peru$Gasto_Social_porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Peru$Gasto_Social_porcentaje_PIB
## W = 0.92956, p-value = 0.107
shapiro.test(Peru$Gasto_Social_USD)
## 
##  Shapiro-Wilk normality test
## 
## data:  Peru$Gasto_Social_USD
## W = 0.91668, p-value = 0.0566
shapiro.test(Peru$Porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Peru$Porcentaje_PIB
## W = 0.78711, p-value = 0.0004195

NORMALIDAD LATAM

shapiro.test(Latam$Esperanza_vida)
## 
##  Shapiro-Wilk normality test
## 
## data:  Latam$Esperanza_vida
## W = 0.95401, p-value = 0.3536
shapiro.test(Latam$Gasto_Social_porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Latam$Gasto_Social_porcentaje_PIB
## W = 0.89079, p-value = 0.0164
shapiro.test(Latam$Gasto_Social_USD)
## 
##  Shapiro-Wilk normality test
## 
## data:  Latam$Gasto_Social_USD
## W = 0.90988, p-value = 0.04062
shapiro.test(Latam$Porcentaje_PIB)
## 
##  Shapiro-Wilk normality test
## 
## data:  Latam$Porcentaje_PIB
## W = 0.91068, p-value = 0.05663
library(dplyr)
data <- (Base)

GRAFICOS CORRELACION SPEARMAN

#data <- rename(data, Gasto_Social_porcen_PIB =  Gasto_Social_porcentaje_PIB)
Base$Esperanza_vida=log(Base$Esperanza_vida)
Base$Porcentaje_PIB=log(Base$Porcentaje_PIB)
ggpairs(data[,2:6], title="correlacion", method = "spearman") 
## Warning in warn_if_args_exist(list(...)): Extra arguments: "method" are being
## ignored.  If these are meant to be aesthetics, submit them using the 'mapping'
## variable within ggpairs with ggplot2::aes or ggplot2::aes_string.
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 10 rows containing missing values
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 10 rows containing non-finite outside the scale range
## (`stat_density()`).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 10 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 10 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 10 rows containing missing values
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 10 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 10 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggpairs(Chile[,2:6], title="Matriz de correlación Chile", method = "spearman") 
## Warning in warn_if_args_exist(list(...)): Extra arguments: "method" are being
## ignored.  If these are meant to be aesthetics, submit them using the 'mapping'
## variable within ggpairs with ggplot2::aes or ggplot2::aes_string.
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_density()`).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

library(dplyr)
library(ggcorrplot)
library(plotly)

# Selecciona las columnas numéricas
dfn <- select(Chile, Año, Esperanza_vida, Porcentaje_PIB, Gasto_Social_USD, Gasto_Social_porcentaje_PIB)

# Calcular la matriz de correlación utilizando Spearman
r <- cor(dfn, method = "spearman", use = "complete.obs")

# Crear el gráfico de la correlación con ggcorrplot
p2 <- ggcorrplot(r, 
                 type = "lower", 
                 title = "Correlaciones entre variables", 
                 colors = c("red", "yellow", "blue"), 
                 lab = TRUE, 
                 outline.color = "black", 
                 ggtheme = theme_test() + theme(text = element_text(size = 7)))

# Convertir el gráfico a interactivo con plotly
p2_interactive <- plotly::ggplotly(p2)

# Mostrar el gráfico interactivo
p2_interactive
ggpairs(Colombia[,2:6], title="Matriz de correlación Colombia", method = "spearman") 
## Warning in warn_if_args_exist(list(...)): Extra arguments: "method" are being
## ignored.  If these are meant to be aesthetics, submit them using the 'mapping'
## variable within ggpairs with ggplot2::aes or ggplot2::aes_string.
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_density()`).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggpairs(Mexico[,2:6], title="Matriz de correlación México", method = "spearman") 
## Warning in warn_if_args_exist(list(...)): Extra arguments: "method" are being
## ignored.  If these are meant to be aesthetics, submit them using the 'mapping'
## variable within ggpairs with ggplot2::aes or ggplot2::aes_string.
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_density()`).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggpairs(Peru[,2:6], title="Matriz de correlación Perú", method = "spearman") 
## Warning in warn_if_args_exist(list(...)): Extra arguments: "method" are being
## ignored.  If these are meant to be aesthetics, submit them using the 'mapping'
## variable within ggpairs with ggplot2::aes or ggplot2::aes_string.
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_density()`).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggpairs(Latam[,2:6], title="Matriz de correlación LATAM", method = "spearman") 
## Warning in warn_if_args_exist(list(...)): Extra arguments: "method" are being
## ignored.  If these are meant to be aesthetics, submit them using the 'mapping'
## variable within ggpairs with ggplot2::aes or ggplot2::aes_string.
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_density()`).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 2 rows containing missing values
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

Analisis de Eficiencia del Gasto

# Cargar librerías
library(Benchmarking)
## Warning: package 'Benchmarking' was built under R version 4.3.3
## Loading required package: lpSolveAPI
## Loading required package: ucminf
## Loading required package: quadprog
library(readxl)
library(ggplot2)
library(dplyr)

# Leer archivo Excel
datos2 <- data

# Eliminar filas con datos faltantes
datos2 <- na.omit(datos2[, c("Pais", "Porcentaje_PIB", "Gasto_Social_USD", 
                           "Gasto_Social_porcentaje_PIB", "Esperanza_vida")])

# Crear un dataframe para guardar resultados por país
resultados <- data.frame()

# Iterar por país y calcular eficiencia DEA
paises <- unique(datos2$Pais)

for (pais in paises) {
  datos_pais <- datos2 %>% filter(Pais == pais)
  
  inputs <- as.matrix(datos_pais[, c("Porcentaje_PIB", "Gasto_Social_USD", "Gasto_Social_porcentaje_PIB")])
  outputs <- as.matrix(datos_pais[, "Esperanza_vida", drop = FALSE])
  
  if (nrow(inputs) > 2) {  # Asegura que haya suficientes datos
    dea_result <- dea(X = inputs, Y = outputs, RTS = "vrs", ORIENTATION = "in")
    
    eficiencia_promedio <- mean(dea_result$eff, na.rm = TRUE)
    
    resultados <- rbind(resultados, data.frame(Pais = pais, Eficiencia_Promedio = eficiencia_promedio))
  }
}

# Ordenar resultados
resultados <- resultados %>% arrange(desc(Eficiencia_Promedio))

# Crear gráfico con ggplot2
ggplot(resultados, aes(x = reorder(Pais, Eficiencia_Promedio), y = Eficiencia_Promedio)) +
  geom_col(fill = "steelblue") +
  coord_flip() +
  labs(title = "Eficiencia promedio del gasto social en salud por país (DEA)",
       x = "País",
       y = "Eficiencia DEA promedio") +
  theme_minimal()