library(readxl)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
Mip2006_ci <- read_excel("C:/Users/ProBook/OneDrive/Desktop/Anexo_Resolución_32_MATRIZ_INSUMO_PRODUCTO_A_PRECIOS_CORRIENTES_EN_DOLARES_1990-2006.xls", 
    sheet = "MIP 2006", range = "I15:BB60", col_names = FALSE) 
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
## • `` -> `...16`
## • `` -> `...17`
## • `` -> `...18`
## • `` -> `...19`
## • `` -> `...20`
## • `` -> `...21`
## • `` -> `...22`
## • `` -> `...23`
## • `` -> `...24`
## • `` -> `...25`
## • `` -> `...26`
## • `` -> `...27`
## • `` -> `...28`
## • `` -> `...29`
## • `` -> `...30`
## • `` -> `...31`
## • `` -> `...32`
## • `` -> `...33`
## • `` -> `...34`
## • `` -> `...35`
## • `` -> `...36`
## • `` -> `...37`
## • `` -> `...38`
## • `` -> `...39`
## • `` -> `...40`
## • `` -> `...41`
## • `` -> `...42`
## • `` -> `...43`
## • `` -> `...44`
## • `` -> `...45`
## • `` -> `...46`
names(Mip2006_ci)<-as.character(1.46)
## Warning: The `value` argument of `names<-()` must have the same length as `x` as of
## tibble 3.0.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `value` argument of `names<-()` can't be empty as of tibble 3.0.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(Mip2006_ci)
## # A tibble: 46 × 46
##    `1.46`    ``    ``    ``    ``    ``    ``    ``    ``    ``    ``     ``
##     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
##  1     86     0     0     0     0     0     0     0     0     0     0      0
##  2      0     0     0     0     0     0     0     0     0     0     0      0
##  3      0     0 11004     0     0  5313 28072     0     0     0     1     65
##  4      0     0     0  3071     0     0     0     0     0     0     0      0
##  5      0   104 38676  1357 11299     0     0     0     0     0    14    469
##  6      0     0     0     0     0  8777    19     0     0     0 89833 144571
##  7      0     0     0     0     0     0 16874     0     7     0  4137     57
##  8    576     1  2000   272   563    65     0    36     0     0   204    734
##  9      0     0     0     0     0     0     0     0 44464     0     0      0
## 10      1     1    73    58    21  2023     6     0     2 15334     3     89
## # ℹ 36 more rows
## # ℹ 34 more variables: `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>,
## #   `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>,
## #   `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>,
## #   `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>,
## #   `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>, `` <dbl>,
## #   `` <dbl>
#Demanda final
Mip2006_h<- read_excel("C:/Users/ProBook/OneDrive/Desktop/Anexo_Resolución_32_MATRIZ_INSUMO_PRODUCTO_A_PRECIOS_CORRIENTES_EN_DOLARES_1990-2006.xls", sheet = "MIP 2006", range = "bd15:be60", col_names = FALSE)
## New names:
## • `` -> `...1`
## • `` -> `...2`
names(Mip2006_h)<-as.character(1:2) 
print(Mip2006_h)
## # A tibble: 46 × 2
##         `1`   `2`
##       <dbl> <dbl>
##  1       0      0
##  2       0      0
##  3  314561.     0
##  4       0      0
##  5 1323380.     0
##  6   50357.     0
##  7  526957.     0
##  8  101237.     0
##  9   44341.     0
## 10    2422      0
## # ℹ 36 more rows
#vector de produccion

Mip2006_x<- read_excel("C:/Users/ProBook/OneDrive/Desktop/Anexo_Resolución_32_MATRIZ_INSUMO_PRODUCTO_A_PRECIOS_CORRIENTES_EN_DOLARES_1990-2006.xls",
                      sheet = "MIP 2006",
                      range = "bi15:bi60", 
                      col_names = FALSE) 
## New names:
## • `` -> `...1`
names(Mip2006_x)<-c("x")
print(Mip2006_x)
## # A tibble: 46 × 1
##           x
##       <dbl>
##  1  182287 
##  2   34523 
##  3  715438.
##  4   80222 
##  5 1474837.
##  6  312032.
##  7  576046.
##  8  172777.
##  9  188273.
## 10  490735.
## # ℹ 36 more rows
library(dplyr)
servicios_row<-colSums(Mip2006_ci[41:46,])
temporal<-rbind(Mip2006_ci[1:40,],servicios_row)
servicios_col<-rowSums(temporal[,41:46])
Mip2006_ci_corregida<-cbind(temporal[,1:40],servicios_col)
names(Mip2006_ci_corregida)<-as.character(1:41)
x<-rbind(Mip2006_x[1:40,],colSums(Mip2006_x[41:46,]))
#Matriz de coeficientes tecnicos
mip_coeficientes_tecnicos<-function(matriz_consumo_intermedio,
                                    vector_demanda_final){
  filas_ci<-nrow(matriz_consumo_intermedio)
  columnas_ci<-ncol(matriz_consumo_intermedio)
  filas_x<-nrow(vector_demanda_final)
  if(filas_ci!=columnas_ci){
    stop("Ingrese una matriz de Consumo Intermedio, Cuadrada",call. = FALSE)
  }
  if(filas_ci!=filas_x){
    stop("Vector de demanda final incompatible (diferente dimensión)",call. = FALSE)
  }
  v<-solve(diag(as.vector(vector_demanda_final)))
  A<-matriz_consumo_intermedio%*%v
  list(A=A,V=v)
}
#matriz tecnológica

mip_matriz_tecnologica<-function(matriz_coeficientes_tecnicos){
  filas_A<-nrow(matriz_coeficientes_tecnicos)
  columnas_A<-ncol(matriz_coeficientes_tecnicos)
  if(filas_A!=columnas_A){
    stop("Ingrese una matriz de coef. técnicos cuadrada",call. = FALSE)
  }
  tipo_matriz<-typeof(matriz_coeficientes_tecnicos)
  if(tipo_matriz!="double"){
    stop("La matriz ingresada no es numerica",call. = FALSE)
  }
  T<-diag(1,filas_A)-matriz_coeficientes_tecnicos
  T
}
mip_matriz_leontief<-function(matriz_tecnologica){
  L<-solve(matriz_tecnologica)
  L
}
mip_multiplicadores_produccion_mp<-function(matriz_leontief){
  mp<-rowSums(matriz_leontief)
  mp
}
mip_multiplicadores_expansion_demanda_me<-function(matriz_leontief){
  me<-colSums(matriz_leontief)
  me
}
mip_encadenamiento_pd<-function(matriz_leontief){
  mp<-mip_multiplicadores_produccion_mp(matriz_leontief)
  mp/mean(mp)
}
mip_encadenamiento_sd<-function(matriz_leontief){
  me<-mip_multiplicadores_expansion_demanda_me(matriz_leontief)
  me/mean(me)
}
mip_tabla_rasmussen<-function(matriz_leontief){
library(dplyr)
pd<-mip_encadenamiento_pd(matriz_leontief)
sd<-mip_encadenamiento_sd(matriz_leontief)
rasmussen<-data.frame(pd=pd,sd=sd)
rasmussen_clasificado<-rasmussen %>% 
  mutate(clasificacion=case_when(pd>1 & sd>1 ~ "Sector Clave",
                                           pd<1 & sd>1 ~"Sector Estrategico",
                                           pd>1 & sd<1 ~"Sector Impulsor",
                                           pd<1 & sd<1 ~"Sector Isla",
                                           TRUE ~ "No clasificado")) %>% mutate(sector=row_number()) %>% select(sector,pd,sd,clasificacion)
rasmussen_clasificado
}
A<-mip_coeficientes_tecnicos(as.matrix(Mip2006_ci_corregida),as.matrix(x))[[1]]
matriz_T<-mip_matriz_tecnologica(matriz_coeficientes_tecnicos = A)
L<-mip_matriz_leontief(matriz_tecnologica = matriz_T)
tabla<-mip_tabla_rasmussen(L) %>% print()
##    sector        pd         sd      clasificacion
## 1       1 0.5266371  0.6742102        Sector Isla
## 2       2 0.5468883  0.5299542        Sector Isla
## 3       3 0.7658552  0.6415055        Sector Isla
## 4       4 0.6759963  0.9598921        Sector Isla
## 5       5 0.6467097  0.5630764        Sector Isla
## 6       6 0.8433116  0.7275592        Sector Isla
## 7       7 0.5510189  0.8757175        Sector Isla
## 8       8 0.6216461  0.5334481        Sector Isla
## 9       9 0.7062793  0.7790059        Sector Isla
## 10     10 1.3339695  0.5564918    Sector Impulsor
## 11     11 0.5974393  0.7277986        Sector Isla
## 12     12 0.5599095  0.7645337        Sector Isla
## 13     13 0.5205150  0.5406925        Sector Isla
## 14     14 0.7042717  0.7928421        Sector Isla
## 15     15 0.5476486  1.0085354 Sector Estrategico
## 16     16 0.9963777  0.6984426        Sector Isla
## 17     17 0.5891340  0.6633745        Sector Isla
## 18     18 0.5197275  0.5194535        Sector Isla
## 19     19 0.7641142  0.6759362        Sector Isla
## 20     20 0.5289720  0.7124806        Sector Isla
## 21     21 0.5869791  0.7247208        Sector Isla
## 22     22 0.5751913  0.6113676        Sector Isla
## 23     23 1.0484997  0.7445995    Sector Impulsor
## 24     24 0.9808336  0.7083989        Sector Isla
## 25     25 1.1170017  0.6300139    Sector Impulsor
## 26     26 3.2965709  0.6444126    Sector Impulsor
## 27     27 0.8121867  0.6631067        Sector Isla
## 28     28 0.7841251  0.7772573        Sector Isla
## 29     29 0.9306187  1.0340246 Sector Estrategico
## 30     30 0.6495298  0.5760867        Sector Isla
## 31     31 0.7959562  0.5654301        Sector Isla
## 32     32 1.4531322  0.8251255    Sector Impulsor
## 33     33 0.5619995  1.1035588 Sector Estrategico
## 34     34 0.6165996  0.8700713        Sector Isla
## 35     35 0.5516892 12.3434144 Sector Estrategico
## 36     36 0.6218298  0.6749219        Sector Isla
## 37     37 5.3517201  0.7258272    Sector Impulsor
## 38     38 0.8262533  0.8751212        Sector Isla
## 39     39 0.9175119  0.6423195        Sector Isla
## 40     40 3.3777885  0.6086018    Sector Impulsor
## 41     41 1.5975616  0.7066691    Sector Impulsor
library(dplyr)
tabla %>% group_by(clasificacion) %>% summarise(total=n()) %>% mutate(porcentaje=round(prop.table(total)*100,2))
tabla %>% filter(clasificacion=="Sector Estrategico")
tabla %>% filter(clasificacion=="Sector Impulsor")