Event probability chain

1

I have 14 events (A: N) and I want to calculate the probability that there is a transition from A to B , or > B to C , A to C , from A to A , etc ... in a vector like this:

  

x < - c ("J", "J", "K", "K", "K", "M", "J", "J", "J", "M")

I made a matrix with all possible combinations of the 14 events, like this:

combn(behavior <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"), 2, Fun = null, simplify = FALSE)

I have not been able to get the probability of every possible event in the chain.

    
asked by Perenelle 25.09.2018 в 19:27
source

1 answer

0

First of all let's put together a factor that has the elements of your vector x and whose levels are the complete universe of event A:N

x <- factor(c("J", "J", "K", "K", "K", "M", "J", "J", "J", "M"),
            levels = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")
)

Now calculating the individual probability of each event of x is simple with table() :

valores <- table(x)/length(x)
valores
x
  A   B   C   D   E   F   G   H   I   J   K   L   M   N 
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.3 0.0 0.2 0.0 

But what you are looking for is the probability of obtaining any combination of two events so we should generate the 14x14 combinations and calculate the probability of each of these:

# Producto cartesiano para generar todas las combinaciones
# de dos eventos
final <- merge(valores, valores, by=NULL)

# Calculo la probabilidad de cada combinación
# Y armamos una columna para mostrar la misma
final$ProbXY <- final$Freq.x * final$Freq.y
final$Combinacion <- paste0(final$x.x, " - ", final$x.y)
final <- final[,c("Combinacion", "ProbXY")]

# Mostramos solo las combinaciones cuya probabilidad > 0
final[final$ProbXY != 0, ]

      Combinacion ProbXY
136       J - J   0.25
137       K - J   0.15
139       M - J   0.10
150       J - K   0.15
151       K - K   0.09
153       M - K   0.06
178       J - M   0.10
179       K - M   0.06
181       M - M   0.04
    
answered by 25.09.2018 в 23:35