Instalamos las librerias adecuadas para los ejercicios
Fijamos el directorio de trabajo donde guardaremos los resultados obtenidos
setwd("C:/Users/irving/Desktop/9no semestre/Series de Tiempo")
Ecuacion de primer grado
Se pretende realizar el mismo ejercicio implementando una diferencia en signos
El objetivo es observar el tiempo en el que demoran en converger a su valor en el Largo Plazo
“Sea \(X_t = 5+0.9Y_{t-1}\)”
“Propuesta de implementacion”
“Sea \(X_t = 5-0.9Y_{t-1}\)” “Ejercicio
de Examen”
Para fines del ejercicio se remplazara Xt con Zt
Tiempo = c(0:100) #Definimos la variable tiempo
Zt1 = rep(NA,101) #Ecuacion con "+"
Zt2 = rep(NA,101) #Ecuacion con "-"
#Parametro X0=10 <Z0=10>
Zt1[1] = 10
Zt2[1] = 10
Definicion de las ecuaciones
for (i in c(2:101)){
Zt1[i] = 5 + 0.9*Zt1[i-1]
Zt2[i] = 5 - 0.9*Zt2[i-1]}
lista = c(1:16, 97:101)
Tiempo1 = Tiempo[lista]
Zt1.1=Zt1[lista]
Zt2.1=Zt2[lista]
Obtencion de la tabla para la presentacion HTML
tabla1 = data.frame(Tiempo1, Zt1.1, Zt2.1)
colnames(tabla1) <- c("Tiempo", "$Z_t = 5+0.9Z_{t-1}$", "$Z_t = 5-0.9Z_{t-1}$")
kable(tabla1, caption = "Dos ejemplos de Ecuaciones Lineales de Primer Orden",
format = "pandoc") %>% kable_styling(font_size = 10)
## Warning in kable_styling(., font_size = 10): Please specify format in kable.
## kableExtra can customize either HTML or LaTeX outputs. See
## https://haozhu233.github.io/kableExtra/ for details.
| Tiempo | \(Z_t = 5+0.9Z_{t-1}\) | \(Z_t = 5-0.9Z_{t-1}\) |
|---|---|---|
| 0 | 10.00000 | 10.0000000 |
| 1 | 14.00000 | -4.0000000 |
| 2 | 17.60000 | 8.6000000 |
| 3 | 20.84000 | -2.7400000 |
| 4 | 23.75600 | 7.4660000 |
| 5 | 26.38040 | -1.7194000 |
| 6 | 28.74236 | 6.5474600 |
| 7 | 30.86812 | -0.8927140 |
| 8 | 32.78131 | 5.8034426 |
| 9 | 34.50318 | -0.2230983 |
| 10 | 36.05286 | 5.2007885 |
| 11 | 37.44758 | 0.3192903 |
| 12 | 38.70282 | 4.7126387 |
| 13 | 39.83254 | 0.7586252 |
| 14 | 40.84928 | 4.3172373 |
| 15 | 41.76435 | 1.1144864 |
| 96 | 49.99838 | 2.6318772 |
| 97 | 49.99854 | 2.6313105 |
| 98 | 49.99869 | 2.6318206 |
| 99 | 49.99882 | 2.6313615 |
| 100 | 49.99894 | 2.6317747 |
tabla <- data.frame( Tiempo, Zt1, Zt2)
Grafica de ecuacion en diferencia de primer orden “+”
EQDif1 <- ggplot( data = tabla , aes(x = Tiempo, y = Zt1) )+
geom_line(col="deeppink4") +
geom_point(col= "deeppink4") +
labs(y=expression(Z[t]))
EQDif1
Visualizacion del comportamiento
EQDif1 + transition_reveal(Zt1)
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
Grafica de ecuacion en diferencia de primer orden “-” EJERCIIO EXAMEN
EQDif1.1 <- ggplot (data = tabla , aes(x = Tiempo, y=Zt2)) +
geom_line(col="deepskyblue4") +
geom_point(col= "deepskyblue4") +
labs(y=expression(Z[t]))
EQDif1.1
Visualizacion del comportamiento
EQDif1.1 + transition_reveal(Zt2) +
shadow_wake(wake_length = 0.1, alpha= FALSE)
Comparamos ambos graficos para observar el comportamiento de las ecuaciones
par(mfrow = c(1, 2))
EQDif1+EQDif1.1+
labs(caption = "Comparacion de comportamiennto Eq de 1er orden")
par(mfrow = c(1, 2)) #Corremos esta parte para volver al panel de grafico normal
Conversion de tabla a excel
#write.xlsx(tabla,"EcuacionG1.xlsx")
Complemento ejercicio 2 ecuacion en diferencia segundo orden
“Sea \(X_t = 14.6 + 1.03X_{t-1} - 0.15X_{t-2}\)”
Asuma que el proceso tiene como valores iniciales “\(X_0 = 10\)” y “\(X_1 = 20\)”. Dicho lo anterior, determine la ecuacion que representa la solucion de la ecuacion (2).
Definicion de los valores iniciales
t = ts(c(0:100))
Zt = 121.6188299+9.208484*(0.1755^t)-120.826484*(0.8544^t)
tabla_2 <- data.frame( t, Zt)
Preparacion del entorno y diseño de la tabla
lista = c(1:16, 97:101)
t1 = t[lista]
Zt1.2=Zt[lista]
Tiempo1 = t[lista]
tabla1 = data.frame(Tiempo1, Zt1.2)
colnames(tabla1) <- c("t", "$Z_t =121.6188299+9.208484(0.1755)^t-120.826484(0.8544)^t$")
kable(tabla1,
caption = "Un ejemplo de Ecuación de Segundo Orden",
format = "pandoc") %>%
kable_styling(font_size = 121.6188299)
## Warning in kable_styling(., font_size = 121.6188299): Please specify format in
## kable. kableExtra can customize either HTML or LaTeX outputs. See
## https://haozhu233.github.io/kableExtra/ for details.
| t | \(Z_t =121.6188299+9.208484(0.1755)^t-120.826484(0.8544)^t\) |
|---|---|
| 0 | 10.00083 |
| 1 | 20.00077 |
| 2 | 33.69920 |
| 3 | 46.30774 |
| 4 | 57.23925 |
| 5 | 66.60698 |
| 6 | 74.61567 |
| 7 | 81.45914 |
| 8 | 87.30636 |
| 9 | 92.30225 |
| 10 | 96.57074 |
| 11 | 100.21775 |
| 12 | 103.33374 |
| 13 | 105.99605 |
| 14 | 108.27073 |
| 15 | 110.21421 |
| 96 | 121.61880 |
| 97 | 121.61880 |
| 98 | 121.61881 |
| 99 | 121.61881 |
| 100 | 121.61881 |
Informacion de la tabla
head(tabla_2)
## t Zt
## 1 0 10.00083
## 2 1 20.00077
## 3 2 33.69920
## 4 3 46.30774
## 5 4 57.23925
## 6 5 66.60698
str(tabla_2)
## 'data.frame': 101 obs. of 2 variables:
## $ t : Time-Series from 1 to 101: 0 1 2 3 4 5 6 7 8 9 ...
## $ Zt: Time-Series from 1 to 101: 10 20 33.7 46.3 57.2 ...
names(tabla_2)
## [1] "t" "Zt"
Grafica de la ecuacion
EqDif2 <- ggplot(data = tabla_2, aes(x = t, y=Zt)) +
geom_line(col="cyan4") +
geom_point(col= "cyan4") +
labs(y=expression(Z[t]))
EqDif2
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
Conversion de las columnas en datos numericos
Este paso se desarollo unicamente para guardar la informacion en formato numerico y a su vez establecer la grafica anterior como objeto predeterminado
tabla_2$t2 <- as.numeric(tabla_2$t)
tabla_2$Zt2 <- as.numeric(tabla_2$Zt)
Visualizacion de la tabla con las nuevas columnas creadas
head(tabla_2)
## t Zt t2 Zt2
## 1 0 10.00083 0 10.00083
## 2 1 20.00077 1 20.00077
## 3 2 33.69920 2 33.69920
## 4 3 46.30774 3 46.30774
## 5 4 57.23925 4 57.23925
## 6 5 66.60698 5 66.60698
str(tabla_2)
## 'data.frame': 101 obs. of 4 variables:
## $ t : Time-Series from 1 to 101: 0 1 2 3 4 5 6 7 8 9 ...
## $ Zt : Time-Series from 1 to 101: 10 20 33.7 46.3 57.2 ...
## $ t2 : num 0 1 2 3 4 5 6 7 8 9 ...
## $ Zt2: num 10 20 33.7 46.3 57.2 ...
names(tabla_2)
## [1] "t" "Zt" "t2" "Zt2"
Realizacion de la grafica con datos numericos
EqDif3 <- ggplot(data = tabla_2, aes(x = t2, y=Zt2)) +
geom_line(col="red") +
geom_point(col= "red") +
labs(y=expression(Z[t]))
EqDif3
Comportamiento de la grafica
EqDif3 + transition_reveal(t2) +
shadow_wake(wake_length = 0.1, alpha= FALSE)
Conversion del dataframe a excel
#write.xlsx(tabla_2,"EcuacionG2.xlsx")
Una vez obtenidos todos los resultados de esta primera parte, se decidio hacer la comparativa de los mismos de manera grafica
Presentacion de las graficas
par(mfrow = c(2, 2)) #Comando que permite dividir el panel de graficas
#Comparativa de las 3 graficas obtenidas
EQDif1 + EQDif1.1 + EqDif3 +
labs(caption = "Comparacion de comportamiento Ecuaciones")
par(mfrow = c(1, 1)) #Ajustamos panel normal para mostrar las graficas del examen
par(mfrow = c(1, 2)) #Volvemos a dividir el panel para mostrar las graficas del examen
#Graficas del examen
EQDif1.1+ EqDif3 +
labs(caption = "Comparacion de comportamiento Ecuaciones")
EJERCICIO NUMERO 3
Se pide que con los resultados del ejercicio 1 y el ejercicio 2 se obtengan los primeros 20 datos para concluir cual de las 2 opciones pasadas converge primero a su valor a largo plazo, los resultados se obtuvieron con las tablas conjuntas.
Una de las opciones es juntar ambas tablas mediante la funcion merge, sin embargo, para el caso practico del ejercicio, estas se ajustaron y trataron con anterioridad para importar unicamente los datos y visualizarlos
Importacion de los resultados de los ejercicios
library(readxl)
Tabla_Final <- read_excel("Resultados ecuaciones.xlsx")
Informacion estadistica primaria
names(Tabla_Final)
## [1] "Tiempo" "Zt1erOrden" "Zt2doOrden"
summary(Tabla_Final)
## Tiempo Zt1erOrden Zt2doOrden
## Min. : 0 Min. :-4.000 Min. : 10.0
## 1st Qu.: 25 1st Qu.: 2.597 1st Qu.:119.3
## Median : 50 Median : 2.632 Median :121.6
## Mean : 50 Mean : 2.670 Mean :113.5
## 3rd Qu.: 75 3rd Qu.: 2.670 3rd Qu.:121.6
## Max. :100 Max. :10.000 Max. :121.6
head(Tabla_Final)
## # A tibble: 6 x 3
## Tiempo Zt1erOrden Zt2doOrden
## <dbl> <dbl> <dbl>
## 1 0 10 10.0
## 2 1 -4 20.0
## 3 2 8.6 33.7
## 4 3 -2.74 46.3
## 5 4 7.47 57.2
## 6 5 -1.72 66.6
str(Tabla_Final)
## tibble [101 x 3] (S3: tbl_df/tbl/data.frame)
## $ Tiempo : num [1:101] 0 1 2 3 4 5 6 7 8 9 ...
## $ Zt1erOrden: num [1:101] 10 -4 8.6 -2.74 7.47 ...
## $ Zt2doOrden: num [1:101] 10 20 33.7 46.3 57.2 ...
Con los resuñtados obtenidos contemplamos y concluimos que la ecuacion numero 2, es decir “Sea \(X_t = 14.6 + 1.03X_{t-1} - 0.15X_{t-2}\)” converge primero a su valor del largo plazo
EJERERCICIO 4
Se solicita llenar la tabla de estadisticas descriptivas con la informacion del número de pasajeros en el Metro de la CDMX, enero de 2000 a junio de 2019
Los datos importados son:
Fuente: [INEGI], (https://www.inegi.org.mx/app/indicadores/?tm=0#D668903)
Analisis exploratorio
Datos1 <- read_excel("Base_Transporte2.xlsx")
head(Datos1)
## # A tibble: 6 x 6
## Periodo Pax_Nal Pax_Int Vue_Nal Vue_Int Pax_Metro
## <dttm> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2000-01-01 00:00:00 1378244 672734 30290 7812 112.
## 2 2000-02-01 00:00:00 1318075 649382 28291 7440 112.
## 3 2000-03-01 00:00:00 1476646 750587 31031 8036 121.
## 4 2000-04-01 00:00:00 1426550 718826 29896 7886 106.
## 5 2000-05-01 00:00:00 1529293 663015 31382 7498 118.
## 6 2000-06-01 00:00:00 1427266 633091 30514 7351 118.
summary(Datos1)
## Periodo Pax_Nal Pax_Int
## Min. :2000-01-01 00:00:00.00 Min. :1246206 Min. : 470638
## 1st Qu.:2004-11-08 12:00:00.00 1st Qu.:1669611 1st Qu.: 856194
## Median :2009-09-16 00:00:00.00 Median :2114245 Median :1148566
## Mean :2009-09-15 15:41:32.30 Mean :2325811 Mean :1236529
## 3rd Qu.:2014-07-24 06:00:00.00 3rd Qu.:2736760 3rd Qu.:1516876
## Max. :2019-06-01 00:00:00.00 Max. :4625916 Max. :2515781
## Vue_Nal Vue_Int Pax_Metro
## Min. :23705 Min. : 5866 Min. : 92.92
## 1st Qu.:30525 1st Qu.: 8744 1st Qu.:117.38
## Median :32454 Median :10637 Median :123.20
## Mean :33330 Mean :10925 Mean :124.30
## 3rd Qu.:36537 3rd Qu.:12474 3rd Qu.:131.56
## Max. :43606 Max. :18127 Max. :154.72
Manejo de dataframes
names(Datos1)
## [1] "Periodo" "Pax_Nal" "Pax_Int" "Vue_Nal" "Vue_Int" "Pax_Metro"
tail(Datos1)
## # A tibble: 6 x 6
## Periodo Pax_Nal Pax_Int Vue_Nal Vue_Int Pax_Metro
## <dttm> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2019-01-01 00:00:00 3899722 2391278 38962 17128 132.
## 2 2019-02-01 00:00:00 3501988 2054807 34162 15253 123.
## 3 2019-03-01 00:00:00 4194556 2515781 38092 17740 135.
## 4 2019-04-01 00:00:00 4358160 2295552 39459 16076 125.
## 5 2019-05-01 00:00:00 4608860 2091288 40555 14943 134.
## 6 2019-06-01 00:00:00 4460465 2139418 39518 15158 129.
str(Datos1)
## tibble [234 x 6] (S3: tbl_df/tbl/data.frame)
## $ Periodo : POSIXct[1:234], format: "2000-01-01" "2000-02-01" ...
## $ Pax_Nal : num [1:234] 1378244 1318075 1476646 1426550 1529293 ...
## $ Pax_Int : num [1:234] 672734 649382 750587 718826 663015 ...
## $ Vue_Nal : num [1:234] 30290 28291 31031 29896 31382 ...
## $ Vue_Int : num [1:234] 7812 7440 8036 7886 7498 ...
## $ Pax_Metro: num [1:234] 112 112 121 106 118 ...
dim(Datos1)
## [1] 234 6
Datos1[ , 2]
## # A tibble: 234 x 1
## Pax_Nal
## <dbl>
## 1 1378244
## 2 1318075
## 3 1476646
## 4 1426550
## 5 1529293
## 6 1427266
## 7 1664868
## 8 1621475
## 9 1337000
## 10 1433870
## # i 224 more rows
Datos1[5 , ]
## # A tibble: 1 x 6
## Periodo Pax_Nal Pax_Int Vue_Nal Vue_Int Pax_Metro
## <dttm> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2000-05-01 00:00:00 1529293 663015 31382 7498 118.
Datos1[c(2:259) , 2]
## # A tibble: 258 x 1
## Pax_Nal
## <dbl>
## 1 1318075
## 2 1476646
## 3 1426550
## 4 1529293
## 5 1427266
## 6 1664868
## 7 1621475
## 8 1337000
## 9 1433870
## 10 1478113
## # i 248 more rows
Datos1[ , c(2:6)]
## # A tibble: 234 x 5
## Pax_Nal Pax_Int Vue_Nal Vue_Int Pax_Metro
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1378244 672734 30290 7812 112.
## 2 1318075 649382 28291 7440 112.
## 3 1476646 750587 31031 8036 121.
## 4 1426550 718826 29896 7886 106.
## 5 1529293 663015 31382 7498 118.
## 6 1427266 633091 30514 7351 118.
## 7 1664868 825149 32367 7690 115.
## 8 1621475 776788 33004 7861 122.
## 9 1337000 597435 30517 7519 117.
## 10 1433870 604420 31999 7874 122.
## # i 224 more rows
Datos1[ , c(2, 3, 4, 5, 6)]
## # A tibble: 234 x 5
## Pax_Nal Pax_Int Vue_Nal Vue_Int Pax_Metro
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1378244 672734 30290 7812 112.
## 2 1318075 649382 28291 7440 112.
## 3 1476646 750587 31031 8036 121.
## 4 1426550 718826 29896 7886 106.
## 5 1529293 663015 31382 7498 118.
## 6 1427266 633091 30514 7351 118.
## 7 1664868 825149 32367 7690 115.
## 8 1621475 776788 33004 7861 122.
## 9 1337000 597435 30517 7519 117.
## 10 1433870 604420 31999 7874 122.
## # i 224 more rows
Datos1[c("Pax_Nal", "Pax_Int", "Vue_Nal", "Vue_Int")]
## # A tibble: 234 x 4
## Pax_Nal Pax_Int Vue_Nal Vue_Int
## <dbl> <dbl> <dbl> <dbl>
## 1 1378244 672734 30290 7812
## 2 1318075 649382 28291 7440
## 3 1476646 750587 31031 8036
## 4 1426550 718826 29896 7886
## 5 1529293 663015 31382 7498
## 6 1427266 633091 30514 7351
## 7 1664868 825149 32367 7690
## 8 1621475 776788 33004 7861
## 9 1337000 597435 30517 7519
## 10 1433870 604420 31999 7874
## # i 224 more rows
Obtencion de Series de Tiempo
Dat_Aereo1 <- ts(Datos1[c("Pax_Nal", "Pax_Int", "Vue_Nal", "Vue_Int")],
start = 2000,
freq = 12)
Pax_Metro1 <- ts(Datos1$Pax_Metro,
start = 2000,
freq = 12)
Pax_Nal1 <- ts(Datos1$Pax_Nal,
start = 2000,
freq = 12)
Pax_Int1 <- ts(Datos1$Pax_Int,
start = 2000,
freq = 12)
Grafica 1
plot(Pax_Metro1,
col = "darkblue",
xlab = "Tiempo",
type = "l", lwd = 2,
ylab = "Millones de personas",
main = "Pasajeros Transportados en el Metro de la CDMX \n(Ene-2000 a Jun-2010)",
sub = "Fuente: Elaboración propia con información del INEGI")
Grafica 2
Pax_MetroG2 <- ggplot(data = Datos1, aes(x = Periodo, y = Pax_Metro)) +
geom_line(size = 0.5, color = "darkblue") +
#geom_point(size = 1.0, color = "darkblue") +
#theme_bw() +
xlab("Tiempo") +
ylab("Millones de pasajeros") +
theme(plot.title = element_text(size = 11, face = "bold", hjust = 0)) +
theme(plot.subtitle = element_text(size = 10, hjust = 0)) +
theme(plot.caption = element_text(size = 10, hjust = 0)) +
theme(plot.margin = unit(c(1,1,1,1), "cm")) +
labs(
title = "Pasajeros Transportados en el Metro de la CDMX",
subtitle = "(Ene-2000 a Jun-2019)",
caption = "Fuente: Elaboración propia con información del INEGI, \nhttps://www.inegi.org.mx/app/indicadores/?tm=0&t=1090"
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## i Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Pax_MetroG2
Comportamiento a traves de los años Grafica 2
Pax_MetroG2 + transition_reveal(Periodo)
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
Grafica 3
Pax_MetroG3 <- ggplot(data = Datos1, aes(x = Periodo)) +
geom_line(aes(y = Pax_Nal, color = "Pax_Nal1")) +
geom_line(aes(y = Pax_Int, color = "Pax_Int1")) +
scale_color_brewer(type = "qual", palette = 6) +
#theme_bw() +
theme(legend.position = "bottom") +
theme(legend.title = element_blank()) +
guides(col = guide_legend(nrow = 1, byrow = TRUE)) +
xlab("Tiempo") +
ylab("Pasajeros") +
theme(plot.title = element_text(size = 11, face = "bold", hjust = 0)) +
theme(plot.subtitle = element_text(size = 10, hjust = 0)) +
theme(plot.caption = element_text(size = 10, hjust = 0)) +
theme(plot.margin = unit(c(1,1,1,1), "cm")) +
labs(
title = "Pasajeros en vuelos nacionales e internacionales (Salidas)",
subtitle = "(Ene-2000 a Jun-2019)",
caption = "Fuente: Elaboración propia con información del INEGI, \n https://www.inegi.org.mx/app/indicadores/?tm=0&t=1090" )
Pax_MetroG3
Comportamiento a traves de los años Grafica 3
Pax_MetroG3 + transition_reveal(Periodo)
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
Grafica 4
Pax_MetroG4 <- ggplot(data = Datos1, aes(x = Periodo)) +
geom_line(aes(y = Pax_Nal1, color = )) +
geom_line(aes(y = Pax_Int1, color = )) +
scale_color_brewer(type = "qual", palette = 6) +
#theme_bw() +
theme(legend.position = "bottom") +
theme(legend.title = element_blank()) +
guides(col = guide_legend(nrow = 1, byrow = TRUE)) +
xlab("Tiempo") +
ylab("Pasajeros") +
theme(plot.title = element_text(size = 11, face = "bold", hjust = 0)) +
theme(plot.subtitle = element_text(size = 10, hjust = 0)) +
theme(plot.caption = element_text(size = 10, hjust = 0)) +
theme(plot.margin = unit(c(1,1,1,1), "cm")) +
labs(
title = "Número de vuelos nacionales e internacionales (Salidas)",
subtitle = "(Ene-2000 a Jun-2019)",
caption = "Fuente: Elaboración propia con información del INEGI, \nhttps://www.inegi.org.mx/app/indicadores/?tm=0&t=1090"
)
Pax_MetroG4
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
Comportamiento a traves de los años Grafica 4
Pax_MetroG4 + transition_reveal(Periodo)
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## i Do you need to adjust the group aesthetic?
Tabla de estadisticas de caracterización de las Series de Tiempo
Creacion de la tabla
Tabla_Pax_Metro1 <- data.frame(matrix(c("Media",
"Varianza",
"Covarianza (t, t - 1)",
"Covarianza (t, t - 2)",
"Covarianza (t, t - 3)",
"Autocorrelacion(1)",
"Autocorrelacion(2)",
"Autocorrelacion(3)",
"Q(1)",
"Q(3)*"),
nrow = 10,
ncol = 1) )
Tabla_Pax_Metro1
## matrix.c..Media....Varianza....Covarianza..t..t...1.....Covarianza..t..t...2....
## 1 Media
## 2 Varianza
## 3 Covarianza (t, t - 1)
## 4 Covarianza (t, t - 2)
## 5 Covarianza (t, t - 3)
## 6 Autocorrelacion(1)
## 7 Autocorrelacion(2)
## 8 Autocorrelacion(3)
## 9 Q(1)
## 10 Q(3)*
Desarrollamos la tabla
names(Tabla_Pax_Metro1)
## [1] "matrix.c..Media....Varianza....Covarianza..t..t...1.....Covarianza..t..t...2...."
names(Tabla_Pax_Metro1) <- c("Estadística")
Tabla_Pax_Metro1
## Estadística
## 1 Media
## 2 Varianza
## 3 Covarianza (t, t - 1)
## 4 Covarianza (t, t - 2)
## 5 Covarianza (t, t - 3)
## 6 Autocorrelacion(1)
## 7 Autocorrelacion(2)
## 8 Autocorrelacion(3)
## 9 Q(1)
## 10 Q(3)*
Tabla_Pax_Metro1$Valor <- 0
Tabla_Pax_Metro1
## Estadística Valor
## 1 Media 0
## 2 Varianza 0
## 3 Covarianza (t, t - 1) 0
## 4 Covarianza (t, t - 2) 0
## 5 Covarianza (t, t - 3) 0
## 6 Autocorrelacion(1) 0
## 7 Autocorrelacion(2) 0
## 8 Autocorrelacion(3) 0
## 9 Q(1) 0
## 10 Q(3)* 0
Llenado de la tabla
Tabla_Pax_Metro1[1, 2] <- round(mean(Pax_Metro1), digits = 5)
Tabla_Pax_Metro1[2, 2] <- round(var(Pax_Metro1), digits = 5)
Tabla_Pax_Metro1[3, 2] <- round(cov(Pax_Metro1[2:234], Pax_Metro1[1:233]), digits = 5)
Tabla_Pax_Metro1[4, 2] <- round(cov(Pax_Metro1[3:234], Pax_Metro1[1:232]), digits = 5)
Tabla_Pax_Metro1[5, 2] <- round(cov(Pax_Metro1[4:234], Pax_Metro1[1:231]), digits = 5)
Tabla_Pax_Metro1[6, 2] <- round(Tabla_Pax_Metro1[3, 2]/Tabla_Pax_Metro1[2, 2], digits = 4)
Tabla_Pax_Metro1[7, 2] <- round(Tabla_Pax_Metro1[4, 2]/Tabla_Pax_Metro1[2, 2], digits = 4)
Tabla_Pax_Metro1[8, 2] <- round(Tabla_Pax_Metro1[5, 2]/Tabla_Pax_Metro1[2, 2], digits = 4)
Tabla_Pax_Metro1[9, 2] <- round(234*(Tabla_Pax_Metro1[6, 2]^2), digits = 5)
Tabla_Pax_Metro1[10, 2] <- round(234*(Tabla_Pax_Metro1[6, 2]^2 +
Tabla_Pax_Metro1[7, 2]^2 +
Tabla_Pax_Metro1[8, 2]^2), digits = 5)
Visualizacion de la tabla Final
Tabla_Pax_Metro1
## Estadística Valor
## 1 Media 124.29820
## 2 Varianza 103.63943
## 3 Covarianza (t, t - 1) 63.10882
## 4 Covarianza (t, t - 2) 72.90575
## 5 Covarianza (t, t - 3) 63.68589
## 6 Autocorrelacion(1) 0.60890
## 7 Autocorrelacion(2) 0.70350
## 8 Autocorrelacion(3) 0.61450
## 9 Q(1) 86.75766
## 10 Q(3)* 290.92792
Gráfica de la Funcion de Autocorrelacion:
acf(Pax_Metro1,
lag.max = 150,
xlab = 'Resagos k en meses',
main="Funcion de Autocorrelación del número de pasajeros del metro")
Estadística Q
Box.test(Pax_Metro1, lag = 1)
##
## Box-Pierce test
##
## data: Pax_Metro1
## X-squared = 86.02, df = 1, p-value < 2.2e-16
Complemento Ejercicio 5 inciso b Propuesta con las bases de datos anteriores
Insertamos los datos
library(stats)
Datos1 <- read_excel("Base_Transporte2.xlsx")
Datos <- read_excel("Base_Transporte.xlsx")
Desarrollamos la serie en niveles de la base Datos
Pax_Nal <- ts(Datos$Pax_Nal,
start = c(2000, 1),
freq = 12)
# Logaritmos:
LPax_Nal <- ts(log(Datos$Pax_Nal),
start = c(2000, 1),
freq = 12)
# Diferencias mensuales:
DLPax_Nal <- ts(log(Datos$Pax_Nal) -
lag(log(Datos$Pax_Nal), 1),
start = c(2000, 1), freq = 12)
Ahora lo hacemos con la base Datos1 Enero 2000 - Jun 2019
Pax_Nal_1 <- ts(Datos1$Pax_Nal,
start = c(2000, 1),
freq = 12)
# Logaritmos:
L_Pax_Nal <- ts(log(Datos1$Pax_Nal),
start = c(2000, 1),
freq = 12)
# Diferencias mensuales:
DL_Pax_Nal <- ts(log(Datos1$Pax_Nal) -
lag(log(Datos1$Pax_Nal), 1),
start = c(2000, 1), freq = 12)
Realizamos las graficas de los panoramas establecidos
Graficas Base Datos
par(mfrow=c(3,1)) #Division del panel de graficas
plot(Pax_Nal, xlab = "Tiempo", ylab = "Pasajeros",
main = "Pasajeros en vuelos nacionales de salida",
col = "darkgreen")
plot(LPax_Nal, xlab = "Tiempo", ylab = "LN Pasajeros",
main = "LN Pasajeros en vuelos nacionales de salida",
col = "darkblue")
plot(DLPax_Nal, xlab = "Tiempo", ylab = "DLN Pasajeros",
main = "Diff LN Pasajeros en vuelos nacionales de salida",
col = "darkred")
par(mfrow=c(1,1)) #Volvemos al panel de graficas original
Graficas Base Datos1 Enero 2000 - Jun 2019
par(mfrow=c(3,1)) #Division del panel de graficas
plot(Pax_Nal_1, xlab = "Tiempo", ylab = "Pasajeros",
main = "Pasajeros en vuelos nacionales de salida",
col = "cyan4")
plot(L_Pax_Nal, xlab = "Tiempo", ylab = "LN Pasajeros",
main = "LN Pasajeros en vuelos nacionales de salida",
col = "darkslategray")
plot(DL_Pax_Nal, xlab = "Tiempo", ylab = "DLN Pasajeros",
main = "Diff LN Pasajeros en vuelos nacionales de salida",
col = "deeppink4")
Regresamos al panel orginal
par(mfrow=c(1,1))
Para la base Datos
estimaremos un \(AR(2)\) para la serie en niveles bajo una transformación logarítmica \((𝐿𝑃 𝑎𝑥𝑁 𝑎𝑙_{t})\) y en diferencias logaritmitcas \((𝐷𝐿𝑃 𝑎𝑥_{N} 𝑎𝑙_{t})\)
source("arroots.R")
source("plot.armaroots.R")
AR_LPax_Nal <- arima(LPax_Nal, order = c(2, 0, 0),
method = "ML")
AR_DLPax_Nal <- arima(DLPax_Nal, order = c(2, 0, 0),
method = "ML")
par(mfrow=c(1,2))
plot.armaroots(arroots(AR_LPax_Nal),
main="Inverse AR roots of \nAR(2): LN Pax Nal")
#
plot.armaroots(arroots(AR_DLPax_Nal),
main="Inverse AR roots of \nAR(2): Diff LN Pax Nal")
par(mfrow=c(1,1))
Realizaremos el mismo ejercicio para la base Datos 1 #Posible error con el modelo arima para la base datos1
source("arroots.R")
source("plot.armaroots.R")
AR1_LPax_Nal <- arima(L_Pax_Nal, order = c(2, 0, 0),
method = "ML")
AR1_DL_Pax_Nal <- arima(DL_Pax_Nal, order = c(2, 0, 0),
method = "ML")
par(mfrow=c(1,2))
plot.armaroots(arroots(AR1_LPax_Nal),
main="Inverse AR roots of \nAR(2): LN Pax Nal")
plot.armaroots(arroots(AR1_DL_Pax_Nal),
main="Inverse AR roots of \nAR(2): Diff LN Pax Nal")
par(mfrow=c(1,1))