Simulating Failure of Engines in R

Here is my R code for 5 engine failures in parallel: all engines start with a failure probability of 0.5...each turn the probability of failure increases by 0.01 ... when it fails for the first time, its out forever. This continues until all 5 engines have failed:

    engines <- paste0("engine_", 1:5)  
prob_working <- 0.5

max_turns <- 100  

df <- data.frame(matrix(ncol = 11, nrow = max_turns))
colnames(df) <- c("turn", paste0("engine_", 1:5), paste0("engine_", 1:5, "_prob"))

for (i in seq_along(.engines)) {
    engine <- .engines[i]
    turn <- 1
    while(turn <= max_turns) {
        result <- ifelse(runif(1) < prob_working, "working", "failure")
        
        df[turn, "turn"] <- turn
        df[turn, engine] <- result
        df[turn, paste0(engine, "_prob")] <- prob_working
        
        if (result == "failure") {
            break
        }
        
        prob_working <- prob_working - 0.01
        
        prob_working <- max(prob_working, 0)
        
        turn <- turn + 1
    }
    

    if (df[turn, engine] == "failure") {
        prob_working <- 0.5
    }
}


df <- df[1:max(df$turn, na.rm = TRUE), ]
df

See below:

 turn engine_1 engine_2 engine_3 engine_4 engine_5 engine_1_prob engine_2_prob engine_3_prob engine_4_prob engine_5_prob
1    1  failure  failure  working  failure  working           0.5           0.5          0.50           0.5          0.50
2    2     <NA>     <NA>  failure     <NA>  working            NA            NA          0.49            NA          0.49
3    3     <NA>     <NA>     <NA>     <NA>  working            NA            NA            NA            NA          0.48
4    4     <NA>     <NA>     <NA>     <NA>  failure            NA            NA            NA            NA          0.47

How can I change this code so that at the start of each turn, we look at how many engines have failed ... and the remaining engines from now on will have a failure probability of 0.01 * (n+1) , where n = number of failed engines?

You currently loop over engines in the outer loop and turns in the inner loop. You will need to rewrite the code with those loops reversed (engines nested inside turns).

1 Like

thank you ... any chance you can help me do this if you have time please?

Sorry, no. Too many things to do, too many people to do them to.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.