-
Notifications
You must be signed in to change notification settings - Fork 0
/
weibull.R
71 lines (47 loc) · 2.3 KB
/
weibull.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## This code is used to calculate probability of pandemic for numerical example 1, 2 as to create Figure 4
## both weibull and exponential implemented
library(Rfast2)
library(ggplot2)
library(fitdistrplus)
library(EnvStats)
graphics.off() # clear all graphs
#rm(list = ls()) # remove all files from your workspace
# change directory to where the script located
my_d <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(my_d)
set.seed(123)
data<-c(3, 49, 7, 42, 3, 3, 53, 10, 19, 39, 11, 9, 26,3, 5,3)
censored<-c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1,0)
fit<-censweibull.mle(data,censored)
#tau=shape
#theta=scale
qqPlot(data, distribution = "weibull", param.list = list(shape = 1/fit$param[[2]], scale = fit$param[[1]]),add.line=TRUE,pch=19, line.lwd=2)
ks.test(data, "pweibull", shape = 1/fit$param[[2]], scale = fit$param[[1]])
P_H<-(pweibull(6,shape = 1/fit$param[[2]], scale = fit$param[[1]])-pweibull(3,shape = 1/fit$param[[2]], scale = fit$param[[1]]))/(1-pweibull(3,shape = 1/fit$param[[2]], scale = fit$param[[1]]))
P_H_c<-1-P_H
P_H
P_H_c
###############For second example
set.seed(123)
data<-c(3, 49, 7, 42, 3, 3, 53, 10, 19, 39, 11, 9, 26,3, 5,5,4)
censored<-c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1,1,0)
fit<-censweibull.mle(data,censored)
#tau=shape
#theta=scale
qqPlot(data, distribution = "weibull", param.list = list(shape = 1/fit$param[[2]], scale = fit$param[[1]]),add.line=TRUE,pch=19, line.lwd=2)
ks.test(data, "pweibull", shape = 1/fit$param[[2]], scale = fit$param[[1]])
P_H<-(pweibull(6,shape = 1/fit$param[[2]], scale = fit$param[[1]])-pweibull(3,shape = 1/fit$param[[2]], scale = fit$param[[1]]))/(1-pweibull(3,shape = 1/fit$param[[2]], scale = fit$param[[1]]))
P_H_c<-1-P_H
P_H
P_H_c
############################Fit an exponential distirbution using mle. Read page 261 in loss models(4th ed)
set.seed(123)
data<-c(3, 49, 7, 42, 3, 3, 53, 10, 19, 39, 11, 9, 26,3, 5,3)
censored<-c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1,0)
theta_mle<-sum(data)/sum(censored)
qqPlot(data, distribution = "exp", param.list = list(rate=1/theta_mle),add.line=TRUE,pch=19, line.lwd=2)
ks.test(data, pexp, rate=1/theta_mle)
P_H<-(pexp(6,rate=1/theta_mle)-pexp(3,rate=1/theta_mle))/(1-pexp(3,rate=1/theta_mle))
P_H_c<-1-P_H
P_H
P_H_c