-
Notifications
You must be signed in to change notification settings - Fork 5
/
homework6.Rmd
158 lines (133 loc) · 5.88 KB
/
homework6.Rmd
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "Homework"
author: "Melanie Fischer, Lisa Berghäuser"
date: "13 Dezember 2018"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Task
**Prepare a R Markdown-based Shiny app (groups of 2) containing documented R code on the problem of learning the annual rate of
GLOFs (*G*lacial *L*ake *O*utburst *F*lood*s*) per year for an area with no observed events (see last question on last slide in PDF of Session 7). Use an exponential prior,
and compute the likelihood, and posterior. Make sure your exponential prior is such that a value of 5 or less is drawn with a probability of 95%.
Please upload your commented script to the GitHub repository. Make sure you include your names.** (ref: moodle2, session 7)
## Solution
The reactive inputs of our shiny app are defined in the user interface (*ui*) section of the code and we choose the *sliderInput()* function.
The choosen input parameters are:
a) the count of GLOF events in a given observation period (starting value set to 0),
b) the length of the observation period in years (starting value set to 30),
c) the prior belief on the GLOF rate (in our case G. believes it is 5 so the starting value is set accordingly) and
d) the certainty of this prior belief (in our case G.'s prior belief has a certainty of 95% so the starting value was set 0.95)
For the calculation of the prior and the prior probability we used an exponential random generation (*rexp*) to create the prior.
The rate of the rexp function (lambda) is estimated with the following equation:
$$ Quant = \frac{ -ln(1-p) }{ \lambda } $$
The quantile is equal to our prior belief of the GLOF rate (in our case 5) and the *p* is described by the certainty of this prior belief (in our case 0.95).
Solving for lambda results in:
$$\lambda = \frac{ -ln(1-p) }{ Quant } $$
This rate is also set in the density function of the exponential distribution, which is applied in the calculation of the prior probability (*prior_prob*).
With the prior probability
the likelihood and the posterior can be estimated. The method and formulas for this have already been presented in previous homeworks.
The resulting prior probability, the related likelihood
and the posterior are presented in 3 arranged plots. The code could be optimized, but the method of reactive values within shiny apps could not be implemented
in the available time frame. This, however, would only serve the readability and the style of the R code. The method and result is not affected.
Please find the shiny application below with its associated code.
```{r eruptions, echo=FALSE}
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Learning Rate of GLOF Events from a Mountainbelt with no Observed Events"),
sidebarLayout(
sidebarPanel(
sliderInput("GLOFaccounts",
"GLOF Occurence in Observation Period",
min = 0,
max = 50,
value = 0),
sliderInput("observationPeriod",
"Observation Period [years]",
min = 10,
max = 50,
value = 30),
sliderInput("priorBelief",
"Prior Belief on GLOF rate per year:",
min = 0,
max = 20,
value = 5),
sliderInput("certainty",
"Certainty of Prior Belief:",
min = 0,
max = 1,
value = 0.95)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("prior"),
plotOutput("likelihood"),
plotOutput("posterior")
)
)
)
server <- function(input, output){
# create plot output for posterior probability
output$prior <- renderPlot({
# prior and prior probability with quantile
LambdaRate <- (-log( 1-input$certainty ) / input$priorBelief )
prior <- rexp(1000, rate = LambdaRate )
prior_prob <- (dexp( prior, rate = LambdaRate )) /sum( dexp( prior, rate = LambdaRate ) )
likeli <- rep(NA, length(prior))
for(i in seq_along(prior)) {
likeli[i] <- prod(dpois((rep(input$GLOFaccounts, input$observationPeriod)), prior[i]))
}
posterior <- (likeli*prior_prob)/sum(likeli*prior_prob)
plot(
prior,
prior_prob,
type = "h",
col = "darkblue",
xlab = "Prior",
ylab = "Prior Probability"
)
})
output$likelihood <- renderPlot({
LambdaRate <- (-log( 1-input$certainty ) / input$priorBelief )
prior <- rexp(1000, rate = LambdaRate )
prior_prob <- (dexp( prior, rate = LambdaRate )) /sum( dexp( prior, rate = LambdaRate ) )
likeli <- rep(NA, length(prior))
for(i in seq_along(prior)) {
likeli[i] <- prod(dpois((rep(input$GLOFaccounts, input$observationPeriod)), prior[i]))
}
posterior <- (likeli*prior_prob)/sum(likeli*prior_prob)
plot(
prior,
likeli,
type="h",
col= "darkblue",
xlab = "Prior",
ylab = "Likelihood"
)
})
# create plot output for posterior
output$posterior <- renderPlot({
LambdaRate <- (-log( 1-input$certainty ) / input$priorBelief )
prior <- rexp(1000, rate = LambdaRate )
prior_prob <- (dexp( prior, rate = LambdaRate )) /sum( dexp( prior, rate = LambdaRate ) )
likeli <- rep(NA, length(prior))
for(i in seq_along(prior)) {
likeli[i] <- prod(dpois((rep(input$GLOFaccounts, input$observationPeriod)), prior[i]))
}
posterior <- (likeli*prior_prob)/sum(likeli*prior_prob)
plot(
prior,
posterior,
type = "h",
col="darkblue",
xlab = "Prior",
ylab = "Posterior"
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
```