# Abriendo librerias
library(readxl)
library(sqldf)
## Loading required package: gsubfn
## Loading required package: proto
## Loading required package: RSQLite
library(moments)
library(tseries)
library(forecast)
library(ggplot2)
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(lmtest)

# Asignando opciones al Script
options(digits = 2)
options(OutDec= ",")
#options(OutDec= ",")  # restore default value
sep.miles <- function(x){
  format(x,big.mark=".")}
fmt.porcen <- function(x){
  paste(format(round(x,2),decimal.mark = ","),'%')}

# Descargando la Base de Datos
BBDD <- read_excel("C:/Users/samora/OneDrive/Estudios/universidad/Desarrollo económico/Taller/BBDD.xls", 
                   sheet = "pwt71_wo_country_names_wo_g_var")
DEF <- read_excel("C:/Users/samora/OneDrive/Estudios/universidad/Desarrollo económico/Taller/BBDD.xls", 
                  sheet = "pwt70_vars")
COLDATA <- sqldf("SELECT * FROM BBDD WHERE isocode in ('COL')")
ARGDATA <- sqldf("SELECT * FROM BBDD WHERE isocode in ('ARG')")
BRADATA <- sqldf("SELECT * FROM BBDD WHERE isocode in ('BRA')")

# Sustrayendo inofmración necesaria de la Base de datos, lenguaje usado en SQL
COL <- sqldf("SELECT year, POP, rgdpch, rgdpwok, ki FROM COLDATA WHERE isocode in ('COL')")
sep.miles(COL) # separando valores en miles
##     year    POP rgdpch rgdpwok ki
## 1  1.950 11.592  2.624   8.157 20
## 2  1.951 11.965  2.583   8.107 19
## 3  1.952 12.351  2.672   8.485 19
## 4  1.953 12.750  2.770   8.885 20
## 5  1.954 13.162  2.912   9.435 23
## 6  1.955 13.588  2.944   9.646 23
## 7  1.956 14.029  2.923   9.674 23
## 8  1.957 14.486  2.854   9.555 20
## 9  1.958 14.958  2.780   9.407 17
## 10 1.959 15.447  2.864   9.803 17
## 11 1.960 15.953  2.938  10.172 19
## 12 1.961 16.476  3.026  10.533 20
## 13 1.962 17.010  3.073  10.752 18
## 14 1.963 17.546  3.075  10.816 16
## 15 1.964 18.090  3.234  11.434 17
## 16 1.965 18.646  3.148  11.189 17
## 17 1.966 19.202  3.249  11.610 17
## 18 1.967 19.764  3.257  11.702 17
## 19 1.968 20.322  3.380  12.209 19
## 20 1.969 20.869  3.512  12.757 18
## 21 1.970 21.430  3.663  13.375 19
## 22 1.971 21.993  3.801  13.764 19
## 23 1.972 22.543  3.936  14.138 17
## 24 1.973 23.069  4.086  14.555 18
## 25 1.974 23.598  4.272  15.094 20
## 26 1.975 24.125  4.211  14.760 16
## 27 1.976 24.638  4.366  15.180 17
## 28 1.977 25.119  4.497  15.514 19
## 29 1.978 25.575  4.783  16.371 18
## 30 1.979 26.072  4.905  16.658 18
## 31 1.980 26.631  5.030  16.949 19
## 32 1.981 27.215  5.090  16.885 20
## 33 1.982 27.826  5.055  16.474 21
## 34 1.983 28.455  4.985  15.996 20
## 35 1.984 29.095  4.990  15.752 19
## 36 1.985 29.748  4.959  15.450 17
## 37 1.986 30.410  5.081  15.609 17
## 38 1.987 31.085  5.223  15.841 17
## 39 1.988 31.771  5.330  15.968 18
## 40 1.989 32.461  5.344  15.787 16
## 41 1.990 33.147  5.410  15.817 15
## 42 1.991 33.832  5.354  15.515 14
## 43 1.992 34.520  5.574  16.071 19
## 44 1.993 35.206  5.797  16.592 23
## 45 1.994 35.888  5.967  16.557 25
## 46 1.995 36.532  6.167  16.566 25
## 47 1.996 37.098  6.097  15.867 22
## 48 1.997 37.620  6.164  15.549 21
## 49 1.998 38.133  6.082  14.878 20
## 50 1.999 38.564  5.673  13.464 13
## 51 2.000 38.910  5.794  13.350 14
## 52 2.001 39.312  5.833  13.056 15
## 53 2.002 39.805  5.901  12.837 16
## 54 2.003 40.351  6.055  12.826 17
## 55 2.004 40.922  6.286  13.632 18
## 56 2.005 41.488  6.491  14.076 20
## 57 2.006 42.046  6.830  15.179 22
## 58 2.007 42.597  7.206  16.119 23
## 59 2.008 43.141  7.356  16.232 24
## 60 2.009 43.677  7.387  15.627 23
## 61 2.010 44.205  7.536  15.761 25
ARG <- sqldf("SELECT year, POP, rgdpch, rgdpwok, ki FROM ARGDATA WHERE isocode in ('ARG')")
sep.miles(ARG) # separando valores en miles
##     year    POP rgdpch rgdpwok ki
## 1  1.950 17.150  5.258  12.120 16
## 2  1.951 17.517  5.464  12.657 22
## 3  1.952 17.877  5.021  11.692 20
## 4  1.953 18.231  5.152  12.060 20
## 5  1.954 18.581  5.231  12.308 19
## 6  1.955 18.928  5.520  13.052 20
## 7  1.956 19.272  5.516  13.113 18
## 8  1.957 19.611  5.675  13.564 19
## 9  1.958 19.947  5.969  14.339 19
## 10 1.959 20.281  5.549  13.403 19
## 11 1.960 20.616  6.043  14.673 25
## 12 1.961 20.951  6.164  14.980 26
## 13 1.962 21.284  6.111  14.866 24
## 14 1.963 21.616  5.783  14.081 20
## 15 1.964 21.949  6.171  15.040 24
## 16 1.965 22.283  6.692  16.324 23
## 17 1.966 22.612  6.607  16.133 22
## 18 1.967 22.934  6.709  16.397 22
## 19 1.968 23.261  6.928  16.949 23
## 20 1.969 23.600  7.485  18.328 26
## 21 1.970 23.962  7.617  18.669 25
## 22 1.971 24.364  7.825  19.224 27
## 23 1.972 24.780  7.824  19.269 27
## 24 1.973 25.210  7.933  19.585 25
## 25 1.974 25.646  8.206  20.308 24
## 26 1.975 26.082  8.043  19.951 24
## 27 1.976 26.531  7.936  19.735 26
## 28 1.977 26.984  8.396  20.929 29
## 29 1.978 27.440  7.926  19.805 25
## 30 1.979 27.902  8.401  21.043 26
## 31 1.980 28.370  8.496  21.333 28
## 32 1.981 28.848  7.877  19.690 24
## 33 1.982 29.330  7.601  18.935 21
## 34 1.983 29.794  7.769  19.408 20
## 35 1.984 30.231  7.647  19.119 17
## 36 1.985 30.672  6.997  17.354 15
## 37 1.986 31.145  7.611  19.011 18
## 38 1.987 31.625  7.693  19.246 21
## 39 1.988 32.099  7.539  18.786 21
## 40 1.989 32.572  6.901  17.028 18
## 41 1.990 33.036  6.928  16.995 16
## 42 1.991 33.504  7.410  18.268 17
## 43 1.992 33.967  7.835  19.362 19
## 44 1.993 34.407  8.161  20.101 21
## 45 1.994 34.847  8.509  20.893 22
## 46 1.995 35.274  8.323  20.207 20
## 47 1.996 35.683  8.642  20.939 21
## 48 1.997 36.102  9.156  22.152 23
## 49 1.998 36.518  9.413  22.705 23
## 50 1.999 36.923  9.062  21.790 20
## 51 2.000 37.336  8.909  21.344 19
## 52 2.001 37.694  8.556  20.347 17
## 53 2.002 37.999  7.916  18.677 12
## 54 2.003 38.337  8.446  19.769 15
## 55 2.004 38.742  8.977  19.705 18
## 56 2.005 39.181  9.671  21.100 19
## 57 2.006 39.614 10.322  22.425 21
## 58 2.007 40.049 11.003  23.922 22
## 59 2.008 40.482 11.483  25.031 24
## 60 2.009 40.914 11.593  25.300 22
## 61 2.010 41.343 12.340  27.149 24
BRA <- sqldf("SELECT year, POP, rgdpch, rgdpwok, ki FROM BRADATA WHERE isocode in ('BRA')")
sep.miles(BRA) # separando valores en miles
##     year     POP rgdpch rgdpwok ki
## 1  1.950  53.443  1.710   4.927 25
## 2  1.951  54.996  1.749   5.061 29
## 3  1.952  56.603  1.866   5.424 30
## 4  1.953  58.266  1.876   5.473 23
## 5  1.954  59.989  1.987   5.818 25
## 6  1.955  61.774  2.051   6.029 24
## 7  1.956  63.632  2.052   6.060 22
## 8  1.957  65.551  2.211   6.553 26
## 9  1.958  67.533  2.277   6.773 25
## 10 1.959  69.580  2.392   7.144 28
## 11 1.960  71.695  2.483   7.448 25
## 12 1.961  73.833  2.752   8.228 24
## 13 1.962  76.039  2.813   8.383 24
## 14 1.963  78.317  2.894   8.597 20
## 15 1.964  80.667  2.936   8.693 21
## 16 1.965  83.093  3.062   9.038 22
## 17 1.966  85.557  3.093   9.100 22
## 18 1.967  88.050  3.166   9.284 20
## 19 1.968  90.569  3.432  10.032 21
## 20 1.969  93.114  3.581  10.436 28
## 21 1.970  95.684  3.853  11.194 25
## 22 1.971  98.265  4.216  12.121 26
## 23 1.972 100.874  4.607  13.105 27
## 24 1.973 103.513  5.114  14.397 28
## 25 1.974 106.182  5.433  15.139 30
## 26 1.975 108.879  5.536  15.270 31
## 27 1.976 111.603  6.023  16.449 30
## 28 1.977 114.373  6.101  16.495 28
## 29 1.978 117.207  6.251  16.736 28
## 30 1.979 120.101  6.549  17.364 27
## 31 1.980 123.020  6.960  18.275 29
## 32 1.981 125.992  6.385  16.529 27
## 33 1.982 129.028  6.302  16.178 25
## 34 1.983 131.960  5.914  14.993 21
## 35 1.984 134.699  5.999  15.137 21
## 36 1.985 137.382  6.143  15.446 22
## 37 1.986 140.196  6.758  16.868 24
## 38 1.987 143.027  6.765  16.495 23
## 39 1.988 145.873  6.548  15.883 22
## 40 1.989 148.659  6.474  15.612 19
## 41 1.990 151.170  6.145  14.673 18
## 42 1.991 153.584  6.107  13.977 20
## 43 1.992 156.032  5.996  13.161 18
## 44 1.993 158.512  6.109  13.331 19
## 45 1.994 161.017  6.369  13.757 20
## 46 1.995 163.544  6.646  14.208 23
## 47 1.996 166.086  6.694  14.627 23
## 48 1.997 168.639  6.830  14.639 25
## 49 1.998 171.201  6.719  14.279 24
## 50 1.999 173.764  6.631  13.836 21
## 51 2.000 176.320  6.839  14.250 22
## 52 2.001 178.870  6.822  14.205 21
## 53 2.002 181.418  6.856  14.008 19
## 54 2.003 183.960  6.818  13.855 18
## 55 2.004 186.489  7.118  14.224 19
## 56 2.005 188.993  7.234  14.236 18
## 57 2.006 191.469  7.432  14.611 19
## 58 2.007 193.919  7.791  15.321 21
## 59 2.008 196.343  8.088  15.771 23
## 60 2.009 198.739  7.977  15.421 20
## 61 2.010 201.103  8.324  15.975 23
# Valores faltantes en el Data COL
COL$Y <- c(COL$POP * COL$rgdpwok)
COL$Inversión <- c(COL$Y * COL$ki)
lim <- NROW(COL$ki)
COL$mean.ki <- mean(COL$ki[2:lim])
COL$K <- c(c(COL$mean.ki * COL$Y * 0.01) / 0.1)
COL$PIBperCapita <- c(COL$Y / COL$POP)
COL$L <- c(COL$Y / c(COL$rgdpch * COL$rgdpwok))
COL$A <- c(COL$Y / c((COL$K ^ 0.36) * c(COL$L ^ c(1 - 0.36))))

# Valores faltantes en el Data ARG
ARG$Y <- c(ARG$POP * ARG$rgdpwok)
ARG$Inversión <- c(ARG$Y * ARG$ki)
lim <- NROW(ARG$ki)
ARG$mean.ki <- mean(ARG$ki[2:lim])
ARG$K <- c(c(ARG$mean.ki * ARG$Y * 0.01) / 0.1)
ARG$PIBperCapita <- c(ARG$Y / ARG$POP)
ARG$L <- c(ARG$Y / c(ARG$rgdpch * ARG$rgdpwok))
ARG$A <- c(ARG$Y / c((ARG$K ^ 0.36) * c(ARG$L ^ c(1 - 0.36))))

# Valores faltantes en el Data BRA
BRA$Y <- c(BRA$POP * BRA$rgdpwok)
BRA$Inversión <- c(BRA$Y * BRA$ki)
lim <- NROW(BRA$ki)
BRA$mean.ki <- mean(BRA$ki[2:lim])
BRA$K <- c(c(BRA$mean.ki * BRA$Y * 0.01) / 0.1)
BRA$PIBperCapita <- c(BRA$Y / BRA$POP)
BRA$L <- c(BRA$Y / c(BRA$rgdpch * BRA$rgdpwok))
BRA$A <- c(BRA$Y / c((BRA$K ^ 0.36) * c(BRA$L ^ c(1 - 0.36))))

#COMPARANDO VARIABLES ENTRE PAISES
Y.PLOT <- ggplot(data = COL,
                 aes(x=COL$year, y=COL$Y, colour="PIB de COL")
                 )+geom_point()+geom_point(
                   data = ARG, aes(x=ARG$year, y=ARG$Y, colour="PIB de ARG")
                 )+geom_point(
                   data = BRA, aes(x=BRA$year, y=BRA$Y, colour="PIB de BRA")
                 )+ggtitle("Comparación de PIB de Colombia contra Argentina y Brazil"
                           )+xlab("Year")+ylab("PIB")
Y.PLOT

Inversión.PLOT <- ggplot(data = COL,
                         aes(x=COL$year, y=COL$Inversión, colour="PIB de COL")
)+geom_point()+geom_point(
  data = ARG, aes(x=ARG$year, y=ARG$Inversión, colour="PIB de ARG")
)+geom_point(
  data = BRA, aes(x=BRA$year, y=BRA$Inversión, colour="PIB de BRA")
)+ggtitle("Comparación de Inversion de Colombia contra Argentina y Brazil"
)+xlab("year")+ylab("Inversión")
Inversión.PLOT

L.PLOT <- ggplot(data = COL,
                 aes(x=COL$year, y=COL$L, colour="PIB de COL")
)+geom_point()+geom_point(
  data = ARG, aes(x=ARG$year, y=ARG$L, colour="PIB de ARG")
)+geom_point(
  data = BRA, aes(x=BRA$year, y=BRA$L, colour="PIB de BRA")
)+ggtitle("Comparación de L de Colombia contra Argentina y Brazil"
)+xlab("Year")+ylab("L")
L.PLOT

K.PLOT <- ggplot(data = COL,
                 aes(x=COL$year, y=COL$K, colour="PIB de COL")
)+geom_point()+geom_point(
  data = ARG, aes(x=ARG$year, y=ARG$K, colour="PIB de ARG")
)+geom_point(
  data = BRA, aes(x=BRA$year, y=BRA$K, colour="PIB de BRA")
)+ggtitle("Comparación de K de Colombia contra Argentina y Brazil"
)+xlab("Year")+ylab("K")
K.PLOT

# Comparando Ciclos de inversión y K entre Brazil y Colombia
PrimeraDiff.col <- diff(COL$Inversión, 2)
PrimeraDiff.arg <- diff(ARG$Inversión, 2)
PrimeraDiff.bra <- diff(BRA$Inversión, 2)
PrimeraDiffK.col <- diff(COL$K, 2)
PrimeraDiffK.arg <- diff(ARG$K, 2)
PrimeraDiffK.bra <- diff(BRA$K, 2)
trend <- c(1:59)
Differencies.col <- data.frame(PrimeraDiff.col, trend)
Differencies.arg <- data.frame(PrimeraDiff.arg, trend)
Differencies.bra <- data.frame(PrimeraDiff.bra, trend)
DifferenciesK.col <- data.frame(PrimeraDiffK.col, trend)
DifferenciesK.arg <- data.frame(PrimeraDiffK.arg, trend)
DifferenciesK.bra <- data.frame(PrimeraDiffK.bra, trend)


Inversión.PLOT.diff <- ggplot(data = Differencies.col,
                              aes(x=Differencies.col$trend,
                                  y=Differencies.col$PrimeraDiff.col, colour="PIB de COL")
)+geom_line()+geom_line(
  data = Differencies.arg,
  aes(x=Differencies.arg$trend,
      y=Differencies.arg$PrimeraDiff.arg, colour="PIB de ARG")
)+geom_line(
  data = Differencies.bra, aes(x=Differencies.bra$trend, y=Differencies.bra$PrimeraDiff.bra, colour="PIB de BRA")
)+ggtitle("Primeras diferencias Inversion de Colombia contra Argentina y Brazil"
)+xlab("year")+ylab("Inversión")
Inversión.PLOT.diff

K.PLOT.diff <- ggplot(data = DifferenciesK.col,
                      aes(x=DifferenciesK.col$trend,
                          y=DifferenciesK.col$PrimeraDiffK.col, colour="PIB de COL")
)+geom_line()+geom_line(
  data = DifferenciesK.arg,
  aes(x=DifferenciesK.arg$trend,
      y=DifferenciesK.arg$PrimeraDiffK.arg, colour="PIB de ARG")
)+geom_line(
  data = DifferenciesK.bra, aes(x=DifferenciesK.bra$trend, y=DifferenciesK.bra$PrimeraDiffK.bra, colour="PIB de BRA")
)+ggtitle("Primeras diferencias K de Colombia contra Argentina y Brazil"
)+xlab("year")+ylab("K")
K.PLOT.diff

# Crendo metodo para hacer vectores
Vectores <- function(x,y){
  X <- data.frame(x, check.names = TRUE)
  names(X)[1] <- y
  return(X)
}

# Asignando nombres a los vectores
POP <- Vectores(COL$POP,"POP")
rgdpch <- Vectores(COL$rgdpch, "rgdpch")
rgdpwok <- Vectores(COL$rgdpwok, "rgdpwok")
ki <- Vectores(COL$ki, "ki")
year <- Vectores(COL$year, "Year")
Y <- Vectores(COL$Y, "Y")
Inversion <- Vectores(COL$Inversión, "Inversión")
K <- Vectores(COL$K, "K")
PIBprCapita <- Vectores(COL$PIBperCapita, "Pib per Capita")
L <- Vectores(COL$L, "L")
A <- Vectores(COL$A, "A")


#Analsis descreiptivo por variable, creando un metodo
Descriptivo <- function(x){
  numeroFilas <- NROW(x)
  X <- x[2:numeroFilas,]
  as.numeric(X)
  library(moments)
  medianx <- median(X)
  meanx <- mean(X)
  sdx <- sd(X)
  kurtosisx <- kurtosis(X)
  skewnessx <- skewness(X)
  minx <- min(X)
  maxx <- max(X)
  #quantilex <- quantile(x)
  Descriptivox <- data.frame(medianx,
                             meanx,
                             sdx,
                             kurtosisx,
                             skewnessx,
                             minx,maxx)
  return(Descriptivox)
}

# Aplicando metodo a variables
POP.Descriptivo <- Descriptivo(POP)
rgdpch.Descriptivo <- Descriptivo(rgdpch)
rgdpwok.Descriptivo <- Descriptivo(rgdpwok)
ki.Descriptivo <- Descriptivo(ki)
Y.Descriptivo <- Descriptivo(Y)
Inversion.Descriptivo <- Descriptivo(Inversion)
K.Descriptivo <- Descriptivo(K)
PIBprCapita.Descriptivo <- Descriptivo(PIBprCapita)
L.Descriptivo <- Descriptivo(L)
A.Descriptivo <- Descriptivo(A)


#Creando matriz con análisis descriptivo
matrix.desciptivo <- rbind(sep.miles(POP.Descriptivo),
                           sep.miles(rgdpch.Descriptivo),
                           sep.miles(rgdpwok.Descriptivo),
                           sep.miles(ki.Descriptivo),
                           format(Y.Descriptivo, scientific=FALSE, big.mark="."),
                           format(Inversion.Descriptivo, scientific=FALSE, big.mark="."),
                           format(K.Descriptivo, scientific=FALSE, big.mark="."),
                           sep.miles(PIBprCapita.Descriptivo),
                           sep.miles(L.Descriptivo),
                           sep.miles(A.Descriptivo))
variables <- c("POP", "rgdpch", "rgdpwok", "ki",
               "Y", "Inversion", "K", "PIB per Capita", "L", "A")
row.names(matrix.desciptivo) <- variables
matrix.desciptivo
##                      medianx         meanx           sdx kurtosisx
## POP                   26.923        27.608         9.895       1,7
## rgdpch                 4.972         4.696         1.419       1,9
## rgdpwok               14.657        13.698         2.611         2
## ki                        19            19           2,8       2,7
## Y                453.267.876   396.435.735   181.177.128       1,8
## Inversion      7.474.394.020 7.622.098.473 4.158.897.811       2,6
## K                859.734.929   751.938.681   343.647.351       1,8
## PIB per Capita        14.657        13.698         2.611         2
## L                        5,9           5,8          0,54       3,2
## A                     88.196        79.265        22.945       1,8
##                skewnessx          minx           maxx
## POP                0,062        11.965         44.205
## rgdpch              0,14         2.583          7.536
## rgdpwok            -0,62         8.107         16.949
## ki                  0,38            13             25
## Y                  -0,18    97.005.027    700.272.290
## Inversion           0,63 1.815.566.547 17.424.074.687
## K                  -0,18   183.994.090  1.328.240.052
## PIB per Capita     -0,62         8.107         16.949
## L                  -0,45           4,5            6,8
## A                   -0,3        38.488        117.271
# Aplicando descriptivo por Cuantiles
summary(COL)
##       year           POP            rgdpch        rgdpwok     
##  Min.   :1950   Min.   :11592   Min.   :2583   Min.   : 8107  
##  1st Qu.:1965   1st Qu.:18646   1st Qu.:3234   1st Qu.:11434  
##  Median :1980   Median :26631   Median :4959   Median :14555  
##  Mean   :1980   Mean   :27345   Mean   :4662   Mean   :13607  
##  3rd Qu.:1995   3rd Qu.:36532   3rd Qu.:5797   3rd Qu.:15817  
##  Max.   :2010   Max.   :44205   Max.   :7536   Max.   :16949  
##        ki             Y              Inversión           mean.ki  
##  Min.   :12,9   Min.   :9,46e+07   Min.   :1,82e+09   Min.   :19  
##  1st Qu.:17,2   1st Qu.:2,09e+08   1st Qu.:3,55e+09   1st Qu.:19  
##  Median :18,6   Median :4,51e+08   Median :7,34e+09   Median :19  
##  Mean   :19,0   Mean   :3,91e+08   Mean   :7,53e+09   Mean   :19  
##  3rd Qu.:20,3   3rd Qu.:5,24e+08   3rd Qu.:9,34e+09   3rd Qu.:19  
##  Max.   :25,0   Max.   :7,00e+08   Max.   :1,74e+10   Max.   :19  
##        K             PIBperCapita         L             A         
##  Min.   :1,79e+08   Min.   : 8107   Min.   :4,4   Min.   : 38488  
##  1st Qu.:3,96e+08   1st Qu.:11434   1st Qu.:5,4   1st Qu.: 55382  
##  Median :8,56e+08   Median :14555   Median :5,9   Median : 88107  
##  Mean   :7,43e+08   Mean   :13607   Mean   :5,8   Mean   : 78606  
##  3rd Qu.:9,94e+08   3rd Qu.:15817   3rd Qu.:6,1   3rd Qu.: 94758  
##  Max.   :1,33e+09   Max.   :16949   Max.   :6,8   Max.   :117271
# Creando Metodo para graficar Serie de Tiempo
grafico <- function(X){
  tittle <- paste("variable", names(X))
  color <- paste("",names(X),"")
  
  plot.X <- ggplot(data = COL,
                   aes(y = X,
                       x = year,
                       colour=color))+geom_line(
                         
                       )+geom_point(
                         
                       )+ggtitle(
                         tittle
                       )+xlab(
                         "Year"
                       )+ylab(
                         color)
  return(plot.X)
}

# Grafico de Serie de tiempo
POP.grafico <- grafico(POP)
POP.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

rgdpch.grafico <- grafico(rgdpch)
rgdpch.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

rgdpwok.grafico <- grafico(rgdpwok)
rgdpwok.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

ki.grafico <- grafico(ki)
ki.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

Y.grafico <- grafico(Y)
Y.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

Inversion.grafico <- grafico(Inversion)
Inversion.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

K.grafico <- grafico(K)
K.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

PIBprCapita.grafico <- grafico(PIBprCapita)
PIBprCapita.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

L.grafico <- grafico(L)
L.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

A.grafico <- grafico(A)
A.grafico
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

# Creando Metodo para graficar Histograma
histograma <- function(x){
  tittle <- paste("variable", names(x))
  color <- paste("",names(x),"")
  
  plot.histo <- ggplot(data = COL,
                       aes(x),
                       colour=color
                       )+geom_freqpoly(
                         binwidth = 500
                       )+geom_histogram(
                         binwidth = 500
                       )+scale_y_sqrt(
                       )+ggtitle(
                         tittle
                       )+xlab(
                         "Year"
                       )+ylab(
                         color)
  #Plot.histox <- hist(x[2:61,])
  return(plot.histo)
}

# Finalmente el loop es
# for(var in seq_along(variables.list)) {
#   # Graficando Histogramas
#   POP$Type <- c("POP")
#   names(POP)[1] <- "Variable"
#   rgdpch$Type <- c("rgdpch")
#   names(rgdpch)[1] <- "Variable"
#   rgdpwok$Type <- c("rgdpwok")
#   names(rgdpwok)[1] <- "Variable"
#   ki$Type <- c("ki")
#   names(ki)[1] <- "Variable"
#   COLvar <- as.data.frame(rbind(POP, rgdpch, rgdpwok, ki))
#   variables.list <- unique(COLvar$Type)
#   
#   C = 1 # Contadora dentro del loop
#  var <- variables.list[C]
#  histo <- subset(COLvar, Type == var)
#  histogr <- histograma(histo$Variable)
#  print(histogr)
#  C = C + 1
#}
# Formateando valores grandes
Y <- format(Y, scientific=FALSE, big.mark=".")
K <- format(K, scientific=FALSE, big.mark=".")
Inversion <- format(Inversion, scientific=FALSE, big.mark=".")


POP.histograma <- histograma(POP)
rgdpch.histograma <- histograma(rgdpch)
rgdpwok.histograma <- histograma(rgdpwok)
ki.histograma <- histograma(ki)
Y.histograma <- histograma(Y)
Inversion.histograma <- histograma(Inversion)
K.histograma <- histograma(K)
PIBprCapita.histograma <- histograma(PIBprCapita)
L.histograma <- histograma(L)
A.histograma <- histograma(A)

# Creando metodo para graficar relaciones individuales
relacion <- function(X,Y){
  tittle <- paste("Relación entre ", names(X), "-",names(Y))
  colorUno <- paste("",names(X),"")
  colorDos <- paste("",names(Y),"")
  color <- paste(names(X),"-",names(Y))
  
  Relacion <- ggplot(data = COL,
                   aes(y = X,
                       x = Y,
                       colour=color))+geom_point(
                         
                       )+ggtitle(
                         tittle
                       )+xlab(
                         colorDos
                       )+ylab(
                         colorUno)
  
  return(Relacion)
}

# Relaciones Globales
pairs(COL)

cov(COL)
##                 year     POP  rgdpch rgdpwok       ki       Y Inversión
## year         3,2e+02 1,8e+05 2,5e+04 3,6e+04  5,2e+00 3,2e+09   6,6e+10
## POP          1,8e+05 1,0e+08 1,4e+07 2,0e+07  3,2e+03 1,8e+12   3,7e+13
## rgdpch       2,5e+04 1,4e+07 2,1e+06 3,0e+06  9,5e+02 2,6e+11   5,7e+12
## rgdpwok      3,6e+04 2,0e+07 3,0e+06 7,2e+06  9,3e+02 4,2e+11   8,9e+12
## ki           5,2e+00 3,2e+03 9,5e+02 9,3e+02  7,8e+00 9,8e+07   5,8e+09
## Y            3,2e+09 1,8e+12 2,6e+11 4,2e+11  9,8e+07 3,4e+16   7,2e+17
## Inversión    6,6e+10 3,7e+13 5,7e+12 8,9e+12  5,8e+09 7,2e+17   1,8e+19
## mean.ki      0,0e+00 0,0e+00 0,0e+00 0,0e+00  0,0e+00 0,0e+00   0,0e+00
## K            6,0e+09 3,4e+12 4,9e+11 8,0e+11  1,9e+08 6,4e+16   1,4e+18
## PIBperCapita 3,6e+04 2,0e+07 3,0e+06 7,2e+06  9,3e+02 4,2e+11   8,9e+12
## L            7,9e+00 4,4e+03 5,4e+02 8,0e+02 -5,5e-01 7,3e+07   1,2e+09
## A            3,9e+05 2,2e+08 3,2e+07 5,7e+07  1,4e+04 4,2e+12   9,2e+13
##              mean.ki       K PIBperCapita        L       A
## year               0 6,0e+09      3,6e+04  7,9e+00 3,9e+05
## POP                0 3,4e+12      2,0e+07  4,4e+03 2,2e+08
## rgdpch             0 4,9e+11      3,0e+06  5,4e+02 3,2e+07
## rgdpwok            0 8,0e+11      7,2e+06  8,0e+02 5,7e+07
## ki                 0 1,9e+08      9,3e+02 -5,5e-01 1,4e+04
## Y                  0 6,4e+16      4,2e+11  7,3e+07 4,2e+12
## Inversión          0 1,4e+18      8,9e+12  1,2e+09 9,2e+13
## mean.ki            0 0,0e+00      0,0e+00  0,0e+00 0,0e+00
## K                  0 1,2e+17      8,0e+11  1,4e+08 8,0e+12
## PIBperCapita       0 8,0e+11      7,2e+06  8,0e+02 5,7e+07
## L                  0 1,4e+08      8,0e+02  3,2e-01 8,3e+03
## A                  0 8,0e+12      5,7e+07  8,3e+03 5,4e+08
cor(COL)
## Warning in cor(COL): the standard deviation is zero
##              year  POP rgdpch rgdpwok    ki    Y Inversión mean.ki    K
## year         1,00 1,00   0,98    0,75  0,10 0,98      0,89      NA 0,98
## POP          1,00 1,00   0,98    0,73  0,12 0,97      0,89      NA 0,97
## rgdpch       0,98 0,98   1,00    0,78  0,24 0,99      0,94      NA 0,99
## rgdpwok      0,75 0,73   0,78    1,00  0,12 0,86      0,79      NA 0,86
## ki           0,10 0,12   0,24    0,12  1,00 0,19      0,49      NA 0,19
## Y            0,98 0,97   0,99    0,86  0,19 1,00      0,94      NA 1,00
## Inversión    0,89 0,89   0,94    0,79  0,49 0,94      1,00      NA 0,94
## mean.ki        NA   NA     NA      NA    NA   NA        NA       1   NA
## K            0,98 0,97   0,99    0,86  0,19 1,00      0,94      NA 1,00
## PIBperCapita 0,75 0,73   0,78    1,00  0,12 0,86      0,79      NA 0,86
## L            0,79 0,78   0,67    0,52 -0,35 0,70      0,51      NA 0,70
## A            0,94 0,93   0,97    0,91  0,22 0,99      0,94      NA 0,99
##              PIBperCapita     L    A
## year                 0,75  0,79 0,94
## POP                  0,73  0,78 0,93
## rgdpch               0,78  0,67 0,97
## rgdpwok              1,00  0,52 0,91
## ki                   0,12 -0,35 0,22
## Y                    0,86  0,70 0,99
## Inversión            0,79  0,51 0,94
## mean.ki                NA    NA   NA
## K                    0,86  0,70 0,99
## PIBperCapita         1,00  0,52 0,91
## L                    0,52  1,00 0,63
## A                    0,91  0,63 1,00
subCOL <- sqldf("SELECT Y,Inversión,K,PIBperCapita,L,A FROM COL")
pairs(subCOL)

cov(subCOL)
##                    Y Inversión       K PIBperCapita       L       A
## Y            3,4e+16    7,2e+17 6,4e+16      4,2e+11 7,3e+07 4,2e+12
## Inversión   7,2e+17    1,8e+19 1,4e+18      8,9e+12 1,2e+09 9,2e+13
## K            6,4e+16    1,4e+18 1,2e+17      8,0e+11 1,4e+08 8,0e+12
## PIBperCapita 4,2e+11    8,9e+12 8,0e+11      7,2e+06 8,0e+02 5,7e+07
## L            7,3e+07    1,2e+09 1,4e+08      8,0e+02 3,2e-01 8,3e+03
## A            4,2e+12    9,2e+13 8,0e+12      5,7e+07 8,3e+03 5,4e+08
cor(subCOL)
##                 Y Inversión    K PIBperCapita    L    A
## Y            1,00       0,94 1,00         0,86 0,70 0,99
## Inversión   0,94       1,00 0,94         0,79 0,51 0,94
## K            1,00       0,94 1,00         0,86 0,70 0,99
## PIBperCapita 0,86       0,79 0,86         1,00 0,52 0,91
## L            0,70       0,51 0,70         0,52 1,00 0,63
## A            0,99       0,94 0,99         0,91 0,63 1,00
# Aplicando relaciones a POP
relacion(POP,rgdpch)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(POP,rgdpwok)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(POP,ki)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

#Aplicandorelacionesargdpch
relacion(rgdpch,POP)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(rgdpch,rgdpwok)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(rgdpch,ki)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

#Aplicandorelacionesargdwok
relacion(rgdpwok,POP)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(rgdpwok,rgdpch)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(rgdpwok,ki)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

#Aplicandorelacionesaki
relacion(ki,POP)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(ki,rgdpch)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

relacion(ki,rgdpwok)
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.

# Haciendo regresión
model <- lm(COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + COL$L + COL$A)
summary(model)
## Warning in summary.lm(model): essentially perfect fit: summary may be
## unreliable
## 
## Call:
## lm(formula = COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + 
##     COL$L + COL$A)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -4,56e-07 -2,39e-08  1,00e-10  2,23e-08  5,97e-07 
## 
## Coefficients:
##                   Estimate Std. Error   t value Pr(>|t|)    
## (Intercept)       4,65e-07   5,58e-07  8,30e-01    0,408    
## COL$Inversión    -2,94e-17   1,30e-17 -2,26e+00    0,028 *  
## COL$K             5,27e-01   9,95e-16  5,30e+14   <2e-16 ***
## COL$PIBperCapita -3,30e-12   3,59e-11 -9,00e-02    0,927    
## COL$L            -3,54e-08   7,36e-08 -4,80e-01    0,633    
## COL$A            -5,20e-12   1,72e-11 -3,00e-01    0,764    
## ---
## Signif. codes:  0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
## 
## Residual standard error: 1,1e-07 on 55 degrees of freedom
## Multiple R-squared:     1,   Adjusted R-squared:     1 
## F-statistic: 3,37e+31 on 5 and 55 DF,  p-value: <2e-16
hist(model$residuals)

#Aplicando test el LM
Durbin.Watson <- dwtest(COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + COL$L + COL$A)
Durbin.Watson
## 
##  Durbin-Watson test
## 
## data:  COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + COL$L + COL$A
## DW = 3, p-value = 0,9
## alternative hypothesis: true autocorrelation is greater than 0
Breusch.Godfrey <- bgtest(COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + COL$L + COL$A)
Breusch.Godfrey
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + COL$L + COL$A
## LM test = 10, df = 1, p-value = 8e-04
Breusch.Pagan <- bptest(COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + COL$L + COL$A)
Breusch.Pagan
## 
##  studentized Breusch-Pagan test
## 
## data:  COL$Y ~ COL$Inversión + COL$K + COL$PIBperCapita + COL$L + COL$A
## BP = 20, df = 5, p-value = 0,002
# Haciendo regresión LOG10
model.lo10 <- lm(log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + log10(COL$PIBperCapita) + log10(COL$L) + log10(COL$A))
summary(model.lo10)
## Warning in summary.lm(model.lo10): essentially perfect fit: summary may be
## unreliable
## 
## Call:
## lm(formula = log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + 
##     log10(COL$PIBperCapita) + log10(COL$L) + log10(COL$A))
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -3,16e-14 -8,70e-16  3,10e-16  1,12e-15  6,33e-15 
## 
## Coefficients: (1 not defined because of singularities)
##                          Estimate Std. Error   t value Pr(>|t|)    
## (Intercept)             -2,78e-01   3,05e-14 -9,10e+12   <2e-16 ***
## log10(COL$Inversión)     1,26e-14   1,29e-14  9,70e-01    0,334    
## log10(COL$K)             1,00e+00   2,03e-14  4,92e+13   <2e-16 ***
## log10(COL$PIBperCapita)  3,32e-14   2,10e-14  1,58e+00    0,120    
## log10(COL$L)             7,23e-14   3,35e-14  2,16e+00    0,035 *  
## log10(COL$A)                   NA         NA        NA       NA    
## ---
## Signif. codes:  0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
## 
## Residual standard error: 4,7e-15 on 56 degrees of freedom
## Multiple R-squared:     1,   Adjusted R-squared:     1 
## F-statistic: 4,41e+28 on 4 and 56 DF,  p-value: <2e-16
hist(model$residuals)

#Aplicando test el LM
Durbin.Watson <- dwtest(log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + log10(COL$PIBperCapita) + log10(COL$L) + log10(COL$A))
## Warning in dwtest(log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) +
## log10(COL$PIBperCapita) + : imaginary parts of eigenvalues discarded
Durbin.Watson
## 
##  Durbin-Watson test
## 
## data:  log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + log10(COL$PIBperCapita) +     log10(COL$L) + log10(COL$A)
## DW = 1, p-value = 2e-10
## alternative hypothesis: true autocorrelation is greater than 0
Breusch.Godfrey <- bgtest(log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + log10(COL$PIBperCapita) + log10(COL$L) + log10(COL$A))
Breusch.Godfrey
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + log10(COL$PIBperCapita) +     log10(COL$L) + log10(COL$A)
## LM test = 5e-04, df = 1, p-value = 1
Breusch.Pagan <- bptest(log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + log10(COL$PIBperCapita) + log10(COL$L) + log10(COL$A))
Breusch.Pagan
## 
##  studentized Breusch-Pagan test
## 
## data:  log10(COL$Y) ~ log10(COL$Inversión) + log10(COL$K) + log10(COL$PIBperCapita) +     log10(COL$L) + log10(COL$A)
## BP = 10, df = 4, p-value = 0,01