Representation quotient analysis

Data preparation

Preparation of dataset

library(haven)
library(mice)
library(miceadds)
library(lmtest)
library(sandwich)
library(psych)
library(foreign)
library(car)
library(mice)
library(miceadds)
library(dplyr)
library(labeling)
library(MatchThem)
library(MatchIt)
library(cobalt)
library(robustbase)
library(broom)
library(estimatr)
library(reshape)
library(reshape2)
library(ggplot2)
library(readr)
library(mitools)
library(mice) 
library(miceadds)
library(cli)
library(haven)
library(MASS)
library(robustbase)
library(ggmice)
library(nnet)
library(readxl)

#Load dataset
KODAP_data_complete <- read_sav("2025-02-17_KODAP_PP_2018-2023_Wrede_Jena.sav")

length(KODAP_data_complete$Patient_ID)
## [1] 22381
#Exclude duplicate patients
KODAP_data_complete <- KODAP_data_complete[!duplicated(KODAP_data_complete$Patient_ID) & !duplicated(KODAP_data_complete$Patient_ID, fromLast = TRUE), ]
#Exclude patients < 18 year
KODAP_data_complete <- subset(KODAP_data_complete, Pat_Alter >= 18)
#Only include CBT treatments
KODAP_data_complete <- subset(KODAP_data_complete, Ther_Verfahren == 1)

#Add NAs to diagnosis variables
KODAP_data_complete$ICD1_pre_clean <- ifelse(KODAP_data_complete$ICD1_pre_clean == "" | KODAP_data_complete$ICD1_pre_clean == "-99",NA,KODAP_data_complete$ICD1_pre_clean)
KODAP_data_complete$ICD2_pre_clean <- ifelse(KODAP_data_complete$ICD2_pre_clean == "" | KODAP_data_complete$ICD2_pre_clean == "-99",NA,KODAP_data_complete$ICD2_pre_clean)
KODAP_data_complete$ICD3_pre_clean <- ifelse(KODAP_data_complete$ICD3_pre_clean == "" | KODAP_data_complete$ICD3_pre_clean == "-99",NA,KODAP_data_complete$ICD3_pre_clean)
KODAP_data_complete$ICD4_pre_clean <- ifelse(KODAP_data_complete$ICD4_pre_clean == "" | KODAP_data_complete$ICD4_pre_clean == "-99",NA,KODAP_data_complete$ICD4_pre_clean)
KODAP_data_complete$ICD5_pre_clean <- ifelse(KODAP_data_complete$ICD5_pre_clean == "" | KODAP_data_complete$ICD5_pre_clean == "-99",NA,KODAP_data_complete$ICD5_pre_clean)

#Exclude treatments that were not reimbursed by health insurances
table(KODAP_data_complete$Abschluss)
## 
##     0     1     2     3     4 
##  6453 10155  2345    41    67
KODAP_data_complete <- subset(KODAP_data_complete, Abschluss %in% c(0,1,2,4))
length(KODAP_data_complete$Patient_ID)
## [1] 19020
#Only include treatments between 2018 and 2023
pss2date <- function(x) as.Date(x/86400, origin = "1582-10-14")
KODAP_data_complete$Therapie_pre <- pss2date(KODAP_data_complete$Therapie_pre)
KODAP_data_complete$Beginn_Therapie <- substr(KODAP_data_complete$Therapie_pre, 1, 4)
table(KODAP_data_complete$Beginn_Therapie)
## 
## 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 
##    7   10   21   79  478 2231 2938 3056 2990 2977 2886  461
KODAP_data_complete <- subset(KODAP_data_complete, Beginn_Therapie %in% c("2018", "2019", "2020", "2021", "2022", "2023"))
length(KODAP_data_complete$Patient_ID)
## [1] 17078
length(unique(KODAP_data_complete$Ambulanz_ID))
## [1] 30
table(KODAP_data_complete$Beginn_Therapie)
## 
## 2018 2019 2020 2021 2022 2023 
## 2231 2938 3056 2990 2977 2886
table(KODAP_data_complete$Beginn_Therapie)/length(KODAP_data_complete$Patient_ID)*100
## 
##     2018     2019     2020     2021     2022     2023 
## 13.06359 17.20342 17.89437 17.50790 17.43178 16.89893
KODAP_data_complete$Age_Stepped_bin <- ifelse(KODAP_data_complete$Pat_Alter %in% c(18:64), 0,
                                              ifelse(KODAP_data_complete$Pat_Alter %in% c(65:100), 1,NA))

KODAP_data_complete$Age_Stepped <- ifelse(KODAP_data_complete$Pat_Alter %in% c(18:64), 0,
                                          ifelse(KODAP_data_complete$Pat_Alter %in% c(65:74), 1,
                                                 ifelse(KODAP_data_complete$Pat_Alter %in% c(75:100), 2,NA)))


KODAP_data_complete$Age_Stepped_2 <- ifelse(KODAP_data_complete$Pat_Alter %in% c(18:34), 0,
                                            ifelse(KODAP_data_complete$Pat_Alter %in% c(35:49), 1,
                                                   ifelse(KODAP_data_complete$Pat_Alter %in% c(50:64), 2,
                                                          ifelse(KODAP_data_complete$Pat_Alter %in% c(65:74), 3,
                                                                 ifelse(KODAP_data_complete$Pat_Alter %in% c(75:100), 4,NA)))))



table(KODAP_data_complete$Age_Stepped)
## 
##     0     1     2 
## 16558   405   115
KODAP_data_complete <- subset(KODAP_data_complete, Art_Diagnoseerhebung_pre %in% c(1,2,3))
length(KODAP_data_complete$Patient_ID)
## [1] 15915
KODAP_data_complete$Missing_diagnosis <- ifelse(is.na(KODAP_data_complete$ICD1_pre_clean) == TRUE & 
                                                  is.na(KODAP_data_complete$ICD2_pre_clean) == TRUE & 
                                                  is.na(KODAP_data_complete$ICD3_pre_clean) == TRUE & 
                                                  is.na(KODAP_data_complete$ICD4_pre_clean) == TRUE & 
                                                  is.na(KODAP_data_complete$ICD5_pre_clean) == TRUE, 1,0)

KODAP_data_complete <- subset(KODAP_data_complete, Missing_diagnosis == 0)
length(KODAP_data_complete$Patient_ID)
## [1] 13635

Preparation of census data

#Age Distibution Germany
Census_data <- read_delim("15_bevoelkerungsvorausberechnung_daten.csv", 
                          delim = ";", escape_double = FALSE, trim_ws = TRUE)
View(Census_data)

census_amount_1864_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_1864_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_1864_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_1864_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_1864_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_1864_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_1834_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_1834_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_1834_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_1834_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_1834_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_1834_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_3549_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_3549_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_3549_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_3549_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_3549_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_3549_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_5064_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_5064_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_5064_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_5064_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_5064_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_5064_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_6574_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_6574_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_6574_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_6574_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_6574_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_6574_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_75plus_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_75plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_75plus_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_75plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_75plus_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_75plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_65plus_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_65plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_65plus_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_65plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_65plus_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_65plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_6569_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_6569_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_6569_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_6569_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_6569_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_6569_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_7074_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_7074_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_7074_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_7074_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_7074_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_7074_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_7579_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_7579_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_7579_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_7579_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_7579_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_7579_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

census_amount_80plus_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018, c(85:104)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
census_amount_80plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(85:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
census_amount_80plus_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020, c(85:104)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
census_amount_80plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(85:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
census_amount_80plus_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(85:104)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
census_amount_80plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(85:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

year_distribution_data <- as.data.frame(table(KODAP_data_complete$Beginn_Therapie))
year_distribution_data
##   Var1 Freq
## 1 2018 2095
## 2 2019 2625
## 3 2020 2432
## 4 2021 2410
## 5 2022 2255
## 6 2023 1818
#Computing dataset specific census amounts: 
census_amount_1864 <- (2095/13635)*census_amount_1864_2018+(2625/13635)*census_amount_1864_2019+(2432/13635)*census_amount_1864_2020+(2410/13635)*census_amount_1864_2021+(2255/13635)*census_amount_1864_2022+(1818/13635)*census_amount_1864_2023

census_amount_1834 <- (2095/13635)*census_amount_1834_2018+(2625/13635)*census_amount_1834_2019+(2432/13635)*census_amount_1834_2020+(2410/13635)*census_amount_1834_2021+(2255/13635)*census_amount_1834_2022+(1818/13635)*census_amount_1834_2023

census_amount_3549 <- (2095/13635)*census_amount_3549_2018+(2625/13635)*census_amount_3549_2019+(2432/13635)*census_amount_3549_2020+(2410/13635)*census_amount_3549_2021+(2255/13635)*census_amount_3549_2022+(1818/13635)*census_amount_3549_2023

census_amount_5064 <- (2095/13635)*census_amount_5064_2018+(2625/13635)*census_amount_5064_2019+(2432/13635)*census_amount_5064_2020+(2410/13635)*census_amount_5064_2021+(2255/13635)*census_amount_5064_2022+(1818/13635)*census_amount_5064_2023

census_amount_6574 <- (2095/13635)*census_amount_6574_2018+(2625/13635)*census_amount_6574_2019+(2432/13635)*census_amount_6574_2020+(2410/13635)*census_amount_6574_2021+(2255/13635)*census_amount_6574_2022+(1818/13635)*census_amount_6574_2023

census_amount_75plus <- (2095/13635)*census_amount_75plus_2018+(2625/13635)*census_amount_75plus_2019+(2432/13635)*census_amount_75plus_2020+(2410/13635)*census_amount_75plus_2021+(2255/13635)*census_amount_75plus_2022+(1818/13635)*census_amount_75plus_2023

census_amount_65plus <- (2095/13635)*census_amount_65plus_2018+(2625/13635)*census_amount_65plus_2019+(2432/13635)*census_amount_65plus_2020+(2410/13635)*census_amount_65plus_2021+(2255/13635)*census_amount_65plus_2022+(1818/13635)*census_amount_65plus_2023

census_amount_6569 <- (2095/13635)*census_amount_6569_2018+(2625/13635)*census_amount_6569_2019+(2432/13635)*census_amount_6569_2020+(2410/13635)*census_amount_6569_2021+(2255/13635)*census_amount_6569_2022+(1818/13635)*census_amount_6569_2023
census_amount_7074 <- (2095/13635)*census_amount_7074_2018+(2625/13635)*census_amount_7074_2019+(2432/13635)*census_amount_7074_2020+(2410/13635)*census_amount_7074_2021+(2255/13635)*census_amount_7074_2022+(1818/13635)*census_amount_7074_2023
census_amount_7579 <- (2095/13635)*census_amount_7579_2018+(2625/13635)*census_amount_7579_2019+(2432/13635)*census_amount_7579_2020+(2410/13635)*census_amount_7579_2021+(2255/13635)*census_amount_7579_2022+(1818/13635)*census_amount_7579_2023
census_amount_80plus <- (2095/13635)*census_amount_80plus_2018+(2625/13635)*census_amount_80plus_2019+(2432/13635)*census_amount_80plus_2020+(2410/13635)*census_amount_80plus_2021+(2255/13635)*census_amount_80plus_2022+(1818/13635)*census_amount_80plus_2023


#Female only
female_amount_1864_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "w", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
female_amount_1864_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "w", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
female_amount_1864_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "w", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
female_amount_1864_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "w", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
female_amount_1864_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "w" & Census_data$Variante == 1, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
female_amount_1864_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "w" & Census_data$Variante == 1, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

female_amount_1834_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "w", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
female_amount_1834_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "w", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
female_amount_1834_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "w", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
female_amount_1834_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "w", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
female_amount_1834_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "w" & Census_data$Variante == 1, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
female_amount_1834_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "w" & Census_data$Variante == 1, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

female_amount_3549_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "w", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
female_amount_3549_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "w", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
female_amount_3549_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "w", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
female_amount_3549_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "w", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
female_amount_3549_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "w" & Census_data$Variante == 1, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
female_amount_3549_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "w" & Census_data$Variante == 1, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

female_amount_5064_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "w", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
female_amount_5064_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "w", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
female_amount_5064_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "w", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
female_amount_5064_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "w", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
female_amount_5064_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "w" & Census_data$Variante == 1, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
female_amount_5064_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "w" & Census_data$Variante == 1, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

female_amount_6574_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "w", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
female_amount_6574_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "w", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
female_amount_6574_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "w", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
female_amount_6574_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "w", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
female_amount_6574_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "w" & Census_data$Variante == 1, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
female_amount_6574_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "w" & Census_data$Variante == 1, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

female_amount_75plus_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "w", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
female_amount_75plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "w", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
female_amount_75plus_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "w", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
female_amount_75plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "w", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
female_amount_75plus_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "w" & Census_data$Variante == 1, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
female_amount_75plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "w" & Census_data$Variante == 1, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

female_amount_65plus_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "w", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
female_amount_65plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "w", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
female_amount_65plus_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "w", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
female_amount_65plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "w", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
female_amount_65plus_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "w" & Census_data$Variante == 1, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
female_amount_65plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "w" & Census_data$Variante == 1, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

female_amount_1864 <- (2095/13635)*female_amount_1864_2018+(2625/13635)*female_amount_1864_2019+(2432/13635)*female_amount_1864_2020+(2410/13635)*female_amount_1864_2021+(2255/13635)*female_amount_1864_2022+(1818/13635)*female_amount_1864_2023

female_amount_1834 <- (2095/13635)*female_amount_1834_2018+(2625/13635)*female_amount_1834_2019+(2432/13635)*female_amount_1834_2020+(2410/13635)*female_amount_1834_2021+(2255/13635)*female_amount_1834_2022+(1818/13635)*female_amount_1834_2023

female_amount_3549 <- (2095/13635)*female_amount_3549_2018+(2625/13635)*female_amount_3549_2019+(2432/13635)*female_amount_3549_2020+(2410/13635)*female_amount_3549_2021+(2255/13635)*female_amount_3549_2022+(1818/13635)*female_amount_3549_2023

female_amount_5064 <- (2095/13635)*female_amount_5064_2018+(2625/13635)*female_amount_5064_2019+(2432/13635)*female_amount_5064_2020+(2410/13635)*female_amount_5064_2021+(2255/13635)*female_amount_5064_2022+(1818/13635)*female_amount_5064_2023

female_amount_6574 <- (2095/13635)*female_amount_6574_2018+(2625/13635)*female_amount_6574_2019+(2432/13635)*female_amount_6574_2020+(2410/13635)*female_amount_6574_2021+(2255/13635)*female_amount_6574_2022+(1818/13635)*female_amount_6574_2023

female_amount_75plus <- (2095/13635)*female_amount_75plus_2018+(2625/13635)*female_amount_75plus_2019+(2432/13635)*female_amount_75plus_2020+(2410/13635)*female_amount_75plus_2021+(2255/13635)*female_amount_75plus_2022+(1818/13635)*female_amount_75plus_2023

female_amount_65plus <- (2095/13635)*female_amount_65plus_2018+(2625/13635)*female_amount_65plus_2019+(2432/13635)*female_amount_65plus_2020+(2410/13635)*female_amount_65plus_2021+(2255/13635)*female_amount_65plus_2022+(1818/13635)*female_amount_65plus_2023


#Male only
male_amount_1864_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "m", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
male_amount_1864_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "m", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
male_amount_1864_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "m", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
male_amount_1864_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "m", c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
male_amount_1864_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "m" & Census_data$Variante == 1, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
male_amount_1864_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "m" & Census_data$Variante == 1, c(23:69)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

male_amount_1834_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "m", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
male_amount_1834_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "m", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
male_amount_1834_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "m", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
male_amount_1834_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "m", c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
male_amount_1834_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "m" & Census_data$Variante == 1, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
male_amount_1834_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "m" & Census_data$Variante == 1, c(23:39)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

male_amount_3549_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "m", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
male_amount_3549_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "m", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
male_amount_3549_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "m", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
male_amount_3549_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "m", c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
male_amount_3549_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "m" & Census_data$Variante == 1, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
male_amount_3549_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "m" & Census_data$Variante == 1, c(40:54)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

male_amount_5064_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "m", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
male_amount_5064_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "m", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
male_amount_5064_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "m", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
male_amount_5064_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "m", c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
male_amount_5064_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "m" & Census_data$Variante == 1, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
male_amount_5064_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "m" & Census_data$Variante == 1, c(55:69)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

male_amount_6574_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "m", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
male_amount_6574_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "m", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
male_amount_6574_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "m", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
male_amount_6574_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "m", c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
male_amount_6574_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "m" & Census_data$Variante == 1, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
male_amount_6574_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "m" & Census_data$Variante == 1, c(70:79)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

male_amount_75plus_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "m", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
male_amount_75plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "m", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
male_amount_75plus_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "m", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
male_amount_75plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "m", c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
male_amount_75plus_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "m" & Census_data$Variante == 1, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
male_amount_75plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "m" & Census_data$Variante == 1, c(80:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])

male_amount_65plus_2018 <- sum(Census_data[Census_data$Simulationsjahr == 2018 & Census_data$mw == "m", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2018, c(23:104)])
male_amount_65plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019 & Census_data$mw == "m", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:104)])
male_amount_65plus_2020 <- sum(Census_data[Census_data$Simulationsjahr == 2020 & Census_data$mw == "m", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2020, c(23:104)])
male_amount_65plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021 & Census_data$mw == "m", c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:104)])
male_amount_65plus_2022 <- sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$mw == "m" & Census_data$Variante == 1, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2022 & Census_data$Variante == 1, c(23:104)])
male_amount_65plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$mw == "m" & Census_data$Variante == 1, c(70:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:104)])


male_amount_1864 <- (2095/13635)*male_amount_1864_2018+(2625/13635)*male_amount_1864_2019+(2432/13635)*male_amount_1864_2020+(2410/13635)*male_amount_1864_2021+(2255/13635)*male_amount_1864_2022+(1818/13635)*male_amount_1864_2023
male_amount_1834 <- (2095/13635)*male_amount_1834_2018+(2625/13635)*male_amount_1834_2019+(2432/13635)*male_amount_1834_2020+(2410/13635)*male_amount_1834_2021+(2255/13635)*male_amount_1834_2022+(1818/13635)*male_amount_1834_2023
male_amount_3549 <- (2095/13635)*male_amount_3549_2018+(2625/13635)*male_amount_3549_2019+(2432/13635)*male_amount_3549_2020+(2410/13635)*male_amount_3549_2021+(2255/13635)*male_amount_3549_2022+(1818/13635)*male_amount_3549_2023
male_amount_5064 <- (2095/13635)*male_amount_5064_2018+(2625/13635)*male_amount_5064_2019+(2432/13635)*male_amount_5064_2020+(2410/13635)*male_amount_5064_2021+(2255/13635)*male_amount_5064_2022+(1818/13635)*male_amount_5064_2023
male_amount_6574 <- (2095/13635)*male_amount_6574_2018+(2625/13635)*male_amount_6574_2019+(2432/13635)*male_amount_6574_2020+(2410/13635)*male_amount_6574_2021+(2255/13635)*male_amount_6574_2022+(1818/13635)*male_amount_6574_2023
male_amount_75plus <- (2095/13635)*male_amount_75plus_2018+(2625/13635)*male_amount_75plus_2019+(2432/13635)*male_amount_75plus_2020+(2410/13635)*male_amount_75plus_2021+(2255/13635)*male_amount_75plus_2022+(1818/13635)*male_amount_75plus_2023
male_amount_65plus <- (2095/13635)*male_amount_65plus_2018+(2625/13635)*male_amount_65plus_2019+(2432/13635)*male_amount_65plus_2020+(2410/13635)*male_amount_65plus_2021+(2255/13635)*male_amount_65plus_2022+(1818/13635)*male_amount_65plus_2023

###Rates of long-term-care dependency
GENESIS <- read_csv2("Long-term care GENESIS.csv")
View(GENESIS)
colnames(GENESIS) <- c("Gender", "Age", "2011", "2013", "2015", "2017", "2019", "2021", "2023")

#2017
census_amount_1824_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(23:29)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_2529_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(30:34)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_3034_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(35:39)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_3539_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(40:44)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_4044_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(45:49)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_4549_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(50:54)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_5054_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(55:59)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_5559_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(60:64)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_6064_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(65:69)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_6569_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_7074_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_7579_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_8084_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(85:89)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_8589_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(90:94)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_9094_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(95:99)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])
census_amount_95plus_2017 <- sum(Census_data[Census_data$Simulationsjahr == 2017, c(100:104)])/sum(Census_data[Census_data$Simulationsjahr == 2017, "Bev"])

sum_1864_2017 <- sum(census_amount_1824_2017, census_amount_2529_2017, census_amount_3034_2017, census_amount_3539_2017, census_amount_4044_2017,
                     census_amount_4549_2017, census_amount_5054_2017, census_amount_5559_2017, census_amount_6064_2017)

Long_term_care_rate_1864_2017 <- ((census_amount_1824_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "20 to under 25 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_2529_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "25 to under 30 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_3034_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "30 to under 35 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_3539_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "35 to under 40 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_4044_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "40 to under 45 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_4549_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "45 to under 50 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_5054_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "50 to under 55 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_5559_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "55 to under 60 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_6064_2017/sum_1864_2017)*as.numeric(GENESIS[GENESIS$Age == "60 to under 65 years" & GENESIS$Gender == "Total", "2017"]))/100

sum_6574_2017 <- sum(census_amount_6569_2017, census_amount_7074_2017)


Long_term_care_rate_6574_2017 <- ((census_amount_6569_2017/sum_6574_2017)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2017"])+
                                    (census_amount_7074_2017/sum_6574_2017)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2017"]))/100


sum_75plus_2017 <- sum(census_amount_7579_2017, census_amount_8084_2017, census_amount_8589_2017, census_amount_9094_2017, census_amount_95plus_2017)

Long_term_care_rate_75plus_2017 <-((census_amount_7579_2017/sum_75plus_2017)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_8084_2017/sum_75plus_2017)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_8589_2017/sum_75plus_2017)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_9094_2017/sum_75plus_2017)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_95plus_2017/sum_75plus_2017)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2017"]))/100

sum_65plus_2017 <- sum(census_amount_6569_2017, census_amount_7074_2017,census_amount_7579_2017, census_amount_8084_2017, census_amount_8589_2017, census_amount_9094_2017, census_amount_95plus_2017)

Long_term_care_rate_65plus_2017 <-((census_amount_6569_2017/sum_65plus_2017)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_7074_2017/sum_65plus_2017)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_7579_2017/sum_65plus_2017)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_8084_2017/sum_65plus_2017)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_8589_2017/sum_65plus_2017)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_9094_2017/sum_65plus_2017)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2017"])+
                                     (census_amount_95plus_2017/sum_65plus_2017)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2017"]))/100



#2019
census_amount_1824_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(23:29)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_2529_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(30:34)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_3034_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(35:39)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_3539_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(40:44)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_4044_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(45:49)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_4549_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(50:54)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_5054_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(55:59)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_5559_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(60:64)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_6064_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(65:69)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_6569_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_7074_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_7579_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_8084_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(85:89)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_8589_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(90:94)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_9094_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(95:99)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])
census_amount_95plus_2019 <- sum(Census_data[Census_data$Simulationsjahr == 2019, c(100:104)])/sum(Census_data[Census_data$Simulationsjahr == 2019, "Bev"])


sum_1864_2019 <- sum(census_amount_1824_2019, census_amount_2529_2019, census_amount_3034_2019, census_amount_3539_2019, census_amount_4044_2019,
                     census_amount_4549_2019, census_amount_5054_2019, census_amount_5559_2019, census_amount_6064_2019)

Long_term_care_rate_1864_2019 <- ((census_amount_1824_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "20 to under 25 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_2529_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "25 to under 30 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_3034_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "30 to under 35 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_3539_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "35 to under 40 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_4044_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "40 to under 45 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_4549_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "45 to under 50 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_5054_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "50 to under 55 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_5559_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "55 to under 60 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_6064_2019/sum_1864_2019)*as.numeric(GENESIS[GENESIS$Age == "60 to under 65 years" & GENESIS$Gender == "Total", "2019"]))/100

sum_6574_2019 <- sum(census_amount_6569_2019, census_amount_7074_2019)

Long_term_care_rate_6574_2019 <- ((census_amount_6569_2019/sum_6574_2019)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2019"])+
                                    (census_amount_7074_2019/sum_6574_2019)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2019"]))/100


sum_75plus_2019 <- sum(census_amount_7579_2019, census_amount_8084_2019, census_amount_8589_2019, census_amount_9094_2019, census_amount_95plus_2019)

Long_term_care_rate_75plus_2019 <-((census_amount_7579_2019/sum_75plus_2019)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_8084_2019/sum_75plus_2019)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_8589_2019/sum_75plus_2019)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_9094_2019/sum_75plus_2019)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_95plus_2019/sum_75plus_2019)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2019"]))/100

sum_65plus_2019 <- sum(census_amount_6569_2019, census_amount_7074_2019,census_amount_7579_2019, census_amount_8084_2019, census_amount_8589_2019, census_amount_9094_2019, census_amount_95plus_2019)

Long_term_care_rate_65plus_2019 <-((census_amount_6569_2019/sum_65plus_2019)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_7074_2019/sum_65plus_2019)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_7579_2019/sum_65plus_2019)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_8084_2019/sum_65plus_2019)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_8589_2019/sum_65plus_2019)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_9094_2019/sum_65plus_2019)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2019"])+
                                     (census_amount_95plus_2019/sum_65plus_2019)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2019"]))/100


#2021
census_amount_1824_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(23:29)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_2529_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(30:34)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_3034_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(35:39)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_3539_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(40:44)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_4044_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(45:49)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_4549_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(50:54)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_5054_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(55:59)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_5559_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(60:64)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_6064_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(65:69)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_6569_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_7074_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_7579_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_8084_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(85:89)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_8589_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(90:94)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_9094_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(95:99)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])
census_amount_95plus_2021 <- sum(Census_data[Census_data$Simulationsjahr == 2021, c(100:104)])/sum(Census_data[Census_data$Simulationsjahr == 2021, "Bev"])

sum_1864_2021 <- sum(census_amount_1824_2021, census_amount_2529_2021, census_amount_3034_2021, census_amount_3539_2021, census_amount_4044_2021,
                     census_amount_4549_2021, census_amount_5054_2021, census_amount_5559_2021, census_amount_6064_2021)

Long_term_care_rate_1864_2021 <- ((census_amount_1824_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "20 to under 25 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_2529_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "25 to under 30 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_3034_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "30 to under 35 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_3539_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "35 to under 40 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_4044_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "40 to under 45 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_4549_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "45 to under 50 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_5054_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "50 to under 55 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_5559_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "55 to under 60 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_6064_2021/sum_1864_2021)*as.numeric(GENESIS[GENESIS$Age == "60 to under 65 years" & GENESIS$Gender == "Total", "2021"]))/100

sum_6574_2021 <- sum(census_amount_6569_2021, census_amount_7074_2021)

Long_term_care_rate_6574_2021 <- ((census_amount_6569_2021/sum_6574_2021)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2021"])+
                                    (census_amount_7074_2021/sum_6574_2021)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2021"]))/100


sum_75plus_2021 <- sum(census_amount_7579_2021, census_amount_8084_2021, census_amount_8589_2021, census_amount_9094_2021, census_amount_95plus_2021)

Long_term_care_rate_75plus_2021 <-((census_amount_7579_2021/sum_75plus_2021)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_8084_2021/sum_75plus_2021)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_8589_2021/sum_75plus_2021)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_9094_2021/sum_75plus_2021)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_95plus_2021/sum_75plus_2021)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2021"]))/100



sum_65plus_2021 <- sum(census_amount_6569_2021, census_amount_7074_2021,census_amount_7579_2021, census_amount_8084_2021, census_amount_8589_2021, census_amount_9094_2021, census_amount_95plus_2021)

Long_term_care_rate_65plus_2021 <-((census_amount_6569_2021/sum_65plus_2021)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_7074_2021/sum_65plus_2021)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_7579_2021/sum_65plus_2021)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_8084_2021/sum_65plus_2021)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_8589_2021/sum_65plus_2021)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_9094_2021/sum_65plus_2021)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2021"])+
                                     (census_amount_95plus_2021/sum_65plus_2021)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2021"]))/100



#2023
census_amount_1824_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(23:29)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_2529_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(30:34)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_3034_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(35:39)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_3539_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(40:44)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_4044_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(45:49)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_4549_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(50:54)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_5054_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(55:59)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_5559_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(60:64)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_6064_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(65:69)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_6569_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(70:74)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_7074_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(75:79)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_7579_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(80:84)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_8084_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(85:89)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_8589_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(90:94)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_9094_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(95:99)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])
census_amount_95plus_2023 <- sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, c(100:104)])/sum(Census_data[Census_data$Simulationsjahr == 2023 & Census_data$Variante == 1, "Bev"])

sum_1864_2023 <- sum(census_amount_1824_2023, census_amount_2529_2023, census_amount_3034_2023, census_amount_3539_2023, census_amount_4044_2023,
                     census_amount_4549_2023, census_amount_5054_2023, census_amount_5559_2023, census_amount_6064_2023)

Long_term_care_rate_1864_2023 <- ((census_amount_1824_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "20 to under 25 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_2529_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "25 to under 30 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_3034_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "30 to under 35 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_3539_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "35 to under 40 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_4044_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "40 to under 45 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_4549_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "45 to under 50 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_5054_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "50 to under 55 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_5559_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "55 to under 60 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_6064_2023/sum_1864_2023)*as.numeric(GENESIS[GENESIS$Age == "60 to under 65 years" & GENESIS$Gender == "Total", "2023"]))/100

sum_6574_2023 <- sum(census_amount_6569_2023, census_amount_7074_2023)

Long_term_care_rate_6574_2023 <- ((census_amount_6569_2023/sum_6574_2023)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2023"])+
                                    (census_amount_7074_2023/sum_6574_2023)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2023"]))/100


sum_75plus_2023 <- sum(census_amount_7579_2023, census_amount_8084_2023, census_amount_8589_2023, census_amount_9094_2023, census_amount_95plus_2023)

Long_term_care_rate_75plus_2023 <-((census_amount_7579_2023/sum_75plus_2023)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_8084_2023/sum_75plus_2023)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_8589_2023/sum_75plus_2023)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_9094_2023/sum_75plus_2023)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_95plus_2023/sum_75plus_2023)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2023"]))/100


sum_65plus_2023 <- sum(census_amount_6569_2023, census_amount_7074_2023,census_amount_7579_2023, census_amount_8084_2023, census_amount_8589_2023, census_amount_9094_2023, census_amount_95plus_2023)

Long_term_care_rate_65plus_2023 <-((census_amount_6569_2023/sum_65plus_2023)*as.numeric(GENESIS[GENESIS$Age == "65 to under 70 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_7074_2023/sum_65plus_2023)*as.numeric(GENESIS[GENESIS$Age == "70 to under 75 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_7579_2023/sum_65plus_2023)*as.numeric(GENESIS[GENESIS$Age == "75 to under 80 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_8084_2023/sum_65plus_2023)*as.numeric(GENESIS[GENESIS$Age == "80 to under 85 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_8589_2023/sum_65plus_2023)*as.numeric(GENESIS[GENESIS$Age == "85 to under 90 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_9094_2023/sum_65plus_2023)*as.numeric(GENESIS[GENESIS$Age == "90 to under 95 years" & GENESIS$Gender == "Total", "2023"])+
                                     (census_amount_95plus_2023/sum_65plus_2023)*as.numeric(GENESIS[GENESIS$Age == "95 years and over" & GENESIS$Gender == "Total", "2023"]))/100


Long_term_care_rate_1864_2017
## [1] 0.009617073
Long_term_care_rate_1864_2019
## [1] 0.01208859
Long_term_care_rate_1864_2021
## [1] 0.01534732
Long_term_care_rate_1864_2023
## [1] 0.01787395
Long_term_care_rate_6574_2017
## [1] 0.04928208
Long_term_care_rate_6574_2019
## [1] 0.05923031
Long_term_care_rate_6574_2021
## [1] 0.07348887
Long_term_care_rate_6574_2023
## [1] 0.08456034
Long_term_care_rate_75plus_2017
## [1] 0.2512004
Long_term_care_rate_75plus_2019
## [1] 0.2922611
Long_term_care_rate_75plus_2021
## [1] 0.3477314
Long_term_care_rate_75plus_2023
## [1] 0.3903115
year_distribution_data <- as.data.frame(table(KODAP_data_complete$Beginn_Therapie))

Long_term_care_rate_1864_average <- (year_distribution_data[year_distribution_data$Var1 == 2018, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_1864_2017+
  (year_distribution_data[year_distribution_data$Var1 == 2019, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_1864_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2020, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_1864_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2021, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_1864_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2022, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_1864_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2023, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_1864_2023
  
Long_term_care_rate_6574_average <- (year_distribution_data[year_distribution_data$Var1 == 2018, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_6574_2017+
  (year_distribution_data[year_distribution_data$Var1 == 2019, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_6574_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2020, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_6574_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2021, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_6574_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2022, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_6574_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2023, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_6574_2023

Long_term_care_rate_75plus_average <- (year_distribution_data[year_distribution_data$Var1 == 2018, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_75plus_2017+
  (year_distribution_data[year_distribution_data$Var1 == 2019, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_75plus_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2020, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_75plus_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2021, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_75plus_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2022, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_75plus_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2023, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_75plus_2023

Long_term_care_rate_65plus_average <- (year_distribution_data[year_distribution_data$Var1 == 2018, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_65plus_2017+
  (year_distribution_data[year_distribution_data$Var1 == 2019, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_65plus_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2020, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_65plus_2019+
  (year_distribution_data[year_distribution_data$Var1 == 2021, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_65plus_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2022, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_65plus_2021+
  (year_distribution_data[year_distribution_data$Var1 == 2023, "Freq"]/sum(year_distribution_data$Freq))*Long_term_care_rate_65plus_2023

Main Analyses

#Read prevalence estimates
Prevalence_estimates <- read_excel("Prevalence_estimates.xlsx")
Prevalence_estimates
## # A tibble: 17 × 6
##    Diagnosis                  `1834y` `3549y` `5064y` `6574y` `75yplus`
##    <chr>                        <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1 Any mental disorder           35.8    28      26.4    19.6      19.6
##  2 Any mood disorder             15.1    10.3     7       5.9       5.9
##  3 Major Depressive Disorder     10       7.2     5.2     4.4       4.4
##  4 Dysthymia                      2.1     1.7     1.3     1.6       1.6
##  5 Any anxiety disorder          18.1    16.2    15.3    11.1      11.1
##  6 Panic disorder/Agoraphobia     4.2     4.1     4.1     3.5       3.5
##  7 Social phobia                  4.6     3.1     2.2     0.7       0.7
##  8 Specific phobias              12.3     9.5    10.9     8.4       8.4
##  9 GAD                            3.3     2       2.3     1.3       1.3
## 10 OCD                            7.2     3.6     2.2     1.1       1.1
## 11 PTSD                           3.7     2.5     1       1.8       1.8
## 12 Any somatoform disorder        4.2     3.8     3.6     2.1       2.1
## 13 Somatization disorder          0.9     0.6     0.9     0.8       0.8
## 14 Pain disorder                  4       3.8     3       1.6       1.6
## 15 Eating disorders               2.3     0.5     0.7     0.4       0.4
## 16 Substance use disorders        8.4     5.9     5.5     2.5       2.5
## 17 Psychotic disorders            4.2     2.2     2.5     1.3       1.3
##################
#####Analyses#####
##################

#####################
#Any Mental Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 81.0  9.3  9.7
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete$Patient_ID)

Observation
## [1] 13218   324    93
round(Observation/length(KODAP_data_complete$Patient_ID)*100, 1)
## [1] 96.9  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 2272.7, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.860477e-234
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.482197e-323
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.19671990 0.25585825 0.07026915
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 12368, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02130213 0.02649473
## sample estimates:
##          p 
## 0.02376238
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.23 0.29
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 13264, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005538338 0.008388176
## sample estimates:
##           p 
## 0.006820682
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.09
#####################
#Any Mood Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 83.4  8.1  8.5
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                        "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                        "F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                        "F34 Anhaltende affektive Störungen",
                                                                                                        "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_any_mood_disorder$Patient_ID)
## [1] 8244
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_mood_disorder$Patient_ID)

Observation
## [1] 8027  179   38
round(Observation/length(KODAP_data_complete_any_mood_disorder$Patient_ID)*100,1)
## [1] 97.4  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1174.8, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.61793e-118
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.096773e-252
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.16716254 0.26786863 0.05441013
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 7541.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01872596 0.02515365
## sample estimates:
##          p 
## 0.02171276
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.27
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.23 0.31
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 8090.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003309074 0.006390114
## sample estimates:
##           p 
## 0.004609413
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.08
###########################
#Major Depressive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 82.4  8.6  9.0
KODAP_data_complete_MDD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert")| 
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_MDD$Patient_ID)
## [1] 7460
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_MDD$Patient_ID)

Observation
## [1] 7261  163   36
round(Observation/length(KODAP_data_complete_MDD$Patient_ID)*100,1)
## [1] 97.3  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1162.4, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 9.582918e-120
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.973743e-244
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.18153500 0.25357980 0.05358668
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 6820.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01870821 0.02549363
## sample estimates:
##          p 
## 0.02184987
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.22 0.30
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 7314.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003431670 0.006750952
## sample estimates:
##           p 
## 0.004825737
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.07
###########################
#########Dysthymia#########
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 74.6 12.4 13.0
KODAP_data_complete_Dysthymia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD5_pre_clean %in% c("F34.1", "F34.10"),]


length(KODAP_data_complete_Dysthymia$Patient_ID)
## [1] 958
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Dysthymia$Patient_ID)

Observation
## [1] 935  22   1
round(Observation/length(KODAP_data_complete_Dysthymia$Patient_ID)*100,1)
## [1] 97.6  2.3  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 269.65, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.823106e-87
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.899135e-29
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.711903e-56
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.308640900 0.184762252 0.008035592
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 870.11, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01479694 0.03514765
## sample estimates:
##          p 
## 0.02296451
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.12 0.28
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 952.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  5.449108e-05 6.751188e-03
## sample estimates:
##           p 
## 0.001043841
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.01
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.05
###########################
##Any anxiety disorder#####
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 80.6  9.5  9.9
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                       "F40.1 Soziale Phobie",
                                                                                                       "F40.2 Spezifische Phobie",
                                                                                                       "F41.1 Generalisierte Angststörung",
                                                                                                       "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_any_anx_disorder$Patient_ID)
## [1] 4453
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_anx_disorder$Patient_ID)

Observation
## [1] 4318  106   29
round(Observation/length(KODAP_data_complete_any_anx_disorder$Patient_ID)*100,1)
## [1] 97.0  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 772.5, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.244667e-240
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.363829e-80
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.210344e-155
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.20357702 0.25051327 0.06557679
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 4037.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01961854 0.02883120
## sample estimates:
##          p 
## 0.02380418
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.30
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4335.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004446095 0.009469305
## sample estimates:
##           p 
## 0.006512464
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.10
#################################
##Panic Disorder/Agoraphobia#####
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 76.7 11.4 11.9
KODAP_data_complete_PanicAgora <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_PanicAgora$Patient_ID)
## [1] 1456
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PanicAgora$Patient_ID)

Observation
## [1] 1400   47    9
round(Observation/length(KODAP_data_complete_PanicAgora$Patient_ID)*100,1)
## [1] 96.2  3.2  0.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 312.33, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.114495e-94
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.321778e-29
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.078787e-65
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.25323710 0.28363345 0.05196721
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1272.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02406506 0.04304906
## sample estimates:
##          p 
## 0.03228022
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.28
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.38
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1418.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003019616 0.012152642
## sample estimates:
##           p 
## 0.006181319
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.10
#################################
##########Social phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 92.8  3.5  3.7
KODAP_data_complete_socialphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]


length(KODAP_data_complete_socialphobia$Patient_ID)
## [1] 1850
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_socialphobia$Patient_ID)

Observation
## [1] 1830   16    4
round(Observation/length(KODAP_data_complete_socialphobia$Patient_ID)*100,1)
## [1] 98.9  0.9  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 103.92, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.089412e-35
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.300956e-12
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.342283e-24
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.06542385 0.24719537 0.05912997
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1784.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005124665 0.014335622
## sample estimates:
##           p 
## 0.008648649
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.15 0.41
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1832, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0006928545 0.0059315037
## sample estimates:
##           p 
## 0.002162162
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.02 0.16
#################################
##########Specific phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 78.4 10.6 11.0
KODAP_data_complete_specificphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]


length(KODAP_data_complete_specificphobia$Patient_ID)
## [1] 703
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_specificphobia$Patient_ID)

Observation
## [1] 678  22   3
round(Observation/length(KODAP_data_complete_specificphobia$Patient_ID)*100,1)
## [1] 96.4  3.1  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 137.53, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.072222e-42
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 6.419946e-13
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.446374e-30
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.22992128 0.29650350 0.03868617
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 615.88, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02018695 0.04777189
## sample estimates:
##          p 
## 0.03129445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.3
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.19 0.45
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 689.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001102765 0.013513634
## sample estimates:
##           p 
## 0.004267425
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.12
################################################
##########Generalized Anxiety Disorder##########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 84.5  7.6  7.9
KODAP_data_complete_GAD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_GAD$Patient_ID)
## [1] 552
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_GAD$Patient_ID)

Observation
## [1] 526  20   6
round(Observation/length(KODAP_data_complete_GAD$Patient_ID)*100,1)
## [1] 95.3  3.6  1.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 51.696, df = 2, p-value = 5.946e-12
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.844488e-15
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.0004446198
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.319313e-12
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1279988 0.4773477 0.1370198
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 473.05, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02285853 0.05637906
## sample estimates:
##          p 
## 0.03623188
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.48
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.30 0.74
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 526.31, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004426108 0.024731112
## sample estimates:
##          p 
## 0.01086957
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.14
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.31
################################################
##########Obsessive compulsive disorders########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 91.5  4.1  4.3
KODAP_data_complete_OCD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F42.X Zwangsstörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_OCD$Patient_ID)
## [1] 791
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_OCD$Patient_ID)

Observation
## [1] 778  11   2
round(Observation/length(KODAP_data_complete_OCD$Patient_ID)*100,1)
## [1] 98.4  1.4  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 48.952, df = 2, p-value = 2.345e-11
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.126015e-16
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.558771e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.09123e-12
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.07474006 0.33524804 0.05832179
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 745.67, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.007329893 0.025530476
## sample estimates:
##          p 
## 0.01390645
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.34
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.18 0.62
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 781.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004380695 0.0101435327
## sample estimates:
##           p 
## 0.002528445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.23
################################################
##########Post traumatic stress disorder########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 78.3 10.6 11.1
KODAP_data_complete_PTSD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_PTSD$Patient_ID)
## [1] 1067
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PTSD$Patient_ID)

Observation
## [1] 1052   13    2
round(Observation/length(KODAP_data_complete_PTSD$Patient_ID)*100,1)
## [1] 98.6  1.2  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 258.38, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.664279e-88
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.194624e-34
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.148891e-50
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.25840984 0.11508209 0.01694032
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1013.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006788272 0.021319397
## sample estimates:
##          p 
## 0.01218369
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.20
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1057, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0003247314 0.0075301326
## sample estimates:
##           p 
## 0.001874414
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.07
################################################
##########Any somatoform Disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 83.7  8.0  8.3
KODAP_data_complete_Somatoform <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_Somatoform$Patient_ID)
## [1] 1074
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatoform$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatoform$Patient_ID)

Observation
## [1] 1009   48   17
round(Observation/length(KODAP_data_complete_Somatoform$Patient_ID)*100,1)
## [1] 93.9  4.5  1.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 88.963, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 8.645902e-24
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.828896e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.153289e-21
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1227562 0.5599319 0.1897449
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 888.76, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03347037 0.05928157
## sample estimates:
##          p 
## 0.04469274
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.56
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.42 0.74
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1005.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.009547387 0.025770512
## sample estimates:
##          p 
## 0.01582868
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.11 0.31
################################################
##########Somatization disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 73.8 12.8 13.4
KODAP_data_complete_Somatization <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                        KODAP_data_complete$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                        KODAP_data_complete$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                        KODAP_data_complete$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                        KODAP_data_complete$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_Somatization$Patient_ID)
## [1] 211
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatization$Patient_ID)

Observation
## [1] 203   7   1
round(Observation/length(KODAP_data_complete_Somatization$Patient_ID)*100,1)
## [1] 96.2  3.3  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 55.336, df = 2, p-value = 9.636e-13
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.943582e-17
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.177964e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.182352e-11
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.30288177 0.25938667 0.03545495
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 182.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01461394 0.06996360
## sample estimates:
##          p 
## 0.03317536
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.55
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 205.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0002474437 0.0302007408
## sample estimates:
##           p 
## 0.004739336
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.23
################################################
##########Pain disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 86.2  6.8  7.1
KODAP_data_complete_Pain <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                          KODAP_data_complete$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                          KODAP_data_complete$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                          KODAP_data_complete$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                          KODAP_data_complete$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_Pain$Patient_ID)
## [1] 508
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Pain$Patient_ID)

Observation
## [1] 465  30  13
round(Observation/length(KODAP_data_complete_Pain$Patient_ID)*100,1)
## [1] 91.5  5.9  2.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 16.893, df = 2, p-value = 0.0002146
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.0007199377
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.441711
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.690537e-05
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0623855 0.8726746 0.3618277
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 393.32, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.04085658 0.08418493
## sample estimates:
##          p 
## 0.05905512
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.87
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.60 1.24
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 455.44, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01428679 0.04450754
## sample estimates:
##          p 
## 0.02559055
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.36
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.20 0.63
################################################
##########Eating disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 89.0  5.4  5.6
KODAP_data_complete_Eating <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD2_pre_recode %in% c("F50.X Essstörung") | 
                                                    KODAP_data_complete$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_Eating$Patient_ID)
## [1] 857
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Eating$Patient_ID)

Observation
## [1] 849   8
Observation[3] <- 0
Observation
## [1] 849   8   0
round(Observation/length(KODAP_data_complete_Eating$Patient_ID)*100,1)
## [1] 99.1  0.9  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 89.395, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 8.208025e-32
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.213906e-11
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.441333e-21
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1130814 0.1735878 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 823.34, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004346906 0.019076925
## sample estimates:
##           p 
## 0.009334889
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.08 0.35
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 855, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000000000 0.005563187
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.0 0.1
#########################################
##########Substance-use disorders########
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 88.0  5.9  6.1
KODAP_data_complete_SubstanceUse <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_SubstanceUse$Patient_ID)
## [1] 764
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_SubstanceUse$Patient_ID)

Observation
## [1] 748  15   1
round(Observation/length(KODAP_data_complete_SubstanceUse$Patient_ID)*100,1)
## [1] 97.9  2.0  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 73.277, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.93638e-23
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.330415e-07
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.308868e-19
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1126184 0.3344931 0.0213365
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 703.26, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01143871 0.03294463
## sample estimates:
##          p 
## 0.01963351
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.33
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.19 0.56
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 758.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  6.832858e-05 8.456433e-03
## sample estimates:
##           p 
## 0.001308901
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.14
#########################################
##########Psychotic disorders############
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios*100,1)
## [1] 86.4  6.6  6.9
KODAP_data_complete_Psychotic <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") | 
                                                       KODAP_data_complete$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_Psychotic$Patient_ID)
## [1] 336
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Psychotic$Patient_ID)

Observation
## [1] 331   4   1
round(Observation/length(KODAP_data_complete_Psychotic$Patient_ID)*100,1)
## [1] 98.5  1.2  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 42.102, df = 2, p-value = 7.207e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.935806e-15
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.411055e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.119362e-09
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.14000572 0.17919911 0.04286503
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 318.24, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003820372 0.032295397
## sample estimates:
##          p 
## 0.01190476
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.49
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 330.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0001553772 0.0190996535
## sample estimates:
##          p 
## 0.00297619
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.28

Subgroups of working-age adults

#Read prevalence estimates
Prevalence_estimates <- read_excel("Prevalence_estimates.xlsx")
Prevalence_estimates
## # A tibble: 17 × 6
##    Diagnosis                  `1834y` `3549y` `5064y` `6574y` `75yplus`
##    <chr>                        <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1 Any mental disorder           35.8    28      26.4    19.6      19.6
##  2 Any mood disorder             15.1    10.3     7       5.9       5.9
##  3 Major Depressive Disorder     10       7.2     5.2     4.4       4.4
##  4 Dysthymia                      2.1     1.7     1.3     1.6       1.6
##  5 Any anxiety disorder          18.1    16.2    15.3    11.1      11.1
##  6 Panic disorder/Agoraphobia     4.2     4.1     4.1     3.5       3.5
##  7 Social phobia                  4.6     3.1     2.2     0.7       0.7
##  8 Specific phobias              12.3     9.5    10.9     8.4       8.4
##  9 GAD                            3.3     2       2.3     1.3       1.3
## 10 OCD                            7.2     3.6     2.2     1.1       1.1
## 11 PTSD                           3.7     2.5     1       1.8       1.8
## 12 Any somatoform disorder        4.2     3.8     3.6     2.1       2.1
## 13 Somatization disorder          0.9     0.6     0.9     0.8       0.8
## 14 Pain disorder                  4       3.8     3       1.6       1.6
## 15 Eating disorders               2.3     0.5     0.7     0.4       0.4
## 16 Substance use disorders        8.4     5.9     5.5     2.5       2.5
## 17 Psychotic disorders            4.2     2.2     2.5     1.3       1.3
##################
#####Analyses#####
##################

#Built Subset Datasets
KODAP_data_complete_s1 <- subset(KODAP_data_complete, Age_Stepped_2 %in% c(0,3,4))
KODAP_data_complete_s2 <- subset(KODAP_data_complete, Age_Stepped_2 %in% c(1,3,4))
KODAP_data_complete_s3 <- subset(KODAP_data_complete, Age_Stepped_2 %in% c(2,3,4))

#####################
#Any Mental Disorder#
#####################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.31 0.23 0.27 0.09 0.10
table(KODAP_data_complete$Age_Stepped_2)
## 
##    0    1    2    3    4 
## 7621 3144 2453  324   93
round(table(KODAP_data_complete$Age_Stepped_2)/length(KODAP_data_complete$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 55.9 23.1 18.0  2.4  0.7
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.62 0.18 0.19
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_s1$Age_Stepped))

Observation
## [1] 7621  324   93
round(Observation/length(KODAP_data_complete_s1$Patient_ID)*100, 1)
## [1] 94.8  4.0  1.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 3622.7, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.482197e-323
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 26.247684 14.515128  3.986448
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 6792.4, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03616484 0.04489775
## sample estimates:
##          p 
## 0.04030853
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.22
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.20 0.24
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 7668.4, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.009398471 0.014220596
## sample estimates:
##          p 
## 0.01157004
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.07
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.55 0.22 0.23
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_s2$Age_Stepped))

Observation
## [1] 3144  324   93
round(Observation/length(KODAP_data_complete_s2$Patient_ID)*100, 1)
## [1] 88.3  9.1  2.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1651.8, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.079779e-94
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.351106e-269
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 10.828332 14.515128  3.986448
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 2381.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.08184183 0.10102412
## sample estimates:
##          p 
## 0.09098568
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.41
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.37 0.46
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 3196.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02124020 0.03204086
## sample estimates:
##          p 
## 0.02611626
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.11
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.09 0.14
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.58 0.20 0.21
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_s3$Age_Stepped))

Observation
## [1] 2453  324   93
round(Observation/length(KODAP_data_complete_s3$Patient_ID)*100, 1)
## [1] 85.5 11.3  3.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 921.22, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.25446e-218
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 7.903984e-38
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 9.47345e-175
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1]  8.448441 14.515128  3.986448
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1718.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1016616 0.1251728
## sample estimates:
##        p 
## 0.112892
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.55
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.50 0.61
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 2508.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02636800 0.03972387
## sample estimates:
##          p 
## 0.03240418
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.15
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.12 0.19
#####################
#Any Mood Disorder#
#####################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.39 0.25 0.20 0.08 0.08
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                              "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                              "F32 Depressive Episode",
                                                                                                              "F33 Rezidivierende depressive Störung",
                                                                                                              "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                              "F34 Anhaltende affektive Störungen",
                                                                                                              "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung"),]

table(KODAP_data_complete_any_mood_disorder$Age_Stepped_2)
## 
##    0    1    2    3    4 
## 4464 1877 1686  179   38
round(table(KODAP_data_complete_any_mood_disorder$Age_Stepped_2)/length(KODAP_data_complete_any_mood_disorder$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 54.1 22.8 20.5  2.2  0.5
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.70 0.15 0.15
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                        "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                        "F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                        "F34 Anhaltende affektive Störungen",
                                                                                                        "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s1$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s1$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s1$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s1$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_any_mood_disorder$Patient_ID)
## [1] 4681
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_mood_disorder$Patient_ID)

Observation
## [1] 4464  179   38
round(Observation/length(KODAP_data_complete_any_mood_disorder$Patient_ID)*100,1)
## [1] 95.4  3.8  0.8
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1457.2, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.400025e-131
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.946114e-273
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.36417267 0.25987270 0.05278597
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 3990.5, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03301440 0.04423693
## sample estimates:
##          p 
## 0.03823969
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.22 0.30
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4528.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005830144 0.011245467
## sample estimates:
##           p 
## 0.008117924
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.07
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.60 0.20 0.21
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                              "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                              "F32 Depressive Episode",
                                                                                                              "F33 Rezidivierende depressive Störung",
                                                                                                              "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                              "F34 Anhaltende affektive Störungen",
                                                                                                              "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s2$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s2$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s2$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s2$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_any_mood_disorder$Patient_ID)
## [1] 2094
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_mood_disorder$Patient_ID)

Observation
## [1] 1877  179   38
round(Observation/length(KODAP_data_complete_any_mood_disorder$Patient_ID)*100,1)
## [1] 89.6  8.5  1.8
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 806.85, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.876927e-205
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.041346e-44
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.729488e-151
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.50235586 0.43342028 0.08803738
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1437.5, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.07402955 0.09848338
## sample estimates:
##          p 
## 0.08548233
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.43
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.38 0.50
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1942.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01304785 0.02508378
## sample estimates:
##          p 
## 0.01814709
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.09
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.12
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.55 0.22 0.23
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                              "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                              "F32 Depressive Episode",
                                                                                                              "F33 Rezidivierende depressive Störung",
                                                                                                              "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                              "F34 Anhaltende affektive Störungen",
                                                                                                              "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s3$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s3$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s3$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung") |
                                                                  KODAP_data_complete_s3$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                                "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                                "F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                                "F34 Anhaltende affektive Störungen",
                                                                                                                "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_any_mood_disorder$Patient_ID)
## [1] 1903
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_mood_disorder$Patient_ID)

Observation
## [1] 1686  179   38
round(Observation/length(KODAP_data_complete_any_mood_disorder$Patient_ID)*100,1)
## [1] 88.6  9.4  2.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 885.1, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.664017e-219
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.151518e-47
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.15282e-155
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.60616664 0.42901808 0.08714319
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1252.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.08150615 0.10828611
## sample estimates:
##          p 
## 0.09406201
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.43
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.37 0.49
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1752.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01436043 0.02759045
## sample estimates:
##          p 
## 0.01996847
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.09
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.12
###########################
#Major Depressive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.36 0.24 0.22 0.09 0.09
KODAP_data_complete_MDD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                               KODAP_data_complete$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                               KODAP_data_complete$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                               KODAP_data_complete$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                               KODAP_data_complete$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

table(KODAP_data_complete_MDD$Age_Stepped_2)
## 
##    0    1    2    3    4 
## 4050 1696 1515  163   36
round(table(KODAP_data_complete_MDD$Age_Stepped_2)/length(KODAP_data_complete_MDD$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 54.3 22.7 20.3  2.2  0.5
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.67 0.16 0.17
KODAP_data_complete_MDD <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                                              "F33 Rezidivierende depressive Störung",
                                                                                                              "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s1$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s1$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s1$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s1$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_MDD$Patient_ID)
## [1] 4249
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_MDD$Patient_ID)

Observation
## [1] 4050  163   36
round(Observation/length(KODAP_data_complete_MDD$Patient_ID)*100,1)
## [1] 95.3  3.8  0.8
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1523.7, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.227732e-139
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.112485e-273
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.41523310 0.24029538 0.05077941
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 3620.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03288231 0.04469253
## sample estimates:
##          p 
## 0.03836197
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.28
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4104.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006027553 0.011842989
## sample estimates:
##           p 
## 0.008472582
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.07
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.58 0.20 0.21
KODAP_data_complete_MDD <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                                              "F33 Rezidivierende depressive Störung",
                                                                                                              "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s2$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s2$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s2$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s2$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_MDD$Patient_ID)
## [1] 1895
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_MDD$Patient_ID)

Observation
## [1] 1696  163   36
round(Observation/length(KODAP_data_complete_MDD$Patient_ID)*100,1)
## [1] 89.5  8.6  1.9
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 789.29, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.244867e-200
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.355558e-44
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.743262e-142
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.54048719 0.41981902 0.08871648
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1297.4, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.07396834 0.09977324
## sample estimates:
##          p 
## 0.08601583
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.42
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.36 0.49
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1751.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01353163 0.02649171
## sample estimates:
##          p 
## 0.01899736
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.09
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.12
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.55 0.22 0.23
KODAP_data_complete_MDD <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                                              "F33 Rezidivierende depressive Störung",
                                                                                                              "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s3$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s3$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s3$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                                  KODAP_data_complete_s3$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                                                "F33 Rezidivierende depressive Störung",
                                                                                                                "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_MDD$Patient_ID)
## [1] 1714
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_MDD$Patient_ID)

Observation
## [1] 1515  163   36
round(Observation/length(KODAP_data_complete_MDD$Patient_ID)*100,1)
## [1] 88.4  9.5  2.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 791.75, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 6.137925e-196
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.023715e-41
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.627408e-138
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.60522031 0.43281648 0.09146312
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1122.4, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.08183082 0.11021635
## sample estimates:
##          p 
## 0.09509918
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.43
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.37 0.50
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1571.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01496409 0.02927602
## sample estimates:
##         p 
## 0.0210035
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.09
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.07 0.13
###########################
#Dysthymia#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.30 0.23 0.21 0.12 0.13
KODAP_data_complete_Dysthymia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD5_pre_clean %in% c("F34.1", "F34.10"),]

table(KODAP_data_complete_Dysthymia$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 508 224 203  22   1
round(table(KODAP_data_complete_Dysthymia$Age_Stepped_2)/length(KODAP_data_complete_Dysthymia$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 53.0 23.4 21.2  2.3  0.1
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.54 0.22 0.23
KODAP_data_complete_Dysthymia <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                          KODAP_data_complete_s1$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                          KODAP_data_complete_s1$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                          KODAP_data_complete_s1$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                          KODAP_data_complete_s1$ICD5_pre_clean %in% c("F34.1", "F34.10"),]

length(KODAP_data_complete_Dysthymia$Patient_ID)
## [1] 531
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Dysthymia$Patient_ID)

Observation
## [1] 508  22   1
round(Observation/length(KODAP_data_complete_Dysthymia$Patient_ID)*100,1)
## [1] 95.7  4.1  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 367.05, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.008176e-102
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.927829e-31
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.032302e-59
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.7597575 0.1856730 0.0080752
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 444.81, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02676267 0.06304503
## sample estimates:
##          p 
## 0.04143126
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.12 0.28
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 525.02, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0000983132 0.0121389098
## sample estimates:
##           p 
## 0.001883239
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.01
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.05
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.47 0.26 0.27
KODAP_data_complete_Dysthymia <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                          KODAP_data_complete_s2$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                          KODAP_data_complete_s2$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                          KODAP_data_complete_s2$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                          KODAP_data_complete_s2$ICD5_pre_clean %in% c("F34.1", "F34.10"),]

length(KODAP_data_complete_Dysthymia$Patient_ID)
## [1] 247
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Dysthymia$Patient_ID)

Observation
## [1] 224  22   1
round(Observation/length(KODAP_data_complete_Dysthymia$Patient_ID)*100,1)
## [1] 90.7  8.9  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 189.42, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 6.385702e-47
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.240164e-10
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.036754e-31
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.91423590 0.34614774 0.01505449
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 165.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.05791464 0.13350995
## sample estimates:
##          p 
## 0.08906883
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.35
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.23 0.52
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 241.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0002113729 0.0258703016
## sample estimates:
##           p 
## 0.004048583
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.0 0.1
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.46 0.27 0.28
KODAP_data_complete_Dysthymia <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                          KODAP_data_complete_s3$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                          KODAP_data_complete_s3$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                          KODAP_data_complete_s3$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                          KODAP_data_complete_s3$ICD5_pre_clean %in% c("F34.1", "F34.10"),]

length(KODAP_data_complete_Dysthymia$Patient_ID)
## [1] 226
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Dysthymia$Patient_ID)

Observation
## [1] 203  22   1
round(Observation/length(KODAP_data_complete_Dysthymia$Patient_ID)*100,1)
## [1] 89.8  9.7  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 180.87, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.775104e-44
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.603655e-09
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.64903e-30
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.9644403 0.3668026 0.0159528
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 144.96, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0633699 0.1455325
## sample estimates:
##          p 
## 0.09734513
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.37
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.24 0.55
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 220.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0002310174 0.0282316966
## sample estimates:
##           p 
## 0.004424779
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.0 0.1
###########################
#Any anxiety disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.29 0.24 0.28 0.10 0.10
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                          "F40.1 Soziale Phobie",
                                                                                          "F40.2 Spezifische Phobie",
                                                                                          "F41.1 Generalisierte Angststörung",
                                                                                          "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                            "F40.1 Soziale Phobie",
                                                                                            "F40.2 Spezifische Phobie",
                                                                                            "F41.1 Generalisierte Angststörung",
                                                                                            "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                            "F40.1 Soziale Phobie",
                                                                                            "F40.2 Spezifische Phobie",
                                                                                            "F41.1 Generalisierte Angststörung",
                                                                                            "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                            "F40.1 Soziale Phobie",
                                                                                            "F40.2 Spezifische Phobie",
                                                                                            "F41.1 Generalisierte Angststörung",
                                                                                            "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                            "F40.1 Soziale Phobie",
                                                                                            "F40.2 Spezifische Phobie",
                                                                                            "F41.1 Generalisierte Angststörung",
                                                                                            "F41.X F40.9 Andere phobische oder Angststörungen"),]

table(KODAP_data_complete_any_anx_disorder$Age_Stepped_2)
## 
##    0    1    2    3    4 
## 2725  929  664  106   29
round(table(KODAP_data_complete_any_anx_disorder$Age_Stepped_2)/length(KODAP_data_complete_any_anx_disorder$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 61.2 20.9 14.9  2.4  0.7
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.60 0.20 0.21
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                              "F40.1 Soziale Phobie",
                                                                                                              "F40.2 Spezifische Phobie",
                                                                                                              "F41.1 Generalisierte Angststörung",
                                                                                                              "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s1$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s1$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s1$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s1$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_any_anx_disorder$Patient_ID)
## [1] 2860
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_anx_disorder$Patient_ID)

Observation
## [1] 2725  106   29
round(Observation/length(KODAP_data_complete_any_anx_disorder$Patient_ID)*100,1)
## [1] 95.3  3.7  1.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1512, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 9.249153e-142
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.89704e-234
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.59656533 0.18798327 0.04920833
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 2449.9, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03057815 0.04482088
## sample estimates:
##          p 
## 0.03706294
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.16 0.23
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 2743.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006925647 0.014729790
## sample estimates:
##          p 
## 0.01013986
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.07
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.55 0.22 0.23
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                              "F40.1 Soziale Phobie",
                                                                                                              "F40.2 Spezifische Phobie",
                                                                                                              "F41.1 Generalisierte Angststörung",
                                                                                                              "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s2$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s2$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s2$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s2$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_any_anx_disorder$Patient_ID)
## [1] 1064
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_anx_disorder$Patient_ID)

Observation
## [1] 929 106  29
round(Observation/length(KODAP_data_complete_any_anx_disorder$Patient_ID)*100,1)
## [1] 87.3 10.0  2.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 454.82, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.933046e-112
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 6.070823e-24
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.89698e-78
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.5791805 0.4556972 0.1192878
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 680.64, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.08261257 0.11959103
## sample estimates:
##          p 
## 0.09962406
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.46
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.38 0.55
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 949.27, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01865566 0.03941759
## sample estimates:
##          p 
## 0.02725564
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.08 0.17
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.59 0.20 0.21
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                              "F40.1 Soziale Phobie",
                                                                                                              "F40.2 Spezifische Phobie",
                                                                                                              "F41.1 Generalisierte Angststörung",
                                                                                                              "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s3$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s3$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s3$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                                  KODAP_data_complete_s3$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                                "F40.1 Soziale Phobie",
                                                                                                                "F40.2 Spezifische Phobie",
                                                                                                                "F41.1 Generalisierte Angststörung",
                                                                                                                "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_any_anx_disorder$Patient_ID)
## [1] 799
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_anx_disorder$Patient_ID)

Observation
## [1] 664 106  29
round(Observation/length(KODAP_data_complete_any_anx_disorder$Patient_ID)*100,1)
## [1] 83.1 13.3  3.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 213.78, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.280559e-48
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.361843e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.612686e-45
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.4125144 0.6590868 0.1725290
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 429.78, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1103173 0.1586226
## sample estimates:
##         p 
## 0.1326658
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.66
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.55 0.79
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 685.36, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02487130 0.05236749
## sample estimates:
##          p 
## 0.03629537
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.12 0.25
###########################
#Panic Disorder/Agoraphobia#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.25 0.23 0.28 0.11 0.12
KODAP_data_complete_PanicAgora <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                              KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                              KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                              KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                              KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

table(KODAP_data_complete_PanicAgora$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 731 370 299  47   9
round(table(KODAP_data_complete_PanicAgora$Age_Stepped_2)/length(KODAP_data_complete_PanicAgora$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 50.2 25.4 20.5  3.2  0.6
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.52 0.23 0.24
KODAP_data_complete_PanicAgora <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s1$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s1$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s1$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s1$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_PanicAgora$Patient_ID)
## [1] 787
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PanicAgora$Patient_ID)

Observation
## [1] 731  47   9
round(Observation/length(KODAP_data_complete_PanicAgora$Patient_ID)*100,1)
## [1] 92.9  6.0  1.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 527.79, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.347813e-138
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.350377e-39
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.678958e-80
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.78164136 0.25516409 0.04675107
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 608.47, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.04465484 0.07922572
## sample estimates:
##          p 
## 0.05972046
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.19 0.34
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 749.46, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005591072 0.022412707
## sample estimates:
##          p 
## 0.01143583
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.02 0.09
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.50 0.25 0.26
KODAP_data_complete_PanicAgora <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s2$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s2$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s2$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s2$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_PanicAgora$Patient_ID)
## [1] 426
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PanicAgora$Patient_ID)

Observation
## [1] 370  47   9
round(Observation/length(KODAP_data_complete_PanicAgora$Patient_ID)*100,1)
## [1] 86.9 11.0  2.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 241, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.383763e-58
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 7.7275e-12
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.870622e-41
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.74360092 0.44959501 0.08237462
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 257.19, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0829624 0.1449315
## sample estimates:
##         p 
## 0.1103286
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.45
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.34 0.59
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 388.85, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01034477 0.04116685
## sample estimates:
##          p 
## 0.02112676
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.16
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.55 0.22 0.23
KODAP_data_complete_PanicAgora <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s3$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s3$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s3$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                                 KODAP_data_complete_s3$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_PanicAgora$Patient_ID)
## [1] 355
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PanicAgora$Patient_ID)

Observation
## [1] 299  47   9
round(Observation/length(KODAP_data_complete_PanicAgora$Patient_ID)*100,1)
## [1] 84.2 13.2  2.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 133.34, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.252067e-31
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.755743e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 9.071155e-28
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.5356883 0.5996379 0.1098654
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 190.42, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.09980566 0.17316442
## sample estimates:
##         p 
## 0.1323944
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.6
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.45 0.78
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 318.02, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0124220 0.0492759
## sample estimates:
##          p 
## 0.02535211
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.11
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.21
###########################
#Social phobia#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.43 0.27 0.23 0.03 0.04
KODAP_data_complete_socialphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                              KODAP_data_complete$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                              KODAP_data_complete$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                              KODAP_data_complete$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                              KODAP_data_complete$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]

table(KODAP_data_complete_socialphobia$Age_Stepped_2)
## 
##    0    1    2    3    4 
## 1407  295  128   16    4
round(table(KODAP_data_complete_socialphobia$Age_Stepped_2)/length(KODAP_data_complete_socialphobia$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 76.1 15.9  6.9  0.9  0.2
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.86 0.07 0.07
KODAP_data_complete_socialphobia <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s1$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s1$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s1$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s1$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]

length(KODAP_data_complete_socialphobia$Patient_ID)
## [1] 1427
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_socialphobia$Patient_ID)

Observation
## [1] 1407   16    4
round(Observation/length(KODAP_data_complete_socialphobia$Patient_ID)*100,1)
## [1] 98.6  1.1  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 195.58, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.003197e-67
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 6.415478e-26
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.729888e-40
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.15129299 0.15970153 0.03820115
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1361.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006646199 0.018566480
## sample estimates:
##          p 
## 0.01121233
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.16
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.09 0.26
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1409.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0008983203 0.0076840834
## sample estimates:
##           p 
## 0.002803083
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.10
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.79 0.10 0.11
KODAP_data_complete_socialphobia <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s2$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s2$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s2$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s2$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]

length(KODAP_data_complete_socialphobia$Patient_ID)
## [1] 315
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_socialphobia$Patient_ID)

Observation
## [1] 295  16   4
round(Observation/length(KODAP_data_complete_socialphobia$Patient_ID)*100,1)
## [1] 93.7  5.1  1.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 43.267, df = 2, p-value = 4.025e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.120926e-12
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.004490586
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.774582e-10
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1860866 0.4936742 0.1180885
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 252.46, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03028244 0.08281635
## sample estimates:
##          p 
## 0.05079365
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.49
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.29 0.80
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 297.26, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004075547 0.034417169
## sample estimates:
##          p 
## 0.01269841
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.32
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.77 0.11 0.12
KODAP_data_complete_socialphobia <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s3$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s3$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s3$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                           KODAP_data_complete_s3$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]

length(KODAP_data_complete_socialphobia$Patient_ID)
## [1] 148
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_socialphobia$Patient_ID)

Observation
## [1] 128  16   4
round(Observation/length(KODAP_data_complete_socialphobia$Patient_ID)*100,1)
## [1] 86.5 10.8  2.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 12.64, df = 2, p-value = 0.0018
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.01035804
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.692998
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.0003292119
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1302657 0.9415832 0.2252299
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 89.358, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.06500968 0.17233195
## sample estimates:
##         p 
## 0.1081081
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.94
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.57 1.50
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 130.55, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.008692983 0.072068030
## sample estimates:
##          p 
## 0.02702703
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.23
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.07 0.60
###########################
#Specific phobias#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.29 0.21 0.29 0.11 0.11
KODAP_data_complete_specificphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                              KODAP_data_complete$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                              KODAP_data_complete$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                              KODAP_data_complete$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                              KODAP_data_complete$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]

table(KODAP_data_complete_specificphobia$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 407 154 117  22   3
round(table(KODAP_data_complete_specificphobia$Age_Stepped_2)/length(KODAP_data_complete_specificphobia$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 57.9 21.9 16.6  3.1  0.4
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.57 0.21 0.22
KODAP_data_complete_specificphobia <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s1$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s1$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s1$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s1$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]

length(KODAP_data_complete_specificphobia$Patient_ID)
## [1] 432
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_specificphobia$Patient_ID)

Observation
## [1] 407  22   3
round(Observation/length(KODAP_data_complete_specificphobia$Patient_ID)*100,1)
## [1] 94.2  5.1  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 245.39, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 6.210734e-68
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.918618e-20
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.551319e-41
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.65100426 0.24257153 0.03164942
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 346.69, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03293848 0.07726170
## sample estimates:
##          p 
## 0.05092593
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.16 0.37
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 418.11, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001795201 0.021904551
## sample estimates:
##           p 
## 0.006944444
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.10
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.49 0.25 0.26
KODAP_data_complete_specificphobia <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s2$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s2$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s2$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s2$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]

length(KODAP_data_complete_specificphobia$Patient_ID)
## [1] 179
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_specificphobia$Patient_ID)

Observation
## [1] 154  22   3
round(Observation/length(KODAP_data_complete_specificphobia$Patient_ID)*100,1)
## [1] 86.0 12.3  1.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 102.78, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.562553e-25
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.000125186
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.342115e-19
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.75814235 0.49222397 0.06422271
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 100.31, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.08030065 0.18225298
## sample estimates:
##        p 
## 0.122905
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.49
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.32 0.73
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 165.27, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004338373 0.052111880
## sample estimates:
##          p 
## 0.01675978
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.02 0.20
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.57 0.21 0.22
KODAP_data_complete_specificphobia <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s3$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s3$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s3$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                             KODAP_data_complete_s3$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]

length(KODAP_data_complete_specificphobia$Patient_ID)
## [1] 142
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_specificphobia$Patient_ID)

Observation
## [1] 117  22   3
round(Observation/length(KODAP_data_complete_specificphobia$Patient_ID)*100,1)
## [1] 82.4 15.5  2.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 42.692, df = 2, p-value = 5.366e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.183246e-09
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.3660296
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.056432e-11
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.43633485 0.74316156 0.09696369
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 66.261, df = 1, p-value = 3.951e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1016958 0.2273905
## sample estimates:
##         p 
## 0.1549296
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.74
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.49 1.09
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 128.35, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005472072 0.065275438
## sample estimates:
##          p 
## 0.02112676
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##   p 
## 0.1
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.30
###########################
#Generalized Anxiety Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.36 0.20 0.28 0.08 0.08
KODAP_data_complete_GAD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

table(KODAP_data_complete_GAD$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 254 147 125  20   6
round(table(KODAP_data_complete_GAD$Age_Stepped_2)/length(KODAP_data_complete_GAD$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 46.0 26.6 22.6  3.6  1.1
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.70 0.15 0.15
KODAP_data_complete_GAD <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s1$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s1$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s1$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s1$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_GAD$Patient_ID)
## [1] 280
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_GAD$Patient_ID)

Observation
## [1] 254  20   6
round(Observation/length(KODAP_data_complete_GAD$Patient_ID)*100,1)
## [1] 90.7  7.1  2.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 60.904, df = 2, p-value = 5.955e-14
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.331386e-16
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.0004063366
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.57727e-13
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.3008649 0.4826548 0.1385432
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 204, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0452875 0.1098397
## sample estimates:
##          p 
## 0.07142857
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.48
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.31 0.74
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 254.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.008740337 0.048335768
## sample estimates:
##          p 
## 0.02142857
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.14
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.31
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.57 0.21 0.22
KODAP_data_complete_GAD <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s2$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s2$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s2$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s2$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_GAD$Patient_ID)
## [1] 173
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_GAD$Patient_ID)

Observation
## [1] 147  20   6
round(Observation/length(KODAP_data_complete_GAD$Patient_ID)*100,1)
## [1] 85.0 11.6  3.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 59.559, df = 2, p-value = 1.166e-13
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.14875e-15
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.004372577
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.60061e-11
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.5015559 0.5446321 0.1563334
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 100.72, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.07376618 0.17513438
## sample estimates:
##         p 
## 0.1156069
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.54
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.35 0.83
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 147.98, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01417609 0.07738998
## sample estimates:
##          p 
## 0.03468208
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.16
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.35
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.65 0.17 0.18
KODAP_data_complete_GAD <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s3$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s3$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s3$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                               KODAP_data_complete_s3$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_GAD$Patient_ID)
## [1] 151
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_GAD$Patient_ID)

Observation
## [1] 125  20   6
round(Observation/length(KODAP_data_complete_GAD$Patient_ID)*100,1)
## [1] 82.8 13.2  4.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 25.563, df = 2, p-value = 2.813e-06
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.065834e-06
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.7057972
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.094089e-06
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2790750 0.7677927 0.2203903
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 80.132, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0847233 0.1995018
## sample estimates:
##         p 
## 0.1324503
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.77
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.49 1.16
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 126.12, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01625461 0.08830168
## sample estimates:
##         p 
## 0.0397351
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.22
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.09 0.49
###########################
#Obsessive Compulsive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.50 0.24 0.18 0.04 0.04
KODAP_data_complete_OCD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F42.X Zwangsstörung") |
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

table(KODAP_data_complete_OCD$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 534 182  62  11   2
round(table(KODAP_data_complete_OCD$Age_Stepped_2)/length(KODAP_data_complete_OCD$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 67.5 23.0  7.8  1.4  0.3
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.86 0.07 0.07
KODAP_data_complete_OCD <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s1$ICD2_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s1$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s1$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s1$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_OCD$Patient_ID)
## [1] 547
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_OCD$Patient_ID)

Observation
## [1] 534  11   2
round(Observation/length(KODAP_data_complete_OCD$Patient_ID)*100,1)
## [1] 97.6  2.0  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 65.297, df = 2, p-value = 6.622e-15
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.530773e-21
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.686373e-07
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.145261e-15
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.14055714 0.28546037 0.04966043
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 501.97, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01060965 0.03680152
## sample estimates:
##          p 
## 0.02010969
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.29
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.15 0.52
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 537.05, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0006335564 0.0146333230
## sample estimates:
##           p 
## 0.003656307
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.20
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.73 0.13 0.14
KODAP_data_complete_OCD <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s2$ICD2_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s2$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s2$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s2$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_OCD$Patient_ID)
## [1] 195
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_OCD$Patient_ID)

Observation
## [1] 182  11   2
round(Observation/length(KODAP_data_complete_OCD$Patient_ID)*100,1)
## [1] 93.3  5.6  1.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 41.064, df = 2, p-value = 1.211e-09
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.902649e-12
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.003795649
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.130132e-09
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2699121 0.4352785 0.0757237
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 151.71, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02993022 0.10132864
## sample estimates:
##          p 
## 0.05641026
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.44
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.23 0.78
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 185.13, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00177848 0.04048321
## sample estimates:
##          p 
## 0.01025641
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.30
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.67 0.16 0.17
KODAP_data_complete_OCD <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s3$ICD2_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s3$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s3$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                    KODAP_data_complete_s3$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_OCD$Patient_ID)
## [1] 75
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_OCD$Patient_ID)

Observation
## [1] 62 11  2
round(Observation/length(KODAP_data_complete_OCD$Patient_ID)*100,1)
## [1] 82.7 14.7  2.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 11.431, df = 2, p-value = 0.003294
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.01295206
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.625811
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.000800487
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2253050 0.9219741 0.1603922
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 36.053, df = 1, p-value = 1.92e-09
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.07896563 0.25153073
## sample estimates:
##         p 
## 0.1466667
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.92
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.50 1.58
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 65.333, df = 1, p-value = 6.324e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004632317 0.101754797
## sample estimates:
##          p 
## 0.02666667
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.16
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.61
###########################
#Post Traumatic Stress Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.40 0.25 0.12 0.11 0.11
KODAP_data_complete_PTSD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

table(KODAP_data_complete_PTSD$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 628 244 180  13   2
round(table(KODAP_data_complete_PTSD$Age_Stepped_2)/length(KODAP_data_complete_PTSD$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 58.9 22.9 16.9  1.2  0.2
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.65 0.17 0.18
KODAP_data_complete_PTSD <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s1$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s1$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s1$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s1$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_PTSD$Patient_ID)
## [1] 643
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PTSD$Patient_ID)

Observation
## [1] 628  13   2
round(Observation/length(KODAP_data_complete_PTSD$Patient_ID)*100,1)
## [1] 97.7  2.0  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 300.67, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.084108e-94
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.422531e-34
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 9.524682e-51
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.50015662 0.11849146 0.01744219
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 590.13, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01127810 0.03524888
## sample estimates:
##          p 
## 0.02021773
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.07 0.21
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 633.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0005389344 0.0124629319
## sample estimates:
##          p 
## 0.00311042
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.07
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.54 0.22 0.23
KODAP_data_complete_PTSD <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s2$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s2$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s2$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s2$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_PTSD$Patient_ID)
## [1] 259
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PTSD$Patient_ID)

Observation
## [1] 244  13   2
round(Observation/length(KODAP_data_complete_PTSD$Patient_ID)*100,1)
## [1] 94.2  5.0  0.8
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 169.17, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.73753e-46
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.856052e-14
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.368796e-26
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.74262349 0.22345344 0.03289281
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 207.81, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02812696 0.08632672
## sample estimates:
##          p 
## 0.05019305
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.22
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.13 0.38
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 249.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001338641 0.030641781
## sample estimates:
##           p 
## 0.007722008
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.13
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.37 0.31 0.32
KODAP_data_complete_PTSD <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s3$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s3$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s3$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                    KODAP_data_complete_s3$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_PTSD$Patient_ID)
## [1] 195
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PTSD$Patient_ID)

Observation
## [1] 180  13   2
round(Observation/length(KODAP_data_complete_PTSD$Patient_ID)*100,1)
## [1] 92.3  6.7  1.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 262.45, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.045117e-60
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.594187e-16
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.13172e-29
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 2.52554367 0.21488036 0.03163084
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 144.74, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03745323 0.11380404
## sample estimates:
##          p 
## 0.06666667
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.21
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.12 0.37
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 185.13, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00177848 0.04048321
## sample estimates:
##          p 
## 0.01025641
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.12
###########################
#Any somatoform disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.30 0.25 0.29 0.08 0.08
KODAP_data_complete_Somatoform <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

table(KODAP_data_complete_Somatoform$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 400 262 347  48  17
round(table(KODAP_data_complete_Somatoform$Age_Stepped_2)/length(KODAP_data_complete_Somatoform$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 37.2 24.4 32.3  4.5  1.6
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.64 0.17 0.18
KODAP_data_complete_any_somatoform_disorder <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s1$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s1$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s1$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s1$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)
## [1] 465
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_somatoform_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)

Observation
## [1] 400  48  17
round(Observation/length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)*100,1)
## [1] 86.0 10.3  3.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 100.59, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.947645e-25
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.641975e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.861552e-21
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.3340877 0.5943375 0.2014040
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 291.23, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.07780415 0.13539946
## sample estimates:
##         p 
## 0.1032258
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.59
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.45 0.78
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 397.63, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02211704 0.05906098
## sample estimates:
##          p 
## 0.03655914
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##   p 
## 0.2
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.12 0.33
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.61 0.19 0.20
KODAP_data_complete_any_somatoform_disorder <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s2$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s2$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s2$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s2$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)
## [1] 327
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_somatoform_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)

Observation
## [1] 262  48  17
round(Observation/length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)*100,1)
## [1] 80.1 14.7  5.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 60.719, df = 2, p-value = 6.531e-14
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.110156e-13
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.1059443
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.011474e-14
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.3237986 0.7604814 0.2577054
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 161.77, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1111872 0.1909164
## sample estimates:
##        p 
## 0.146789
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.76
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.58 0.99
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 260.75, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03152128 0.08350049
## sample estimates:
##          p 
## 0.05198777
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.16 0.41
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.64 0.18 0.18
KODAP_data_complete_any_somatoform_disorder <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s3$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s3$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s3$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                     KODAP_data_complete_s3$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)
## [1] 412
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_somatoform_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)

Observation
## [1] 347  48  17
round(Observation/length(KODAP_data_complete_any_somatoform_disorder$Patient_ID)*100,1)
## [1] 84.2 11.7  4.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 80.254, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.060843e-19
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.003524904
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.518411e-17
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.3160686 0.6617851 0.2242601
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 240.84, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.08794403 0.15242546
## sample estimates:
##         p 
## 0.1165049
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.66
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.50 0.87
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 344.97, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02497916 0.06654107
## sample estimates:
##          p 
## 0.04126214
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.22
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.14 0.36
###########################
#Somatization disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.27 0.17 0.31 0.13 0.13
KODAP_data_complete_Somatization <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                       KODAP_data_complete$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                       KODAP_data_complete$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                       KODAP_data_complete$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                       KODAP_data_complete$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

table(KODAP_data_complete_Somatization$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 105  42  56   7   1
round(table(KODAP_data_complete_Somatization$Age_Stepped_2)/length(KODAP_data_complete_Somatization$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 49.8 19.9 26.5  3.3  0.5
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.51 0.24 0.25
KODAP_data_complete_Somatization <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s1$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                             KODAP_data_complete_s1$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s1$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s1$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_Somatization$Patient_ID)
## [1] 113
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatization$Patient_ID)

Observation
## [1] 105   7   1
round(Observation/length(KODAP_data_complete_Somatization$Patient_ID)*100,1)
## [1] 92.9  6.2  0.9
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 81.944, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.954247e-22
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.294859e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.540788e-13
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.83920676 0.25605270 0.03499924
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 84.991, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02741511 0.12796283
## sample estimates:
##         p 
## 0.0619469
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.53
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 107.08, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004621214 0.0554818076
## sample estimates:
##           p 
## 0.008849558
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.22
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.39 0.30 0.31
KODAP_data_complete_Somatization <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s2$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                             KODAP_data_complete_s2$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s2$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s2$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_Somatization$Patient_ID)
## [1] 50
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatization$Patient_ID)

Observation
## [1] 42  7  1
round(Observation/length(KODAP_data_complete_Somatization$Patient_ID)*100,1)
## [1] 84 14  2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 44.139, df = 2, p-value = 2.602e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.658583e-10
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.03871072
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.288633e-07
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 2.16183642 0.46826887 0.06400657
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 24.5, df = 1, p-value = 7.431e-07
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.06277009 0.27356376
## sample estimates:
##    p 
## 0.14
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.47
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.92
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 44.18, df = 1, p-value = 2.995e-11
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001044888 0.120108120
## sample estimates:
##    p 
## 0.02
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.38
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.54 0.23 0.24
KODAP_data_complete_Somatization <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s3$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                             KODAP_data_complete_s3$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s3$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                             KODAP_data_complete_s3$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_Somatization$Patient_ID)
## [1] 64
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatization$Patient_ID)

Observation
## [1] 56  7  1
round(Observation/length(KODAP_data_complete_Somatization$Patient_ID)*100,1)
## [1] 87.5 10.9  1.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 30.466, df = 2, p-value = 2.424e-07
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.360789e-08
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.07319309
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.035337e-06
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.6251259 0.4846108 0.0662403
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 37.516, df = 1, p-value = 9.068e-10
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.04878566 0.21841190
## sample estimates:
##        p 
## 0.109375
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.48
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.22 0.97
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 58.141, df = 1, p-value = 2.44e-14
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0008161675 0.0954135903
## sample estimates:
##        p 
## 0.015625
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.0 0.4
###########################
#Pain disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.31 0.28 0.27 0.07 0.07
KODAP_data_complete_Pain <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                          KODAP_data_complete$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                          KODAP_data_complete$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                          KODAP_data_complete$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                          KODAP_data_complete$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

table(KODAP_data_complete_Pain$Age_Stepped_2)
## 
##   0   1   2   3   4 
##  99 125 241  30  13
round(table(KODAP_data_complete_Pain$Age_Stepped_2)/length(KODAP_data_complete_Pain$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 19.5 24.6 47.4  5.9  2.6
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.69 0.15 0.16
KODAP_data_complete_Pain <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s1$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                             KODAP_data_complete_s1$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s1$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s1$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_Pain$Patient_ID)
## [1] 142
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Pain$Patient_ID)

Observation
## [1] 99 30 13
round(Observation/length(KODAP_data_complete_Pain$Patient_ID)*100,1)
## [1] 69.7 21.1  9.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 7.4268, df = 2, p-value = 0.02439
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.1351746
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.1097324
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0044328 1.4124871 0.5856443
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 46.204, df = 1, p-value = 1.065e-11
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1491534 0.2893742
## sample estimates:
##         p 
## 0.2112676
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 1.41
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 1.00 1.93
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 93.134, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.05163224 0.15451986
## sample estimates:
##         p 
## 0.0915493
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.59
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.33 0.99
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.67 0.16 0.17
KODAP_data_complete_Pain <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s2$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                             KODAP_data_complete_s2$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s2$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s2$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_Pain$Patient_ID)
## [1] 168
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Pain$Patient_ID)

Observation
## [1] 125  30  13
round(Observation/length(KODAP_data_complete_Pain$Patient_ID)*100,1)
## [1] 74.4 17.9  7.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 10.157, df = 2, p-value = 0.00623
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.1207022
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.594905
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.00269835
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1137879 1.1001194 0.4561306
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 68.149, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1255001 0.2467382
## sample estimates:
##         p 
## 0.1785714
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 1.1
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.77 1.52
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 118.34, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.04354488 0.13145155
## sample estimates:
##          p 
## 0.07738095
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.46
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.26 0.77
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.66 0.17 0.17
KODAP_data_complete_Pain <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s3$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                             KODAP_data_complete_s3$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s3$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                             KODAP_data_complete_s3$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_Pain$Patient_ID)
## [1] 284
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Pain$Patient_ID)

Observation
## [1] 241  30  13
round(Observation/length(KODAP_data_complete_Pain$Patient_ID)*100,1)
## [1] 84.9 10.6  4.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 48.214, df = 2, p-value = 3.391e-11
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.184466e-12
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.01537115
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.042024e-10
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2850833 0.6360339 0.2637119
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 175.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.07351973 0.14878350
## sample estimates:
##         p 
## 0.1056338
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.64
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.44 0.90
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 232.57, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02563367 0.07888574
## sample estimates:
##          p 
## 0.04577465
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.15 0.45
###########################
#Eating disorders#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.57 0.12 0.20 0.05 0.06
KODAP_data_complete_Eating <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F50.X Essstörung") |
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F50.X Essstörung"),]

table(KODAP_data_complete_Eating$Age_Stepped_2)
## 
##   0   1   2   3 
## 592 168  89   8
round(table(KODAP_data_complete_Eating$Age_Stepped_2)/length(KODAP_data_complete_Eating$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3 
## 69.1 19.6 10.4  0.9
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.84 0.08 0.08
KODAP_data_complete_Eating <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s1$ICD2_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s1$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s1$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s1$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_Eating$Patient_ID)
## [1] 600
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Eating$Patient_ID)

Observation
## [1] 592   8
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_Eating$Patient_ID)*100,1)
## [1] 98.7  1.3  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 97.384, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.296144e-34
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.250234e-12
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.383093e-22
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1757212 0.1695813 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 566.48, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006212746 0.027177067
## sample estimates:
##          p 
## 0.01333333
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.08 0.35
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 598, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000000000 0.007930758
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.0 0.1
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.51 0.24 0.25
KODAP_data_complete_Eating <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s2$ICD2_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s2$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s2$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s2$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_Eating$Patient_ID)
## [1] 176
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Eating$Patient_ID)

Observation
## [1] 168   8
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_Eating$Patient_ID)*100,1)
## [1] 95.5  4.5  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 137.31, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.306154e-38
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.277145e-11
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.446139e-22
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.8557976 0.1914184 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 143.64, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02128851 0.09072927
## sample estimates:
##          p 
## 0.04545455
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.09 0.38
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 174.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.02662404
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.11
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.64 0.17 0.18
KODAP_data_complete_Eating <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s3$ICD2_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s3$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s3$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                                        KODAP_data_complete_s3$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_Eating$Patient_ID)
## [1] 97
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Eating$Patient_ID)

Observation
## [1] 89  8
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_Eating$Patient_ID)*100,1)
## [1] 91.8  8.2  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 33.461, df = 2, p-value = 5.419e-08
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.289087e-09
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.04589785
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.866035e-08
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.4231861 0.4747262 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 65.979, df = 1, p-value = 4.557e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03885986 0.16071710
## sample estimates:
##          p 
## 0.08247423
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.47
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.22 0.93
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 95.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.04747222
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.26
###########################
#Substance use disorders#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.37 0.24 0.27 0.06 0.06
KODAP_data_complete_SubstanceUse <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                    KODAP_data_complete$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                    KODAP_data_complete$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                    KODAP_data_complete$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                    KODAP_data_complete$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

table(KODAP_data_complete_SubstanceUse$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 400 217 131  15   1
round(table(KODAP_data_complete_SubstanceUse$Age_Stepped_2)/length(KODAP_data_complete_SubstanceUse$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 52.4 28.4 17.1  2.0  0.1
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.75 0.12 0.13
KODAP_data_complete_SubstanceUse <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s1$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s1$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s1$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s1$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_SubstanceUse$Patient_ID)
## [1] 416
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_SubstanceUse$Patient_ID)

Observation
## [1] 400  15   1
round(Observation/length(KODAP_data_complete_SubstanceUse$Patient_ID)*100,1)
## [1] 96.2  3.6  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 99.23, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.068019e-30
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.153776e-09
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.494718e-22
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.27682976 0.29863526 0.01904922
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 356.31, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02105849 0.06009847
## sample estimates:
##          p 
## 0.03605769
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.3
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.17 0.50
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 410.02, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0001254939 0.0154621692
## sample estimates:
##           p 
## 0.002403846
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.12
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.67 0.16 0.17
KODAP_data_complete_SubstanceUse <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s2$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s2$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s2$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s2$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_SubstanceUse$Patient_ID)
## [1] 233
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_SubstanceUse$Patient_ID)

Observation
## [1] 217  15   1
round(Observation/length(KODAP_data_complete_SubstanceUse$Patient_ID)*100,1)
## [1] 93.1  6.4  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 76.115, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.231021e-21
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.297435e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.881608e-17
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.39707901 0.39493620 0.02519202
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 175.12, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03775731 0.10606033
## sample estimates:
##          p 
## 0.06437768
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.39
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.23 0.65
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 227.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0002240757 0.0273980814
## sample estimates:
##           p 
## 0.004291845
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.16
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.70 0.15 0.16
KODAP_data_complete_SubstanceUse <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s3$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s3$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s3$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                       KODAP_data_complete_s3$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_SubstanceUse$Patient_ID)
## [1] 147
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_SubstanceUse$Patient_ID)

Observation
## [1] 131  15   1
round(Observation/length(KODAP_data_complete_SubstanceUse$Patient_ID)*100,1)
## [1] 89.1 10.2  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 31.234, df = 2, p-value = 1.651e-07
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.705711e-08
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.3932277
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.312169e-09
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.28182632 0.68472367 0.04367686
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 91.537, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.06019127 0.16552137
## sample estimates:
##         p 
## 0.1020408
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.68
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.40 1.11
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 141.06, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0003552053 0.0429952284
## sample estimates:
##           p 
## 0.006802721
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.28
###########################
#Psychotic disorders#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])
prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

expected_ratio_1834 <- (census_amount_1834*prevalence_1834)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_3549 <- (census_amount_3549*prevalence_3549)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_5064 <- (census_amount_5064*prevalence_5064)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1834)+(census_amount_3549*prevalence_3549)+(census_amount_5064*prevalence_5064)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

round(c(expected_ratio_1834,expected_ratio_3549,expected_ratio_5064,expected_ratio_6574,expected_ratio_75plus), 2)
## [1] 0.40 0.19 0.27 0.07 0.07
KODAP_data_complete_Psychotic <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                   "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                   "F25.X Schizoaffektive Störungen") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                     "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                     "F25.X Schizoaffektive Störungen") |
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                     "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                     "F25.X Schizoaffektive Störungen") |
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                     "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                     "F25.X Schizoaffektive Störungen") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                     "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                     "F25.X Schizoaffektive Störungen"),]

table(KODAP_data_complete_Psychotic$Age_Stepped_2)
## 
##   0   1   2   3   4 
## 161 121  49   4   1
round(table(KODAP_data_complete_Psychotic$Age_Stepped_2)/length(KODAP_data_complete_Psychotic$Age_Stepped_2)*100, 1)
## 
##    0    1    2    3    4 
## 47.9 36.0 14.6  1.2  0.3
#18-34 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- prevalence_1834

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1834*prevalence_1864)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1834*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.75 0.12 0.13
KODAP_data_complete_Psychotic <- KODAP_data_complete_s1[KODAP_data_complete_s1$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                         "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                         "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s1$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s1$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s1$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s1$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_Psychotic$Patient_ID)
## [1] 166
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Psychotic$Patient_ID)

Observation
## [1] 161   4   1
round(Observation/length(KODAP_data_complete_Psychotic$Patient_ID)*100,1)
## [1] 97.0  2.4  0.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 44.222, df = 2, p-value = 2.496e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.89182e-14
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.61255e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.338643e-08
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.30062692 0.19378954 0.04635511
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 148.49, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.007746952 0.064467251
## sample estimates:
##          p 
## 0.02409639
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.52
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 160.05, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0003145389 0.0381918572
## sample estimates:
##           p 
## 0.006024096
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.29
#35-49 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- prevalence_3549

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_3549*prevalence_1864)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_3549*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.59 0.20 0.21
KODAP_data_complete_Psychotic <- KODAP_data_complete_s2[KODAP_data_complete_s2$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                         "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                         "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s2$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s2$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s2$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s2$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_Psychotic$Patient_ID)
## [1] 126
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Psychotic$Patient_ID)

Observation
## [1] 121   4   1
round(Observation/length(KODAP_data_complete_Psychotic$Patient_ID)*100,1)
## [1] 96.0  3.2  0.8
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 71.905, df = 2, p-value = 2.433e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.042486e-21
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.22867e-07
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.955996e-11
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.63004042 0.15802099 0.03779915
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 108.64, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01021808 0.08420081
## sample estimates:
##          p 
## 0.03174603
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.16
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.05 0.42
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 120.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004144261 0.0499367756
## sample estimates:
##           p 
## 0.007936508
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.24
#50-64 years
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_5064*prevalence_1864)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_5064*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.67 0.16 0.17
KODAP_data_complete_Psychotic <- KODAP_data_complete_s3[KODAP_data_complete_s3$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                         "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                         "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s3$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s3$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s3$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen") |
                                                             KODAP_data_complete_s3$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                           "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                           "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_Psychotic$Patient_ID)
## [1] 54
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Psychotic$Patient_ID)

Observation
## [1] 49  4  1
round(Observation/length(KODAP_data_complete_Psychotic$Patient_ID)*100,1)
## [1] 90.7  7.4  1.9
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 14.684, df = 2, p-value = 0.0006476
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.0001932065
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.2852669
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.002563779
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.3624838 0.4535607 0.1084933
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 37.5, df = 1, p-value = 9.141e-10
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02399672 0.18741913
## sample estimates:
##          p 
## 0.07407407
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.45
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.15 1.15
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 48.167, df = 1, p-value = 3.915e-12
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0009674283 0.1118384229
## sample estimates:
##          p 
## 0.01851852
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.11
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.66

Gender-specific analyses

#Only include female patients
KODAP_data_complete_female <- subset(KODAP_data_complete, Pat_Geschlecht == 2)
#Read prevalence estimates
Prevalence_estimates <- read_excel("Prevalence_estimates_female.xlsx")
Prevalence_estimates
## # A tibble: 17 × 6
##    Diagnosis                  `1834y` `3549y` `5064y` `6574y` `75yplus`
##    <chr>                        <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1 Any mental disorder           41.8    34.9    31.6    24.4      24.4
##  2 Any mood disorder             20.7    14.8     7.8     8.4       8.4
##  3 Major Depressive Disorder     15.1    10.6     5.8     6.2       6.2
##  4 Dysthymia                      2.4     2.4     1.4     2.3       2.3
##  5 Any anxiety disorder          26.5    21.8    21.4    14.9      14.9
##  6 Panic disorder/Agoraphobia     6.1     5.5     6       4.8       4.8
##  7 Social phobia                  6.8     4.2     2.5     0.7       0.7
##  8 Specific phobias              19.9    13.8    16.7    11.3      11.3
##  9 GAD                            4.7     2.3     2.8     2.1       2.1
## 10 OCD                            7.4     4.2     2.2     2         2  
## 11 PTSD                           6.1     3.8     1.7     2.9       2.9
## 12 Any somatoform disorder        6.5     6.4     4.6     3         3  
## 13 Somatization disorder          1.3     1.2     0.3     1.1       1.1
## 14 Pain disorder                  6.4     6.3     4.5     2.5       2.5
## 15 Eating disorders               3.6     0.8     0.9     0.3       0.3
## 16 Substance use disorders        3.9     4.4     3       2.6       2.6
## 17 Psychotic disorders            5.6     2.6     2.4     1.6       1.6
##################
#####Analyses#####
##################

#####################
#Any Mental Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.78 0.10 0.12
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female$Patient_ID)

Observation
## [1] 8447  224   67
round(Observation/length(KODAP_data_complete_female$Patient_ID)*100, 1)
## [1] 96.7  2.6  0.8
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1751.8, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.801795e-162
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.23451433 0.25661306 0.06551135
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 7863.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02247042 0.02922398
## sample estimates:
##          p 
## 0.02563516
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.22 0.29
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 8470.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005992240 0.009789213
## sample estimates:
##           p 
## 0.007667659
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.08
#####################
#Any Mood Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.80 0.09 0.11
KODAP_data_complete_female_any_mood_disorder <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                        "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                        "F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                        "F34 Anhaltende affektive Störungen",
                                                                                                        "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_female$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_female$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_female$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_female$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_female_any_mood_disorder$Patient_ID)
## [1] 5241
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_any_mood_disorder$Patient_ID)

Observation
## [1] 5093  121   27
round(Observation/length(KODAP_data_complete_female_any_mood_disorder$Patient_ID)*100,1)
## [1] 97.2  2.3  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 946.48, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 6.439132e-296
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.306085e-88
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.910448e-207
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.20856048 0.25588457 0.04873416
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 4766.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01926997 0.02761936
## sample estimates:
##         p 
## 0.0230872
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.31
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 5131.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003465327 0.007597990
## sample estimates:
##           p 
## 0.005151689
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.07
###########################
#Major Depressive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.80 0.09 0.11
KODAP_data_complete_female_MDD <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete_female$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete_female$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete_female$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert")| 
                                                 KODAP_data_complete_female$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_female_MDD$Patient_ID)
## [1] 4773
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_MDD$Patient_ID)

Observation
## [1] 4639  109   25
round(Observation/length(KODAP_data_complete_female_MDD$Patient_ID)*100,1)
## [1] 97.2  2.3  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 877.96, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.436099e-275
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.104863e-82
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.757643e-190
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.21199486 0.25037097 0.04901269
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 4345, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01887173 0.02758819
## sample estimates:
##          p 
## 0.02283679
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.30
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4671.5, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003465793 0.007845503
## sample estimates:
##           p 
## 0.005237796
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.07
###########################
#########Dysthymia#########
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.68 0.15 0.17
KODAP_data_complete_female_Dysthymia <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete_female$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete_female$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete_female$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete_female$ICD5_pre_clean %in% c("F34.1", "F34.10"),]


length(KODAP_data_complete_female_Dysthymia$Patient_ID)
## [1] 549
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_Dysthymia$Patient_ID)

Observation
## [1] 528  20   1
round(Observation/length(KODAP_data_complete_female_Dysthymia$Patient_ID)*100,1)
## [1] 96.2  3.6  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 198.83, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.743738e-60
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.54765e-16
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.266975e-42
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.40685683 0.25005007 0.01067107
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 470.06, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02298407 0.05668338
## sample estimates:
##          p 
## 0.03642987
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.16 0.39
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 543.02, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  9.508956e-05 1.174384e-02
## sample estimates:
##           p 
## 0.001821494
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.01
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.07
###########################
##Any anxiety disorder#####
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.79 0.10 0.11
KODAP_data_complete_female_any_anx_disorder <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                       "F40.1 Soziale Phobie",
                                                                                                       "F40.2 Spezifische Phobie",
                                                                                                       "F41.1 Generalisierte Angststörung",
                                                                                                       "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete_female$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete_female$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete_female$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete_female$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_female_any_anx_disorder$Patient_ID)
## [1] 2874
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_any_anx_disorder$Patient_ID)

Observation
## [1] 2784   69   21
round(Observation/length(KODAP_data_complete_female_any_anx_disorder$Patient_ID)*100,1)
## [1] 96.9  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 548.22, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.150625e-170
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.655144e-52
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.646734e-114
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.22253012 0.25109478 0.06522572
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 2602.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01886099 0.03046568
## sample estimates:
##          p 
## 0.02400835
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.20 0.32
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 2788.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004646579 0.011354394
## sample estimates:
##           p 
## 0.007306889
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.10
#################################
##Panic Disorder/Agoraphobia#####
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.75 0.11 0.13
KODAP_data_complete_female_PanicAgora <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete_female$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete_female$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete_female$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete_female$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_female_PanicAgora$Patient_ID)
## [1] 957
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_PanicAgora$Patient_ID)

Observation
## [1] 923  29   5
round(Observation/length(KODAP_data_complete_female_PanicAgora$Patient_ID)*100,1)
## [1] 96.4  3.0  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 236.84, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.483517e-73
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 7.073749e-21
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.243387e-51
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.28493785 0.26385897 0.03882888
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 842.64, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02074943 0.04379001
## sample estimates:
##          p 
## 0.03030303
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.18 0.38
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 935.13, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001925064 0.012895172
## sample estimates:
##          p 
## 0.00522466
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.10
#################################
##########Social phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.94 0.03 0.03
KODAP_data_complete_female_socialphobia <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete_female$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete_female$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete_female$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete_female$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]


length(KODAP_data_complete_female_socialphobia$Patient_ID)
## [1] 1105
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_socialphobia$Patient_ID)

Observation
## [1] 1094    9    2
round(Observation/length(KODAP_data_complete_female_socialphobia$Patient_ID)*100,1)
## [1] 99.0  0.8  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 51.227, df = 2, p-value = 7.519e-12
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.541934e-17
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.878888e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.71703e-13
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.05439582 0.28981209 0.05496864
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1067.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003980008 0.015994121
## sample estimates:
##           p 
## 0.008144796
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.29
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.14 0.57
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1095, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000313562 0.007272171
## sample estimates:
##           p 
## 0.001809955
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.22
#################################
##########Specific phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.79 0.10 0.12
KODAP_data_complete_female_specificphobia <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete_female$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete_female$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete_female$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete_female$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]


length(KODAP_data_complete_female_specificphobia$Patient_ID)
## [1] 512
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_specificphobia$Patient_ID)

Observation
## [1] 492  17   3
round(Observation/length(KODAP_data_complete_female_specificphobia$Patient_ID)*100,1)
## [1] 96.1  3.3  0.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 95.833, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.216676e-29
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.609206e-08
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.314941e-22
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.22351522 0.33598159 0.05060563
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 444.39, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02007705 0.05370694
## sample estimates:
##          p 
## 0.03320312
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.34
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.20 0.54
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 498.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001514477 0.018511450
## sample estimates:
##           p 
## 0.005859375
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.16
################################################
##########Generalized Anxiety Disorder##########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.79 0.10 0.11
KODAP_data_complete_female_GAD <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete_female$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") | 
                                                 KODAP_data_complete_female$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete_female$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete_female$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_female_GAD$Patient_ID)
## [1] 399
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_GAD$Patient_ID)

Observation
## [1] 379  14   6
round(Observation/length(KODAP_data_complete_female_GAD$Patient_ID)*100,1)
## [1] 95.0  3.5  1.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 61.597, df = 2, p-value = 4.212e-14
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.65055e-18
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.592153e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.805281e-13
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1996566 0.3659621 0.1338660
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 343.11, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02007309 0.05956381
## sample estimates:
##          p 
## 0.03508772
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.37
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.62
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 373.42, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006127367 0.034097737
## sample estimates:
##          p 
## 0.01503759
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.13
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.30
################################################
##########Obsessive compulsive disorders########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.85 0.07 0.08
KODAP_data_complete_female_OCD <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete_female$ICD2_pre_recode %in% c("F42.X Zwangsstörung") | 
                                                 KODAP_data_complete_female$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete_female$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete_female$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_female_OCD$Patient_ID)
## [1] 478
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_OCD$Patient_ID)

Observation
## [1] 469   7   2
round(Observation/length(KODAP_data_complete_female_OCD$Patient_ID)*100,1)
## [1] 98.1  1.5  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 67.693, df = 2, p-value = 1.998e-15
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 8.995839e-23
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.876961e-08
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.09016e-15
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.16023445 0.20606077 0.05025026
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 448.47, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0064319 0.0313004
## sample estimates:
##          p 
## 0.01464435
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.21
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.09 0.44
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 468.05, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0007250526 0.0167270175
## sample estimates:
##         p 
## 0.0041841
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.20
################################################
##########Post traumatic stress disorder########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.76 0.11 0.13
KODAP_data_complete_female_PTSD <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete_female$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete_female$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete_female$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete_female$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_female_PTSD$Patient_ID)
## [1] 826
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_PTSD$Patient_ID)

Observation
## [1] 815   9   2
round(Observation/length(KODAP_data_complete_female_PTSD$Patient_ID)*100,1)
## [1] 98.7  1.1  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 233.05, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.619396e-79
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.667058e-29
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.731105e-46
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.29831656 0.09857881 0.01869744
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 788.44, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005326636 0.021361366
## sample estimates:
##          p 
## 0.01089588
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.1
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.05 0.19
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 816.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004195024 0.0097159245
## sample estimates:
##           p 
## 0.002421308
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.08
################################################
##########Any somatoform Disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.82 0.08 0.09
KODAP_data_complete_female_Somatoform <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete_female$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") | 
                                                        KODAP_data_complete_female$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete_female$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete_female$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_female_Somatoform$Patient_ID)
## [1] 717
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_Somatoform$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_Somatoform$Patient_ID)

Observation
## [1] 668  38  11
round(Observation/length(KODAP_data_complete_female_Somatoform$Patient_ID)*100,1)
## [1] 93.2  5.3  1.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 64.208, df = 2, p-value = 1.141e-14
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.898274e-16
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.01463704
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.874491e-18
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1294049 0.6573448 0.1624102
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 571.27, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03826044 0.07270099
## sample estimates:
##          p 
## 0.05299861
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.66
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.47 0.90
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 671.74, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.008088178 0.028144685
## sample estimates:
##         p 
## 0.0153417
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.16
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.09 0.30
################################################
##########Somatization disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.67 0.15 0.18
KODAP_data_complete_female_Somatization <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete_female$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                          KODAP_data_complete_female$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete_female$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete_female$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_female_Somatization$Patient_ID)
## [1] 138
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_Somatization$Patient_ID)

Observation
## [1] 134   4
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_female_Somatization$Patient_ID)*100,1)
## [1] 97.1  2.9  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 58.174, df = 2, p-value = 2.332e-13
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.878195e-18
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.412681e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.220013e-12
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.4583628 0.1883615 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 120.59, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.009325662 0.077119213
## sample estimates:
##          p 
## 0.02898551
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.50
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 136.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.03375451
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.19
################################################
##########Pain disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.85 0.07 0.08
KODAP_data_complete_female_Pain <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete_female$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                  KODAP_data_complete_female$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete_female$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete_female$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_female_Pain$Patient_ID)
## [1] 373
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_Pain$Patient_ID)

Observation
## [1] 336  28   9
round(Observation/length(KODAP_data_complete_female_Pain$Patient_ID)*100,1)
## [1] 90.1  7.5  2.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 16.713, df = 2, p-value = 0.0002349
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.00929641
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.055383
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.367474e-05
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0629533 1.0686467 0.2931768
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 267.71, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.05133188 0.10789203
## sample estimates:
##          p 
## 0.07506702
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 1.07
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.73 1.54
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 335.97, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01182026 0.04693220
## sample estimates:
##          p 
## 0.02412869
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.29
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.14 0.57
################################################
##########Eating disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.93 0.03 0.04
KODAP_data_complete_female_Eating <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete_female$ICD2_pre_recode %in% c("F50.X Essstörung") | 
                                                    KODAP_data_complete_female$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete_female$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete_female$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_female_Eating$Patient_ID)
## [1] 760
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_Eating$Patient_ID)

Observation
## [1] 754   6
Observation[3] <- 0
Observation
## [1] 754   6   0
round(Observation/length(KODAP_data_complete_female_Eating$Patient_ID)*100,1)
## [1] 99.2  0.8  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 42.522, df = 2, p-value = 5.84e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 6.40344e-15
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.0001407409
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.997406e-12
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0622881 0.2594979 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 734.22, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003213246 0.018006521
## sample estimates:
##           p 
## 0.007894737
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.59
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 758, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000000000 0.006269615
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.18
#########################################
##########Substance-use disorders########
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.78 0.10 0.12
KODAP_data_complete_female_SubstanceUse <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete_female$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") | 
                                                          KODAP_data_complete_female$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete_female$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete_female$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_female_SubstanceUse$Patient_ID)
## [1] 322
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_SubstanceUse$Patient_ID)

Observation
## [1] 317   5
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_female_SubstanceUse$Patient_ID)*100,1)
## [1] 98.4  1.6  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 79.775, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.083161e-27
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.848148e-09
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.630713e-18
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2650321 0.1520459 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 300.38, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005730545 0.037951171
## sample estimates:
##          p 
## 0.01552795
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.15
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.37
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 320, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.01469619
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.12
#########################################
##########Psychotic disorders############
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- (female_amount_1834/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_1834+
  (female_amount_3549/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_3549+
  (female_amount_5064/sum(female_amount_1834,female_amount_3549, female_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (female_amount_1864*prevalence_1864)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (female_amount_6574*prevalence_6574)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (female_amount_75plus*prevalence_75plus)/((female_amount_1864*prevalence_1864)+(female_amount_6574*prevalence_6574)+(female_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.84 0.07 0.09
KODAP_data_complete_female_Psychotic <- KODAP_data_complete_female[KODAP_data_complete_female$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete_female$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") | 
                                                       KODAP_data_complete_female$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete_female$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete_female$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_female_Psychotic$Patient_ID)
## [1] 153
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_female_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_female_Psychotic$Patient_ID)

Observation
## [1] 153
Observation[2] <- 0
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_female_Psychotic$Patient_ID)*100,1)
## [1] 100   0   0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 28.661, df = 2, p-value = 5.976e-07
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.940281e-11
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.562047e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.860853e-06
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.187324 0.000000 0.000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 151.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.03052722
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.00 0.42
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 151.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.03052722
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.36
#only include male patients
KODAP_data_complete_male <- subset(KODAP_data_complete, Pat_Geschlecht == 1)
#Read prevalence estimates
Prevalence_estimates <- read_excel("Prevalence_estimates_male.xlsx")
Prevalence_estimates
## # A tibble: 17 × 6
##    Diagnosis                  `1834y` `3549y` `5064y` `6574y` `75yplus`
##    <chr>                        <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1 Any mental disorder           30      21.2    21.2    14.3      14.3
##  2 Any mood disorder              9.6     5.9     6.3     3         3  
##  3 Major Depressive Disorder      4.9     3.9     4.6     2.3       2.3
##  4 Dysthymia                      1.9     0.9     1.2     0.8       0.8
##  5 Any anxiety disorder           9.9    10.7     9.2     6.7       6.7
##  6 Panic disorder/Agoraphobia     2.2     2.7     2.3     2         2  
##  7 Social phobia                  2.5     2.1     1.8     0.7       0.7
##  8 Specific phobias               4.9     5.3     5.1     5.1       5.1
##  9 GAD                            1.9     1.7     1.8     0.4       0.4
## 10 OCD                            7.1     3       2.1     0.2       0.2
## 11 PTSD                           1.4     1.3     0.3     0.6       0.6
## 12 Any somatoform disorder        2       1.2     2.7     1         1  
## 13 Somatization disorder          0.5     0.1     1.5     0.5       0.5
## 14 Pain disorder                  1.5     1.2     1.6     0.6       0.6
## 15 Eating disorders               1       0.1     0.4     0.5       0.5
## 16 Substance use disorders       12.8     7.5     8       2.4       2.4
## 17 Psychotic disorders            2.9     1.8     2.5     1         1
##################
#####Analyses#####
##################

#####################
#Any Mental Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.84 0.08 0.07
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male$Patient_ID)

Observation
## [1] 4741  100   26
round(Observation/length(KODAP_data_complete_male$Patient_ID)*100, 1)
## [1] 97.4  2.1  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 628.67, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.773893e-197
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.025387e-74
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.354386e-121
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.15347769 0.25153988 0.07236907
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 4473.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01682993 0.02503880
## sample estimates:
##          p 
## 0.02054654
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.21 0.31
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4761.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003564934 0.007937938
## sample estimates:
##         p 
## 0.0053421
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.11
#####################
#Any Mood Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.89 0.06 0.05
KODAP_data_complete_male_any_mood_disorder <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                        "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                        "F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                        "F34 Anhaltende affektive Störungen",
                                                                                                        "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_male$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_male$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_male$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete_male$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_male_any_mood_disorder$Patient_ID)
## [1] 2984
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_any_mood_disorder$Patient_ID)

Observation
## [1] 2915   58   11
round(Observation/length(KODAP_data_complete_male_any_mood_disorder$Patient_ID)*100,1)
## [1] 97.7  1.9  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 247.96, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.895501e-76
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.37731e-26
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.461431e-55
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.10199026 0.32591352 0.06839744
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 2754.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01491969 0.02523148
## sample estimates:
##        p 
## 0.019437
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.33
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.25 0.42
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 2938.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001939974 0.006803219
## sample estimates:
##           p 
## 0.003686327
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.13
###########################
#Major Depressive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.86 0.07 0.07
KODAP_data_complete_male_MDD <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete_male$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete_male$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete_male$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert")| 
                                                 KODAP_data_complete_male$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_male_MDD$Patient_ID)
## [1] 2669
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_MDD$Patient_ID)

Observation
## [1] 2604   54   11
round(Observation/length(KODAP_data_complete_male_MDD$Patient_ID)*100,1)
## [1] 97.6  2.0  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 291.89, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.377908e-91
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.903859e-33
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 9.799118e-61
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.13097098 0.28045043 0.06321612
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 2455.5, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01537684 0.02651475
## sample estimates:
##         p 
## 0.0202323
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.28
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.37
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 2623.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.002169078 0.007604448
## sample estimates:
##           p 
## 0.004121394
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.12
###########################
#########Dysthymia#########
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.84 0.08 0.07
KODAP_data_complete_male_Dysthymia <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete_male$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete_male$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete_male$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete_male$ICD5_pre_clean %in% c("F34.1", "F34.10"),]


length(KODAP_data_complete_male_Dysthymia$Patient_ID)
## [1] 408
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_Dysthymia$Patient_ID)

Observation
## [1] 406   2
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_Dysthymia$Patient_ID)*100,1)
## [1] 99.5  0.5  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 70.932, df = 2, p-value = 3.957e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.307675e-26
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.406227e-12
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.102318e-13
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.17934055 0.05973366 0.00000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 398.06, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000849515 0.019567205
## sample estimates:
##           p 
## 0.004901961
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.01 0.24
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 406, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.01162768
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.16
###########################
##Any anxiety disorder#####
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.83 0.09 0.08
KODAP_data_complete_male_any_anx_disorder <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                       "F40.1 Soziale Phobie",
                                                                                                       "F40.2 Spezifische Phobie",
                                                                                                       "F41.1 Generalisierte Angststörung",
                                                                                                       "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete_male$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete_male$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete_male$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete_male$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_male_any_anx_disorder$Patient_ID)
## [1] 1572
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_any_anx_disorder$Patient_ID)

Observation
## [1] 1527   37    8
round(Observation/length(KODAP_data_complete_male_any_anx_disorder$Patient_ID)*100,1)
## [1] 97.1  2.4  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 233.65, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.158145e-73
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.536612e-27
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.213092e-46
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.17592028 0.25759384 0.06163062
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1425.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01685529 0.03263877
## sample estimates:
##         p 
## 0.0235369
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.18 0.36
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1538.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00236820 0.01042901
## sample estimates:
##           p 
## 0.005089059
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.13
#################################
##Panic Disorder/Agoraphobia#####
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.79 0.11 0.10
KODAP_data_complete_male_PanicAgora <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete_male$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete_male$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete_male$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete_male$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_male_PanicAgora$Patient_ID)
## [1] 498
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_PanicAgora$Patient_ID)

Observation
## [1] 476  18   4
round(Observation/length(KODAP_data_complete_male_PanicAgora$Patient_ID)*100,1)
## [1] 95.6  3.6  0.8
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 81.715, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 8.496355e-25
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.626748e-08
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.344942e-16
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.20460169 0.33317612 0.08192834
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 426.75, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02220042 0.05761352
## sample estimates:
##          p 
## 0.03614458
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.33
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.20 0.53
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 480.16, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.002576109 0.021886544
## sample estimates:
##           p 
## 0.008032129
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.22
#################################
##########Social phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.91 0.05 0.04
KODAP_data_complete_male_socialphobia <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete_male$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete_male$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete_male$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete_male$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]


length(KODAP_data_complete_male_socialphobia$Patient_ID)
## [1] 743
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_socialphobia$Patient_ID)

Observation
## [1] 734   7   2
round(Observation/length(KODAP_data_complete_male_socialphobia$Patient_ID)*100,1)
## [1] 98.8  0.9  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 57.847, df = 2, p-value = 2.746e-13
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.50549e-20
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.154687e-08
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.998588e-12
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.08906484 0.19305330 0.06103542
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 713.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004134465 0.020213199
## sample estimates:
##           p 
## 0.009421265
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.08 0.41
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 733.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004663784 0.0107951039
## sample estimates:
##          p 
## 0.00269179
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.24
#################################
##########Specific phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.76 0.12 0.11
KODAP_data_complete_male_specificphobia <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete_male$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete_male$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete_male$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete_male$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]


length(KODAP_data_complete_male_specificphobia$Patient_ID)
## [1] 189
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_specificphobia$Patient_ID)

Observation
## [1] 184   5
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_specificphobia$Patient_ID)*100,1)
## [1] 97.4  2.6  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 46.917, df = 2, p-value = 6.488e-11
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.745768e-15
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 7.830223e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.825605e-10
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.276389 0.212262 0.000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 167.64, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00977982 0.06399206
## sample estimates:
##          p 
## 0.02645503
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.21
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.08 0.51
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 187.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.02482965
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.22
################################################
##########Generalized Anxiety Disorder##########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.94 0.03 0.03
KODAP_data_complete_male_GAD <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete_male$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") | 
                                                 KODAP_data_complete_male$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete_male$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete_male$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_male_GAD$Patient_ID)
## [1] 150
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_GAD$Patient_ID)

Observation
## [1] 144   6
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_GAD$Patient_ID)*100,1)
## [1] 96  4  0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
## Warning in chisq.test(Observation, p = expected_ratios): Chi-squared
## approximation may be incorrect
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 4.8538, df = 2, p-value = 0.08831
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.9450625
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.942317
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.0485118
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.026167 1.180960 0.000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 125.13, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01636367 0.08887123
## sample estimates:
##    p 
## 0.04
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 1.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.48 2.62
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 148.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.03112234
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 1.02
################################################
##########Obsessive compulsive disorders########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.98 0.01 0.01
KODAP_data_complete_male_OCD <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete_male$ICD2_pre_recode %in% c("F42.X Zwangsstörung") | 
                                                 KODAP_data_complete_male$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete_male$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete_male$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_male_OCD$Patient_ID)
## [1] 311
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_OCD$Patient_ID)

Observation
## [1] 307   4
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_OCD$Patient_ID)*100,1)
## [1] 98.7  1.3  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
## Warning in chisq.test(Observation, p = expected_ratios): Chi-squared
## approximation may be incorrect
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 3.1764, df = 2, p-value = 0.2043
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.9608062
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.5453243
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.002337 1.614700 0.000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 293.26, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004128066 0.034853324
## sample estimates:
##          p 
## 0.01286174
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 1.61
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.52 4.38
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 309, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.01520958
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 2.11
################################################
##########Post traumatic stress disorder########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.84 0.08 0.08
KODAP_data_complete_male_PTSD <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete_male$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete_male$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete_male$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete_male$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_male_PTSD$Patient_ID)
## [1] 237
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_PTSD$Patient_ID)

Observation
## [1] 233   4
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_PTSD$Patient_ID)*100,1)
## [1] 98.3  1.7  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 36.983, df = 2, p-value = 9.315e-09
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 8.559405e-13
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.645795e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.417012e-08
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1724788 0.1989466 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 219.34, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005420257 0.045526571
## sample estimates:
##          p 
## 0.01687764
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.2
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.54
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 235, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.01988197
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.26
################################################
##########Any somatoform Disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.87 0.07 0.06
KODAP_data_complete_male_Somatoform <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete_male$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") | 
                                                        KODAP_data_complete_male$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete_male$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete_male$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_male_Somatoform$Patient_ID)
## [1] 356
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_Somatoform$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_Somatoform$Patient_ID)

Observation
## [1] 340  10   6
round(Observation/length(KODAP_data_complete_male_Somatoform$Patient_ID)*100,1)
## [1] 95.5  2.8  1.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 24.399, df = 2, p-value = 5.034e-06
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.52104e-07
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.002453925
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.0001395291
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1023611 0.4001811 0.2656933
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 315.24, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01433688 0.05270533
## sample estimates:
##          p 
## 0.02808989
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.4
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.20 0.75
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 330.47, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006869443 0.038159467
## sample estimates:
##          p 
## 0.01685393
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.27
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.11 0.60
################################################
##########Somatization disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.83 0.09 0.08
KODAP_data_complete_male_Somatization <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete_male$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                          KODAP_data_complete_male$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete_male$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete_male$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_male_Somatization$Patient_ID)
## [1] 72
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_Somatization$Patient_ID)

Observation
## [1] 68  3  1
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_Somatization$Patient_ID)*100,1)
## [1] 94.4  4.2  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 9.1102, df = 2, p-value = 0.01051
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.004472029
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.6357538
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.01198469
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.141453 0.459581 0.000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 57.69, df = 1, p-value = 3.068e-14
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01097609 0.12665763
## sample estimates:
##          p 
## 0.04225352
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.47
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.12 1.40
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 69.014, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.06395388
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.78
################################################
##########Pain disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.89 0.06 0.05
KODAP_data_complete_male_Pain <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete_male$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                  KODAP_data_complete_male$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete_male$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete_male$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_male_Pain$Patient_ID)
## [1] 135
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_Pain$Patient_ID)

Observation
## [1] 129   2   4
round(Observation/length(KODAP_data_complete_male_Pain$Patient_ID)*100,1)
## [1] 95.6  1.5  3.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 6.8381, df = 2, p-value = 0.03274
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.0279163
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.08032174
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.7656564
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0787432 0.2469717 0.5465752
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 125.19, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.002570188 0.057923575
## sample estimates:
##          p 
## 0.01481481
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.04 0.97
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 117.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.009533825 0.078775571
## sample estimates:
##          p 
## 0.02962963
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.55
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.18 1.45
################################################
##########Eating disorders##################
################################################
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.81 0.10 0.09
KODAP_data_complete_male_Eating <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete_male$ICD2_pre_recode %in% c("F50.X Essstörung") | 
                                                    KODAP_data_complete_male$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete_male$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete_male$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_male_Eating$Patient_ID)
## [1] 97
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_Eating$Patient_ID)

Observation
## [1] 95  2
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_Eating$Patient_ID)*100,1)
## [1] 97.9  2.1  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 17.855, df = 2, p-value = 0.0001327
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.253243e-06
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.01677083
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.0006759089
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2045851 0.2099521 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 87.258, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003579326 0.079655349
## sample estimates:
##          p 
## 0.02061856
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.21
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.04 0.81
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 95.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.04747222
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.53
#########################################
##########Substance-use disorders########
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.93 0.04 0.03
KODAP_data_complete_male_SubstanceUse <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete_male$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") | 
                                                          KODAP_data_complete_male$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete_male$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete_male$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_male_SubstanceUse$Patient_ID)
## [1] 442
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_SubstanceUse$Patient_ID)

Observation
## [1] 431  10   1
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_SubstanceUse$Patient_ID)*100,1)
## [1] 97.5  2.3  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 19.376, df = 2, p-value = 6.201e-05
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.695199e-05
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.3154647
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 9.629037e-07
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0520828 0.5887187 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 400, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01156375 0.04267376
## sample estimates:
##          p 
## 0.02267574
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.59
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.30 1.11
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 439, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.01076518
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.31
#########################################
##########Psychotic disorders############
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- (male_amount_1834/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_1834+
  (male_amount_3549/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_3549+
  (male_amount_5064/sum(male_amount_1834,male_amount_3549, male_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (male_amount_1864*prevalence_1864)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (male_amount_6574*prevalence_6574)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (male_amount_75plus*prevalence_75plus)/((male_amount_1864*prevalence_1864)+(male_amount_6574*prevalence_6574)+(male_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.89 0.06 0.05
KODAP_data_complete_male_Psychotic <- KODAP_data_complete_male[KODAP_data_complete_male$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete_male$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") | 
                                                       KODAP_data_complete_male$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete_male$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete_male$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_male_Psychotic$Patient_ID)
## [1] 181
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_male_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_male_Psychotic$Patient_ID)

Observation
## [1] 176   4   1
Observation[2] <- 0
Observation[3] <- 0
round(Observation/length(KODAP_data_complete_male_Psychotic$Patient_ID)*100,1)
## [1] 97.2  0.0  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 22.578, df = 2, p-value = 1.251e-05
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.610405e-09
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.0001168851
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.0004248988
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.097117 0.000000 0.000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 174.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.02662404
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.00 0.45
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 174.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.00000000 0.02662404
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.49

Sensitivity analysis 1: Equal prevalences across age groups

#Read prevalence estimates
Prevalence_estimates <- read_excel("Prevalence_estimates.xlsx")
Prevalence_estimates$'1834y' <- 1
Prevalence_estimates$'3549y' <- 1
Prevalence_estimates$'5064y' <- 1
Prevalence_estimates$'6574y' <- 1
Prevalence_estimates$'75yplus' <- 1
Prevalence_estimates
## # A tibble: 17 × 6
##    Diagnosis                  `1834y` `3549y` `5064y` `6574y` `75yplus`
##    <chr>                        <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1 Any mental disorder              1       1       1       1         1
##  2 Any mood disorder                1       1       1       1         1
##  3 Major Depressive Disorder        1       1       1       1         1
##  4 Dysthymia                        1       1       1       1         1
##  5 Any anxiety disorder             1       1       1       1         1
##  6 Panic disorder/Agoraphobia       1       1       1       1         1
##  7 Social phobia                    1       1       1       1         1
##  8 Specific phobias                 1       1       1       1         1
##  9 GAD                              1       1       1       1         1
## 10 OCD                              1       1       1       1         1
## 11 PTSD                             1       1       1       1         1
## 12 Any somatoform disorder          1       1       1       1         1
## 13 Somatization disorder            1       1       1       1         1
## 14 Pain disorder                    1       1       1       1         1
## 15 Eating disorders                 1       1       1       1         1
## 16 Substance use disorders          1       1       1       1         1
## 17 Psychotic disorders              1       1       1       1         1
##################
#####Analyses#####
##################

#####################
#Any Mental Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete$Patient_ID)

Observation
## [1] 13218   324    93
round(Observation/length(KODAP_data_complete$Patient_ID)*100, 1)
## [1] 96.9  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 3832.9, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.482197e-323
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.31665260 0.18427180 0.05060858
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 12368, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02130213 0.02649473
## sample estimates:
##          p 
## 0.02376238
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.17 0.21
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 13264, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005538338 0.008388176
## sample estimates:
##           p 
## 0.006820682
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.06
#####################
#Any Mood Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                        "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                        "F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                        "F34 Anhaltende affektive Störungen",
                                                                                                        "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_any_mood_disorder$Patient_ID)
## [1] 8244
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_mood_disorder$Patient_ID)

Observation
## [1] 8027  179   38
round(Observation/length(KODAP_data_complete_any_mood_disorder$Patient_ID)*100,1)
## [1] 97.4  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 2402.7, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.179034e-269
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.482197e-323
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.32243969 0.16837750 0.03420125
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 7541.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01872596 0.02515365
## sample estimates:
##          p 
## 0.02171276
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.15 0.20
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 8090.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003309074 0.006390114
## sample estimates:
##           p 
## 0.004609413
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.02 0.05
###########################
#Major Depressive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_MDD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert")| 
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_MDD$Patient_ID)
## [1] 7460
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_MDD$Patient_ID)

Observation
## [1] 7261  163   36
round(Observation/length(KODAP_data_complete_MDD$Patient_ID)*100,1)
## [1] 97.3  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 2167.7, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.563341e-243
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.482197e-323
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.32195967 0.16944072 0.03580635
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 6820.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01870821 0.02549363
## sample estimates:
##          p 
## 0.02184987
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.15 0.20
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 7314.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003431670 0.006750952
## sample estimates:
##           p 
## 0.004825737
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.05
###########################
#########Dysthymia#########
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_Dysthymia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD5_pre_clean %in% c("F34.1", "F34.10"),]


length(KODAP_data_complete_Dysthymia$Patient_ID)
## [1] 958
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Dysthymia$Patient_ID)

Observation
## [1] 935  22   1
round(Observation/length(KODAP_data_complete_Dysthymia$Patient_ID)*100,1)
## [1] 97.6  2.3  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 285.35, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.129894e-91
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.795348e-31
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.999601e-58
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.325582304 0.178084528 0.007745168
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 870.11, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01479694 0.03514765
## sample estimates:
##          p 
## 0.02296451
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.27
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 952.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  5.449108e-05 6.751188e-03
## sample estimates:
##           p 
## 0.001043841
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.01
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.05
###########################
##Any anxiety disorder#####
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                       "F40.1 Soziale Phobie",
                                                                                                       "F40.2 Spezifische Phobie",
                                                                                                       "F41.1 Generalisierte Angststörung",
                                                                                                       "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_any_anx_disorder$Patient_ID)
## [1] 4453
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_anx_disorder$Patient_ID)

Observation
## [1] 4318  106   29
round(Observation/length(KODAP_data_complete_any_anx_disorder$Patient_ID)*100,1)
## [1] 97.0  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1254.8, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.074186e-138
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.350017e-228
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.31701445 0.18459596 0.04832164
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 4037.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01961854 0.02883120
## sample estimates:
##          p 
## 0.02380418
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.15 0.22
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4335.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004446095 0.009469305
## sample estimates:
##           p 
## 0.006512464
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.07
#################################
##Panic Disorder/Agoraphobia#####
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_PanicAgora <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_PanicAgora$Patient_ID)
## [1] 1456
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PanicAgora$Patient_ID)

Observation
## [1] 1400   47    9
round(Observation/length(KODAP_data_complete_PanicAgora$Patient_ID)*100,1)
## [1] 96.2  3.2  0.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 384.51, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.325822e-116
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.524164e-37
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.424431e-76
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.30595213 0.25032574 0.04586458
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1272.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02406506 0.04304906
## sample estimates:
##          p 
## 0.03228022
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.25
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.19 0.33
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1418.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003019616 0.012152642
## sample estimates:
##           p 
## 0.006181319
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.02 0.09
#################################
##########Social phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_socialphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]


length(KODAP_data_complete_socialphobia$Patient_ID)
## [1] 1850
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_socialphobia$Patient_ID)

Observation
## [1] 1830   16    4
round(Observation/length(KODAP_data_complete_socialphobia$Patient_ID)*100,1)
## [1] 98.9  0.9  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 609.76, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.845015e-208
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.092294e-85
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.289734e-108
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.34350707 0.06706830 0.01604296
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1784.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005124665 0.014335622
## sample estimates:
##           p 
## 0.008648649
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.04 0.11
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1832, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0006928545 0.0059315037
## sample estimates:
##           p 
## 0.002162162
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.04
#################################
##########Specific phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_specificphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]


length(KODAP_data_complete_specificphobia$Patient_ID)
## [1] 703
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_specificphobia$Patient_ID)

Observation
## [1] 678  22   3
round(Observation/length(KODAP_data_complete_specificphobia$Patient_ID)*100,1)
## [1] 96.4  3.1  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 190.54, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 8.387325e-59
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 7.026761e-19
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 9.582599e-39
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.30989042 0.24268133 0.03166374
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 615.88, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02018695 0.04777189
## sample estimates:
##          p 
## 0.03129445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.16 0.37
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 689.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001102765 0.013513634
## sample estimates:
##           p 
## 0.004267425
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.10
################################################
##########Generalized Anxiety Disorder##########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_GAD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_GAD$Patient_ID)
## [1] 552
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_GAD$Patient_ID)

Observation
## [1] 526  20   6
round(Observation/length(KODAP_data_complete_GAD$Patient_ID)*100,1)
## [1] 95.3  3.6  1.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 134.86, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.643319e-40
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.419061e-13
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.69178e-26
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.29421748 0.28096999 0.08065077
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 473.05, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02285853 0.05637906
## sample estimates:
##          p 
## 0.03623188
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.28
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.18 0.44
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 526.31, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004426108 0.024731112
## sample estimates:
##          p 
## 0.01086957
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.18
################################################
##########Obsessive compulsive disorders########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_OCD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F42.X Zwangsstörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_OCD$Patient_ID)
## [1] 791
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_OCD$Patient_ID)

Observation
## [1] 778  11   2
round(Observation/length(KODAP_data_complete_OCD$Patient_ID)*100,1)
## [1] 98.4  1.4  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 249.53, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.157589e-83
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.687856e-32
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.111026e-46
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.33586850 0.10784133 0.01876073
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 745.67, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.007329893 0.025530476
## sample estimates:
##          p 
## 0.01390645
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.11
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.20
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 781.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004380695 0.0101435327
## sample estimates:
##           p 
## 0.002528445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.08
################################################
##########Post traumatic stress disorder########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_PTSD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_PTSD$Patient_ID)
## [1] 1067
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PTSD$Patient_ID)

Observation
## [1] 1052   13    2
round(Observation/length(KODAP_data_complete_PTSD$Patient_ID)*100,1)
## [1] 98.6  1.2  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 342.99, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.380815e-115
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.537755e-45
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 6.614146e-63
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.33909663 0.09448175 0.01390791
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1013.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006788272 0.021319397
## sample estimates:
##          p 
## 0.01218369
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.09
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.05 0.17
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1057, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0003247314 0.0075301326
## sample estimates:
##           p 
## 0.001874414
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.01
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.06
################################################
##########Any somatoform Disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_Somatoform <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_Somatoform$Patient_ID)
## [1] 1074
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatoform$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatoform$Patient_ID)

Observation
## [1] 1009   48   17
round(Observation/length(KODAP_data_complete_Somatoform$Patient_ID)*100,1)
## [1] 93.9  4.5  1.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 232.11, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.559916e-66
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.846748e-20
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.480989e-44
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2759906 0.3465820 0.1174467
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 888.76, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03347037 0.05928157
## sample estimates:
##          p 
## 0.04469274
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.35
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.26 0.46
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1005.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.009547387 0.025770512
## sample estimates:
##          p 
## 0.01582868
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.07 0.19
################################################
##########Somatization disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_Somatization <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                          KODAP_data_complete$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_Somatization$Patient_ID)
## [1] 211
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatization$Patient_ID)

Observation
## [1] 203   7   1
round(Observation/length(KODAP_data_complete_Somatization$Patient_ID)*100,1)
## [1] 96.2  3.3  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 56.095, df = 2, p-value = 6.593e-13
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.132822e-17
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.006803e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.235903e-11
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.30669485 0.25726731 0.03516526
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 182.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01461394 0.06996360
## sample estimates:
##          p 
## 0.03317536
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.54
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 205.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0002474437 0.0302007408
## sample estimates:
##           p 
## 0.004739336
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.22
################################################
##########Pain disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_Pain <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                  KODAP_data_complete$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_Pain$Patient_ID)
## [1] 508
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Pain$Patient_ID)

Observation
## [1] 465  30  13
round(Observation/length(KODAP_data_complete_Pain$Patient_ID)*100,1)
## [1] 91.5  5.9  2.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 86.307, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.568767e-24
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.174023e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.261151e-17
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2432253 0.4579590 0.1898786
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 393.32, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.04085658 0.08418493
## sample estimates:
##          p 
## 0.05905512
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.46
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.32 0.65
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 455.44, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01428679 0.04450754
## sample estimates:
##          p 
## 0.02559055
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.11 0.33
################################################
##########Eating disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_Eating <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD2_pre_recode %in% c("F50.X Essstörung") | 
                                                    KODAP_data_complete$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_Eating$Patient_ID)
## [1] 857
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Eating$Patient_ID)

Observation
## [1] 849   8
Observation[3] <- 0
Observation
## [1] 849   8   0
round(Observation/length(KODAP_data_complete_Eating$Patient_ID)*100,1)
## [1] 99.1  0.9  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 285.92, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.019125e-99
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.615027e-39
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.956119e-54
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.34551166 0.07238993 0.00000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 823.34, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004346906 0.019076925
## sample estimates:
##           p 
## 0.009334889
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.03 0.15
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 855, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000000000 0.005563187
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.04
#########################################
##########Substance-use disorders########
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_SubstanceUse <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_SubstanceUse$Patient_ID)
## [1] 764
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_SubstanceUse$Patient_ID)

Observation
## [1] 748  15   1
round(Observation/length(KODAP_data_complete_SubstanceUse$Patient_ID)*100,1)
## [1] 97.9  2.0  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 232.94, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 6.715239e-76
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.148797e-27
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.919025e-46
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.329746437 0.152253372 0.009711872
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 703.26, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01143871 0.03294463
## sample estimates:
##          p 
## 0.01963351
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.15
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.09 0.26
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 758.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  6.832858e-05 8.456433e-03
## sample estimates:
##           p 
## 0.001308901
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.01
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.06
#########################################
##########Psychotic disorders############
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.74 0.13 0.13
KODAP_data_complete_Psychotic <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") | 
                                                       KODAP_data_complete$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_Psychotic$Patient_ID)
## [1] 336
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Psychotic$Patient_ID)

Observation
## [1] 331   4   1
round(Observation/length(KODAP_data_complete_Psychotic$Patient_ID)*100,1)
## [1] 98.5  1.2  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 107.26, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.100542e-36
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 9.229047e-15
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.52678e-19
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.33797905 0.09231871 0.02208295
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 318.24, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003820372 0.032295397
## sample estimates:
##          p 
## 0.01190476
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.09
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.03 0.25
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 330.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0001553772 0.0190996535
## sample estimates:
##          p 
## 0.00297619
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.14

Sensitivity analysis 2: Lower prevalences in old-old compared to young-old adults

#Read prevalence estimates
Prevalence_estimates <- read_excel("Prevalence_estimates.xlsx")
Prevalence_estimates_v2 <- Prevalence_estimates
Prevalence_estimates_v2$'75yplus' <- 0.5*Prevalence_estimates_v2$'75yplus'
Prevalence_estimates_v2
## # A tibble: 17 × 6
##    Diagnosis                  `1834y` `3549y` `5064y` `6574y` `75yplus`
##    <chr>                        <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1 Any mental disorder           35.8    28      26.4    19.6      9.8 
##  2 Any mood disorder             15.1    10.3     7       5.9      2.95
##  3 Major Depressive Disorder     10       7.2     5.2     4.4      2.2 
##  4 Dysthymia                      2.1     1.7     1.3     1.6      0.8 
##  5 Any anxiety disorder          18.1    16.2    15.3    11.1      5.55
##  6 Panic disorder/Agoraphobia     4.2     4.1     4.1     3.5      1.75
##  7 Social phobia                  4.6     3.1     2.2     0.7      0.35
##  8 Specific phobias              12.3     9.5    10.9     8.4      4.2 
##  9 GAD                            3.3     2       2.3     1.3      0.65
## 10 OCD                            7.2     3.6     2.2     1.1      0.55
## 11 PTSD                           3.7     2.5     1       1.8      0.9 
## 12 Any somatoform disorder        4.2     3.8     3.6     2.1      1.05
## 13 Somatization disorder          0.9     0.6     0.9     0.8      0.4 
## 14 Pain disorder                  4       3.8     3       1.6      0.8 
## 15 Eating disorders               2.3     0.5     0.7     0.4      0.2 
## 16 Substance use disorders        8.4     5.9     5.5     2.5      1.25
## 17 Psychotic disorders            4.2     2.2     2.5     1.3      0.65
##################
#####Analyses#####
##################

#####################
#Any Mental Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mental disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.85 0.10 0.05
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete$Patient_ID)

Observation
## [1] 13218   324    93
round(Observation/length(KODAP_data_complete$Patient_ID)*100, 1)
## [1] 96.9  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1506.9, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.806245e-257
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.404068e-187
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1386400 0.2434408 0.1337176
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 12368, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02130213 0.02649473
## sample estimates:
##          p 
## 0.02376238
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.22 0.27
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 13264, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005538338 0.008388176
## sample estimates:
##           p 
## 0.006820682
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.13
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.11 0.16
#####################
#Any Mood Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.87 0.08 0.04
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                        "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                        "F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                        "F34 Anhaltende affektive Störungen",
                                                                                                        "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_any_mood_disorder$Patient_ID)
## [1] 8244
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_mood_disorder$Patient_ID)

Observation
## [1] 8027  179   38
round(Observation/length(KODAP_data_complete_any_mood_disorder$Patient_ID)*100,1)
## [1] 97.4  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 777.85, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.008437e-240
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.726796e-128
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.391126e-108
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1177238 0.2565222 0.1042108
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 7541.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01872596 0.02515365
## sample estimates:
##          p 
## 0.02171276
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.22 0.30
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 8090.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003309074 0.006390114
## sample estimates:
##           p 
## 0.004609413
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##   p 
## 0.1
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.07 0.14
###########################
#Major Depressive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.86 0.09 0.05
KODAP_data_complete_MDD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert")| 
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_MDD$Patient_ID)
## [1] 7460
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_MDD$Patient_ID)

Observation
## [1] 7261  163   36
round(Observation/length(KODAP_data_complete_MDD$Patient_ID)*100,1)
## [1] 97.3  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 775.99, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.352649e-240
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.493828e-130
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.27984e-105
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1283336 0.2421618 0.1023476
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 6820.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01870821 0.02549363
## sample estimates:
##          p 
## 0.02184987
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.28
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 7314.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003431670 0.006750952
## sample estimates:
##           p 
## 0.004825737
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##   p 
## 0.1
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.07 0.14
###########################
#########Dysthymia#########
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.80 0.13 0.07
KODAP_data_complete_Dysthymia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD5_pre_clean %in% c("F34.1", "F34.10"),]


length(KODAP_data_complete_Dysthymia$Patient_ID)
## [1] 958
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Dysthymia$Patient_ID)

Observation
## [1] 935  22   1
round(Observation/length(KODAP_data_complete_Dysthymia$Patient_ID)*100,1)
## [1] 97.6  2.3  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 189.92, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.767317e-62
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.068134e-32
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.857502e-28
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.22364321 0.17276174 0.01502734
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 870.11, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01479694 0.03514765
## sample estimates:
##          p 
## 0.02296451
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.26
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 952.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  5.449108e-05 6.751188e-03
## sample estimates:
##           p 
## 0.001043841
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.0 0.1
###########################
##Any anxiety disorder#####
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.85 0.10 0.05
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                       "F40.1 Soziale Phobie",
                                                                                                       "F40.2 Spezifische Phobie",
                                                                                                       "F41.1 Generalisierte Angststörung",
                                                                                                       "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_any_anx_disorder$Patient_ID)
## [1] 4453
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_anx_disorder$Patient_ID)

Observation
## [1] 4318  106   29
round(Observation/length(KODAP_data_complete_any_anx_disorder$Patient_ID)*100,1)
## [1] 97.0  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 514.84, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.160606e-158
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.341044e-88
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.479462e-65
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1438131 0.2380740 0.1246411
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 4037.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01961854 0.02883120
## sample estimates:
##          p 
## 0.02380418
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.20 0.29
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4335.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004446095 0.009469305
## sample estimates:
##           p 
## 0.006512464
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.09 0.18
#################################
##Panic Disorder/Agoraphobia#####
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.82 0.12 0.06
KODAP_data_complete_PanicAgora <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_PanicAgora$Patient_ID)
## [1] 1456
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PanicAgora$Patient_ID)

Observation
## [1] 1400   47    9
round(Observation/length(KODAP_data_complete_PanicAgora$Patient_ID)*100,1)
## [1] 96.2  3.2  0.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 207.6, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.549702e-63
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.581766e-33
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 8.70023e-29
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1787030 0.2667648 0.0977531
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1272.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02406506 0.04304906
## sample estimates:
##          p 
## 0.03228022
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.27
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.20 0.36
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1418.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003019616 0.012152642
## sample estimates:
##           p 
## 0.006181319
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##   p 
## 0.1
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.19
#################################
##########Social phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.95 0.04 0.02
KODAP_data_complete_socialphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]


length(KODAP_data_complete_socialphobia$Patient_ID)
## [1] 1850
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_socialphobia$Patient_ID)

Observation
## [1] 1830   16    4
round(Observation/length(KODAP_data_complete_socialphobia$Patient_ID)*100,1)
## [1] 98.9  0.9  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 68.426, df = 2, p-value = 1.385e-15
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.597859e-22
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.007926e-13
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.090319e-10
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0459446 0.2426759 0.1160978
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1784.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005124665 0.014335622
## sample estimates:
##           p 
## 0.008648649
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.14 0.40
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1832, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0006928545 0.0059315037
## sample estimates:
##           p 
## 0.002162162
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.32
#################################
##########Specific phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.83 0.11 0.06
KODAP_data_complete_specificphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]


length(KODAP_data_complete_specificphobia$Patient_ID)
## [1] 703
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_specificphobia$Patient_ID)

Observation
## [1] 678  22   3
round(Observation/length(KODAP_data_complete_specificphobia$Patient_ID)*100,1)
## [1] 96.4  3.1  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 91.277, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.456607e-28
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.164906e-14
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.063969e-14
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.16208570 0.28015003 0.07310491
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 615.88, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02018695 0.04777189
## sample estimates:
##          p 
## 0.03129445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.28
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.18 0.43
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 689.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001102765 0.013513634
## sample estimates:
##           p 
## 0.004267425
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.02 0.23
################################################
##########Generalized Anxiety Disorder##########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.88 0.08 0.04
KODAP_data_complete_GAD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_GAD$Patient_ID)
## [1] 552
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_GAD$Patient_ID)

Observation
## [1] 526  20   6
round(Observation/length(KODAP_data_complete_GAD$Patient_ID)*100,1)
## [1] 95.3  3.6  1.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 28.541, df = 2, p-value = 6.345e-07
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.779346e-08
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.0001577275
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.0001774772
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0832576 0.4584141 0.2631701
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 473.05, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02285853 0.05637906
## sample estimates:
##          p 
## 0.03623188
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.46
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.29 0.71
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 526.31, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004426108 0.024731112
## sample estimates:
##          p 
## 0.01086957
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.11 0.60
################################################
##########Obsessive compulsive disorders########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.94 0.04 0.02
KODAP_data_complete_OCD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F42.X Zwangsstörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_OCD$Patient_ID)
## [1] 791
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_OCD$Patient_ID)

Observation
## [1] 778  11   2
round(Observation/length(KODAP_data_complete_OCD$Patient_ID)*100,1)
## [1] 98.4  1.4  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 30.859, df = 2, p-value = 1.991e-07
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.083752e-10
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.499091e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.579006e-05
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0514433 0.3279810 0.1141151
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 745.67, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.007329893 0.025530476
## sample estimates:
##          p 
## 0.01390645
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.33
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.17 0.60
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 781.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004380695 0.0101435327
## sample estimates:
##           p 
## 0.002528445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.11
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.02 0.46
################################################
##########Post traumatic stress disorder########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.83 0.11 0.06
KODAP_data_complete_PTSD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_PTSD$Patient_ID)
## [1] 1067
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PTSD$Patient_ID)

Observation
## [1] 1052   13    2
round(Observation/length(KODAP_data_complete_PTSD$Patient_ID)*100,1)
## [1] 98.6  1.2  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 185.08, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 9.512809e-64
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.061882e-37
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.505605e-24
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.18878952 0.10871529 0.03200623
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1013.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006788272 0.021319397
## sample estimates:
##          p 
## 0.01218369
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.11
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.19
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1057, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0003247314 0.0075301326
## sample estimates:
##           p 
## 0.001874414
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.13
################################################
##########Any somatoform Disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.87 0.08 0.04
KODAP_data_complete_Somatoform <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_Somatoform$Patient_ID)
## [1] 1074
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatoform$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatoform$Patient_ID)

Observation
## [1] 1009   48   17
round(Observation/length(KODAP_data_complete_Somatoform$Patient_ID)*100,1)
## [1] 93.9  4.5  1.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 43.547, df = 2, p-value = 3.499e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.411975e-12
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 3.105672e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.928161e-06
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0759256 0.5365769 0.3636612
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 888.76, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03347037 0.05928157
## sample estimates:
##          p 
## 0.04469274
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.54
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.40 0.71
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1005.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.009547387 0.025770512
## sample estimates:
##          p 
## 0.01582868
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.36
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.22 0.59
################################################
##########Somatization disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.79 0.14 0.07
KODAP_data_complete_Somatization <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                          KODAP_data_complete$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_Somatization$Patient_ID)
## [1] 211
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatization$Patient_ID)

Observation
## [1] 203   7   1
round(Observation/length(KODAP_data_complete_Somatization$Patient_ID)*100,1)
## [1] 96.2  3.3  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 37.568, df = 2, p-value = 6.952e-09
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 4.922326e-12
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.806064e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.719855e-05
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.21580233 0.24205029 0.06617057
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 182.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01461394 0.06996360
## sample estimates:
##          p 
## 0.03317536
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.24
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.51
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 205.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0002474437 0.0302007408
## sample estimates:
##           p 
## 0.004739336
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.42
################################################
##########Pain disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.89 0.07 0.04
KODAP_data_complete_Pain <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                  KODAP_data_complete$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_Pain$Patient_ID)
## [1] 508
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Pain$Patient_ID)

Observation
## [1] 465  30  13
round(Observation/length(KODAP_data_complete_Pain$Patient_ID)*100,1)
## [1] 91.5  5.9  2.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 2.8689, df = 2, p-value = 0.2382
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.3414028
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.153037
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.7069781
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0248165 0.8418143 0.6980647
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 393.32, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.04085658 0.08418493
## sample estimates:
##          p 
## 0.05905512
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.84
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.58 1.20
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 455.44, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01428679 0.04450754
## sample estimates:
##          p 
## 0.02559055
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##   p 
## 0.7
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.39 1.21
################################################
##########Eating disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.92 0.06 0.03
KODAP_data_complete_Eating <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD2_pre_recode %in% c("F50.X Essstörung") | 
                                                    KODAP_data_complete$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_Eating$Patient_ID)
## [1] 857
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Eating$Patient_ID)

Observation
## [1] 849   8
Observation[3] <- 0
Observation
## [1] 849   8   0
round(Observation/length(KODAP_data_complete_Eating$Patient_ID)*100,1)
## [1] 99.1  0.9  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 62.799, df = 2, p-value = 2.308e-14
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.8057e-22
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.234246e-12
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.938637e-11
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0818019 0.1687097 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 823.34, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004346906 0.019076925
## sample estimates:
##           p 
## 0.009334889
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.08 0.34
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 855, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000000000 0.005563187
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.19
#########################################
##########Substance-use disorders########
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.91 0.06 0.03
KODAP_data_complete_SubstanceUse <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_SubstanceUse$Patient_ID)
## [1] 764
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_SubstanceUse$Patient_ID)

Observation
## [1] 748  15   1
round(Observation/length(KODAP_data_complete_SubstanceUse$Patient_ID)*100,1)
## [1] 97.9  2.0  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 47.616, df = 2, p-value = 4.573e-11
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.153667e-15
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 2.423304e-07
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.750416e-09
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0784913 0.3242332 0.0413641
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 703.26, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01143871 0.03294463
## sample estimates:
##          p 
## 0.01963351
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.32
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.19 0.54
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 758.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  6.832858e-05 8.456433e-03
## sample estimates:
##           p 
## 0.001308901
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.04
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.27
#########################################
##########Psychotic disorders############
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates_v2[Prevalence_estimates_v2$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864*prevalence_1864)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574*prevalence_6574)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus*prevalence_75plus)/((census_amount_1864*prevalence_1864)+(census_amount_6574*prevalence_6574)+(census_amount_75plus*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.90 0.07 0.04
KODAP_data_complete_Psychotic <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") | 
                                                       KODAP_data_complete$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_Psychotic$Patient_ID)
## [1] 336
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Psychotic$Patient_ID)

Observation
## [1] 331   4   1
round(Observation/length(KODAP_data_complete_Psychotic$Patient_ID)*100,1)
## [1] 98.5  1.2  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 29.017, df = 2, p-value = 5.001e-07
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.983926e-10
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.083205e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.000311267
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.10042947 0.17297806 0.08275387
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 318.24, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003820372 0.032295397
## sample estimates:
##          p 
## 0.01190476
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.17
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.47
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 330.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0001553772 0.0190996535
## sample estimates:
##          p 
## 0.00297619
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.53

Sensitivity analysis 3: Influence of long-term care needs

#Read prevalence estimates
Prevalence_estimates <- read_excel("Prevalence_estimates.xlsx")
Prevalence_estimates
## # A tibble: 17 × 6
##    Diagnosis                  `1834y` `3549y` `5064y` `6574y` `75yplus`
##    <chr>                        <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1 Any mental disorder           35.8    28      26.4    19.6      19.6
##  2 Any mood disorder             15.1    10.3     7       5.9       5.9
##  3 Major Depressive Disorder     10       7.2     5.2     4.4       4.4
##  4 Dysthymia                      2.1     1.7     1.3     1.6       1.6
##  5 Any anxiety disorder          18.1    16.2    15.3    11.1      11.1
##  6 Panic disorder/Agoraphobia     4.2     4.1     4.1     3.5       3.5
##  7 Social phobia                  4.6     3.1     2.2     0.7       0.7
##  8 Specific phobias              12.3     9.5    10.9     8.4       8.4
##  9 GAD                            3.3     2       2.3     1.3       1.3
## 10 OCD                            7.2     3.6     2.2     1.1       1.1
## 11 PTSD                           3.7     2.5     1       1.8       1.8
## 12 Any somatoform disorder        4.2     3.8     3.6     2.1       2.1
## 13 Somatization disorder          0.9     0.6     0.9     0.8       0.8
## 14 Pain disorder                  4       3.8     3       1.6       1.6
## 15 Eating disorders               2.3     0.5     0.7     0.4       0.4
## 16 Substance use disorders        8.4     5.9     5.5     2.5       2.5
## 17 Psychotic disorders            4.2     2.2     2.5     1.3       1.3
##################
#####Analyses#####
##################

#####################
#Any Mental Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mental disorder", "75yplus"])

census_amount_1864_LTC <- census_amount_1864*(1-Long_term_care_rate_1864_average)
census_amount_6574_LTC <- census_amount_6574*(1-Long_term_care_rate_6574_average)
census_amount_75plus_LTC <- census_amount_75plus*(1-Long_term_care_rate_75plus_average)

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.84 0.09 0.07
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete$Patient_ID)

Observation
## [1] 13218   324    93
round(Observation/length(KODAP_data_complete$Patient_ID)*100, 1)
## [1] 96.9  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 1725, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.482197e-323
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.395244e-225
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.324963e-291
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.15497259 0.26077571 0.09808828
#Confidence-intervals of Representation quotients

#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 12368, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02130213 0.02649473
## sample estimates:
##          p 
## 0.02376238
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574, 2)
## [1] 0.23 0.29
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 13264, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005538338 0.008388176
## sample estimates:
##           p 
## 0.006820682
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##   p 
## 0.1
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.08 0.12
#####################
#Any Mood Disorder#
#####################

prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any mood disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.86 0.08 0.06
KODAP_data_complete_any_mood_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                        "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                        "F32 Depressive Episode",
                                                                                                        "F33 Rezidivierende depressive Störung",
                                                                                                        "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                        "F34 Anhaltende affektive Störungen",
                                                                                                        "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD2_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD3_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD4_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung") |
                                                               KODAP_data_complete$ICD5_pre_recode %in% c("F30.X F31.X Manische Episode oder Bipolare Störungen",
                                                                                                          "F31.7 Bipolare affktive Störung, gegenwärtig remittiert",
                                                                                                          "F32 Depressive Episode",
                                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert",
                                                                                                          "F34 Anhaltende affektive Störungen",
                                                                                                          "F38.X Andere affektive Störung"),]

length(KODAP_data_complete_any_mood_disorder$Patient_ID)
## [1] 8244
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_mood_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_mood_disorder$Patient_ID)

Observation
## [1] 8027  179   38
round(Observation/length(KODAP_data_complete_any_mood_disorder$Patient_ID)*100,1)
## [1] 97.4  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 891.56, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.309088e-275
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 8.323006e-113
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.856054e-164
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.13162641 0.27427242 0.07630004
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 7541.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01872596 0.02515365
## sample estimates:
##          p 
## 0.02171276
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.27
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.24 0.32
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 8090.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003309074 0.006390114
## sample estimates:
##           p 
## 0.004609413
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.11
###########################
#Major Depressive Disorder#
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Major Depressive Disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.85 0.08 0.06
KODAP_data_complete_MDD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F32 Depressive Episode",
                                                                                          "F33 Rezidivierende depressive Störung",
                                                                                          "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert")| 
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F32 Depressive Episode",
                                                                                            "F33 Rezidivierende depressive Störung",
                                                                                            "F33.4 Rezidivierende depressive Störung, gegenwärtig remittiert"),]

length(KODAP_data_complete_MDD$Patient_ID)
## [1] 7460
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_MDD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_MDD$Patient_ID)

Observation
## [1] 7261  163   36
round(Observation/length(KODAP_data_complete_MDD$Patient_ID)*100,1)
## [1] 97.3  2.2  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 886.4, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.229158e-275
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 6.302609e-115
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.181828e-159
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.1432943 0.2591282 0.0749966
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 6820.3, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01870821 0.02549363
## sample estimates:
##          p 
## 0.02184987
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.22 0.30
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 7314.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003431670 0.006750952
## sample estimates:
##           p 
## 0.004825737
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.05 0.10
###########################
#########Dysthymia#########
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Dysthymia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.78 0.12 0.09
KODAP_data_complete_Dysthymia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD2_pre_clean %in% c("F34.1", "F34.10") |
                                                       KODAP_data_complete$ICD3_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD4_pre_clean %in% c("F34.1", "F34.10") | 
                                                       KODAP_data_complete$ICD5_pre_clean %in% c("F34.1", "F34.10"),]


length(KODAP_data_complete_Dysthymia$Patient_ID)
## [1] 958
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Dysthymia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Dysthymia$Patient_ID)

Observation
## [1] 935  22   1
round(Observation/length(KODAP_data_complete_Dysthymia$Patient_ID)*100,1)
## [1] 97.6  2.3  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 212.56, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 7.423182e-69
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 6.365835e-29
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.660416e-39
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.24754529 0.18601057 0.01107967
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 870.11, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01479694 0.03514765
## sample estimates:
##          p 
## 0.02296451
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.12 0.28
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 952.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  5.449108e-05 6.751188e-03
## sample estimates:
##           p 
## 0.001043841
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.01
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.07
###########################
##Any anxiety disorder#####
###########################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any anxiety disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.84 0.09 0.07
KODAP_data_complete_any_anx_disorder <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                       "F40.1 Soziale Phobie",
                                                                                                       "F40.2 Spezifische Phobie",
                                                                                                       "F41.1 Generalisierte Angststörung",
                                                                                                       "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") |
                                                              KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen") | 
                                                              KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung",
                                                                                                         "F40.1 Soziale Phobie",
                                                                                                         "F40.2 Spezifische Phobie",
                                                                                                         "F41.1 Generalisierte Angststörung",
                                                                                                         "F41.X F40.9 Andere phobische oder Angststörungen"),]

length(KODAP_data_complete_any_anx_disorder$Patient_ID)
## [1] 4453
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_any_anx_disorder$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_any_anx_disorder$Patient_ID)

Observation
## [1] 4318  106   29
round(Observation/length(KODAP_data_complete_any_anx_disorder$Patient_ID)*100,1)
## [1] 97.0  2.4  0.7
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 588.25, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.925044e-181
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 9.40372e-78
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.508303e-100
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.16061923 0.25511451 0.09146172
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 4037.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01961854 0.02883120
## sample estimates:
##          p 
## 0.02380418
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.31
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 4335.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004446095 0.009469305
## sample estimates:
##           p 
## 0.006512464
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.09
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.06 0.13
#################################
##Panic Disorder/Agoraphobia#####
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Panic disorder/Agoraphobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.80 0.11 0.09
KODAP_data_complete_PanicAgora <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung") | 
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F40.0X F41.0 Agoraphobie/Panikstörung"),]

length(KODAP_data_complete_PanicAgora$Patient_ID)
## [1] 1456
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PanicAgora$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PanicAgora$Patient_ID)

Observation
## [1] 1400   47    9
round(Observation/length(KODAP_data_complete_PanicAgora$Patient_ID)*100,1)
## [1] 96.2  3.2  0.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 237.65, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.397963e-72
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.296736e-28
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.769212e-43
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.19966264 0.28672917 0.07194963
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1272.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02406506 0.04304906
## sample estimates:
##          p 
## 0.03228022
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.29
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.21 0.38
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1418.2, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003019616 0.012152642
## sample estimates:
##           p 
## 0.006181319
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.07
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.04 0.14
#################################
##########Social phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Social phobia", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.94 0.03 0.03
KODAP_data_complete_socialphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F40.1 Soziale Phobie") | 
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F40.1 Soziale Phobie") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F40.1 Soziale Phobie"),]


length(KODAP_data_complete_socialphobia$Patient_ID)
## [1] 1850
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_socialphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_socialphobia$Patient_ID)

Observation
## [1] 1830   16    4
round(Observation/length(KODAP_data_complete_socialphobia$Patient_ID)*100,1)
## [1] 98.9  0.9  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 78.562, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 5.45309e-26
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.212814e-11
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 2.086187e-15
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.05142230 0.25762242 0.08439869
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1784.6, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.005124665 0.014335622
## sample estimates:
##           p 
## 0.008648649
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.15 0.43
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1832, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0006928545 0.0059315037
## sample estimates:
##           p 
## 0.002162162
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.03 0.23
#################################
##########Specific phobia##########
#################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Specific phobias", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.82 0.10 0.08
KODAP_data_complete_specificphobia <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD2_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD3_pre_recode %in% c("F40.2 Spezifische Phobie") |
                                                            KODAP_data_complete$ICD4_pre_recode %in% c("F40.2 Spezifische Phobie") | 
                                                            KODAP_data_complete$ICD5_pre_recode %in% c("F40.2 Spezifische Phobie"),]


length(KODAP_data_complete_specificphobia$Patient_ID)
## [1] 703
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_specificphobia$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_specificphobia$Patient_ID)

Observation
## [1] 678  22   3
round(Observation/length(KODAP_data_complete_specificphobia$Patient_ID)*100,1)
## [1] 96.4  3.1  0.4
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 104.6, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.290104e-32
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.75807e-12
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.106364e-20
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.18116165 0.30071174 0.05373547
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 615.88, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02018695 0.04777189
## sample estimates:
##          p 
## 0.03129445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.3
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.19 0.46
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 689.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.001102765 0.013513634
## sample estimates:
##           p 
## 0.004267425
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.17
################################################
##########Generalized Anxiety Disorder##########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "GAD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.87 0.07 0.06
KODAP_data_complete_GAD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F41.1 Generalisierte Angststörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F41.1 Generalisierte Angststörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F41.1 Generalisierte Angststörung"),]

length(KODAP_data_complete_GAD$Patient_ID)
## [1] 552
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_GAD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_GAD$Patient_ID)

Observation
## [1] 526  20   6
round(Observation/length(KODAP_data_complete_GAD$Patient_ID)*100,1)
## [1] 95.3  3.6  1.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 35.361, df = 2, p-value = 2.096e-08
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 3.100055e-10
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.0009535313
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 1.546598e-07
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0958392 0.4897355 0.1925285
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 473.05, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.02285853 0.05637906
## sample estimates:
##          p 
## 0.03623188
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.49
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.31 0.76
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 526.31, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004426108 0.024731112
## sample estimates:
##          p 
## 0.01086957
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.19
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.08 0.44
################################################
##########Obsessive compulsive disorders########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "OCD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.93 0.04 0.03
KODAP_data_complete_OCD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD2_pre_recode %in% c("F42.X Zwangsstörung") | 
                                                 KODAP_data_complete$ICD3_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD4_pre_recode %in% c("F42.X Zwangsstörung") |
                                                 KODAP_data_complete$ICD5_pre_recode %in% c("F42.X Zwangsstörung"),]

length(KODAP_data_complete_OCD$Patient_ID)
## [1] 791
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_OCD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_OCD$Patient_ID)

Observation
## [1] 778  11   2
round(Observation/length(KODAP_data_complete_OCD$Patient_ID)*100,1)
## [1] 98.4  1.4  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 36.12, df = 2, p-value = 1.435e-08
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.298215e-11
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 0.0001091024
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.469058e-08
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.05799452 0.34852567 0.08303939
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 745.67, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.007329893 0.025530476
## sample estimates:
##          p 
## 0.01390645
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.35
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.18 0.64
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 781.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0004380695 0.0101435327
## sample estimates:
##           p 
## 0.002528445
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.08
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.01 0.33
################################################
##########Post traumatic stress disorder########
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "PTSD", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.82 0.10 0.08
KODAP_data_complete_PTSD <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD2_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD3_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") |
                                                  KODAP_data_complete$ICD4_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung") | 
                                                  KODAP_data_complete$ICD5_pre_recode %in% c("F43.1 Posttraumatische Belastungsstörung"),]

length(KODAP_data_complete_PTSD$Patient_ID)
## [1] 1067
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_PTSD$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_PTSD$Patient_ID)

Observation
## [1] 1052   13    2
round(Observation/length(KODAP_data_complete_PTSD$Patient_ID)*100,1)
## [1] 98.6  1.2  0.2
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 205.77, df = 2, p-value < 2.2e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.22548e-70
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.63473e-33
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.623017e-35
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.20836735 0.11670062 0.02352729
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 1013.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.006788272 0.021319397
## sample estimates:
##          p 
## 0.01218369
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.12
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.07 0.20
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1057, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0003247314 0.0075301326
## sample estimates:
##           p 
## 0.001874414
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.02
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.09
################################################
##########Any somatoform Disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Any somatoform disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.86 0.08 0.06
KODAP_data_complete_Somatoform <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD2_pre_recode %in% c("F45.X Somatoforme Störungen") | 
                                                        KODAP_data_complete$ICD3_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD4_pre_recode %in% c("F45.X Somatoforme Störungen") |
                                                        KODAP_data_complete$ICD5_pre_recode %in% c("F45.X Somatoforme Störungen"),]

length(KODAP_data_complete_Somatoform$Patient_ID)
## [1] 1074
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatoform$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatoform$Patient_ID)

Observation
## [1] 1009   48   17
round(Observation/length(KODAP_data_complete_Somatoform$Patient_ID)*100,1)
## [1] 93.9  4.5  1.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 56.955, df = 2, p-value = 4.29e-13
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.534682e-15
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 5.407048e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 7.536937e-12
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0890947 0.5735931 0.2662096
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 888.76, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.03347037 0.05928157
## sample estimates:
##          p 
## 0.04469274
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.57
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.43 0.76
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 1005.1, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.009547387 0.025770512
## sample estimates:
##          p 
## 0.01582868
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.27
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.16 0.43
################################################
##########Somatization disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Somatization disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.78 0.13 0.10
KODAP_data_complete_Somatization <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD2_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") | 
                                                          KODAP_data_complete$ICD3_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD4_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10") |
                                                          KODAP_data_complete$ICD5_pre_clean %in% c("F45.0","F45.00", "F45.1", "F45.10"),]

length(KODAP_data_complete_Somatization$Patient_ID)
## [1] 211
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Somatization$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Somatization$Patient_ID)

Observation
## [1] 203   7   1
round(Observation/length(KODAP_data_complete_Somatization$Patient_ID)*100,1)
## [1] 96.2  3.3  0.5
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 42.653, df = 2, p-value = 5.47e-10
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.155877e-13
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.156523e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 5.203577e-08
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.2402898 0.2607680 0.0488167
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 182.07, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01461394 0.06996360
## sample estimates:
##          p 
## 0.03317536
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.26
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.11 0.55
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 205.04, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0002474437 0.0302007408
## sample estimates:
##           p 
## 0.004739336
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.05
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.31
################################################
##########Pain disorder##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Pain disorder", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.88 0.07 0.05
KODAP_data_complete_Pain <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD2_pre_clean %in% c("F45.4","F45.40", "F45.41") | 
                                                  KODAP_data_complete$ICD3_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD4_pre_clean %in% c("F45.4","F45.40", "F45.41") |
                                                  KODAP_data_complete$ICD5_pre_clean %in% c("F45.4","F45.40", "F45.41"),]

length(KODAP_data_complete_Pain$Patient_ID)
## [1] 508
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Pain$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Pain$Patient_ID)

Observation
## [1] 465  30  13
round(Observation/length(KODAP_data_complete_Pain$Patient_ID)*100,1)
## [1] 91.5  5.9  2.6
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 7.0277, df = 2, p-value = 0.02978
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 0.07927109
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.775826
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 0.02398158
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.035381 0.898171 0.510027
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 393.32, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.04085658 0.08418493
## sample estimates:
##          p 
## 0.05905512
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##   p 
## 0.9
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.62 1.28
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 455.44, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01428679 0.04450754
## sample estimates:
##          p 
## 0.02559055
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.51
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.28 0.89
################################################
##########Eating disorders##################
################################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Eating disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.91 0.05 0.04
KODAP_data_complete_Eating <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD2_pre_recode %in% c("F50.X Essstörung") | 
                                                    KODAP_data_complete$ICD3_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD4_pre_recode %in% c("F50.X Essstörung") |
                                                    KODAP_data_complete$ICD5_pre_recode %in% c("F50.X Essstörung"),]

length(KODAP_data_complete_Eating$Patient_ID)
## [1] 857
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Eating$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Eating$Patient_ID)

Observation
## [1] 849   8
Observation[3] <- 0
Observation
## [1] 849   8   0
round(Observation/length(KODAP_data_complete_Eating$Patient_ID)*100,1)
## [1] 99.1  0.9  0.0
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 70.355, df = 2, p-value = 5.281e-16
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 6.865318e-25
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 4.926831e-11
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.119904e-15
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.0905979 0.1796162 0.0000000
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 823.34, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.004346906 0.019076925
## sample estimates:
##           p 
## 0.009334889
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.08 0.37
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 855, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.000000000 0.005563187
## sample estimates:
## p 
## 0
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
## p 
## 0
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.14
#########################################
##########Substance-use disorders########
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Substance use disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.90 0.06 0.04
KODAP_data_complete_SubstanceUse <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD2_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") | 
                                                          KODAP_data_complete$ICD3_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD4_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen") |
                                                          KODAP_data_complete$ICD5_pre_recode %in% c("F1X.X Psychische und Verhaltensstörungen durch psychotrope Substanzen"),]

length(KODAP_data_complete_SubstanceUse$Patient_ID)
## [1] 764
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_SubstanceUse$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_SubstanceUse$Patient_ID)

Observation
## [1] 748  15   1
round(Observation/length(KODAP_data_complete_SubstanceUse$Patient_ID)*100,1)
## [1] 97.9  2.0  0.1
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 55.102, df = 2, p-value = 1.083e-12
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 2.32986e-17
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.982076e-06
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 3.457945e-13
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.08808815 0.34545660 0.03017966
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 703.26, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.01143871 0.03294463
## sample estimates:
##          p 
## 0.01963351
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.35
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.20 0.58
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 758.01, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  6.832858e-05 8.456433e-03
## sample estimates:
##           p 
## 0.001308901
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.03
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.19
#########################################
##########Psychotic disorders############
#########################################
prevalence_1834 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "1834y"])
prevalence_3549 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "3549y"])
prevalence_5064 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "5064y"])

prevalence_1864 <- (census_amount_1834/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_1834+
  (census_amount_3549/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_3549+
  (census_amount_5064/sum(census_amount_1834,census_amount_3549, census_amount_5064))*prevalence_5064

prevalence_6574 <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "6574y"])
prevalence_75plus <- as.numeric(Prevalence_estimates[Prevalence_estimates$Diagnosis == "Psychotic disorders", "75yplus"])

#Ratios in reference population
expected_ratio_1864 <- (census_amount_1864_LTC*prevalence_1864)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_6574 <- (census_amount_6574_LTC*prevalence_6574)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))
expected_ratio_75plus <- (census_amount_75plus_LTC*prevalence_75plus)/((census_amount_1864_LTC*prevalence_1864)+(census_amount_6574_LTC*prevalence_6574)+(census_amount_75plus_LTC*prevalence_75plus))

expected_ratios <- c(expected_ratio_1864,
                     expected_ratio_6574,
                     expected_ratio_75plus)
round(expected_ratios,2)
## [1] 0.89 0.06 0.05
KODAP_data_complete_Psychotic <- KODAP_data_complete[KODAP_data_complete$ICD1_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD2_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") | 
                                                       KODAP_data_complete$ICD3_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD4_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen") |
                                                       KODAP_data_complete$ICD5_pre_recode %in% c("F20.X Schizophrenie",
                                                                                                  "F21 F22 F23 F24 F28 F29 Psychotische oder wahnhafte Störungen (außer Schizophrenie)",
                                                                                                  "F25.X Schizoaffektive Störungen"),]

length(KODAP_data_complete_Psychotic$Patient_ID)
## [1] 336
#Observed vs. expected: 
Observation <- as.vector(table(KODAP_data_complete_Psychotic$Age_Stepped))
Expected <- expected_ratios*length(KODAP_data_complete_Psychotic$Patient_ID)

Observation
## [1] 331   4   1
round(Observation/length(KODAP_data_complete_Psychotic$Patient_ID)*100,1)
## [1] 98.5  1.2  0.3
# Chi-squared test for given probabilities
test <- chisq.test(Observation, p = expected_ratios)
test
## 
##  Chi-squared test for given probabilities
## 
## data:  Observation
## X-squared = 32.724, df = 2, p-value = 7.834e-08
# Post-hoc binomial-test for each category with Bonferroni-correction
binom.test(Observation[1], sum(Observation), expected_ratios[1])$p.value*3
## [1] 1.819468e-11
binom.test(Observation[2], sum(Observation), expected_ratios[2])$p.value*3
## [1] 1.773712e-05
binom.test(Observation[3], sum(Observation), expected_ratios[3])$p.value*3
## [1] 4.828575e-06
#Estimating underrepresentation compared to reference population
#Representation quotients
Observation/Expected
## [1] 1.11155865 0.18452267 0.06045077
#Confidence-intervals of Representation quotients
#Confidence-intervals around sample proportion
prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[2] out of sum(Observation), null probability 0.5
## X-squared = 318.24, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.003820372 0.032295397
## sample estimates:
##          p 
## 0.01190476
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_6574,2)
##    p 
## 0.18
round(as.vector(prop.test(x = Observation[2], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_6574,2)
## [1] 0.06 0.50
#Confidence-intervals around sample proportion
prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  Observation[3] out of sum(Observation), null probability 0.5
## X-squared = 330.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.0001553772 0.0190996535
## sample estimates:
##          p 
## 0.00297619
#Confidence-intervals around sample proportion relative to expected proportion
round(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$estimate/expected_ratio_75plus,2)
##    p 
## 0.06
round(as.vector(prop.test(x = Observation[3], n = sum(Observation), conf.level = .95)$conf.int)/expected_ratio_75plus,2)
## [1] 0.00 0.39