-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule_three.R
186 lines (140 loc) · 5.78 KB
/
module_three.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Including the core functionality
source("module_three_core.R")
## ======================= UTILITY LIST ==========================
module_three_list<-c( "Permutations",
"Combinations",
"Basic Probability",
"Conditional Probability",
"Bayes Theorem"
)
## ========================= I/O FUNCTIONS ==============================
## =========================== permutations ======================================
my_permutation_input<-function(){
tagList(
textInput('my_permutation_input_dataOne', 'Enter n (objects) ', "4"),
textInput('my_permutation_input_dataTwo', 'Enter r (sample) ', "2")
)
}
my_permutation_output<-function(){
tagList(
renderPrint({
# get data from UI and prepare
n <- as.numeric(unlist(strsplit(input$my_permutation_input_dataOne,",")))
r <- as.numeric(unlist(strsplit(input$my_permutation_input_dataTwo,",")))
# Nicely Display the source data
cat("Total ( n ) number of objects : ",n)
cat("\nTaken ( r ) at a time : ",r)
# calling the core function
result<-my_permutation(n,r)
# Print the result
cat(sprintf("\n\nPermutations : \n%s",result))
})
)
}
## =========================== combinations ======================================
my_combination_input<-function(){
tagList(
textInput('my_combination_input_dataOne', 'Enter n (objects) ', "4"),
textInput('my_combination_input_dataTwo', 'Enter r (sample) ', "2")
)
}
my_combination_output<-function(){
tagList(
renderPrint({
# get data from UI and prepare
n <- as.numeric(unlist(strsplit(input$my_combination_input_dataOne,",")))
r <- as.numeric(unlist(strsplit(input$my_combination_input_dataTwo,",")))
# Nicely Display the source data
cat("Total ( n ) number of objects : ",n)
cat("\nTaken ( r ) at a time : ",r)
# calling the core function
result<-my_combination(n,r)
# Print the result
cat(sprintf("\n\nCombination : \n%s",result))
})
)
}
## =========================== Basic probability ======================================
my_basic_prob_input<-function(){
tagList(
textInput('my_basic_prob_input_dataOne', 'Enter total number of outcomes ', "6"),
textInput('my_basic_prob_input_dataTwo', 'Enter number of favourable outcomes ', "3")
)
}
my_basic_prob_output<-function(){
tagList(
renderPrint({
# get data from UI and prepare
total <- as.numeric(unlist(strsplit(input$my_basic_prob_input_dataOne,",")))
fav <- as.numeric(unlist(strsplit(input$my_basic_prob_input_dataTwo,",")))
# Nicely Display the source data
cat("Total number of outcomes : ",total)
cat("\nNumber of favourable outcomes : ",fav)
# calling the core function
result<-my_basic_prob(fav,total)
# Print the result
cat(sprintf("\n\nProbability : \n%.6f",result))
})
)
}
## =========================== Conditional probability ======================================
my_cond_prob_input<-function(){
tagList(
textInput('my_cond_prob_input_dataOne', 'Sample Space (S) ', "1,2,3,4,5,6"),
textInput('my_cond_prob_input_dataTwo', 'fav outcomes for Given Event (B) ', "1,3,5"),
textInput('my_cond_prob_input_dataThree', 'fav outcomes for Desired Event (A) ', "1")
)
}
my_cond_prob_output<-function(){
tagList(
renderPrint({
# get data from UI and prepare
S <- as.numeric(unlist(strsplit(input$my_cond_prob_input_dataOne,",")))
B <- as.numeric(unlist(strsplit(input$my_cond_prob_input_dataTwo,",")))
A <- as.numeric(unlist(strsplit(input$my_cond_prob_input_dataThree,",")))
# Nicely Display the source data
cat("Sample Space : \n",S)
cat("\n\nFavourable outcomes for given event B:\n",B)
cat("\n\nFavourable outcomes for desired event A:\n",A)
# calling the core function
result<-my_cond_prob(A,B,S)
# Print the result
cat(sprintf("\n\nProbability of A given B: \n%.6f",result))
})
)
}
## =========================== Baye's theorm ======================================
my_bayes_theorm_input<-function(){
tagList(
textInput('my_bayes_theorm_input_dataZero', 'Sample space can be divided into N events. Enter N :', "3"),
textInput('my_bayes_theorm_input_dataOne', "For each event i, Enter P(Ai) : ( sum of probablitities of Ai must be 1 )", ".30, .30, .40"),
textInput('my_bayes_theorm_input_dataTwo', 'For each event i, Enter P( Hypothesis | Ai )', ".20, .15, .10"),
textInput('my_bayes_theorm_input_dataThree', 'Objective : P( Ak | Hypothesis ), Enter k :', "1")
)
}
my_bayes_theorm_output<-function(){
tagList(
renderPrint({
# get data from UI and prepare
n <- as.numeric(unlist(strsplit(input$my_bayes_theorm_input_dataZero,",")))
An <- as.numeric(unlist(strsplit(input$my_bayes_theorm_input_dataOne,",")))
Bn_given_An <- as.numeric(unlist(strsplit(input$my_bayes_theorm_input_dataTwo,",")))
k <- as.numeric(unlist(strsplit(input$my_bayes_theorm_input_dataThree,",")))
# Nicely Display the source data
cat("Number of events in sample space : \n",n)
cat("\n\nProbability of each event i : \n",An)
if(length(An)==n && length(Bn_given_An)==n){
cat("\n\nP( Hypothesis | Ai ) :\n",Bn_given_An)
cat("\n\nFixed event (Hypothesis event) :\n",k)
if(k==1){ cat("st") }
else if(k==2){ cat("nd") }
else if(k==3){ cat("rd") }
else { cat("th") }
# calling the core function
result<-my_bayes_theorm(An,Bn_given_An,k)
# Print the result
cat(sprintf("\n\nP( Hypothesis | Ak ): \n%.6f",result))
}
})
)
}