library(rio)
ELECTRICITY = import("ELECTRICITY.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
BROADBAND = import("BROADBAND.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
CO2_EMISSIONS = import("CO2 EMISSIONS.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
DEBT_EXTERNAL = import("DEBT EXTERNAL.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
ENERGY_CON_PERCAPITA = import("ENERGY CON PERCAPITA.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
INFLA_RATE = import("INFLA RATE.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
PUBLIC_DEBT = import("PUBLIC DEBT.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
REFINED_PETRO_PRO = import("REFINED PETRO PRO.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
TELE_FIXED_LINES = import("TELE FIXED LINES.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
TELE_MOBILE_CEL = import("TELE MOBILE CEL.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
YOUTH_UNEMPLOY = import("YOUTH UNEMPLOY.csv")
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, : Found
## and resolved improper quoting in first 100 rows. If the fields are not quoted
## (e.g. field separator does not appear within any field), try quote="" to avoid
## this warning.
## Warning in (function (input = "", file = NULL, text = NULL, cmd = NULL, :
## Detected 6 column names but the data has 5 columns. Filling rows automatically.
## Set fill=TRUE explicitly to avoid this warning.
DEBT_EXTERNAL <- DEBT_EXTERNAL[, 1:2]
ELECTRICITY <- ELECTRICITY[, 1:2]
BROADBAND <- BROADBAND[, 1:2]
CO2_EMISSIONS <- CO2_EMISSIONS[, 1:2]
ENERGY_CON_PERCAPITA <- ENERGY_CON_PERCAPITA[, 1:2]
INFLA_RATE <- INFLA_RATE[, 1:2]
PUBLIC_DEBT <- PUBLIC_DEBT[, 1:2]
REFINED_PETRO_PRO <- REFINED_PETRO_PRO[, 1:2]
TELE_FIXED_LINES <- TELE_FIXED_LINES[, 1:2]
TELE_MOBILE_CEL <- TELE_MOBILE_CEL[, 1:2]
YOUTH_UNEMPLOY <- YOUTH_UNEMPLOY[, 1:2]
colnames(DEBT_EXTERNAL) = c("country", "deuda")
colnames(ELECTRICITY) = c("country", "electricidad")
colnames(BROADBAND) = c("country", "banda ancha")
colnames(CO2_EMISSIONS) = c("country", "emisiones co2")
colnames(ENERGY_CON_PERCAPITA) = c("country", "consumo energia")
colnames(INFLA_RATE) = c("country", "tasa inflasion")
colnames(PUBLIC_DEBT) = c("country", "deuda publica")
colnames(REFINED_PETRO_PRO) = c("country", "produccion petroleo")
colnames(TELE_FIXED_LINES) = c("country", "lineas fijas")
colnames(TELE_MOBILE_CEL) = c("country", "lineas mobiles")
colnames(YOUTH_UNEMPLOY) = c("country","desempleo juvenil")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
DATA_CIA = merge(ELECTRICITY, BROADBAND, by = "country")
DATA_CIA = merge(DATA_CIA, CO2_EMISSIONS, by = "country")
DATA_CIA = merge(DATA_CIA, DEBT_EXTERNAL, by = "country")
DATA_CIA = merge(DATA_CIA, ENERGY_CON_PERCAPITA, by = "country")
DATA_CIA = merge(DATA_CIA, INFLA_RATE, by = "country")
DATA_CIA = merge(DATA_CIA, PUBLIC_DEBT, by = "country")
DATA_CIA = merge(DATA_CIA, REFINED_PETRO_PRO, by = "country")
DATA_CIA = merge(DATA_CIA, TELE_FIXED_LINES, by = "country")
DATA_CIA = merge(DATA_CIA, TELE_MOBILE_CEL, by = "country")
DATA_CIA = merge(DATA_CIA, YOUTH_UNEMPLOY, by = "country")
DATA_CIA$electricidad=gsub('"',' ', DATA_CIA$electricidad)
DATA_CIA$`banda ancha`=gsub('"',' ', DATA_CIA$`banda ancha`)
DATA_CIA$`emisiones co2`=gsub('"',' ', DATA_CIA$`emisiones co2`)
DATA_CIA$deuda=gsub('"',' ', DATA_CIA$deuda)
DATA_CIA$`consumo energia`=gsub('"',' ', DATA_CIA$`consumo energia`)
DATA_CIA$`tasa inflasion`=gsub('"',' ', DATA_CIA$`tasa inflasion`)
DATA_CIA$`deuda publica`=gsub('"',' ', DATA_CIA$`deuda publica`)
DATA_CIA$`produccion petroleo`=gsub('"',' ', DATA_CIA$`produccion petroleo`)
DATA_CIA$`lineas fijas`=gsub('"',' ', DATA_CIA$`lineas fijas`)
DATA_CIA$`lineas mobiles`=gsub('"',' ', DATA_CIA$`lineas mobiles`)
DATA_CIA$`desempleo juvenil`=gsub('"',' ', DATA_CIA$`desempleo juvenil`)

#DEUDA EXTERNA HIJUEPUTA TE ODIOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

DATA_CIA$deuda=gsub("\\$","", DATA_CIA$deuda)
DATA_CIA$electricidad=gsub(",","", DATA_CIA$electricidad)
DATA_CIA$`banda ancha`=gsub(",","", DATA_CIA$`banda ancha`)
DATA_CIA$`emisiones co2`=gsub(",","", DATA_CIA$`emisiones co2`)
DATA_CIA$deuda=gsub(","," ", DATA_CIA$deuda)
DATA_CIA$`consumo energia`=gsub(",","", DATA_CIA$`consumo energia`)
DATA_CIA$`produccion petroleo`=gsub(",","", DATA_CIA$`produccion petroleo`)
DATA_CIA$`lineas fijas`=gsub(",","", DATA_CIA$`lineas fijas`)
DATA_CIA$`lineas mobiles`=gsub(",","", DATA_CIA$`lineas mobiles`)
DATA_CIA$`deuda`=gsub(" ","", DATA_CIA$`deuda`)
DATA_CIA$electricidad=as.numeric(DATA_CIA$electricidad)
DATA_CIA$`banda ancha`=as.numeric(DATA_CIA$`banda ancha`)
DATA_CIA$`emisiones co2`=as.numeric(DATA_CIA$`emisiones co2`)
DATA_CIA$`consumo energia`=as.numeric(DATA_CIA$`consumo energia`)
DATA_CIA$`tasa inflasion`=as.numeric(DATA_CIA$`tasa inflasion`)
## Warning: NAs introducidos por coerción
DATA_CIA$`deuda publica`=as.numeric(DATA_CIA$`deuda publica`)
DATA_CIA$`produccion petroleo`=as.numeric(DATA_CIA$`produccion petroleo`)
DATA_CIA$`lineas fijas`=as.numeric(DATA_CIA$`lineas fijas`)
DATA_CIA$`lineas mobiles`=as.numeric(DATA_CIA$`lineas mobiles`)
DATA_CIA$`desempleo juvenil`=as.numeric(DATA_CIA$`desempleo juvenil`)
DATA_CIA$`deuda`=as.numeric(DATA_CIA$`deuda`)
dontselect=c("country")
select=setdiff(names(DATA_CIA),dontselect) 
DATA_FACTO=DATA_CIA[,select]
library(polycor)
corMatrix=polycor::hetcor(DATA_FACTO)$correlations
round(corMatrix,2)
##                     electricidad banda ancha emisiones co2 deuda
## electricidad                1.00        0.96          0.99  0.49
## banda ancha                 0.96        1.00          0.96  0.32
## emisiones co2               0.99        0.96          1.00  0.44
## deuda                       0.49        0.32          0.44  1.00
## consumo energia             0.12        0.07          0.13  0.28
## tasa inflasion             -0.06       -0.05         -0.06 -0.07
## deuda publica               0.09        0.06          0.05  0.30
## produccion petroleo         0.83        0.67          0.82  0.75
## lineas fijas                0.96        0.93          0.94  0.55
## lineas mobiles              0.87        0.85          0.89  0.21
## desempleo juvenil          -0.08       -0.07         -0.07 -0.12
##                     consumo energia tasa inflasion deuda publica
## electricidad                   0.12          -0.06          0.09
## banda ancha                    0.07          -0.05          0.06
## emisiones co2                  0.13          -0.06          0.05
## deuda                          0.28          -0.07          0.30
## consumo energia                1.00          -0.14          0.08
## tasa inflasion                -0.14           1.00          0.19
## deuda publica                  0.08           0.19          1.00
## produccion petroleo            0.24          -0.06          0.14
## lineas fijas                   0.13          -0.05          0.19
## lineas mobiles                 0.00          -0.04          0.01
## desempleo juvenil             -0.14           0.05          0.10
##                     produccion petroleo lineas fijas lineas mobiles
## electricidad                       0.83         0.96           0.87
## banda ancha                        0.67         0.93           0.85
## emisiones co2                      0.82         0.94           0.89
## deuda                              0.75         0.55           0.21
## consumo energia                    0.24         0.13           0.00
## tasa inflasion                    -0.06        -0.05          -0.04
## deuda publica                      0.14         0.19           0.01
## produccion petroleo                1.00         0.81           0.63
## lineas fijas                       0.81         1.00           0.82
## lineas mobiles                     0.63         0.82           1.00
## desempleo juvenil                 -0.08        -0.09          -0.05
##                     desempleo juvenil
## electricidad                    -0.08
## banda ancha                     -0.07
## emisiones co2                   -0.07
## deuda                           -0.12
## consumo energia                 -0.14
## tasa inflasion                   0.05
## deuda publica                    0.10
## produccion petroleo             -0.08
## lineas fijas                    -0.09
## lineas mobiles                  -0.05
## desempleo juvenil                1.00
library(ggcorrplot)
## Loading required package: ggplot2
ggcorrplot(corMatrix)

library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following object is masked from 'package:polycor':
## 
##     polyserial
psych::KMO(corMatrix) 
## Kaiser-Meyer-Olkin factor adequacy
## Call: psych::KMO(r = corMatrix)
## Overall MSA =  0.76
## MSA for each item = 
##        electricidad         banda ancha       emisiones co2               deuda 
##                0.83                0.69                0.79                0.80 
##     consumo energia      tasa inflasion       deuda publica produccion petroleo 
##                0.56                0.48                0.56                0.69 
##        lineas fijas      lineas mobiles   desempleo juvenil 
##                0.87                0.73                0.52
cortest.bartlett(corMatrix,n=nrow(DATA_FACTO))$p.value>0.05
## [1] FALSE
library(matrixcalc)

is.singular.matrix(corMatrix)
## [1] FALSE
fa.parallel(DATA_FACTO, fa = 'fa',correct = T,plot = F)
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Parallel analysis suggests that the number of factors =  3  and the number of components =  NA
library(GPArotation)
## 
## Attaching package: 'GPArotation'
## The following objects are masked from 'package:psych':
## 
##     equamax, varimin
resfa <- fa(DATA_FACTO,
            nfactors = 3,
            cor = 'mixed',
            rotate = "varimax", #oblimin?
            fm="minres")
## Warning in cor.smooth(model): Matrix was not positive definite, smoothing was
## done
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68279e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68275e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68236e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68216e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68206e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68201e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68279e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68202e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.682e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68199e+09, ..): not converged in 1000000
## iter.
## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.

## Warning in pchisq(df = result$dof, ncp = x, q = result$STATISTIC):
## pnchisq(x=6.68279e+09, f=25, theta=6.68198e+09, ..): not converged in 1000000
## iter.
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected.  Examine the results carefully
print(resfa$loadings)
## 
## Loadings:
##                     MR1    MR2    MR3   
## electricidad         0.970  0.247       
## banda ancha          0.962              
## emisiones co2        0.976  0.199       
## deuda                0.277  0.895       
## consumo energia             0.351 -0.147
## tasa inflasion                     0.367
## deuda publica               0.273  0.557
## produccion petroleo  0.693  0.604       
## lineas fijas         0.917  0.321       
## lineas mobiles       0.905              
## desempleo juvenil          -0.125  0.214
## 
##                  MR1   MR2   MR3
## SS loadings    5.039 1.598 0.536
## Proportion Var 0.458 0.145 0.049
## Cumulative Var 0.458 0.603 0.652
print(resfa$loadings,cutoff = 0.5)
## 
## Loadings:
##                     MR1    MR2    MR3   
## electricidad         0.970              
## banda ancha          0.962              
## emisiones co2        0.976              
## deuda                       0.895       
## consumo energia                         
## tasa inflasion                          
## deuda publica                      0.557
## produccion petroleo  0.693  0.604       
## lineas fijas         0.917              
## lineas mobiles       0.905              
## desempleo juvenil                       
## 
##                  MR1   MR2   MR3
## SS loadings    5.039 1.598 0.536
## Proportion Var 0.458 0.145 0.049
## Cumulative Var 0.458 0.603 0.652
fa.diagram(resfa,main = "Resultados del EFA")

sort(resfa$communality)
##   desempleo juvenil      tasa inflasion     consumo energia       deuda publica 
##           0.0632538           0.1441495           0.1458296           0.3875264 
##      lineas mobiles produccion petroleo               deuda         banda ancha 
##           0.8218706           0.8469766           0.8833300           0.9306771 
##        lineas fijas       emisiones co2        electricidad 
##           0.9465482           0.9994178           1.0026642
sort(resfa$complexity)
##      lineas mobiles         banda ancha       emisiones co2        electricidad 
##            1.008962            1.012640            1.096941            1.132735 
##      tasa inflasion               deuda        lineas fijas     consumo energia 
##            1.142050            1.204542            1.250717            1.360144 
##       deuda publica   desempleo juvenil produccion petroleo 
##            1.474156            1.701039            1.972397
resfa$TLI
## [1] -5192419
resfa$rms
## [1] 0.02122214
resfa$RMSEA
##      RMSEA      lower      upper confidence 
##   1218.633         NA   1221.957      0.900
resfa$BIC
## [1] 6682794128
as.data.frame(resfa$scores)%>%head()
##           MR1        MR2        MR3
## 1 -0.16023459 -0.2677912 -0.6700292
## 2 -0.11561090 -0.2236387  0.2988112
## 3 -0.19466844 -0.2303205 -0.1989317
## 4 -0.08076028 -0.2664034  0.2888470
## 5 -0.20153409 -0.1825821  0.4754322
## 6 -0.10645089 -0.2358216  0.1756472

#AGNES #PREPA

library(BBmisc)
## 
## Attaching package: 'BBmisc'
## The following objects are masked from 'package:dplyr':
## 
##     coalesce, collapse, symdiff
## The following object is masked from 'package:base':
## 
##     isFALSE
boxplot(normalize(DATA_FACTO,method='range',range=c(0,10)))

boxplot(normalize(DATA_FACTO,method='standardize'))

DATA_CLUS=normalize(DATA_FACTO,method='standardize')
library(cluster)
g.dist = daisy(DATA_CLUS, metric="gower")
DATA_CLUS <- subset(DATA_CLUS, !(row.names(DATA_CLUS) == 175))
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_nbclust(DATA_CLUS, hcut,diss=g.dist,method = "gap_stat",k.max = 10,verbose = F,hc_func = "agnes")