344 lines
8.4 KiB
Plaintext
344 lines
8.4 KiB
Plaintext
---
|
||
title: "3 Krebsfälle in Deutschland"
|
||
output: html_notebook
|
||
---
|
||
|
||
# 3 Krebsfälle in Deutschland
|
||
|
||
### Alle benötigten Libraries aktivieren
|
||
|
||
```{r}
|
||
|
||
library(ggplot2)
|
||
library(dplyr)
|
||
library(tidyr)
|
||
library(tibble)
|
||
|
||
```
|
||
|
||
------------------------------------------------------------------------
|
||
|
||
### Dunkles Design für die Grafiken setten
|
||
|
||
```{r}
|
||
|
||
# theme_set(
|
||
# theme(
|
||
# plot.background = element_rect(fill = "#1E1E1E", color = NA),
|
||
# panel.background = element_rect(fill = "#1E1E1E", color = NA),
|
||
# text = element_text(color = "white"),
|
||
# axis.text = element_text(color = "white"),
|
||
# panel.grid = element_line(color = "#444444"),
|
||
# legend.background = element_rect(fill = "#1E1E1E", color = NA),
|
||
# legend.key = element_rect(fill = "#1E1E1E", color = NA),
|
||
# strip.background = element_rect(fill = "grey20", color = NA),
|
||
# strip.text = element_text(color = "white")
|
||
# )
|
||
# )
|
||
|
||
## To reset all colors to default:
|
||
theme_set(theme_grey())
|
||
|
||
```
|
||
|
||
------------------------------------------------------------------------
|
||
|
||
## 3.1 Einlesen der Daten und Data Preprocessing
|
||
|
||
```{r}
|
||
|
||
krebs_df = read.csv2(
|
||
"Krebsdaten.csv",
|
||
header = FALSE,
|
||
skip = 1, # skips the first row
|
||
stringsAsFactors = FALSE # FALSE because we will convert all data types manually later
|
||
)
|
||
|
||
```
|
||
|
||
```{r}
|
||
|
||
head(krebs_df, 5)
|
||
|
||
```
|
||
|
||
### Letzte leere Spalte entfernen
|
||
|
||
```{r}
|
||
|
||
krebs_df = krebs_df[, 1:5]
|
||
|
||
```
|
||
|
||
### Spalten Namen benennen
|
||
|
||
```{r}
|
||
|
||
colnames(krebs_df) = c(
|
||
"Jahr",
|
||
"Diagnose",
|
||
"Geschlecht",
|
||
"Altersgruppe",
|
||
"Prävalenz"
|
||
)
|
||
|
||
```
|
||
|
||
```{r}
|
||
|
||
head(krebs_df, 5)
|
||
|
||
```
|
||
|
||
```{r}
|
||
|
||
dim(krebs_df)
|
||
|
||
```
|
||
|
||
### Data Frame in ein Tibble umwandeln
|
||
|
||
```{r}
|
||
|
||
krebs_tib = as_tibble(krebs_df)
|
||
|
||
head(krebs_tib, 5)
|
||
|
||
```
|
||
|
||
### Datentypen aller Spalten überprüfen
|
||
|
||
```{r}
|
||
|
||
sapply(krebs_tib, class)
|
||
|
||
```
|
||
|
||
### Spalten vor der Data Type Conversion genauer analysieren
|
||
|
||
For each column, we should check all unique values so that we can see if there are any Null values, etc... For the `Prävalenz` column, we need to do some additional analysis steps.
|
||
|
||
**1. Werte der Spalte `Jahr` checken**
|
||
|
||
```{r}
|
||
|
||
sort(unique(krebs_tib$Jahr))
|
||
|
||
```
|
||
|
||
**2. Werte der Spalte `Diagnose` checken**
|
||
|
||
```{r}
|
||
|
||
unique(krebs_tib$Diagnose)
|
||
|
||
```
|
||
|
||
**3. Werte der Spalte `Geschlecht` checken**
|
||
|
||
```{r}
|
||
|
||
unique(krebs_tib$Geschlecht)
|
||
|
||
```
|
||
|
||
**4. Werte der Spalte `Altersgruppe` checken**
|
||
|
||
```{r}
|
||
|
||
unique(krebs_tib$Altersgruppe)
|
||
|
||
```
|
||
|
||
**5. Werte der Spalte `Prävalenz` checken**
|
||
|
||
We should manually check if there are any non-numeric values. We can simply convert the entire column to numeric and anything that is not able to be converted will automatically be converted to `NA`. Then we can just count those `NA`s to see how many they are. *(It's important to know how many they are so that later we can either fill them or delete the entire row, etc...)*
|
||
|
||
```{r}
|
||
|
||
# Original values
|
||
krebs_prävalenz = krebs_tib$Prävalenz
|
||
|
||
# Converting to numeric
|
||
converted = as.numeric(gsub("\\.", "", krebs_prävalenz))
|
||
|
||
# Find all values that could not be converted
|
||
invalid_values = krebs_prävalenz[is.na(converted)]
|
||
|
||
# Show all the non-numeric values that were not converted
|
||
unique(invalid_values)
|
||
length(invalid_values)
|
||
|
||
```
|
||
|
||
Deleting those rows will lead to a loss of information, so we will keep them.
|
||
|
||
#### Fehlende Werte pro Spalte prüfen (vor der Data Type Conversion)
|
||
|
||
```{r}
|
||
|
||
colSums(is.na(krebs_tib))
|
||
|
||
```
|
||
|
||
### Convert Data Types
|
||
|
||
```{r}
|
||
|
||
krebs_tib = krebs_tib |>
|
||
mutate(
|
||
Geschlecht = as.factor(Geschlecht),
|
||
Altersgruppe = as.factor(Altersgruppe),
|
||
Prävalenz = as.numeric(gsub("\\.", "", Prävalenz)) # Removes the '.' from '600.000' => '600000'
|
||
)
|
||
|
||
```
|
||
|
||
#### Datentypen nach der Conversion checken
|
||
|
||
```{r}
|
||
|
||
sapply(krebs_tib, class)
|
||
|
||
```
|
||
|
||
#### Noch einmal auf fehlende Werte checken
|
||
|
||
*We see that these 630 missing values in `Prävalenz` are the values we analysed above.*
|
||
|
||
```{r}
|
||
|
||
colSums(is.na(krebs_tib))
|
||
|
||
```
|
||
|
||
------------------------------------------------------------------------
|
||
|
||
### Warum enthält der Datensatz `x`-Werte?
|
||
|
||
Bei der Datenaufbereitung wurden **630 Einträge (Cells) mit dem Wert `x`** in der Spalte `Prävalenz` gefunden.
|
||
|
||
Laut der Datenbank kennzeichnet `x` eine **nicht sinnvolle Merkmalskombination**, also zum Beispiel bei geschlechtsspezifischen Krebsdiagnosen.
|
||
|
||
Das sind alle Kombinationen:
|
||
|
||
| Diagnose | Geschlecht | Anzahl `x` |
|
||
|-----------------------------|------------|-----------:|
|
||
| Eierstöcke (C56) | männlich | 105 |
|
||
| Gebärmutterhals (C53) | männlich | 105 |
|
||
| Gebärmutterkörper (C54–C55) | männlich | 105 |
|
||
| Vulva (C51) | männlich | 105 |
|
||
| Hoden (C62) | weiblich | 105 |
|
||
| Prostata (C61) | weiblich | 105 |
|
||
|
||
**Beispiel:**
|
||
|
||
Prostatakrebs ist in dieser Statistik eine männliche Krebsdiagnose. Deshalb wird für die Kombination `Prostata (C61)` und `weiblich` keine numerische Prävalenz angegeben, sondern `x`.
|
||
|
||
Die Werte sind also **absichtlich nicht angegeben**. Es handelt sich nicht um zufällig fehlende Messwerte. Also macht es auch keinen Sinn die Werte zu **imputieren**.
|
||
|
||
------------------------------------------------------------------------
|
||
|
||
### **Lösung:** Geben Sie die ersten 10 Zeilen und 7 Spalten als Tabelle aus
|
||
|
||
```{r}
|
||
|
||
head(krebs_tib, 10)
|
||
|
||
```
|
||
|
||
**Note**
|
||
|
||
Im originalen Datensatz haben wir nur 5 Spalten und nicht 7.
|
||
|
||
## 3.2 Übersicht aller Krebsarten
|
||
|
||
### Daten für die Visualization vorbereiten
|
||
|
||
```{r}
|
||
|
||
präv_krebs = krebs_tib |>
|
||
filter(Jahr == 2024 & Altersgruppe == "55 - 64" & Diagnose != "Krebs gesamt (C00-C97 ohne C44)") |>
|
||
drop_na(Prävalenz)
|
||
|
||
dim(präv_krebs)
|
||
head(präv_krebs, 5)
|
||
|
||
```
|
||
|
||
**Note**
|
||
|
||
*We filter out "Krebs gesamt" because it isn't an actual Diagnosis.*
|
||
|
||
### **Lösung:** Balkendiagramm erstellen
|
||
|
||
```{r fig.width=10, fig.height=6}
|
||
|
||
ggplot(präv_krebs) +
|
||
geom_bar(aes(y = Diagnose, x = Prävalenz, fill = Geschlecht), stat = "identity") +
|
||
labs(
|
||
title = "Krebsprävalenz nach Diagnose und Geschlecht",
|
||
subtitle = "2024, Altersgruppe 55–64 Jahre"
|
||
) +
|
||
scale_fill_discrete(
|
||
labels = c("Männlich", "Weiblich")
|
||
)
|
||
|
||
```
|
||
|
||
**Begründung unserer Motivation für den Plot Design**
|
||
|
||
Für die Position der Bars, haben wir den default value `stacked` benutzt, da es die unterschiedlichen Geschlechte schon klar darstellt. Eine andere Option wäre `dodge`, aber da der Plot schon ziemlich groß ist, würde es ein bisschen chaotisch sein. Zusatätzlich, haben wir die Legend Labels überschrieben und titel/ subtitel hinzugefügt.
|
||
|
||
## 3.3 Prävalenz nach Altergruppen und Geschlecht
|
||
|
||
### Dataset kurz für einen Überblick zeigen
|
||
|
||
```{r}
|
||
|
||
head(krebs_tib, 100)
|
||
|
||
```
|
||
|
||
### Daten für die Visualization vorbereiten
|
||
|
||
```{r}
|
||
|
||
präv_krebs_2 = krebs_tib |>
|
||
filter(Diagnose == "Krebs gesamt (C00-C97 ohne C44)") |> # We only keep the 'Krebs gesamt' Diagnosen
|
||
mutate(
|
||
Altersstufe = ifelse( # Add a new column Altersstufe consisting of
|
||
Altersgruppe == "0 - 44" | Altersgruppe == "45 - 54", # values either `bis 54` or `ab 55` for each
|
||
"bis 54", # existing Altersgruppe
|
||
"ab 55"
|
||
)
|
||
) |>
|
||
group_by(Jahr, Geschlecht, Altersstufe) |>
|
||
summarize("Prävalenz Altersstufe" = sum(Prävalenz)) # Add a new column Prävalenz Altersstufe
|
||
# containing the summed Prävalenz per Altersstufe
|
||
|
||
dim(präv_krebs_2)
|
||
head(präv_krebs_2, 100)
|
||
|
||
```
|
||
|
||
*Here we don't have to drop `NA`s because we are only using the `Diagnose` "Krebs gesamt" which contains all the summed `Prävalenzen` of each `Diagnose`, so we wouldn't get any 0 values/ Null values.*
|
||
|
||
### **Lösung:** 2 Liniendiagramme erstellen
|
||
|
||
```{r fig.width=10, fig.height=6}
|
||
|
||
ggplot(präv_krebs_2, aes(Jahr, `Prävalenz Altersstufe`, color = Altersstufe)) +
|
||
geom_line(linewidth = 1.5) +
|
||
facet_wrap(~ Geschlecht) +
|
||
labs(
|
||
title = "Krebsprävalenz nach Altersstufe und Geschlecht",
|
||
subtitle = "2004 bis 2024, Alterstufen: bis 54 & ab 55"
|
||
) +
|
||
scale_y_continuous(
|
||
labels = scales::label_number(),
|
||
limits = c(0, NA)
|
||
)
|
||
|
||
```
|