-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path99_psa_optimizedistr.R
29 lines (24 loc) · 1.15 KB
/
99_psa_optimizedistr.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
## Find alpha and beta parameter for beta or gamma distribution based on mean and 95% credible intervals
############################################
###Get distribution param for gammas #######
#############################################
getdistr_parms <- function(int.quantiles, int.mean,starting.params,distrib){
objective.function <- function(params) {
alpha <- params[1]
beta <- params[2]
if (distrib == "beta"){
calculated.quantiles <- qbeta(p=c(0.025, 0.975), shape1=alpha, shape2=beta)
} else if (distrib == "gamma"){
calculated.quantiles <- qgamma(p=c(0.025, 0.975), shape=alpha, rate=beta)
}
calculate.mean <- function(alpha, beta) {
return(qbeta(p=0.5, shape1=alpha, shape2=beta))
}
squared.error.quantiles <- sum((int.quantiles - calculated.quantiles)^2)
calculated.mode <- calculate.mean(alpha, beta)
squared.error.mode <- (int.mean - calculated.mode)^2
return(squared.error.quantiles + squared.error.mode)
}
nlm.result <- nlm(f = objective.function, p = starting.params)
nlm.result$estimate
}