I'm building a Markov chain model to determine the probability of two states, WS (Working State) and CFS (Complete Failure State), for production machines. I applied the following code to generate a chain for each machine:
This is markov_df (the dataframe contains 3 years of history):
markov_df <- data.frame(
machine = c(
"T", "T", "T", "T", "T", "T", "T",
"Z-2H", "Z-2H", "Z-2H", "Z-2H", "Z-2H", "Z-2H", "Z-2H",
"R-3H", "R-3H", "R-3H", "R-3H", "R-3H", "R-3H", "R-3H",
"X-11H", "X-11H", "X-11H", "X-11H", "X-11H", "X-11H", "X-11H",
"X-33H", "X-33H", "X-33H", "X-33H", "X-33H", "X-33H", "X-33H",
"X-34H", "X-34H", "X-34H", "X-34H", "X-34H", "X-34H", "X-34H",
"Y-7H", "Y-7H", "Y-7H", "Y-7H", "Y-7H", "Y-7H", "Y-7H",
"Y-9H", "Y-9H", "Y-9H", "Y-9H", "Y-9H", "Y-9H", "Y-9H"),
date = rep(
c(
"01/09/2024", "01/10/2024", "01/11/2024", "01/12/2024",
"01/13/2024", "01/14/2024", "01/15/2024"),
8),
up_down = c(
"WS", "WS", "WS", "WS", "WS", "WS", "WS",
"WS", "WS", "WS", "WS", "WS", "WS", "WS",
"WS", "WS", "WS", "WS", "WS", "WS", "WS",
"WS", "WS", "WS", "WS", "WS", "CFS", "CFS",
"WS", "WS", "WS", "WS", "WS", "CFS", "CFS",
"WS", "WS", "WS", "WS", "WS", "CFS", "CFS",
"WS", "WS", "WS", "WS", "WS", "WS", "WS",
"WS", "WS", "WS", "WS", "WS", "CFS", "CFS"))
library(markovchain)
unique_machine <- unique(markov_df$machine)
data_transition <- data.frame(Machine = character(), State_origin = character(),
Final_state = character(), Probability = numeric(), stringsAsFactors = FALSE)
for (machine in unique_machine) {
df_machine <- subset(markov_df, machine == machine)
matriz_transition_estimate <- markovchainFit(data = df_machine$up_down, method = "mle")
matriz_transition_df <- as.data.frame(as(matriz_transition_estimate$estimate, "matrix"))
names(matriz_transition_df) <- unique(df_machine$up_down)
matriz_transition_long <- reshape2::melt(matriz_transition_df,
varnames = c("State_origin", "Final_state"),
value.name = "Probability")
matriz_transition_long$machine <- machine
data_transition <- rbind(data_transition, matriz_transition_long)
}
data_transition <- data_transition %>%
group_by(machine) %>%
mutate(new_column = row_number())
data_transition$State <- ifelse(data_transition$new_column == 1, "CFS-CFS",
ifelse(data_transition$new_column == 2, "WS-CFS",
ifelse(data_transition$new_column == 3, "CFS-WS",
ifelse(data_transition$new_column == 4, "WS-WS","Check"))))
Now I want to find the state probabilities (I have tried with steadyStates) and the Markov property (I have tried with verifyMarkovProperty) of each machine, and save the input in a dataframe with the machine's name and the results.
for (machine in unique_machine) {
df_machine2 <- subset(markov_df, unique_machine == unique_machine)
markov_chain <- markovchainFit(data = df_machine2$up_down, method = "mle")
prob_ <- steadyStates(markov_chain)
result[[machine]] <- prob_state
}
results_df <- do.call(rbind, lapply(seq_along(result), function(i) {
data.frame(machine = names(result)[i], Estado = c("WS", "CFS"), Probability = result[[i]])
}))
But it's doesn´t works!