-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
218 lines (185 loc) · 8.47 KB
/
app.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(ggplot2)
library(dplyr)
library(palmerpenguins)
library(mice)
library(DT)
titanic_data <- read.csv2("titanic_data.csv", header = TRUE, sep= ",")
int_columns <- c("Survived", "Age", "Pclass")
int_data <- titanic_data[int_columns]
int_data$Age <- as.numeric(int_data$Age)
full_Age <- mice::complete(mice(int_data, method = "cart"))$Age
titanic_data$Age <- as.character(full_Age)
# format data for prediction
titanic_data_filtered <- select(titanic_data, -c(PassengerId, Name, Ticket, Cabin, SibSp, Parch, Fare))
titanic_data_filtered <- titanic_data_filtered %>%
as_tibble() %>%
mutate(
Survived = factor(Survived),
Pclass = factor(Pclass),
Age = factor(Age),
Sex = factor(Sex),
Embarked = factor(Embarked)
) %>%
filter(!is.na(Survived), !is.na(Pclass), !is.na(Age))
# Define UI for the application
ui <- fluidPage(
titlePanel("Titanic Survival Analysis"),
tabsetPanel(
tabPanel("Titanic App",
sidebarLayout(
sidebarPanel(
h2("Vorhersage der Überlebenswahrscheinlichkeit"),
h4("Bitte wähle die Daten für die Berechnung der Überlebenswahrscheinlichkeit aus."),
selectInput("sex", "Geschlecht:", c("male", "female")),
sliderInput("alter", "Alter:", 1, 58, value = 25, step = 1),
selectInput("hafen", "An diesem Hafen zugestiegen:", c("Cherbourg" = 'C', "Queenstown" = 'Q', "Southhampton" = 'S')),
numericInput("klasse", "Ticketklasse:", value = 3, min = 1, max = 3, step = 1)
),
mainPanel(
h2(textOutput("prob"))
)
),
sidebarLayout(
sidebarPanel(
h2("Ausführliches Histogramm"),
checkboxInput("pclass_toggle", "Gesamt Übersicht", value = FALSE),
conditionalPanel(
condition = "input.pclass_toggle == false",
radioButtons("class_selector", "Passagier Klasse",
choices = c("1", "2", "3"),
selected = "1"),
),
sliderInput("age_group_selector", "Sequenz der Altersgruppen festlegen:",
min = 10, max = 25, value = 10, step = 5)
),
mainPanel(
plotOutput("survival_histogram")
)
),
sidebarLayout(
sidebarPanel(
h2("Ausführlicher Mosaikplot"),
selectInput("mplot_merkmal", "Wähle ein Merkmal zum Vergleich:",
choices = c("PClass", "Gender", "Age", "Fare")),
conditionalPanel(
condition = "input.mplot_merkmal == 'Age'",
selectInput("mplot_intervall", "Wähle ein Intervall für Age:",
choices = c("Alle Passagiere",
"Passagiere bis zu 20 Jahren",
"Passagiere zwischen 20 - 40 Jahren",
"Passagiere zwischen 40 - 60 Jahren",
"Passagiere zwischen 60 - 85 Jahren"))),
conditionalPanel(
condition = "input.mplot_merkmal == 'Fare'",
selectInput("mplotfare_intervall", "Wähle ein Intervall für Fare:",
choices = c("Tickets aller Passagiere",
"Tickets zwischen 100 - 200 GE",
"Tickets zwischen 200 - 550 GE")))
),
mainPanel(
plotOutput("mplotOutput")
)
),
),
tabPanel("Daten",
dataTableOutput("tableView")
),
)
)
# Define server logic
server <- function(input, output) {
# Tabellenausgabe
output$tableView <- renderDT(
titanic_data, options = list(lengthChange = FALSE)
)
# Logik für Überlebenschance-Vorhersage:
output$prob <- renderText({
prediction_model <- glm(formula=Survived ~. , family = binomial(link = "logit"), data = titanic_data_filtered)
prediction_input_data <- data.frame(
Sex = as.factor(input$sex),
Age = as.factor(as.character(input$alter)),
Embarked = as.factor(input$hafen),
Pclass = as.factor(as.character(input$klasse))
)
prob <- predict(prediction_model, newdata = prediction_input_data, type = "response")
paste(round(as.numeric(as.character(prob))*100, 2), "%")
})
output$mplotOutput <- renderPlot({
titanic_data_filtered <- na.omit(titanic_data)
breaks <- switch(input$mplot_intervall,
"Alle Passagiere" = c(0, 18, 30, 60, 100),
"Passagiere bis zu 20 Jahren" = c(0, 5, 10, 15, 20),
"Passagiere zwischen 20 - 40 Jahren" = c(20, 25, 30, 35, 40),
"Passagiere zwischen 40 - 60 Jahren" = c(40, 45, 50, 55, 60),
"Passagiere zwischen 60 - 85 Jahren" = c(60, 65, 70, 85))
breaks_fare <- switch(input$mplotfare_intervall,
"Tickets aller Passagiere" = c(0, 25, 50, 100, 550),
"Tickets zwischen 100 - 200 GE" = c(100, 125, 150, 200),
"Tickets zwischen 200 - 550 GE" = c(200, 230, 270, 550))
chosen <- switch(input$mplot_merkmal,
"PClass" = titanic_data_filtered$Pclass,
"Gender" = titanic_data_filtered$Sex,
"Age" = cut(as.numeric(titanic_data_filtered$Age), breaks = breaks),
"Fare" = cut(as.numeric(titanic_data_filtered$Fare), breaks = breaks_fare))
survival <- titanic_data_filtered$Survived
table_data <- data.frame(chosen, survival)
colnames(table_data) <- c(input$mplot_merkmal, "Survived")
mosaic_data <- table(table_data)
mosaicplot(mosaic_data, labs(title = "Mosaikplot"))
#Ghazi Plot
output$survival_histogram <- renderPlot({
age_group <- input$age_group_selector
if (input$pclass_toggle) {
hist_data <- titanic_data %>%
filter(!is.na(Age)) %>%
mutate(Age_Group = cut(as.numeric(Age), breaks = seq(0, 100, by = age_group),
labels = seq(0, 100, by = age_group)[-1]))
survival_probabilities <- hist_data %>%
group_by(Age_Group = cut(as.numeric(Age), breaks = seq(0, 100, by = age_group)), Pclass) %>%
summarize(Survival_Probability = sum(Survived == 1) / n())
ggplot(survival_probabilities, aes(x = as.factor(Age_Group), y = Survival_Probability, fill = as.factor(Pclass))) +
geom_bar(stat = "identity", position = "dodge", color = "black") +
labs(title = "Überlebenswahrscheinlichkeit nach Altersgruppen und Passagiere Klasse",
x = "Altersgruppen", y = "Überlebenswahrscheinlichkeit",
fill = "Passagier Klasse") + # Hier wird die Legendenbeschriftung hinzugefügt
scale_x_discrete(na.translate = FALSE) +
theme_minimal() +
scale_fill_manual(values = c("#FFD700", "#C0C0C0", "#CD7F32"), name = "Passagier Klasse") + # Hier wird die Legendenüberschrift angepasst
theme(axis.title.y.right = element_text(color = "skyblue"),
axis.text.y.right = element_text(color = "skyblue"))
} else {
selected_class <- as.numeric(input$class_selector)
hist_data <- titanic_data %>%
filter(Pclass == selected_class, !is.na(Age)) %>%
mutate(Age_Group = cut(as.numeric(Age), breaks = seq(0, 100, by = age_group),
labels = seq(0, 100, by = age_group)[-1]))
survival_probabilities <- hist_data %>%
group_by(Age_Group = cut(as.numeric(Age), breaks = seq(0, 100, by = age_group))) %>%
summarize(Survival_Probability = sum(Survived == 1) / n(),
Absolute_Count = n())
ggplot(survival_probabilities, aes(x = as.factor(Age_Group), y = Survival_Probability * 100)) +
geom_bar(stat = "identity", fill = "skyblue", color = "black") +
geom_text(aes(label = Absolute_Count), vjust = -0.5, color = "black") +
labs(title = paste("Überlebenswahrscheinlichkeit in Passagiere Klasse", selected_class),
x = "Altersgruppen", y = "Überlebenswahrscheinlichkeit (%)") +
theme_minimal() +
scale_x_discrete(na.translate = FALSE) +
scale_y_continuous(limits = c(0, 100), name = "Überlebenswahrscheinlichkeit (%)") +
theme(axis.title.y.right = element_text(color = "skyblue"),
axis.text.y.right = element_text(color = "skyblue"))
}
}
)
})
}
# Run the application
shinyApp(ui = ui, server = server)