markov

library(markovchain)
Warning: package 'markovchain' was built under R version 4.4.2
Package:  markovchain
Version:  0.9.5
Date:     2023-09-24 09:20:02 UTC
BugReport: https://github.com/spedygiorgio/markovchain/issues
#1.- Crear matriz de transición P:

P= matrix(c(0,.5,.5,.5,0,.5,.5,.5,0), nrow = 3, byrow = TRUE )
P
     [,1] [,2] [,3]
[1,]  0.0  0.5  0.5
[2,]  0.5  0.0  0.5
[3,]  0.5  0.5  0.0
#Creando la matriz de transicion
mc <- new("markovchain", transitionMatrix=P, states=c("a","b","c"), name="Cadena 1")
mc
Cadena 1 
 A  3 - dimensional discrete Markov Chain defined by the following states: 
 a, b, c 
 The transition matrix  (by rows)  is defined as follows: 
    a   b   c
a 0.0 0.5 0.5
b 0.5 0.0 0.5
c 0.5 0.5 0.0
#estructura de la matriz
str(mc)
Formal class 'markovchain' [package "markovchain"] with 4 slots
  ..@ states          : chr [1:3] "a" "b" "c"
  ..@ byrow           : logi TRUE
  ..@ transitionMatrix: num [1:3, 1:3] 0 0.5 0.5 0.5 0 0.5 0.5 0.5 0
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:3] "a" "b" "c"
  .. .. ..$ : chr [1:3] "a" "b" "c"
  ..@ name            : chr "Cadena 1"
summary(mc)
Cadena 1  Markov chain that is composed by: 
Closed classes: 
a b c 
Recurrent classes: 
{a,b,c}
Transient classes: 
NONE 
The Markov chain is irreducible 
The absorbing states are: NONE
plot(mc)

#Se utilizara la base de veces que llueve por semana
data(rain)
str(rain)
'data.frame':   1096 obs. of  2 variables:
 $ V1  : int  3 2 2 2 2 2 2 3 3 3 ...
 $ rain: chr  "6+" "1-5" "1-5" "1-5" ...
summary(rain)
       V1            rain          
 Min.   :1.000   Length:1096       
 1st Qu.:1.000   Class :character  
 Median :1.500   Mode  :character  
 Mean   :1.731                     
 3rd Qu.:2.000                     
 Max.   :3.000                     
table(rain$rain)

  0 1-5  6+ 
548 295 253 
head(rain)
  V1 rain
1  3   6+
2  2  1-5
3  2  1-5
4  2  1-5
5  2  1-5
6  2  1-5
tail(rain)
     V1 rain
1091  1    0
1092  2  1-5
1093  1    0
1094  3   6+
1095  3   6+
1096  2  1-5
#La funcion "createSequenceMatrix()" crea una matriz de secuencia
P1 <- createSequenceMatrix(rain$rain)
P1
      0 1-5  6+
0   362 126  60
1-5 136  90  68
6+   50  79 124
#La función "markovchainFit()" estima la matriz de transición para el registro de datos, haciendo uso del método de máxima verosimilitud (MLE)
Fit <- markovchainFit(data=rain$rain, confidencelevel = .95)
Fit
$estimate
MLE Fit 
 A  3 - dimensional discrete Markov Chain defined by the following states: 
 0, 1-5, 6+ 
 The transition matrix  (by rows)  is defined as follows: 
            0       1-5        6+
0   0.6605839 0.2299270 0.1094891
1-5 0.4625850 0.3061224 0.2312925
6+  0.1976285 0.3122530 0.4901186


$standardError
             0        1-5         6+
0   0.03471952 0.02048353 0.01413498
1-5 0.03966634 0.03226814 0.02804834
6+  0.02794888 0.03513120 0.04401395

$confidenceLevel
[1] 0.95

$lowerEndpointMatrix
            0       1-5        6+
0   0.5925349 0.1897800 0.0817850
1-5 0.3848404 0.2428780 0.1763188
6+  0.1428496 0.2433971 0.4038528

$upperEndpointMatrix
            0       1-5        6+
0   0.7286330 0.2700740 0.1371931
1-5 0.5403296 0.3693669 0.2862663
6+  0.2524073 0.3811089 0.5763843

$logLikelihood
[1] -1040.419
#Dicha función devuelve una lista de todos los resultados estimados. Para el ejemplo anterior se tienen los diguientes resultados:
mc <- Fit$estimate
mc
MLE Fit 
 A  3 - dimensional discrete Markov Chain defined by the following states: 
 0, 1-5, 6+ 
 The transition matrix  (by rows)  is defined as follows: 
            0       1-5        6+
0   0.6605839 0.2299270 0.1094891
1-5 0.4625850 0.3061224 0.2312925
6+  0.1976285 0.3122530 0.4901186
plot(mc)

#HACER PREDICCIONES DE LAS SIGUIENTES 3 SEMANAS
steadyStates(mc)
             0       1-5        6+
[1,] 0.5008871 0.2693656 0.2297473
predict(mc, newdata="0",n.ahead=2)
[1] "0" "0"