library(markovchain)
## Package:  markovchain
## Version:  0.8.6
## Date:     2021-05-17
## BugReport: https://github.com/spedygiorgio/markovchain/issues
My_function_Comp6a<-function(x){
  
  CadenasPoliticas<-list(0,0)
  
  lambda<-x
  
  #Modelacion del manejo de inventarios de Wattenspapier con cadenas de Markov
  #Crear los estados
  capacidad_inv <-22
  estados<-c(0:capacidad_inv)
  
  #*****Crear y llenar la matriz P de la política actual*****
  matrizP<-matrix(0,nrow = length(estados), ncol = length(estados))
  rownames(matrizP)<-estados
  colnames(matrizP)<-estados
  
  #Para la Política Actual -> si i<=10 solicita 12 resmas
  for (i in estados) {
    for (j in estados) {
      if(i<=10 & j>0){
        matrizP[i+1,j+1]= dpois(12+i-j,lambda=lambda)
      }else if(i<=10 & j==0){
        matrizP[i+1,j+1]=ppois(12+i-1,lambda = lambda, lower.tail = F)
      }else if(i>10 & j>0){
        matrizP[i+1,j+1]=dpois(i-j,lambda=lambda)
      }else if(i>10 & j==0){
        matrizP[i+1,j+1]=ppois(i-1,lambda = lambda, lower.tail = F)
      }
    }
  }
  #*****Crear y llenar la matriz P para el caso de la política Nueva*****
  matrizPNueva<-matrix(0,nrow = length(estados), ncol = length(estados))
  rownames(matrizPNueva)<-estados
  colnames(matrizPNueva)<-estados
  #Para la nueva Política  -> solicitar hasta la capacidad máxima
  for (i in estados) {
    for (j in estados) {
      if(j>0){
        matrizPNueva[i+1,j+1]= dpois(capacidad_inv-j,lambda=lambda)
      }else if(j==0){
        matrizPNueva[i+1,j+1]=ppois(capacidad_inv-1,lambda = lambda, lower.tail = F)
      }
    }
  }
  
  #*****Crear las dos cadenas usando el paquete markovchain*****
  #Política actual
  cmtdActual<-new("markovchain", states=as.character(estados), transitionMatrix=matrizP)
  #Política Nueva
  cmtdNueva<-new("markovchain", states=as.character(estados), transitionMatrix=matrizPNueva)
  
  CadenasPoliticas[[1]]<-cmtdActual
  CadenasPoliticas[[2]]<-cmtdNueva
  
  return(CadenasPoliticas)
}
#Probar
My_function_Comp6a(x = 15)
## [[1]]
## Unnamed Markov chain 
##  A  23 - dimensional discrete Markov Chain defined by the following states: 
##  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 
##  The transition matrix  (by rows)  is defined as follows: 
##             0          1          2          3          4          5
## 0  0.81524820 0.06628739 0.04861075 0.03240717 0.01944430 0.01037029
## 1  0.73238897 0.08285923 0.06628739 0.04861075 0.03240717 0.01944430
## 2  0.63678216 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 3  0.53434629 0.10243587 0.09560681 0.08285923 0.06628739 0.04861075
## 4  0.43191042 0.10243587 0.10243587 0.09560681 0.08285923 0.06628739
## 5  0.33587680 0.09603362 0.10243587 0.10243587 0.09560681 0.08285923
## 6  0.25114125 0.08473555 0.09603362 0.10243587 0.10243587 0.09560681
## 7  0.18052829 0.07061296 0.08473555 0.09603362 0.10243587 0.10243587
## 8  0.12478122 0.05574707 0.07061296 0.08473555 0.09603362 0.10243587
## 9  0.08297091 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 10 0.05310641 0.02986450 0.04181031 0.05574707 0.07061296 0.08473555
## 11 0.88153559 0.04861075 0.03240717 0.01944430 0.01037029 0.00483947
## 12 0.81524820 0.06628739 0.04861075 0.03240717 0.01944430 0.01037029
## 13 0.73238897 0.08285923 0.06628739 0.04861075 0.03240717 0.01944430
## 14 0.63678216 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 15 0.53434629 0.10243587 0.09560681 0.08285923 0.06628739 0.04861075
## 16 0.43191042 0.10243587 0.10243587 0.09560681 0.08285923 0.06628739
## 17 0.33587680 0.09603362 0.10243587 0.10243587 0.09560681 0.08285923
## 18 0.25114125 0.08473555 0.09603362 0.10243587 0.10243587 0.09560681
## 19 0.18052829 0.07061296 0.08473555 0.09603362 0.10243587 0.10243587
## 20 0.12478122 0.05574707 0.07061296 0.08473555 0.09603362 0.10243587
## 21 0.08297091 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 22 0.05310641 0.02986450 0.04181031 0.05574707 0.07061296 0.08473555
##              6            7            8            9           10           11
## 0  0.004839470 0.0019357881 0.0006452627 1.720701e-04 3.441401e-05 4.588535e-06
## 1  0.010370294 0.0048394703 0.0019357881 6.452627e-04 1.720701e-04 3.441401e-05
## 2  0.019444300 0.0103702935 0.0048394703 1.935788e-03 6.452627e-04 1.720701e-04
## 3  0.032407167 0.0194443003 0.0103702935 4.839470e-03 1.935788e-03 6.452627e-04
## 4  0.048610751 0.0324071672 0.0194443003 1.037029e-02 4.839470e-03 1.935788e-03
## 5  0.066287387 0.0486107508 0.0324071672 1.944430e-02 1.037029e-02 4.839470e-03
## 6  0.082859234 0.0662873875 0.0486107508 3.240717e-02 1.944430e-02 1.037029e-02
## 7  0.095606809 0.0828592344 0.0662873875 4.861075e-02 3.240717e-02 1.944430e-02
## 8  0.102435867 0.0956068089 0.0828592344 6.628739e-02 4.861075e-02 3.240717e-02
## 9  0.102435867 0.1024358667 0.0956068089 8.285923e-02 6.628739e-02 4.861075e-02
## 10 0.096033625 0.1024358667 0.1024358667 9.560681e-02 8.285923e-02 6.628739e-02
## 11 0.001935788 0.0006452627 0.0001720701 3.441401e-05 4.588535e-06 3.059023e-07
## 12 0.004839470 0.0019357881 0.0006452627 1.720701e-04 3.441401e-05 4.588535e-06
## 13 0.010370294 0.0048394703 0.0019357881 6.452627e-04 1.720701e-04 3.441401e-05
## 14 0.019444300 0.0103702935 0.0048394703 1.935788e-03 6.452627e-04 1.720701e-04
## 15 0.032407167 0.0194443003 0.0103702935 4.839470e-03 1.935788e-03 6.452627e-04
## 16 0.048610751 0.0324071672 0.0194443003 1.037029e-02 4.839470e-03 1.935788e-03
## 17 0.066287387 0.0486107508 0.0324071672 1.944430e-02 1.037029e-02 4.839470e-03
## 18 0.082859234 0.0662873875 0.0486107508 3.240717e-02 1.944430e-02 1.037029e-02
## 19 0.095606809 0.0828592344 0.0662873875 4.861075e-02 3.240717e-02 1.944430e-02
## 20 0.102435867 0.0956068089 0.0828592344 6.628739e-02 4.861075e-02 3.240717e-02
## 21 0.102435867 0.1024358667 0.0956068089 8.285923e-02 6.628739e-02 4.861075e-02
## 22 0.096033625 0.1024358667 0.1024358667 9.560681e-02 8.285923e-02 6.628739e-02
##              12           13           14           15           16
## 0  3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 1  4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00
## 2  3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00
## 3  1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00
## 4  6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07
## 5  1.935788e-03 6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06
## 6  4.839470e-03 1.935788e-03 6.452627e-04 1.720701e-04 3.441401e-05
## 7  1.037029e-02 4.839470e-03 1.935788e-03 6.452627e-04 1.720701e-04
## 8  1.944430e-02 1.037029e-02 4.839470e-03 1.935788e-03 6.452627e-04
## 9  3.240717e-02 1.944430e-02 1.037029e-02 4.839470e-03 1.935788e-03
## 10 4.861075e-02 3.240717e-02 1.944430e-02 1.037029e-02 4.839470e-03
## 11 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 12 3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 13 4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00
## 14 3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00
## 15 1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00
## 16 6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07
## 17 1.935788e-03 6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06
## 18 4.839470e-03 1.935788e-03 6.452627e-04 1.720701e-04 3.441401e-05
## 19 1.037029e-02 4.839470e-03 1.935788e-03 6.452627e-04 1.720701e-04
## 20 1.944430e-02 1.037029e-02 4.839470e-03 1.935788e-03 6.452627e-04
## 21 3.240717e-02 1.944430e-02 1.037029e-02 4.839470e-03 1.935788e-03
## 22 4.861075e-02 3.240717e-02 1.944430e-02 1.037029e-02 4.839470e-03
##              17           18           19           20           21
## 0  0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 1  0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 2  0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 3  0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 4  0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 5  3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 6  4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00
## 7  3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00
## 8  1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00
## 9  6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07
## 10 1.935788e-03 6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06
## 11 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 12 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 13 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 14 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 15 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 16 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 17 3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 18 4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00 0.000000e+00
## 19 3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00 0.000000e+00
## 20 1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07 0.000000e+00
## 21 6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06 3.059023e-07
## 22 1.935788e-03 6.452627e-04 1.720701e-04 3.441401e-05 4.588535e-06
##              22
## 0  0.000000e+00
## 1  0.000000e+00
## 2  0.000000e+00
## 3  0.000000e+00
## 4  0.000000e+00
## 5  0.000000e+00
## 6  0.000000e+00
## 7  0.000000e+00
## 8  0.000000e+00
## 9  0.000000e+00
## 10 3.059023e-07
## 11 0.000000e+00
## 12 0.000000e+00
## 13 0.000000e+00
## 14 0.000000e+00
## 15 0.000000e+00
## 16 0.000000e+00
## 17 0.000000e+00
## 18 0.000000e+00
## 19 0.000000e+00
## 20 0.000000e+00
## 21 0.000000e+00
## 22 3.059023e-07
## 
## 
## [[2]]
## Unnamed Markov chain 
##  A  23 - dimensional discrete Markov Chain defined by the following states: 
##  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 
##  The transition matrix  (by rows)  is defined as follows: 
##             0         1          2          3          4          5          6
## 0  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 1  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 2  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 3  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 4  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 5  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 6  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 7  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 8  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 9  0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 10 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 11 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 12 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 13 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 14 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 15 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 16 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 17 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 18 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 19 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 20 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 21 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
## 22 0.05310641 0.0298645 0.04181031 0.05574707 0.07061296 0.08473555 0.09603362
##            7         8          9         10         11         12         13
## 0  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 1  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 2  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 3  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 4  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 5  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 6  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 7  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 8  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 9  0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 10 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 11 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 12 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 13 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 14 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 15 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 16 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 17 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 18 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 19 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 20 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 21 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
## 22 0.1024359 0.1024359 0.09560681 0.08285923 0.06628739 0.04861075 0.03240717
##           14         15         16          17           18           19
## 0  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 1  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 2  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 3  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 4  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 5  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 6  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 7  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 8  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 9  0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 10 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 11 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 12 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 13 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 14 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 15 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 16 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 17 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 18 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 19 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 20 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 21 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
## 22 0.0194443 0.01037029 0.00483947 0.001935788 0.0006452627 0.0001720701
##              20           21           22
## 0  3.441401e-05 4.588535e-06 3.059023e-07
## 1  3.441401e-05 4.588535e-06 3.059023e-07
## 2  3.441401e-05 4.588535e-06 3.059023e-07
## 3  3.441401e-05 4.588535e-06 3.059023e-07
## 4  3.441401e-05 4.588535e-06 3.059023e-07
## 5  3.441401e-05 4.588535e-06 3.059023e-07
## 6  3.441401e-05 4.588535e-06 3.059023e-07
## 7  3.441401e-05 4.588535e-06 3.059023e-07
## 8  3.441401e-05 4.588535e-06 3.059023e-07
## 9  3.441401e-05 4.588535e-06 3.059023e-07
## 10 3.441401e-05 4.588535e-06 3.059023e-07
## 11 3.441401e-05 4.588535e-06 3.059023e-07
## 12 3.441401e-05 4.588535e-06 3.059023e-07
## 13 3.441401e-05 4.588535e-06 3.059023e-07
## 14 3.441401e-05 4.588535e-06 3.059023e-07
## 15 3.441401e-05 4.588535e-06 3.059023e-07
## 16 3.441401e-05 4.588535e-06 3.059023e-07
## 17 3.441401e-05 4.588535e-06 3.059023e-07
## 18 3.441401e-05 4.588535e-06 3.059023e-07
## 19 3.441401e-05 4.588535e-06 3.059023e-07
## 20 3.441401e-05 4.588535e-06 3.059023e-07
## 21 3.441401e-05 4.588535e-06 3.059023e-07
## 22 3.441401e-05 4.588535e-06 3.059023e-07
My_function_Comp6b<-function(y,z){
  
  capacidad_inv <- 22  
  DatosCostos<-data.frame()
  
  #________Costos anuales de las dos políticas_______:
  #Se recuperan las cadenas por aparte
  cmtdActual<-y[[1]]
  cmtdNueva<-y[[2]]
  #La función `steadyStates` calcula las probabilidades en estado estable
  #P. Actual
  eestableA<-steadyStates(cmtdActual)
  vEsperadoActual<-0
  for (i in 1:length(eestableA)) {
    vEsperadoActual<- vEsperadoActual+eestableA[i]*(i-1)
  }
  
  #P. Nueva
  eestableN<-steadyStates(cmtdNueva)
  vEsperadoNuevo<-0
  for (i in 1:length(eestableN)) {
    vEsperadoNuevo<- vEsperadoNuevo+eestableN[i]*(i-1)
  }
  
  #vector de costos de ordenar
  vec_cost_Ord=seq(20000,50000,by = 1000)
  #Valor del costo unitario de inventario
  cInventario<-z
  
  #P. actual
  
  #costo de inventario
  cAnualInvACT<-cInventario*vEsperadoActual*4*12
  #costo de ordenar (sólo se incurre en este costo cuando el inventario es <=10)
  cAnualOrdACT<-(vec_cost_Ord*sum(eestableA[1:11]))*4*12
  #costo total
  CTotal_Actual<-cAnualInvACT+cAnualOrdACT
  
  #P. nueva
  
  #costo de inventario
  cAnualInvNUE<-cInventario*vEsperadoNuevo*4*12
  #costo ordenar (sólo se incurre en este costo cuando el inventario es <22)
  cAnualOrdNUE<-((vec_cost_Ord*sum(eestableA[1:(capacidad_inv-1)]))*4*12)-500000
  #Costo total
  CTotal_Nuevo<-cAnualInvNUE+cAnualOrdNUE
  
  DatosCostos <- data.frame(vec_cost_Ord,CTotal_Actual,CTotal_Nuevo)
  
  return(DatosCostos)
  
}

#Probar
My_function_Comp6b(y =My_function_Comp6a(x = 15),z = 5000 )
##    vec_cost_Ord CTotal_Actual CTotal_Nuevo
## 1         20000       1138136      2158293
## 2         21000       1186056      2206293
## 3         22000       1233976      2254293
## 4         23000       1281896      2302293
## 5         24000       1329816      2350293
## 6         25000       1377735      2398293
## 7         26000       1425655      2446293
## 8         27000       1473575      2494293
## 9         28000       1521495      2542293
## 10        29000       1569415      2590293
## 11        30000       1617334      2638293
## 12        31000       1665254      2686293
## 13        32000       1713174      2734293
## 14        33000       1761094      2782293
## 15        34000       1809014      2830293
## 16        35000       1856934      2878293
## 17        36000       1904853      2926293
## 18        37000       1952773      2974293
## 19        38000       2000693      3022293
## 20        39000       2048613      3070293
## 21        40000       2096533      3118293
## 22        41000       2144452      3166293
## 23        42000       2192372      3214293
## 24        43000       2240292      3262293
## 25        44000       2288212      3310293
## 26        45000       2336132      3358293
## 27        46000       2384051      3406293
## 28        47000       2431971      3454293
## 29        48000       2479891      3502293
## 30        49000       2527811      3550293
## 31        50000       2575731      3598293

R Markdown

library(plotly)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(readxl)
library(markovchain)
library(fitdistrplus)
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
## 
##     select
## Loading required package: survival
library(shiny)
#source("comple7.R")


# Definir interfaz de la aplicacion
ui <- fluidPage(
  
  #Poner titulo de la aplicacion
  titlePanel(title = "Aplicacion Politicas de Inventario - Parte 2"),
  
  #Poner estructura de la aplicacion
  sidebarLayout(
    
    #Crear barra lateral para poner inputs
    sidebarPanel(
      
      #Input cargar archivo demanda
      fileInput(inputId = "datosDemanda",label = "Cargue el archivo de demanda historico"),
      
      #Input de costo de manterner inventario en un slider
      sliderInput(inputId = "costoMantener",
                  label = "Costo unitario de inventario por semana",
                  min = 1000,
                  max = 15000,
                  value = 5000),
      
      #Input de boxplot para pvalue
      checkboxInput(inputId = "PruebaAjuste",label = "Mostrar P value",value = FALSE,width = NULL),
      
      #Output del boxplot
      conditionalPanel("input.PruebaAjuste",verbatimTextOutput(outputId = "pValue"))
      
    ),
    
    mainPanel(
      
      #Output de una grafica de costos
      plotlyOutput(outputId = "GraficaCostos")
      
    )
    
  )
)


# Define server para logica de la app
server <- function(input, output) {
  
  #Funcion reactiva para calcular lambda con datos
  Lambda<-eventReactive(eventExpr = input$datosDemanda,
                        valueExpr = {
                          Archivo<-input$datosDemanda
                          Datos<-read_excel(path = Archivo$datapath,sheet = 1,col_names = TRUE)
                          Demanda<-Datos[[2]]
                          fit<-fitdist(Demanda,"pois")
                          fit
                        })
  
  #Funcion reactiva para crear cadenas de markov con lambda
  Cadenas<-eventReactive(eventExpr = input$datosDemanda,
                         valueExpr = {
                           My_function_Comp6a(Lambda()$estimate)
                         } )
  #Generar grafica de costos
  output$GraficaCostos<-renderPlotly({
    
    data<-My_function_Comp6b(Cadenas(),input$costoMantener)
    
    plot_ly(data,x=~vec_cost_Ord,y=~CTotal_Actual,name="Politica Actual",type = 'scatter', mode = 'lines+markers')%>%
      add_trace(y=~CTotal_Nuevo,name = 'Politica Nueva', type = 'scatter', mode = 'lines+markers')%>%
      layout(title="Costo anual",xaxis=list(title="Costo Ordenar"),yaxis=list(title="Costo Total"))
    
  })  

  
  #Calcular y retornar el pvalue de la prueba de bondad de ajuste
  output$pValue<-renderText({
    pValue<-gofstat(Lambda())$chisqpvalue
    pValue
  })
  
}

# Run the application 
#shinyApp(ui = ui, server = server)