#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 tibble: 46 x 46
##      `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`  `10`  `11`   `12`
##    <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
## # i 36 more rows
## # i 34 more variables: `13` <dbl>, `14` <dbl>, `15` <dbl>, `16` <dbl>,
## #   `17` <dbl>, `18` <dbl>, `19` <dbl>, `20` <dbl>, `21` <dbl>, `22` <dbl>,
## #   `23` <dbl>, `24` <dbl>, `25` <dbl>, `26` <dbl>, `27` <dbl>, `28` <dbl>,
## #   `29` <dbl>, `30` <dbl>, `31` <dbl>, `32` <dbl>, `33` <dbl>, `34` <dbl>,
## #   `35` <dbl>, `36` <dbl>, `37` <dbl>, `38` <dbl>, `39` <dbl>, `40` <dbl>,
## #   `41` <dbl>, `42` <dbl>, `43` <dbl>, `44` <dbl>, `45` <dbl>, `46` <dbl>
## # A tibble: 46 x 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
## # i 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,]))
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")