---
title: "Reliability Testing"
output: 
  learnr::tutorial:
    progressive: true
    allow_skip: true
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(learnr)
library(ReliaGrowR)
library(WeibullR.ALT)
```

## Introduction

Welcome to the tutorial on **Reliability Testing**! In this tutorial, we will explore key concepts and models used in reliability testing, including the Crow-AMSAA model, Duane model, and Accelerated Life Testing (ALT) methods such as the Arrhenius and Power Law models. These models help engineers and reliability professionals assess and improve the reliability of products and systems over time. 

## Learning Objectives

By the end of this tutorial, learners will be able to:

* Define key reliability growth concepts, including the Crow-AMSAA and Duane models.
* Explain fundamental principles of accelerated life testing, including the Arrhenius and Power Law models.
* Fit reliability growth models to real-world datasets using R.
* Apply the Crow-AMSAA model to assess reliability growth.
* Conduct accelerated life tests with real-world datasets, utilizing R for analysis.
* Interpret accelerated life testing plots to identify trends.
* Analyze the impact of stress factors on product reliability using Arrhenius and Power Law models.

## What is Reliability Testing?

Reliability testing is a process to ensure that a product or system performs its intended functions under specified conditions over a defined period. For example, in the manufacturing industry, we may want to design a machine that runs without breaking down for 5 to 10 years.

## Why is Reliability Testing important?

Reliability testing is important for several reasons: 

* Helps identify potential failure modes and weaknesses in a product or system.
* Ensures that products meet quality and safety standards.
* Enhances customer satisfaction by delivering reliable products.
* Reduces costs associated with warranty claims, repairs, and downtime.

## Reliability Growth Analysis

**Reliability Growth Analysis (RGA)** is a method used to monitor and improve the reliability of a product or system over time. It involves analyzing failure data collected during testing phases to identify trends and patterns in reliability performance. 

## The Duane Model

**The Duane Model**, developed by Duane in 1964, is one of the earliest and most widely used models for reliability growth analysis. The model is particularly useful due to its simplicity and ease of interpretation. The Duane plot is a graphical representation that helps visualize whether failure rates are improving, stable, or worsening over time.

Specifically, the Duane Model is a log-log plot of the cumulative Mean Time Between Failures (MTBF) vs cumulative time, where the MTBF is calculated as the total operating time divided by the number of failures observed up to that time.

The slope of the line on the plot indicates the rate of reliability growth:

* A positive slope means that the system is improving (reliability is growing, the failure rate is decreasing).
* A zero slope means there is no change in reliability (the system is stable).
* A negative slope indicates that reliability is worsening (the failure rate is increasing).

### The Duane Model: Reliability Growth vs Deterioration

To illustrate the Duane Model, take the following example. Adjust the Beta value 
input to see the failure rate both decreasing and increasing.

```{r echo=FALSE}
numericInput("beta", label = h3("Beta Value:"), value = 1, min = 0, max = 5, step = 0.1, width = '50%') # Numeric input for beta value
plotOutput("duanePlot") # Plot output for the Duane plot
```

```{r, context="server"}
# Plotting the Duane Model based on user input for beta value
output$duanePlot <- renderPlot({
  x <- seq(1, 100, by = .1)
  y <- 0.1 * x ^ (input$beta - 1)
  log_x <- log(x)
  log_y <- log(y)
  mainTitle <- if (input$beta == 1) {
    "Duane Plot with Stable Failure Rate"
  } else if (input$beta < 1) {
    "Duane Plot with Increasing Failure Rate"
  } else {
    "Duane Plot with Decreasing Failure Rate"
  }
  plot(log_x, log_y, type = "l", col = "blue", xlab = "Time", ylab = "Cumulative MTBF", main = mainTitle)
})
```

As you adjust the Beta value, observe how the slope of the line changes. When
Beta is greater than 1, the slope is positive, indicating that the system is improving. 
When Beta is less than 1, the slope is negative, indicating that the system is worsening. 
When Beta equals 1, the slope is zero, indicating that the system is stable.

### The Duane Model Equation

The cumulative MTBF at any time \( t \) can be expressed as:

\[
CMBTF(t) = K \cdot t^{\beta - 1}
\]

Where:

- \( CMBTF(t) \) is the cumulative MTBF at time \( t \),
- \( K \) is a constant related to the initial failure rate,
- \( t \) is the cumulative operating time,
- \( \beta \) is the reliability growth parameter (slope of the line on the log-log plot).

  - If \( \beta > 1 \), the system reliability is improving (the failure rate decreases over time).
  - If \( \beta = 1 \), the system reliability is constant (no improvement or degradation).
  - If \( \beta < 1 \), the system reliability is deteriorating (the failure rate increases over time).

## ReliaGrowR

### Getting Started with ReliaGrowR

For this tutorial, we will use **ReliaGrowR**: an R package for Reliability Growth Analysis.

First, check if ReliaGrowR is installed in R and install if not.

```{r eval=FALSE}
pak::pkg_install("ReliaGrowR")
```

As an example, suppose a manufacturer wants to analyze the reliability growth of a new product. The product has been tested over a period of time, and the manufacturer has collected data on the cumulative operating time and the number of failures that occurred during that time. The cumulative time and failure data are as follows: 

```{r echo=TRUE}
times <- c(100, 200, 300, 400, 500)
failures <- c(1, 2, 1, 3, 2)
```

We can use the `duane` function with the cumulative time and failure data to fit a Duane model. Then we can use the `plot` function to visualize the results. The `main`, `xlab`, and `ylab` parameters are used to customize the plot title and axis labels.

```{r echo=TRUE}
fit <- duane(times, failures)
plot(fit, main = "Duane Model Example", xlab = "Cumulative Time", ylab = "Cumulative MTBF")
```

The plot shows a Duane Model fit to the data. The slope of the line indicates whether the system is improving, stable, or worsening. In this case, the slope is greater than 1, indicating that the system is improving over time.

```{r quiz1, echo=FALSE}
quiz(caption = "Quiz: Duane Model",
  question("What does a Duane Plot with a slope greater than 1 indicate?",
    answer("The system's reliability is improving", correct = TRUE, 
           message = "A slope greater than 1 indicates improvement."),
    answer("The system's reliability is deteriorating", 
           message = "A slope less than 1 indicates deterioration."),
    answer("The system has no change in reliability", 
           message = "A slope of 1 indicates no change in reliability."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("What does a Duane Plot with a slope less than 1 indicate?",
    answer("The system's reliability is improving",
           message = "A slope greater than 1 indicates improvement."),
    answer("The system's reliability is deteriorating", correct = TRUE, 
           message = "A slope less than 1 indicates deterioration."),
    answer("The system has no change in reliability", 
           message = "A slope of 1 indicates no change in reliability."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the previous example, what is the approximate cumulative MTBF at time 1000?",
    answer("200", message = "200 is too high, try again."),
    answer("100", message = "100 is too low, try again."),
    answer("120", message = "120 is slightly too low, try again."),
    answer("150", correct = TRUE, message = "The cumulative MTBF at time 1000 is approximately 150."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the previous example, how would you describe the reliability of the system?",
    answer("The system reliability is improving", correct = TRUE, 
           message = "The slope is greater than 1, so the system is improving."),
    answer("The system reliability is worsening", 
           message = "The slope is not less than 1, so the system is not worsening."),
    answer("The system reliability is stable", 
           message = "The slope is not close to 1, so the system is not stable."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  )
)
```

### Exercise: Fit a Duane Model

A new system was tested and the following cumulative failure counts were recorded at each cumulative test time. Fit a Duane model using `duane()` and plot the result.

```{r ex-rt1, exercise=TRUE}
times    <- c(200, 450, 750, 1100, 1500, 2000)
failures <- c(3, 2, 2, 1, 1, 1)

```

```{r ex-rt1-hint}
# Use duane() with cumulative times and per-interval failure counts, then plot
# fit <- duane(time = times, event = failures)
# plot(fit)
```

```{r ex-rt1-solution}
times    <- c(200, 450, 750, 1100, 1500, 2000)
failures <- c(3, 2, 2, 1, 1, 1)
fit <- duane(time = times, event = failures)
plot(fit, main = "Duane Reliability Growth Plot",
     xlab = "Cumulative Test Time", ylab = "Cumulative Failures")
```

## The Crow-AMSAA Model

The **Crow-AMSAA Model** is another widely used model for reliability growth analysis, developed by Larry Crow and the  Army Materiel Systems Analysis Activity (AMSAA) organization in the 1970s. This model is particularly useful for systems that undergo continuous improvements and modifications during their development and testing phases. The Crow-AMSAA model is based on the concept of a Non-Homogeneous Poisson Process (NHPP), which allows for the modeling of failure behavior that changes over time.

Specifically, the Crow-AMSAA Model is a model of the cumulative number of failures vs cumulative time. 

Similar to the Duane Model, the shape of the model indicates the rate of reliability growth:

* A model fit with downward curvature means that the system is improving (reliability is growing, the failure rate is decreasing).
* A linear model means there is no change in reliability (the system is stable).
* A model fit with upward curvature indicates that reliability is worsening (the failure rate is increasing).

### The Crow-AMSAA Model: Reliability Growth vs Deterioration

To illustrate the Crow-AMSAA model, take the following example. Adjust the Beta 
value input to see the failure rate both decreasing and increasing.

```{r echo=FALSE}
numericInput("beta", label = h3("Beta Value:"), value = 1, min = 0, max = 5, step = 0.1, width = '50%') # Numeric input for beta value
plotOutput("crowPlot") # Plot output for the Crow-AMSAA plot
```

```{r, context="server"}
# Plotting the Crow-AMSAA Model based on user input for beta value
output$crowPlot <- renderPlot({
  x <- seq(1, 100, by = .1)
  y <- 0.5 * x ^ input$beta
  mainTitle <- if (input$beta == 1) {
    "Crow-AMSAA Model with Stable Failure Rate"
  } else if (input$beta < 1) {
    "Crow-AMSAA Model with Decreasing Failure Rate"
  } else {
    "Crow-AMSAA Model with Increasing Failure Rate"
  }
  plot(x, y, type = "l", col = "darkgreen", xlab = "Time", ylab = "Cumulative Failures", main = mainTitle)
})
```

As you adjust the Beta value, observe how the curvature of the line changes. When
Beta is greater than 1, the line curves upward, indicating that the system is worsening. 
When Beta is less than 1, the line curves downward, indicating that the system is improving. 
When Beta equals 1, the line is linear, indicating that the system is stable.

### The Crow-AMSAA Model Equation:

The cumulative number of failures up to time \( t \) is given by:

\[N(t) = \lambda_0 \cdot t^{\beta}\]

Where:

* \( N(t) \) is the cumulative number of failures,
* \( \lambda_0 \) is the scale parameter.

* The Shape Parameter (\( \beta \)) indicates whether the system is improving or deteriorating:

  * If \( \beta \) > 1, failures are increasing over time, indicating that reliability is worsening.
  * If \( \beta \) < 1, failures are decreasing, indicating that reliability is improving over time.
  * \( \beta \) = 1 implies a constant failure rate (no growth or degradation).

### Crow-AMSAA with ReliaGrowR

As an example, suppose the same manufacturer from the Duane Model example also wants to analyze the reliability growth of a new product using the Crow-AMSAA model. The cumulative time and failure data are as follows:

```{r}
times <- c(100, 200, 300, 400, 500)
failures <- c(1, 2, 1, 3, 2)
```

To run a Crow-AMSAA model with ReliaGrowR, use the `rga` function to fit the model and the `plot` function to plot the results:

```{r}
result <- rga(times, failures)
plot(result, main = "Crow-AMSAA Model Example", xlab = "Cumulative Time", ylab = "Cumulative Failures")
```

The plot shows a Crow-AMSAA Model fit to the data. The curvature of the line indicates whether the system is improving, stable, or worsening. In this case, the slope is curving downward, indicating that the system is improving over time.

```{r quiz2, echo=FALSE}
quiz(caption = "Quiz: Crow-AMSAA Model",
  question("What does a Crow-AMSAA Model with a Beta greater than 1 indicate?",
    answer("The system's reliability is improving", 
           message = "A Beta less than 1 indicates improvement."),
    answer("The system's reliability is deteriorating", correct = TRUE, 
           message = "A Beta greater than 1 indicates deterioration."),
    answer("The system has no change in reliability", 
           message = "A Beta of 1 indicates no change in reliability."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("What does a Crow-AMSAA Model with a Beta less than 1 indicate?",
    answer("The system's reliability is improving", correct = TRUE, 
           message = "A Beta less than 1 indicates improvement."),
    answer("The system's reliability is deteriorating", 
           message = "A Beta greater than 1 indicates deterioration."),
    answer("The system has no change in reliability", 
           message = "A Beta of 1 indicates no change in reliability."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the previous example, what is the cumulative number of failures at time 1000?",
    answer("4", message = "4 is too low, try again."),
    answer("8", message = "8 is slightly too high, try again."),
    answer("6", message = "6 is slightly too low, try again."),
    answer("7", correct = TRUE, 
           message = "The cumulative number of failures at time 1000 is approximately 7."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the previous example, how would you describe the reliability of this system?",
    answer("The reliability of the system is improving", correct = TRUE, 
           message = "The slope is curving downward, so the reliability is improving."),
    answer("The reliability of the system is worsening", 
           message = "The slope is not curving upward, so the reliability is not worsening."),
    answer("The reliability of the system is stable", 
           message = "The slope is not linear, so the reliability is not stable."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  )
)
```

### Exercise: Fit a Crow-AMSAA Model

A development test recorded the following cumulative failure counts at cumulative test times. Use `rga()` with `model_type = "Crow-AMSAA"` to fit the model and plot the result.

```{r ex-rt2, exercise=TRUE}
times    <- c(50, 120, 220, 360, 530, 740, 1000)
failures <- c(4, 3, 3, 2, 2, 1, 1)

```

```{r ex-rt2-hint}
# Use rga() with model_type = "Crow-AMSAA", then plot
# fit <- rga(time = times, event = failures, model_type = "Crow-AMSAA")
# plot(fit)
```

```{r ex-rt2-solution}
times    <- c(50, 120, 220, 360, 530, 740, 1000)
failures <- c(4, 3, 3, 2, 2, 1, 1)
fit <- rga(time = times, event = failures, model_type = "Crow-AMSAA")
plot(fit, main = "Crow-AMSAA Reliability Growth",
     xlab = "Cumulative Test Time", ylab = "Cumulative Failures")
```

### The Piecewise NHPP Model

**The Piecewise NHPP model** is another extension of the Crow-AMSAA model that allows for different phases of reliability growth or deterioration. This model is particularly useful when a system undergoes significant changes in its failure behavior over time, such as during different stages of development or after major design changes.

For a Piecewise NHPP model, the cumulative number of failures is modeled as a piecewise function, where each segment has its own parameters. Formally, for time t within phase i, the relationship between the cumulative number of failures and cumulative time is given by:

\[
N_i(t) = N(t_{i-1}) + \lambda_i (t - t_{i-1})^{\beta_i}, \, \text{for } t_{i-1} < t \leq t_i
\]

where \(N_i(t)\) is the cumulative number of failures by time \(t\) in phase \(i\), \(\lambda_i\) is the scale parameter for phase \(i\), and \(\beta_i\) is the shape parameter for phase \(i\).

### Example

To illustrate the Piecewise NHPP model, take the following example. Suppose a manufacturer has tested a product over a period of time and collected data on the cumulative operating time and the number of failures that occurred during that time. At about time 500, a significant design change was made to the product, which is expected to impact its reliability. The cumulative time, failure data, and breakpoint are as follows:

```{r}
times <- c(25, 55, 97, 146, 201, 268, 341, 423, 513, 609, 710, 820, 940, 1072, 1217)
failures <- c(1, 1, 2, 4, 4, 1, 1, 2, 1, 4, 1, 1, 3, 3, 4)
breaks <- 500
```

To run a Piecewise NHPP model with ReliaGrowR, use the `rga` function with `model_type` set to "Piecewise NHPP" and `breaks` set to the breakpoint.

```{r}
result <- rga(times, failures, model_type = "Piecewise NHPP", breaks = breaks)
plot(result, main = "Piecewise NHPP Model Example", xlab = "Cumulative Time", ylab = "Cumulative Failures")
```

The plot shows a Piecewise NHPP Model fit to the data, with a breakpoint at time 500. The two segments of the model have different slopes, indicating that the failure behavior changed after the design change. The first segment has a higher slope, than the second segment, indicating that the failure rate decreased after the design change. 

### The Piecewise NHPP with Change Point Detection

The **Piecewise NHPP with Change Point Detection** is an advanced model that identifies change points in the failure data to automatically segment the data into different phases of reliability growth or deterioration. This approach is particularly useful when the exact timing of changes in failure behavior is unknown or when multiple changes may occur over times. 

### Example

To illustrate the Piecewise NHPP with Change Point Detection, take the previous example but without specifying a breakpoint. The cumulative time and failure data are as follows:

```{r}
times <- c(25, 55, 97, 146, 201, 268, 341, 423, 513, 609, 710, 820, 940, 1072, 1217)
failures <- c(1, 1, 2, 4, 4, 1, 1, 2, 1, 4, 1, 1, 3, 3, 4)
```

To run a Piecewise NHPP model with Change Point Detection using ReliaGrowR, use the `rga` function with `model_type` set to "Piecewise NHPP" without specifying `breaks`.

```{r}
result <- rga(times, failures, model_type = "Piecewise NHPP")
plot(result, main = "Piecewise NHPP with Change Point Detection", xlab = "Cumulative Time", ylab = "Cumulative Failures")
```

The plot shows a Piecewise NHPP Model fit to the data, with change points automatically detected. The change point is close to time 500, which is consistent with the design change made to the product. 

## Accelerated Life Testing

**Accelerated Life Testing (ALT)** is a method used to estimate the life of a product or system under normal operating conditions by subjecting it to elevated stress levels in a controlled environment. The goal is to identify potential failure modes and predict how long the product will last under normal conditions without having to wait for failures to occur naturally over a longer time period.

### Acceleration Factor

Suppose you are testing a product that is expected to last a long time under normal conditions. Waiting for it to fail naturally could take months or even years. Instead, you can use ALT to accelerate the testing process by applying higher stress levels, such as increased temperature, voltage, or pressure, to induce failures more quickly.

The relationship between stress and life is often modeled using an exponential function, where the life of the product decreases as the stress level increases. The rate of this decrease is determined by the Acceleration Factor (AF), which quantifies how much faster the product fails under elevated stress compared to normal conditions. 

The plot below illustrates this concept. Adjust the Acceleration Factor (AF) to see how it impacts the expected life of the product.

```{r echo=FALSE}
sliderInput("af", label = h3("Acceleration Factor:"), value = 1, min = 1, max = 10, step = 0.1, width = '50%') # Slider input for acceleration factor
plotOutput("altPlot") # Plot output for the ALT plot
```

```{r, context="server"}
# Plotting the Accelerated Life Testing model based on user input for acceleration factor
output$altPlot <- renderPlot({
  y <- seq(1, 10, by = .1)
  x <- 3678.79 * exp(1 / y)
  life <- 3678.79 * exp(1 / input$af)
  plot(x, y, type = "l", col = "blue", xlab = "Life", ylab = "Stress Factor", main = "Stress Factor vs Life")
  abline(v = life, col = "red", lty = 2)
  legend("topright", legend = c("Expected Life = ", ceiling(life)))
})
```

In this plot, the blue curve represents the relationship between the stress and the expected life. The red dashed line indicates the expected life for the selected acceleration factor. As you increase the acceleration factor, the expected life decreases, demonstrating how elevated stress levels can significantly reduce the lifespan of a product.

## The Arrhenius Model

The **Arrhenius Model** is one of the most commonly used models in ALT. It describes the relationship between temperature and the rate of chemical reactions, which can be directly related to the degradation or failure rate of materials. The model is based on the Arrhenius equation, which states that the rate of a chemical reaction increases exponentially with an increase in temperature.

The Acceleration Factor (AF) for the Arrhenius model is given by:

$$ AF = \exp\left(\frac{E_a}{k} \left( \frac{1}{T_{\text{use}}} - \frac{1}{T_{\text{stress}}} \right) \right) $$

Where:

- \( E_a \) = Activation energy in electron volts (eV). The activation energy is a measure of the energy barrier that must be overcome for a chemical reaction to occur. Higher activation energies indicate that the reaction is more sensitive to temperature changes.
- \( k \) = Boltzmann constant (\( 8.617 \times 10^{-5} \, \text{eV/K} \)). The Boltzmann constant relates the average kinetic energy of particles in a gas with the temperature of the gas.
- \( T_{\text{use}} \) = Temperature at normal use condition in Kelvin
- \( T_{\text{stress}} \) = Temperature at accelerated stress condition in Kelvin

By using the Arrhenius model, you can determine how much faster a product will fail at an elevated temperature compared to its normal operating temperature. 

## WeibullR.ALT

For this tutorial, we will use **WeibullR.ALT**, an extension of the WeibullR package for Accelerated Life Testing.

First, check if WeibullR.ALT is installed in R and install if not.

```{r eval=FALSE}
pak::pkg_install("WeibullR.ALT")
```

To illustrate the Arrhenius Model, we will use the data set `MeekerData` that comes prepackaged with `WeibullR.ALT`. This data set contains time to failure data for carbon-film resistors tested at different temperature levels. The testing was conducted at 4 increasing temperature levels to accelerate the failure process. The temperature levels were 10, 40, 60, and 80 degrees Celsius.

First, load the data set and use the `head` function to view the first few rows.

```{r}
data <- MeekerData("table10")
head(data)
```

The data set contains columns for the time to failure (time), event type (event) (1 = failure, 0 = suspension), quantity of samples (qty) and temperature as the acceleration factor (TempC). 

Next, use the `alt.data` function to create a separate object for the data at each temperature level. Each data set is created by subsetting the original data set for each temperature level and selecting only the relevant columns (time, event, qty). The `stress` parameter is set to the corresponding temperature level. The `which` function is used to filter the rows based on the temperature level.

```{r}
data10C <- alt.data(data[which(data$TempC==10), 1:3], 
                    stress = data[which(data$TempC==10), 4][1])
data40C <- alt.data(data[which(data$TempC==40), 1:3], 
                    stress = data[which(data$TempC==40), 4][1])
data60C <- alt.data(data[which(data$TempC==60), 1:3], 
                    stress = data[which(data$TempC==60), 4][1])
data80C <- alt.data(data[which(data$TempC==80), 1:3], 
                    stress = data[which(data$TempC==80), 4][1])
```

Then, use the `alt.make` function to fit an Arrhenius-Weibull model to the data. Each data set is passed as a list to the `alt.make` function, along with the distribution type (`dist = "weibull"`), the acceleration model (`alt.model = "arrhenius"`), and the fitting method (`method.fit = "mle"`). The resulting object `arrobj` contains the fitted model.

```{r}
arrobj <- alt.make(list(data10C, data40C, data60C, data80C), dist = "weibull",
                alt.model = "arrhenius", method.fit = "mle")
```

The plot shows an Arrhenius-Weibull model fit to the data. The different colors represent the different temperature levels. Starting with a temperature level of 40 degrees C on the right, the curves shift to the left as the temperature increases, indicating that higher temperatures lead to shorter life.

Finally, calculate the AF at 80 degrees C using the Arrhenius Model. Assume that the Activation Energy is 0.7 eV. The code below calculates the AF using the formula provided earlier, with \( T_{\text{use}} \) set to 10 degrees C (283K) and \( T_{\text{stress}} \) set to 80 degrees C (353K).

```{r, results='asis'}
E_a <- 0.7
k <- 8.617E-5
T_use <- 10 + 273
T_stress <- 80 + 273
AF <- exp(E_a / k * (1 / T_use - 1 / T_stress))
cat(paste0("The Acceleration Factor at 80 degrees C is ", round(AF, 2)))
```

The AF at 80 degrees C is approximately 300. This means that the resistors tested at 80 degrees C are expected to fail 300 times faster than those tested at 10 degrees C.

```{r quiz3, echo=FALSE}
quiz(caption = "Quiz: Arrhenius Model",
  question("What does the Arrhenius-Weibull model indicate about the relationship between temperature and life?",
    answer("Higher temperatures lead to longer life", 
           message = "Higher temperatures actually lead to shorter life."),
    answer("Higher temperatures lead to shorter life", correct = TRUE, 
           message = "Higher temperatures lead to shorter life, as indicated by the leftward shift of the curves."),
    answer("Temperature has no impact on life", 
           message = "Temperature does have an impact on life, as shown by the different curves."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the Arrhenius Model, what does the Activation Energy (E_a) represent?",
    answer("The energy required to initiate a chemical reaction", correct = TRUE, 
           message = "Activation Energy is the energy required to initiate a chemical reaction."),
    answer("The temperature at which a material fails", 
           message = "Activation Energy is not the temperature at which a material fails."),
    answer("The rate of degradation of a material", 
           message = "Activation Energy is not the rate of degradation of a material."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the previous example, what is the approximate unreliability for 
           a duration of 3000 and an operating temperature of 60 (the green curve)?",
    answer("90%", message = "90% is the approximate unreliability for a duration of 
           3000 and an operating temperature of 80 (the purple curve)."),
    answer("10%", message = "10% is too low, try again."),
    answer("70%", message = "70% is the reliability at these levels, not the unreliability"),
    answer("30%", correct = TRUE, 
           message = "The approximate unreliability for a duration of 3000 and 
           an operating temperature of 60 is 30%."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the previous example, what is the approximate reliability for a duration of 1000 and an operating stress of 80 (the purple curve)?",
    answer("70%", message = "70% is too high, try again."),
    answer("30%", message = "30% is too low, try again."),
    answer("40%", message = "40% is the unreliability at these levels, not the reliability"),
    answer("60%", correct = TRUE, message = "The approximate reliability for a duration of 1000 and 
           an operating temperature of 80 is 60%."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  )
)
```

### Exercise: Compute an Arrhenius Acceleration Factor

A component is tested at 85°C to accelerate failures. The use temperature is 25°C. The activation energy for the failure mechanism is 0.65 eV. Compute the Acceleration Factor using the Arrhenius model.

```{r ex-rt3, exercise=TRUE}
E_a      <- 0.65        # activation energy (eV)
k        <- 8.617e-5    # Boltzmann's constant (eV/K)
T_use    <- 25 + 273.15 # use temperature in Kelvin
T_stress <- 85 + 273.15 # stress temperature in Kelvin

```

```{r ex-rt3-hint}
# AF = exp((E_a / k) * (1/T_use - 1/T_stress))
```

```{r ex-rt3-solution}
E_a      <- 0.65
k        <- 8.617e-5
T_use    <- 25 + 273.15
T_stress <- 85 + 273.15
AF <- exp((E_a / k) * (1 / T_use - 1 / T_stress))
cat("Acceleration Factor:", round(AF, 1), "\n")
```

## The Power Law Model

The **Power Law Model** is another commonly used model in ALT, particularly for mechanical systems and components. This model describes how a product's life is affected by a certain stress factor (such as temperature, voltage, or pressure). Unlike the Arrhenius model, which is primarily used for temperature-based stresses, the Power Law Model can be applied to a wide variety of stress factors.

The AF for the Power Law model is given by:

$$ AF = \left( \frac{S_{\text{stress}}}{S_{\text{use}}} \right)^{n} $$

Where:

- \( S_{\text{stress}} \) = Stress level during accelerated testing (e.g., voltage, load)
- \( S_{\text{use}} \) = Stress level during normal use conditions
- \( n \) = Stress exponent, indicating the sensitivity of the failure mechanism to the applied stress

To illustrate the Power Law Model, we will use the data set `NelsonData` that comes prepackaged with `WeibullR.ALT`. This data set contains time to failure data for electrical insulating fluids tested at different voltage stress levels. The testing was conducted at 7 increasing voltage levels to accelerate the failure process. 

First, load the data set..

```{r results='asis'}
data <- NelsonData("table3.1")
```

The data is a list of time to failure data at 7 different voltage stress levels, 26 kilovolts (KV), 28kV, 30kV, 32kV, 34kV, 36kV, and 38kV.

Next, use the `alt.data` function to create a separate object for the data at each stess level. The `stress` parameter is set to the corresponding voltage level.

```{r}
data26 <- alt.data(data$kV26, stress = 26)
data28 <- alt.data(data$kV28, stress = 28)
data30 <- alt.data(data$kV30, stress = 30)
data32 <- alt.data(data$kV32, stress = 32)
data34 <- alt.data(data$kV34, stress = 34)
data36 <- alt.data(data$kV36, stress = 36)
data38 <- alt.data(data$kV38, stress = 38)
```

Then, use the `alt.make` function to fit a Power-Weibull model to the data and plot the results. Similar to the previous example, each data set is passed as a list to the `alt.make` function, along with the distribution type (`dist = "weibull"`) and the acceleration model (`alt.model = "power"`). The resulting object `pwrobj` contains the fitted model.

```{r}
pwrobj <- alt.make(list(data26, data28, data30, data32, data34, data36, data38), 
                   dist = "weibull", alt.model = "power")
```

The plot shows a Power-Weibull model fit to the data. The different colors represent the different voltage stress levels. Starting with a stress level of 28kV on the right (dark green curve), the curves shift to the left as the stress increases, indicating that higher stress leads to shorter life.

```{r quiz4, echo=FALSE}
quiz(caption = "Quiz: Power Law Model",
  question("In the previous example, which subset of data does not seem to follow the Power Law model?",
              answer("The data with a stress level of 38kV", 
                     message = "The data with a stress level of 38kV seems to follow the same shape as the other subsets."),
              answer("The data with a stress level of 28kV", 
                     message = "The data with a stress level of 28kV seems to follow the same shape as the other subsets."),
              answer("The data with a stress level of 36kV", 
                     message = "The data with a stress level of 36kV seems to follow the same shape as the other subsets."),
              answer("The data with a stress level of 26kV", correct = TRUE, 
                     message = "The data with a stress level of 26kV has a different shape than the other subsets. Although this may be due to limited data at this stress level (only 3 failures)."),
              random_answer_order = TRUE,
              allow_retry = TRUE
     ),
   question("In the previous example, what is the relationship between the data with a stress level of 26kV (the dark green curve) and the data with a stress level of 30kV (the red curve)?",
            answer("The plot to the right (dark green) has a lower unreliability than the one to the left (red)", correct = TRUE),
            answer("The plot to the right (dark green) a higher unreliability than the one to the left (red)"),
            answer("The plot to the right (dark green) has a lower reliability than the one to the left (red)"),
            answer("The plot to the right (dark green) has a higher reliability than the one to the left (red)", correct = TRUE),
            random_answer_order = TRUE,
            allow_retry = TRUE
   )
)
```

## Multiple Stress Factors

The **Eyring Model** is an extension of the Arrhenius model and is commonly used in ALT to account for the combined effects of multiple stressors such as temperature, voltage, and humidity on the failure mechanisms of a product. This model is particularly useful when the product is exposed to environmental factors beyond just temperature.

The general form of the Eyring Model is:

\[\text{Life}(S) = A \cdot \exp\left(\frac{B}{S} + C \cdot S\right)\]

Where:

* \( \text{Life}(S) \) is the expected life under stress level \( S \),
* \( A \) is a constant,
* \( B \) and \( C \) are model parameters,
* \( S \) is the applied stress factor (e.g., temperature, voltage, humidity).

This module does not include an example of the Eyring Model, but the `WeibullR.ALT` package does support it. See the package documentation for more information.

## Parallelization

Parallelization is a technique used in ALT to simplify the analysis of data collected at multiple stress levels. By assuming that the shape factor (slope) of the life distribution is the same across all stress levels, the analysis can be streamlined, making it easier to interpret the results and draw conclusions about the relationship between stress and life.

To fit a parallel model to the Meeker data, use the `alt.parallel` function.
```{r}
prlobj <- alt.parallel(arrobj)
```

The plot shows a Parallel model fit to the data. All curves have the same slope, indicating that all subsets have the same shape factor.

Similarly, to fit a parallel model to the Nelson data, use the `alt.parallel` function.
```{r}
expobj <- alt.parallel(pwrobj)
```

## Relationship Plots

Another useful tool in ALT is the relationship plot. The relationship plot provides a visual representation of how the life of a product changes as the stress factor increases. A relationship plot is a log-linear plot with the stress factor on the x-axis and the log of the unreliability (failure probability) on the y-axis. 

A linear model is fit to the data, which shows patterns indicating how life changes as stress increases. The slope of the line provides insights into the relationship between stress and life:

* A **negative slope** (decreasing life with increasing stress) indicates that higher stress leads to shorter life. 
* A **positive slope** (increasing life with increasing stress) is less common but indicates that life improves with higher stress.
* A **flat slope** indicates that higher stress does not have an impact on life. 

To fit a relationship plot to the Meeker data, first fit a linear model to the data using the `alt.fit` function and then plot the results using the `plot` function.

```{r}
lnrobj <- alt.fit(prlobj)
plot(lnrobj, suppress.dev.new = TRUE)
```

The relationship plot shows a negative slope, indicating that higher temperature stress tends to lead to shorter life. Each line indicates a different probability of failure. For example, the P63.2 line indicates the stress vs life relationship for a 63.2% probability of failure. The P90 line indicates the stress vs life relationship for a 90% probability of failure. 

Similarly, to fit a relationship plot to the Nelson data, first fit a linear model to the data and then plot the results.

```{r}
lnrobj2 <- alt.fit(expobj)
plot(lnrobj2, suppress.dev.new = TRUE)
```

Similarly, the relationship plot shows a negative slope, indicating that higher voltage stress tends to lead to shorter life. 

```{r quiz5, echo=FALSE}
quiz(caption = "Quiz: Relationship Plots",
  question("In the Arrhenius Weibull example, what is the approximate P63.2 time to failure at a stress level of 50?",
    answer("1000", message = "1000 is too low, try again."),
    answer("5,000", correct = TRUE, 
           message = "The approximate P63.2 time to failure at a stress level of 50 is 5,000."),
    answer("50,000", message = "50,000 is too high, try again."),
    answer("100,000", message = "100,000 is way too high, try again."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the Arrhenius Weibull example, what does the slope of the relationship plot indicate about the relationship between stress and life?",
    answer("Higher stress does not seem to have an impact on life", 
           message = "A flat slope would indicate that higher stress does not have an impact on life."),
    answer("Higher stress tends to lead to longer life", message = "A positive slope would indicate that higher stress leads to longer life."),
    answer("Higher stress tends to lead to shorter life", correct = TRUE, 
           message = "The negative slope indicates that higher stress tends to lead to shorter life."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the Power Weibull example, what does the slope of the relationship plot indicate about the relationship between stress and life?",
    answer("Higher stress does not seem to have an impact on life", 
           message = "A flat slope would indicate that higher stress does not have an impact on life."),
    answer("Higher stress tends to lead to longer life", message = "A positive slope would indicate that higher stress leads to longer life."),
    answer("Higher stress tends to lead to shorter life", correct = TRUE, 
           message = "The negative slope indicates that higher stress tends to lead to shorter life."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  ),
  question("In the Power Weibull example, what is the approximate P90 time to failure at a stress level of 30?",
    answer("100", correct = TRUE, 
           message = "The approximate P90 time to failure at a stress level of 30 is 100."),
    answer("10", message = "10 is too low, try again."),
    answer("1000", message = "1000 is too high, try again."),
    answer("100,000", message = "100,000 is way too high, try again."),
    random_answer_order = TRUE,
    allow_retry = TRUE
  )
)
```

## Summary

Congratulations on completing the Reliability Testing tutorial!

In this tutorial, we've introduced the concepts of Reliability Growth Analysis and Accelerated Life Testing. You have learned to calculate and visualize reliability using different types of models.

### To Get Help

To get help with a specific function in ReliaGrowR, type a question mark before the function name.

```{r echo=TRUE, eval=FALSE}
?rga
```

Similarly, to get help with a specific function in WeibullR.ALT, type:

```{r echo=TRUE, eval=FALSE}
?alt.make
```

For more help with the ReliaGrowR package, type:

```{r echo=TRUE, eval=FALSE}
help(package="ReliaGrowR")
```

And for more help with the WeibullR.ALT package, type:

```{r echo=TRUE, eval=FALSE}
help(package="WeibullR.ALT")
```

### Additional Resources

- The ReliaGrowR package documentation: <https://cran.r-project.org/package=ReliaGrowR>
- The WeibullR.ALT package documentation: <https://cran.r-project.org/package=WeibullR.ALT>
- The WeibullR package documentation: <https://cran.r-project.org/package=WeibullR>

### References

- Abernethy, R.B. (2004). The New Weibull Handbook. Fifth Edition.

- Aden-Buie G, Schloerke B, Allaire J, Rossell Hayes A (2023). learnr: Interactive Tutorials for R. https://rstudio.github.io/learnr/, https://github.com/rstudio/learnr.

- Crow, Larry H. (1975). Reliability Analysis for Complex, Repairable Systems. Army Material Systems Analysis Activity, 40. <https://apps.dtic.mil/sti/citations/ADA020296>

- Fasola S, Muggeo VMR, Kuchenhoff K. (2018). A heuristic, iterative algorithm for change-point detection in abrupt change models. Computational Statistics, 33, 997-1015.

- Govan P (2024). ReliaGrowR: Reliability Growth Analysis. R package version 0.1,
<https://cran.r-project.org/package=ReliaGrowR>.

- Guo, H., Mettas, A., Sarakakis, G. and Niu, P., (2010). Piecewise NHPP models with maximum likelihood estimation for repairable systems. 2010 Proceedings - Annual Reliability and Maintainability Symposium (RAMS), 1-7. <https://doi.org/10.1109/RAMS.2010.5448029>.
  
- Duane J. T. (1964). Learning Curve Approach to Reliability Monitoring. IEEE Transactions on Aerospace, 2, 563-566, <https://doi.org/10.1109/TA.1964.4319640>.

- Meeker W. Q., Escobar L. A., (1998). Statistical Methods for Reliability Data, Wiley-Interscience, New York.

- Nelson W, (1990). Accelerated Testing, Wiley-Interscience, New York.

- Silkworth, David. (2020). WeibullR: An R Package for Weibull Analysis for Reliability Engineers. 43-53. <https://doi.org/10.35566/isdsa2019c3>.

- Silkworth D, Symynck J (2022). WeibullR: Weibull Analysis for Reliability Engineering. R package version 1.2.1, <https://CRAN.R-project.org/package=WeibullR>.

- Silkworth D (2022). WeibullR.ALT: Accelerated Life Testing Using 'WeibullR'. R package version 0.7.2, <https://CRAN.R-project.org/package=WeibullR.ALT>.
