Bullmer Fabric Cutter Reliability - Refined

library(WeibullR)
library(readxl)
rm(list = ls()) # remove all environment
Blmr <- read_excel("Bullmer.xlsx", col_types = c("date", "skip", "text", "text", "text", "text", 
                                                    "numeric", "numeric", "numeric", "skip", 
                                                    "skip", "skip"))
#View(Blmr)

f <- function(time, title, pch) {
  today = as.numeric(Sys.Date()-as.Date("2010-01-01"))
  fday = as.numeric(as.Date("2018-06-01")-as.Date("2010-01-01"))
  
  d <- wblr(time, pp = "median", pch = pch)
  d$options$main <- c(title , d$options$main)
  d2 <- wblr.conf(wblr.fit(d, dist = "weibull2p"))
  d3 <- wblr.fit(d, dist = "weibull3p")
  e2 <- wblr.conf(wblr.fit(d, dist = "lognormal"))
  e3 <- wblr.fit(d, dist = "lognormal3p")
  
  time<-c(sort(time),fday)
  l<-length(time)
  w <- c(d2$fit[[1]]$beta,d2$fit[[1]]$eta)
  haz2 <- w["Beta"]/w["Eta"]*(time/w["Eta"])^(w["Beta"]-1)
  w <- c(d3$fit[[1]]$beta,d3$fit[[1]]$eta, d3$fit[[1]]$t0)
  haz3 <- w["Beta"]/w["Eta"]*((time-w["t0"])/w["Eta"])^(w["Beta"]-1)
  
  w4 <- e2$fit[[1]]$fit_vec
  w5 <- e3$fit[[1]]$fit_vec
  haz4 <- dlnorm(time, w4["Mulog"], w4["Sigmalog"],log = F)/(plnorm(time,w4["Mulog"], w4["Sigmalog"],lower.tail = F, log.p = F))
  haz5 <- dlnorm(time, w5["Mulog"], w5["Sigmalog"],log = F)/(plnorm(time,w5["Mulog"], w5["Sigmalog"],lower.tail = F, log.p = F))
  
  LEGEND<-c(paste("Weibull 2p:",format(d2$fit[[1]]$fit_vec[3],digits = 3)), 
            paste("Weibull 3p:",format(d3$fit[[1]]$fit_vec[4],digits = 3)),
            paste("Lognormal 2p:",format(w4[3], digits = 3)),
            paste("Lognormal 3p:",format(w5[4], digits = 3)))

  cat("Weibull 2-paprameters likelyhood")
  plot.wblr(d2, in.legend.blives = F)
  lines(c(today,today),c(-10,100),type = "l", col = "blue")
  lines(c(fday,fday),c(-10,100),type = "l", col = "red")
  cat("Weibull 3-paprameters likelyhood")
  plot.wblr(d3)
  lines(c(today,today),c(-10,100),type = "l", col = "blue")
  lines(c(fday,fday),c(-10,100),type = "l", col = "red")
  cat("Log-normal 2-paprameters likelyhood")
  plot.wblr(e2, in.legend.blives = F)
  lines(c(today,today),c(-10,100),type = "l", col = "blue")
  lines(c(fday,fday),c(-10,100),type = "l", col = "red")
  cat("Log-normal 3-paprameters likelyhood")
  plot.wblr(e3)
  lines(c(today,today),c(-10,100),type = "l", col = "blue")
  lines(c(fday,fday),c(-10,100),type = "l", col = "red")

  if (w["t0"]>0) {
    YLIM<-c(min(haz3,haz2,haz4,haz5),max(haz3,haz2,haz4,haz5))
  } else { YLIM<-c(min(haz2,haz4),max(haz2,haz4)) }
  YLIM<-YLIM*100
  cat("Failure prediction")
  plot(time,haz2*100, type = "b", main = c(title,"Hazard rate"), xlab = "Time", ylab="Failure rate [%]",col="blue", ylim = YLIM, pch=0, family = "serif")
  lines(time,haz4*100, type = "b", col="green", lty=3,pch=2)
  if (w["t0"]>0) {
    lines(time,haz3*100, type = "b", col="red", lty=2,pch=1 ) 
    lines(time,haz5*100, type = "b" ,col="black" , lty=4,pch=3)
  }
  grid()
  legend(x="topleft",legend = LEGEND, cex = 0.7, col=c("blue","red","green","black"),pch=c(0,1,2,3))
}

f2<-function(frame, title) {
  mldt<-mean(frame$LDT , na.rm = T)
  mttr<-mean(frame$TTR , na.rm = T)
  mtbf<-f3(frame$Event)
  cat(paste(title," Availability:\n"))
  cat(paste("MTBF:",mtbf,"\n"))
  cat(paste("MTTR:",mttr,"\n"))
  cat(paste("MLDT:",mldt,"\n"))
  cat(paste("Technical:",format(mtbf/(mtbf+mttr),digits = 3),"\n"))
  cat(paste("Operational:",format(mtbf/(mtbf+mldt+mttr),digits = 3),"\n"))
}

f3<-function(frame) {
  frame<-sort(frame)
  l<-length(frame)
  x<-0.0
  for (i in 2:l) {
    x<-x+frame[i]-frame[i-1]
  }
  return(x/(l-1))
}
q <- wblr(Blmr$Event, pch = 0)
q1 <- wblr.conf(wblr.fit(q, dist = "weibull2p"))
plot.wblr(q1, in.legend.blives = F)

q2 <- wblr.conf(wblr.fit(q, dist = "lognormal"))
plot.wblr(q2, in.legend.blives = F)

q3 <- wblr.fit(q, dist = "weibull3p")
plot.wblr(q3)

q4 <- wblr.fit(q, dist = "lognormal3p")
plot.wblr(q4)

f2(Blmr,"Bulmer fabric cutter")
## Bulmer fabric cutter  Availability:
## MTBF: 43.1833333333333 
## MTTR: 0.211805555555554 
## MLDT: 3.5 
## Technical: 0.995 
## Operational: 0.921
f(time = Blmr$Event[23:28], title = "Control events", pch = 2)
## Weibull 2-paprameters likelyhood

## Weibull 3-paprameters likelyhood

## Log-normal 2-paprameters likelyhood

## Log-normal 3-paprameters likelyhood

## Failure prediction

f2(Blmr[23:28,],"Control")
## Control  Availability:
## MTBF: 54.4 
## MTTR: 0.204166666665697 
## MLDT: 6.2 
## Technical: 0.996 
## Operational: 0.895
f(time = Blmr$Event[29:38], title = "Cutter System failures", pch = 3)
## Weibull 2-paprameters likelyhood

## Weibull 3-paprameters likelyhood

## Log-normal 2-paprameters likelyhood

## Log-normal 3-paprameters likelyhood

## Failure prediction

f2(Blmr[29:38,],"Cutter System")
## Cutter System  Availability:
## MTBF: 266.888888888889 
## MTTR: 0.406250000001819 
## MLDT: 0 
## Technical: 0.998 
## Operational: 0.998
f(time = Blmr$Event[39:45], title = "General Electric events", pch = 4)
## Weibull 2-paprameters likelyhood

## Weibull 3-paprameters likelyhood

## Log-normal 2-paprameters likelyhood

## Log-normal 3-paprameters likelyhood

## Failure prediction

f2(Blmr[39:45,],"Electric")
## Electric  Availability:
## MTBF: 379.166666666667 
## MTTR: 0.0625 
## MLDT: 0 
## Technical: 1 
## Operational: 1
f(time = Blmr$Event[46:52], pch = 6, title = "Software related events")
## Weibull 2-paprameters likelyhood

## Weibull 3-paprameters likelyhood

## Log-normal 2-paprameters likelyhood

## Log-normal 3-paprameters likelyhood

## Failure prediction

f2(Blmr[46:52,],"Software")
## Software  Availability:
## MTBF: 291.833333333333 
## MTTR: 0.194444444445253 
## MLDT: 0.333333333333333 
## Technical: 0.999 
## Operational: 0.998
f(time = Blmr$Event[53:55], pch = 8, title = "Stopper related events")
## Weibull 2-paprameters likelyhood

## Weibull 3-paprameters likelyhood

## Log-normal 2-paprameters likelyhood

## Log-normal 3-paprameters likelyhood

## Failure prediction

f2(Blmr[53:55,],"Stopper")
## Stopper  Availability:
## MTBF: 387.5 
## MTTR: 0.12499999999635 
## MLDT: 15 
## Technical: 1 
## Operational: 0.962
f(time = Blmr$Event[56:61], pch = 9, title = "Vacuum System events")
## Weibull 2-paprameters likelyhood

## Weibull 3-paprameters likelyhood

## Log-normal 2-paprameters likelyhood

## Log-normal 3-paprameters likelyhood

## Failure prediction

f2(Blmr[56:61,],"Vacuum")
## Vacuum  Availability:
## MTBF: 423.2 
## MTTR: NaN 
## MLDT: NaN 
## Technical: NaN 
## Operational: NaN