library(patchwork) library(ggplot2) shape <- 0.8 scale <- 4.0 my <- scale*(gamma(1 + 1/shape)) gam1 <- gamma(1 + 2/shape) gam2 <- (gamma(1 + 1/shape))**2 sigma <- sqrt(scale**2*(gam1 - gam2)) my <- round(my, 1) sigma <- round(sigma, 1) maxX <- 15 for (j in 1:10) { shape <- round(shape, 1) my <- scale*(gamma(1 + 1/shape)) gam1 <- gamma(1 + 2/shape) gam2 <- (gamma(1 + 1/shape))**2 sigma <- sqrt(scale**2*(gam1 - gam2)) my <- round(my, 1) sigma <- round(sigma, 1) raknare <- paste0(j,"(10)") shapetext <- paste0("Shape = ", shape) scaletext <- paste0("Scale = ", scale) mytext <- paste0("\u03BC = ", my) sigmatext <- paste0("\u03C3 = ", sigma) ft <- function(x) { dweibull(x, shape, scale) } graf1 <- ggplot() + xlim(0.1, maxX) + stat_function(fun = ft, colour='red', linewidth=1, linetype=1) + ylim(0, 0.30) + ylab("") + xlab("") graf1 <- graf1 + annotate("text", x = 1, y = Inf, label="f(t)", hjust=0, vjust = 1.5, size = 6, colour = "blue" ) graf1 <- graf1 + annotate("text", x = 14, y = Inf, label=raknare, vjust = 1.5, size = 3, colour = "black" ) graf1 <- graf1 + annotate("text", x = 11, y = Inf, label=shapetext, hjust=0, vjust = 5, size = 3, colour = "red" ) graf1 <- graf1 + annotate("text", x = 11, y = Inf, label=scaletext, hjust=0, vjust = 7, size = 3, colour = "red" ) graf1 <- graf1 + annotate("text", x = 11, y = Inf, label=mytext, hjust=0, vjust = 10, size = 3, colour = "black" ) graf1 <- graf1 + annotate("text", x = 11, y = Inf, label=sigmatext, hjust=0, vjust = 12, size = 3, colour = "black" ) graf1 Ft <- function(x) { pweibull(x, shape, scale) } graf2 <- ggplot() + xlim(0.1, maxX) +stat_function(fun = Ft, colour='red', linewidth=1, linetype=1, n=500) + ylab("") + xlab("") graf2 <- graf2 + annotate("text", x = 1, y = Inf, label="F(t)", hjust=0, vjust = 1.5, size = 6, colour = "blue" ) + ylim(0, 1.02) graf2 haz <- function(x) { dweibull(x, shape, scale)/(1-pweibull(x, shape, scale)) } graf3 <- ggplot() + xlim(0.1, maxX) + stat_function(fun = haz, colour='red', linewidth=1, linetype=1, n=500) + ylab("") + xlab("") graf3 <- graf3 + annotate("text", x = 1, y = Inf, label="h(t) = f(t)/(1 - F(t))", hjust=0, vjust = 1.5, size = 6, colour = "blue" ) + ylim(0, 5) graf3 graf4 <- ggplot() + xlim(0.1, maxX) + stat_function(fun = haz, colour='blue', linewidth=0.5, linetype=1, n=500) + ylab("") + xlab("") graf4 <- graf4 + annotate("text", x = 3, y = Inf, label="h(t) = f(t)/(1 - F(t))", hjust=0, vjust = 1.5, size = 4, colour = "red" ) graf4 <- graf4 + annotate("text", x = 1, y = Inf, label="NB: same graph, different Y-scale", hjust=0, vjust = 4.5, size = 4, colour = "grey" ) graf4 <- graf4 + annotate("text", x = 1, y = Inf, label="Low shapes:", hjust=0, vjust = 6.5, size = 4, colour = "grey" ) graf4 <- graf4 + annotate("text", x = 1, y = Inf, label="High shapes:", hjust=0, vjust = 8.5, size = 4, colour = "grey" ) graf4 <- graf4 + annotate("text", x = 6.5, y = Inf, label="h(t) is decreasing", hjust=0, vjust = 6.5, size = 4, colour = "grey" ) graf4 <- graf4 + annotate("text", x = 6.5, y = Inf, label="h(t) is increasing", hjust=0, vjust = 8.5, size = 4, colour = "grey" ) graf4 <- graf4 + theme(axis.text.y = element_blank(), axis.title.y = element_blank()) graf4 allt <- graf1 + graf2 + graf3 + graf4 + plot_layout(ncol = 2) # 3 fields in two columns filnamntext = paste0("weibftFtht", j) filnamn <- paste("~/Desktop/Rgrafer/", filnamntext,".jpeg", sep="") ggsave(filnamn, width = 7.5, height = 6.5) # Units i tum shape = shape + 0.2 }
Show/hide comments
Show/hide R-code
Weibull – f(t), F(t) and h(t)
Stop
Start
<
>