-
Notifications
You must be signed in to change notification settings - Fork 1
/
expert.K.Norm.just1plot.R
135 lines (94 loc) · 3.94 KB
/
expert.K.Norm.just1plot.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#This file is part of ElicitN.
#Copyright 2011 Rebecca Fisher and Rebecca O'Leary.
#ElicitN is free software: you can redistribute it and/or modify it under
#the terms of the GNU General Public License as published by the Free Software
#Foundation, either version 3 of the License, or any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY
#WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
#PARTICULAR PURPOSE. See the GNU General Public License
#(http://www.gnu.org/licenses/) for more details.
expert.K.Norm.just1plot <-function(Lhat, Uhat, Mhat, pihat, new.alpha,best.type.status, ee.type){
eval.text <- getURL("https://raw.githubusercontent.com/beckyfisher/ElicitN/master/modal.R", ssl.verifypeer = FALSE)
eval(parse(text = eval.text))
#source('modal.R')
fit.score.mode <- function(Lhat, Uhat, Mhat, pihat, w=rep(1/3,3)) {
#possible mu
mu <- Mhat
sig<-seq(0,10000,by=0.1)
score.norm <- ( (((Lhat)-qnorm(1-pihat, mean=mu, sd=sig))^2)*1/2 +
(((Uhat) - qnorm(pihat, mean=mu, sd=sig))^2 )*1/2)
return(as.data.frame(cbind(score.mode=score.norm, mu=mu, sig=sig)))
}
######
ss <- function(mu, sig, alpha) {
m <- mu
v <- sd(rnorm(1000000,mu, sig))
ci <- qnorm(c(1-alpha, .5, alpha), mean=mu, sd=sig)
return(list(mean=m, mode=m, median=m, var=v,lower=ci[1],
median=ci[2], upper=ci[3]))
}
feedbackN <- function(mu, sig,alpha,low,upp,new.alpha) {
m <- mu
#sd(rnorm(1000000,mu, sig))
ci <- qnorm(c(1-alpha, .5, alpha, 1-new.alpha, new.alpha), mean=mu, sd=sig)
pci <- pnorm(c(upp,low), mean=mu, sd=sig)
return(list(lower=ci[1], median=ci[2], upper=ci[3], mean=m, mode=m, prob.upper=pci[1],
prob.lower=pci[2], new.alpha=new.alpha, new.lower=ci[4], new.upper=ci[5]))
}
#############################
#MODE
fit.results.mode <-fit.score.mode(Lhat, Uhat, Mhat, pihat,rep(1/2,2))
fit.best.mode.mu <-modal(fit.results.mode[which(fit.results.mode[,1]
==min(fit.results.mode[,1])),]$mu)
fit.best.mode.sig <-modal(fit.results.mode[which(fit.results.mode[,1]
==min(fit.results.mode[,1])),]$sig)
ss.mode.results <-ss(fit.best.mode.mu, fit.best.mode.sig , pihat)
feedback.mode.results <- feedbackN(fit.best.mode.mu, fit.best.mode.sig, pihat,Lhat, Uhat,new.alpha=new.alpha)
############################
#PLOT
X11()
par(mfrow=c(1,1))
if (ss.mode.results$lower <0) {
ss.mode.results$lower =0
}
if (feedback.mode.results$new.lower<0){
feedback.mode.results$new.lower =0
}
#find min & max for plot
plot.K.min <- min(ss.mode.results$lower,
feedback.mode.results$new.lower)
plot.K.max <- max(ss.mode.results$upper,
feedback.mode.results$new.upper)
#PLOT
#MODE
plot(rep(c(1),2), rep(c(ss.mode.results$lower, ss.mode.results$upper),1)
, xlim=c(0,3), xlab="", ylab="", type="l",
ylim=c(plot.K.min-plot.K.min*.1, plot.K.max+plot.K.max*.1),
xaxt = "n", lwd=2)
lines(rep(2,2),c(ss.mode.results$lower, ss.mode.results$upper),lwd=2)
lines(c(1,2), rep(ss.mode.results$lower,2),lwd=2)
lines(c(1,2), rep(ss.mode.results$upper,2),lwd=2)
lines(c(1,2),rep(ss.mode.results$mode,2), col="red",lwd=2)#maths
lines(c(1,2), rep(Mhat,2),col=1,lwd=2)#best guess
legend("topleft", lty=1, col=c(1, "red"), bty="n", legend=c("elicited parameters",
"estimated (fitted) "),cex=.8) #BEST GUESS IS MODE
legend("topright", paste("new
sureness =", new.alpha), cex=.8, bty="n")
#plot new lower & upper conf
lines(c(1,2), rep(feedback.mode.results$new.lower,2), col="red" ,lwd=2)
lines(c(1,2), rep(feedback.mode.results$new.upper,2), col="red",lwd=2)
################
#calculate K
Ksp <-rnorm(10000, fit.best.mode.mu, fit.best.mode.sig)
#################
#Store values
ss.mode.results=as.list(ss.mode.results)
fit.best.mode.sig=fit.best.mode.sig
fit.best.mode.mu = fit.best.mode.mu
feedback.mode.results=feedback.mode.results
return(list(pihat=pihat,ss.mode.results=ss.mode.results ,
fit.best.mode.mu=fit.best.mode.mu,
fit.best.mode.sig=fit.best.mode.sig,
feedback.mode.results=feedback.mode.results,
Ksp=Ksp, MhatK=Mhat))
}