Desarrollaremos un modelo de correlación de el M2 (liquidez
monetaria) y tasa de interés activa, en el mismo veremos la relación
existente entre estos valores monetarios y sus variaciones en el
tiempo.
# Cargar las librerías
library(readxl)
library(corrplot)
## corrplot 0.95 loaded
library(psych)
library(PerformanceAnalytics)
## Cargando paquete requerido: xts
## Cargando paquete requerido: zoo
##
## Adjuntando el paquete: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Adjuntando el paquete: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(ggplot2)
##
## Adjuntando el paquete: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::%+%() masks psych::%+%()
## ✖ ggplot2::alpha() masks psych::alpha()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(apaTables)
library(psych)
library(corrr)
library(corrplot)
library(palmerpenguins)
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
library(ggplot2)
Importar desde excel los datos recolectados
# Importar datos
data <- read_excel("C:/Users/MARK/Desktop/Modelo.xlsx")
## New names:
## • `` -> `...1`
str(data)
## tibble [21 × 5] (S3: tbl_df/tbl/data.frame)
## $ ...1 : num [1:21] 2004 2005 2006 2007 2008 ...
## $ tasa de interes a: num [1:21] 17.9 16.4 15.4 17.3 23.2 ...
## $ M2 : num [1:21] 463991 708745 1446694 1769533 2179031 ...
## $ inflacion : num [1:21] 21.5 22.7 23.8 24.9 26 ...
## $ reservas i : num [1:21] 24208 30368 37440 34286 43127 ...
view(data)
# Renombrar columnas (ajusta según los nombres reales)
data <- data %>%
rename(tasa_interes = "tasa de interes a",inflacion_anual = "inflacion",reservas_i = "reservas i")
# Seleccionar variables numéricas
cor_data <- data[, c("tasa_interes", "M2", "inflacion_anual", "reservas_i")]
# Matriz de correlación de Spearman
cor_spearman <- cor(cor_data, method = "spearman")
print(cor_spearman)
## tasa_interes M2 inflacion_anual reservas_i
## tasa_interes 1.0000000 0.3974026 0.4844156 -0.6454545
## M2 0.3974026 1.0000000 0.1246753 -0.4012987
## inflacion_anual 0.4844156 0.1246753 1.0000000 -0.7324675
## reservas_i -0.6454545 -0.4012987 -0.7324675 1.0000000
# Prueba entre M2 e inflación
cor.test(cor_data$M2, cor_data$inflacion_anual, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: cor_data$M2 and cor_data$inflacion_anual
## S = 1348, p-value = 0.589
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.1246753
# Prueba entre tasa de interes y reservas
cor.test(cor_data$tasa_interes, cor_data$reservas_i, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: cor_data$tasa_interes and cor_data$reservas_i
## S = 2534, p-value = 0.002021
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.6454545
# Prueba entre M2 y reservas
cor.test(cor_data$M2, cor_data$reservas_i, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: cor_data$M2 and cor_data$reservas_i
## S = 2158, p-value = 0.07247
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.4012987
# Prueba entre M2 y tasa de interés
cor.test(cor_data$M2, cor_data$tasa_interes, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: cor_data$M2 and cor_data$tasa_interes
## S = 928, p-value = 0.07549
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.3974026
Pruebas estadisticas
# Test de normalidad Shapiro-Wilk para cada variable
shapiro.test(cor_data$tasa_interes)
##
## Shapiro-Wilk normality test
##
## data: cor_data$tasa_interes
## W = 0.74339, p-value = 0.0001007
shapiro.test(cor_data$M2)
##
## Shapiro-Wilk normality test
##
## data: cor_data$M2
## W = 0.4076, p-value = 3.176e-08
shapiro.test(cor_data$inflacion_anual)
##
## Shapiro-Wilk normality test
##
## data: cor_data$inflacion_anual
## W = 0.26246, p-value = 2.434e-09
shapiro.test(cor_data$reservas_i)
##
## Shapiro-Wilk normality test
##
## data: cor_data$reservas_i
## W = 0.89602, p-value = 0.02932
# Coeficiente de determinación (R²) - ejemplo: M2 explicado por tasa de interés
modelo_lineal <- lm(M2 ~ tasa_interes, data = cor_data)
summary(modelo_lineal) # Aquí verás el R²
##
## Call:
## lm(formula = M2 ~ tasa_interes, data = cor_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -97871736 -33598814 -19331821 -11126802 502693040
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -30226372 60582361 -0.499 0.624
## tasa_interes 2961785 2112731 1.402 0.177
##
## Residual standard error: 1.22e+08 on 19 degrees of freedom
## Multiple R-squared: 0.09374, Adjusted R-squared: 0.04604
## F-statistic: 1.965 on 1 and 19 DF, p-value: 0.1771
# Extraer solo el R²
r_squared <- summary(modelo_lineal)$r.squared
cat("Coeficiente de determinación (R²) entre M2 y tasa de interés:", round(r_squared, 3), "\n")
## Coeficiente de determinación (R²) entre M2 y tasa de interés: 0.094
Visualización de la matriz de correlación
pairs(cor_data,main = "Grafico de pares entre variables monetarias",pch = 19,col = "darkblue",cex = 0.8)

ggpairs(cor_data, title = "Matriz de dispersion y correlacion entre variables economicas")

# Matriz de dispersión con correlaciones
pairs(cor_data, main = "Matriz de dispersion")

# Matriz con PerformanceAnalytics
chart.Correlation(cor_data, histogram = TRUE, pch = 19, col = "blue")

# Matriz con psych (muestra r y si es significativo)
pairs.panels(cor_data, stars = TRUE, main = "Correlaciones entre variables economicas")

# Red de correlaciones con corrr
cor_data %>%
correlate() %>%
network_plot(min_cor = 0.3)
## Correlation computed with
## • Method: 'pearson'
## • Missing treated using: 'pairwise.complete.obs'
