# Actividad Sesión 1. Modelo Econometrico
## 1. Formular la pregunta de interés
#Queremos analizar si a mayor ingreso, las personas tienden a gastar más y en qué magnitud. Teniendo como pregunta *¿Cómo afecta el ingreso de una persona a su consumo?*
## 2. Construir un modelo económico
#C=β0+β1YC
#Donde:
#* C es el consumo de una persona.
#* Y es el ingreso de la persona.
#* β0 es el consumo autónomo (lo que la persona gasta aunque su ingreso sea cero).
#* β1 es la propensión marginal a consumir (qué parte del ingreso adicional se gasta).
## 3. Transformar el modelo económico a econométrico
#Ci=β0+β1Yi+εiC_i
#Donde:
#CiC_i y YiY_i son los valores de consumo e ingreso para la persona ii.
#εi es un término de error que representa otros factores que influyen en el consumo.
# Actividad Sesión 2,3 y 4. Analisis de datos de Panel
##Instalar paquetes y llamar librerías
#install.packages("WDI")
library(WDI)
#install.packages("wbstats")
library(wbstats)
#install.packages("tidyverse")
library(ggplot2)
#install.packages("plm")
library(plm)
## Obtener la información de 1 país
PIB_MEX <- wb_data(country= "MX", indicator = "NY.GDP.PCAP.CD",
start_date = 1900, end_date = 2025)
summary(PIB_MEX)
## iso2c iso3c country date
## Length:64 Length:64 Length:64 Min. :1960
## Class :character Class :character Class :character 1st Qu.:1976
## Mode :character Mode :character Mode :character Median :1992
## Mean :1992
## 3rd Qu.:2007
## Max. :2023
## NY.GDP.PCAP.CD unit obs_status footnote
## Min. : 355.1 Length:64 Length:64 Length:64
## 1st Qu.: 1427.8 Class :character Class :character Class :character
## Median : 4006.5 Mode :character Mode :character Mode :character
## Mean : 5097.1
## 3rd Qu.: 8905.4
## Max. :13790.0
## last_updated
## Min. :2025-01-28
## 1st Qu.:2025-01-28
## Median :2025-01-28
## Mean :2025-01-28
## 3rd Qu.:2025-01-28
## Max. :2025-01-28
ggplot(PIB_MEX, aes(x= date, y=NY.GDP.PCAP.CD)) +
geom_point() +
geom_line() +
labs(title="PIB per Capita en México (Current USD$)", x= "Año", y= "Valor")
## Obtener la información de varios país
PIB_PANEL <- wb_data(country= c("MX", "US", "CA"), indicator = "NY.GDP.PCAP.CD",
start_date = 1900, end_date = 2025)
summary(PIB_PANEL)
## iso2c iso3c country date
## Length:192 Length:192 Length:192 Min. :1960
## Class :character Class :character Class :character 1st Qu.:1976
## Mode :character Mode :character Mode :character Median :1992
## Mean :1992
## 3rd Qu.:2007
## Max. :2023
## NY.GDP.PCAP.CD unit obs_status footnote
## Min. : 355.1 Length:192 Length:192 Length:192
## 1st Qu.: 4059.2 Class :character Class :character Class :character
## Median :10544.4 Mode :character Mode :character Mode :character
## Mean :19152.2
## 3rd Qu.:29010.1
## Max. :82769.4
## last_updated
## Min. :2025-01-28
## 1st Qu.:2025-01-28
## Median :2025-01-28
## Mean :2025-01-28
## 3rd Qu.:2025-01-28
## Max. :2025-01-28
ggplot(PIB_PANEL, aes(x= date, y=NY.GDP.PCAP.CD, color=iso3c)) +
geom_point() +
geom_line() +
labs(title="PIB per Capita en Norteamérica (Current USD$)", x= "Año", y= "Valor")
## Obtener la información de varios indicadores en varios paises
PIB_VARIOS <- wb_data(country= c("MX", "US", "CA"), indicator = c("NY.GDP.PCAP.CD", "SP.DYN.LE00.IN"),
start_date = 1900, end_date = 2025)
summary(PIB_VARIOS)
## iso2c iso3c country date
## Length:192 Length:192 Length:192 Min. :1960
## Class :character Class :character Class :character 1st Qu.:1976
## Mode :character Mode :character Mode :character Median :1992
## Mean :1992
## 3rd Qu.:2007
## Max. :2023
##
## NY.GDP.PCAP.CD SP.DYN.LE00.IN
## Min. : 355.1 Min. :55.02
## 1st Qu.: 4059.2 1st Qu.:71.11
## Median :10544.4 Median :74.36
## Mean :19152.2 Mean :73.41
## 3rd Qu.:29010.1 3rd Qu.:77.49
## Max. :82769.4 Max. :82.22
## NA's :3
##Heterogeneidad Variación entre individuos
#install.packages("gplots")
library(gplots)
plotmeans(NY.GDP.PCAP.CD ~ country, main = "Heterogenidad entre paises", xlab = "Pais" , ylab = "PIB per Capita", data= PIB_VARIOS)
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
# *Interpretación*:
# 1) Alta heterogenidad: Si los puntos estan muy separados entre paises
# 2) Baja heterogenidad: Si los puntos estan cerca uno de otos
# En este caso EUA y Canada tiene un PIB per capita mayor que México, mostrando alta heterogenidad entre paises
## Modelos de efectos fijos y aleatorios
###Paso 1, Convertir la base de datos a formato de panel
datos_panel <-pdata.frame(PIB_PANEL, index = c("country", "date"))
### Modelo de efectos fijos
modelo_efectos_fijos <- plm(NY.GDP.PCAP.CD ~ date, data=datos_panel, model= "within")
summary(modelo_efectos_fijos)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = NY.GDP.PCAP.CD ~ date, data = datos_panel, model = "within")
##
## Balanced Panel: n = 3, T = 64, N = 192
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -22869.42 -3713.59 -740.79 4417.57 22788.54
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## date1961 19.689 7891.777 0.0025 0.9980133
## date1962 93.003 7891.777 0.0118 0.9906159
## date1963 182.117 7891.777 0.0231 0.9816255
## date1964 329.256 7891.777 0.0417 0.9667868
## date1965 493.812 7891.777 0.0626 0.9502057
## date1966 705.548 7891.777 0.0894 0.9289037
## date1967 836.074 7891.777 0.1059 0.9157965
## date1968 1051.287 7891.777 0.1332 0.8942375
## date1969 1278.661 7891.777 0.1620 0.8715461
## date1970 1483.079 7891.777 0.1879 0.8512361
## date1971 1757.600 7891.777 0.2227 0.8241196
## date1972 2139.145 7891.777 0.2711 0.7867884
## date1973 2652.616 7891.777 0.3361 0.7373364
## date1974 3306.205 7891.777 0.4189 0.6759711
## date1975 3736.686 7891.777 0.4735 0.6366822
## date1976 4425.604 7891.777 0.5608 0.5759388
## date1977 4698.806 7891.777 0.5954 0.5526405
## date1978 5234.634 7891.777 0.6633 0.5083487
## date1979 6060.354 7891.777 0.7679 0.4439640
## date1980 7072.576 7891.777 0.8962 0.3718573
## date1981 8188.133 7891.777 1.0376 0.3014655
## date1982 7987.390 7891.777 1.0121 0.3134224
## date1983 8523.654 7891.777 1.0801 0.2821751
## date1984 9312.706 7891.777 1.1801 0.2402027
## date1985 9796.257 7891.777 1.2413 0.2167918
## date1986 9909.818 7891.777 1.2557 0.2115431
## date1987 10895.002 7891.777 1.3806 0.1698612
## date1988 12362.836 7891.777 1.5665 0.1197288
## date1989 13585.668 7891.777 1.7215 0.0876150 .
## date1990 14316.347 7891.777 1.8141 0.0720442 .
## date1991 14759.335 7891.777 1.8702 0.0637741 .
## date1992 14990.000 7891.777 1.8994 0.0597918 .
## date1993 15667.517 7891.777 1.9853 0.0492832 *
## date1994 16091.651 7891.777 2.0390 0.0435376 *
## date1995 15978.167 7891.777 2.0247 0.0450159 *
## date1996 16773.055 7891.777 2.1254 0.0355067 *
## date1997 17769.387 7891.777 2.2516 0.0260772 *
## date1998 18030.354 7891.777 2.2847 0.0240026 *
## date1999 19236.904 7891.777 2.4376 0.0161811 *
## date2000 20835.037 7891.777 2.6401 0.0093360 **
## date2001 21096.198 7891.777 2.6732 0.0085083 **
## date2002 21538.969 7891.777 2.7293 0.0072554 **
## date2003 23202.118 7891.777 2.9400 0.0039054 **
## date2004 25366.654 7891.777 3.2143 0.0016609 **
## date2005 27852.977 7891.777 3.5294 0.0005823 ***
## date2006 30232.924 7891.777 3.8309 0.0002003 ***
## date2007 32408.252 7891.777 4.1066 7.172e-05 ***
## date2008 33394.731 7891.777 4.2316 4.431e-05 ***
## date2009 30291.171 7891.777 3.8383 0.0001950 ***
## date2010 33440.081 7891.777 4.2373 4.333e-05 ***
## date2011 35778.148 7891.777 4.5336 1.331e-05 ***
## date2012 36526.334 7891.777 4.6284 9.027e-06 ***
## date2013 37214.927 7891.777 4.7157 6.286e-06 ***
## date2014 37345.549 7891.777 4.7322 5.866e-06 ***
## date2015 35011.917 7891.777 4.4365 1.971e-05 ***
## date2016 34666.237 7891.777 4.3927 2.348e-05 ***
## date2017 36493.760 7891.777 4.6243 9.182e-06 ***
## date2018 38068.376 7891.777 4.8238 3.990e-06 ***
## date2019 38902.406 7891.777 4.9295 2.543e-06 ***
## date2020 37056.865 7891.777 4.6956 6.833e-06 ***
## date2021 42836.438 7891.777 5.4280 2.815e-07 ***
## date2022 46436.696 7891.777 5.8842 3.387e-08 ***
## date2023 48123.578 7891.777 6.0979 1.218e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 5.061e+10
## Residual Sum of Squares: 1.1771e+10
## R-Squared: 0.76742
## Adj. R-Squared: 0.64743
## F-statistic: 6.59909 on 63 and 126 DF, p-value: < 2.22e-16
### Modelo de efectos aleatorios
modelo_efectos_aleatorios <- plm(NY.GDP.PCAP.CD ~ date, data=datos_panel, model= "random")
summary(modelo_efectos_aleatorios)
## Oneway (individual) effect Random Effect Model
## (Swamy-Arora's transformation)
##
## Call:
## plm(formula = NY.GDP.PCAP.CD ~ date, data = datos_panel, model = "random")
##
## Balanced Panel: n = 3, T = 64, N = 192
##
## Effects:
## var std.dev share
## idiosyncratic 93420218 9665 0.375
## individual 155441504 12468 0.625
## theta: 0.9035
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -24225.08 -3320.91 -892.17 5059.72 23751.53
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 1873.296 9107.904 0.2057 0.8370424
## date1961 19.689 7891.777 0.0025 0.9980093
## date1962 93.003 7891.777 0.0118 0.9905973
## date1963 182.117 7891.777 0.0231 0.9815890
## date1964 329.256 7891.777 0.0417 0.9667208
## date1965 493.812 7891.777 0.0626 0.9501065
## date1966 705.548 7891.777 0.0894 0.9287617
## date1967 836.074 7891.777 0.1059 0.9156280
## date1968 1051.287 7891.777 0.1332 0.8940250
## date1969 1278.661 7891.777 0.1620 0.8712866
## date1970 1483.079 7891.777 0.1879 0.8509338
## date1971 1757.600 7891.777 0.2227 0.8237590
## date1972 2139.145 7891.777 0.2711 0.7863449
## date1973 2652.616 7891.777 0.3361 0.7367774
## date1974 3306.205 7891.777 0.4189 0.6752578
## date1975 3736.686 7891.777 0.4735 0.6358628
## date1976 4425.604 7891.777 0.5608 0.5749430
## date1977 4698.806 7891.777 0.5954 0.5515726
## date1978 5234.634 7891.777 0.6633 0.5071370
## date1979 6060.354 7891.777 0.7679 0.4425272
## date1980 7072.576 7891.777 0.8962 0.3701483
## date1981 8188.133 7891.777 1.0376 0.2994785
## date1982 7987.390 7891.777 1.0121 0.3114828
## date1983 8523.654 7891.777 1.0801 0.2801120
## date1984 9312.706 7891.777 1.1801 0.2379796
## date1985 9796.257 7891.777 1.2413 0.2144858
## date1986 9909.818 7891.777 1.2557 0.2092195
## date1987 10895.002 7891.777 1.3806 0.1674170
## date1988 12362.836 7891.777 1.5665 0.1172207
## date1989 13585.668 7891.777 1.7215 0.0851607 .
## date1990 14316.347 7891.777 1.8141 0.0696648 .
## date1991 14759.335 7891.777 1.8702 0.0614537 .
## date1992 14990.000 7891.777 1.8994 0.0575059 .
## date1993 15667.517 7891.777 1.9853 0.0471115 *
## date1994 16091.651 7891.777 2.0390 0.0414460 *
## date1995 15978.167 7891.777 2.0247 0.0429023 *
## date1996 16773.055 7891.777 2.1254 0.0335546 *
## date1997 17769.387 7891.777 2.2516 0.0243455 *
## date1998 18030.354 7891.777 2.2847 0.0223303 *
## date1999 19236.904 7891.777 2.4376 0.0147856 *
## date2000 20835.037 7891.777 2.6401 0.0082883 **
## date2001 21096.198 7891.777 2.6732 0.0075134 **
## date2002 21538.969 7891.777 2.7293 0.0063470 **
## date2003 23202.118 7891.777 2.9400 0.0032817 **
## date2004 25366.654 7891.777 3.2143 0.0013076 **
## date2005 27852.977 7891.777 3.5294 0.0004166 ***
## date2006 30232.924 7891.777 3.8309 0.0001277 ***
## date2007 32408.252 7891.777 4.1066 4.016e-05 ***
## date2008 33394.731 7891.777 4.2316 2.320e-05 ***
## date2009 30291.171 7891.777 3.8383 0.0001239 ***
## date2010 33440.081 7891.777 4.2373 2.262e-05 ***
## date2011 35778.148 7891.777 4.5336 5.799e-06 ***
## date2012 36526.334 7891.777 4.6284 3.685e-06 ***
## date2013 37214.927 7891.777 4.7157 2.409e-06 ***
## date2014 37345.549 7891.777 4.7322 2.221e-06 ***
## date2015 35011.917 7891.777 4.4365 9.143e-06 ***
## date2016 34666.237 7891.777 4.3927 1.119e-05 ***
## date2017 36493.760 7891.777 4.6243 3.759e-06 ***
## date2018 38068.376 7891.777 4.8238 1.408e-06 ***
## date2019 38902.406 7891.777 4.9295 8.245e-07 ***
## date2020 37056.865 7891.777 4.6956 2.658e-06 ***
## date2021 42836.438 7891.777 5.4280 5.699e-08 ***
## date2022 46436.696 7891.777 5.8842 4.000e-09 ***
## date2023 48123.578 7891.777 6.0979 1.074e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 5.0797e+10
## Residual Sum of Squares: 1.1958e+10
## R-Squared: 0.76459
## Adj. R-Squared: 0.64873
## Chisq: 415.742 on 63 DF, p-value: < 2.22e-16
### Prueba de Hausman
phtest(modelo_efectos_fijos, modelo_efectos_aleatorios)
##
## Hausman Test
##
## data: NY.GDP.PCAP.CD ~ date
## chisq = 3.8736e-13, df = 63, p-value = 1
## alternative hypothesis: one model is inconsistent
# Como el p-value es mayor a 0.05, usamos el modelo de efectos aletorios.
## Codigo ejercicio 3 por mesas
#Instalar paquetes y llamar librerias
#install.packages("WDI")
library(WDI)
#install.packages("wbstats")
library(wbstats)
#install.packages("tidyverse")
library(ggplot2)
#install.packages("gplots")
library(gplots)
# Obtener la información de 1 país
AGRICULTURA_MEX <- wb_data(country= "MX", indicator = "NV.AGR.TOTL.ZS",
start_date = 1960, end_date = 2025)
summary(AGRICULTURA_MEX)
## iso2c iso3c country date
## Length:64 Length:64 Length:64 Min. :1960
## Class :character Class :character Class :character 1st Qu.:1976
## Mode :character Mode :character Mode :character Median :1992
## Mean :1992
## 3rd Qu.:2007
## Max. :2023
##
## NV.AGR.TOTL.ZS unit obs_status footnote
## Min. : 2.943 Length:64 Length:64 Length:64
## 1st Qu.: 3.216 Class :character Class :character Class :character
## Median : 4.580 Mode :character Mode :character Mode :character
## Mean : 6.059
## 3rd Qu.: 7.995
## Max. :13.149
## NA's :5
## last_updated
## Min. :2025-01-28
## 1st Qu.:2025-01-28
## Median :2025-01-28
## Mean :2025-01-28
## 3rd Qu.:2025-01-28
## Max. :2025-01-28
##
ggplot(AGRICULTURA_MEX, aes(x= date, y= NV.AGR.TOTL.ZS)) +
geom_point() +
geom_line() +
labs(title="Agricultura como % del PIB en México", x= "Año", y= "Porcentaje del PIB")
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_line()`).
# Obtener la información de varios países
AGRICULTURA_PANEL <- wb_data(country= c("MX", "US", "CA"), indicator = "NV.AGR.TOTL.ZS",
start_date = 1960, end_date = 2025)
summary(AGRICULTURA_PANEL)
## iso2c iso3c country date
## Length:192 Length:192 Length:192 Min. :1960
## Class :character Class :character Class :character 1st Qu.:1976
## Mode :character Mode :character Mode :character Median :1992
## Mean :1992
## 3rd Qu.:2007
## Max. :2023
##
## NV.AGR.TOTL.ZS unit obs_status footnote
## Min. : 0.8326 Length:192 Length:192 Length:192
## 1st Qu.: 1.5774 Class :character Class :character Class :character
## Median : 3.0280 Mode :character Mode :character Mode :character
## Mean : 3.9749
## 3rd Qu.: 5.5139
## Max. :13.1492
## NA's :84
## last_updated
## Min. :2025-01-28
## 1st Qu.:2025-01-28
## Median :2025-01-28
## Mean :2025-01-28
## 3rd Qu.:2025-01-28
## Max. :2025-01-28
##
ggplot(AGRICULTURA_PANEL, aes(x= date, y= NV.AGR.TOTL.ZS, color= iso3c)) +
geom_point() +
geom_line() +
labs(title="Agricultura como % del PIB en Norteamérica", x= "Año", y= "Porcentaje del PIB")
## Warning: Removed 84 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 84 rows containing missing values or values outside the scale range
## (`geom_line()`).
# Obtener la información de varios indicadores en varios países
AGRICULTURA_VARIOS <- wb_data(country= c("MX", "US", "CA"),
indicator = c("NV.AGR.TOTL.ZS", "AG.LND.AGRI.ZS"),
start_date = 1960, end_date = 2025)
summary(AGRICULTURA_VARIOS)
## iso2c iso3c country date
## Length:192 Length:192 Length:192 Min. :1960
## Class :character Class :character Class :character 1st Qu.:1976
## Mode :character Mode :character Mode :character Median :1992
## Mean :1992
## 3rd Qu.:2007
## Max. :2023
##
## AG.LND.AGRI.ZS NV.AGR.TOTL.ZS
## Min. : 6.371 Min. : 0.8326
## 1st Qu.: 6.970 1st Qu.: 1.5774
## Median :46.532 Median : 3.0280
## Mean :35.038 Mean : 3.9749
## 3rd Qu.:50.286 3rd Qu.: 5.5139
## Max. :54.888 Max. :13.1492
## NA's :6 NA's :84
# CLASE 3
# Heterogeneidad
# Variación entre países
plotmeans(NV.AGR.TOTL.ZS ~ iso3c, main = "Heterogeneidad en la Agricultura (% PIB)",
xlab = "País" , ylab = "Agricultura como % del PIB", data= AGRICULTURA_VARIOS)
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
# Interpretación:
# Alta heterogeneidad: Si los puntos están muy separados entre países.
# Baja heterogeneidad: Si los puntos están cerca unos de otros.