-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysisForPublicationRmd.Rmd
311 lines (244 loc) · 8.34 KB
/
AnalysisForPublicationRmd.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
---
title: "DIMIS Analysis"
output:
html_document:
df_print: paged
html: default
pdf_document: default
date: "2024-11-27"
---
# load libraries
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Load Data and Data Cleaning
```{r}
# Load necessary libraries
library(readxl)
library(dplyr)
library(writexl)
library(ggplot2)
# Load the data
Master <- read_excel("DIMIS_Master.xlsx")
# Inspect data structure and column names
names(Master)
str(Master)
```
## Clean and Restructure Data
```{r}
# Clean and restructure data
Master <- Master %>%
mutate(
Domain = as.factor(Domain),
ISO_LOINC = as.factor(ISO_LOINC),
ISO_SCT = as.factor(ISO_SCT),
NH_SCT = as.numeric(NH_SCT),
MM_SCT = as.numeric(MM_SCT)
)
# Check the updated structure
str(Master)
# Export cleaned data to Excel for quality check
write_xlsx(Master, "DIMIS_Master_Clean.xlsx")
```
***
# ISO-Scores of SCT Codes
## Percentages of ISO-Scores
```{r}
# Calculate counts and percentages for ISO_LOINC
summary_table_SCT <- Master %>%
count(ISO_SCT) %>%
mutate(Percentage = (n / sum(n)) * 100)
# Display the summary table
knitr::kable(summary_table_SCT, caption = "Counts and Percentages of ISO SCT Codes")
```
## Calculate ISO-Score
```{r}
# Ensure ISO_LOINC is converted to numeric if it's a factor
summary_table_SCT$ISO_SCT <- as.numeric(as.character(summary_table_SCT$ISO_SCT))
# Calculate the weighted sum of equivalence measures
weighted_sum_SCT <- sum(summary_table_SCT$ISO_SCT * summary_table_SCT$n)
# Calculate the total number of maps
total_maps_SCT <- sum(summary_table_SCT$n)
# Compute the average equivalence score
average_equivalence_score_SCT <- weighted_sum_SCT / total_maps_SCT
# Print the result
cat("The Equivalence Score is:", round(average_equivalence_score_SCT, 2), "\n")
```
***
# ISO-Scores of LOINC Codes
## Percentages of ISO-Scores
```{r}
# Calculate counts and percentages for ISO_LOINC
summary_table_LOINC <- Master %>%
count(ISO_LOINC) %>%
mutate(Percentage = (n / sum(n)) * 100)
# Display the summary table
knitr::kable(summary_table_LOINC, caption = "Counts and Percentages of ISO LOINC Codes")
```
## Calculate ISO-Score
```{r}
# Ensure ISO_LOINC is converted to numeric if it's a factor
summary_table_LOINC$ISO_LOINC <- as.numeric(as.character(summary_table_LOINC$ISO_LOINC))
# Calculate the weighted sum of equivalence measures
weighted_sum_LOINC <- sum(summary_table_LOINC$ISO_LOINC * summary_table_LOINC$n)
# Calculate the total number of maps
total_maps_LOINC <- sum(summary_table_LOINC$n)
# Compute the average equivalence score
average_equivalence_score_LOINC <- weighted_sum_LOINC / total_maps_LOINC
# Print the result
cat("The Equivalence Score is:", round(average_equivalence_score_LOINC, 2), "\n")
```
# ISO-Scores by Domain
***
Prepare SCT Data
## Cross-Table of ISO Scores and Domains
```{r}
# Generate a cross-table of ISO_SCT by Domain and calculate proportions
SCT_table <- table(Master$ISO_SCT, Master$Domain)
SCT_prob <- prop.table(SCT_table, 2)
# Convert the table to a data frame
SCT_dataframe <- as.data.frame.matrix(SCT_prob)
# Add rownames as a column for long-format conversion
SCT_dataframe$ISO_SCT <- rownames(SCT_dataframe)
```
## Convert Data to Long Format
```{r}
# Convert wide-format dataframe to long format
library(reshape2)
SCT_DataLong <- melt(
SCT_dataframe,
id.vars = "ISO_SCT",
value.name = "proportion"
)
```
***
Prepare LOINC Data
## Cross-Table of ISO Scores and Domains
```{r}
# Generate a cross-table of ISO_LOINC by Domain and calculate proportions
LOINC_table <- table(Master$ISO_LOINC, Master$Domain)
LOINC_prob <- prop.table(LOINC_table, 2)
# Convert the table to a data frame
LOINC_dataframe <- as.data.frame.matrix(LOINC_prob)
# Add rownames as a column for long-format conversion
LOINC_dataframe$ISO_Loinc <- rownames(LOINC_dataframe)
```
## Convert Data to Long Format
```{r}
# Convert wide-format dataframe to long format
LOINC_DataLong <- melt(
LOINC_dataframe,
id.vars = "ISO_Loinc",
value.name = "proportion"
)
```
## Prepare Both Datasets for Combined Dataset
```{r}
# Add a source column to each dataset
SCT_DataLong$Source <- "SNOMED"
LOINC_DataLong$Source <- "LOINC"
# all columns need to have the same name
colnames(SCT_DataLong)[colnames(SCT_DataLong) == "ISO_SCT"] <- "ISO_Code"
colnames(LOINC_DataLong)[colnames(LOINC_DataLong) == "ISO_Loinc"] <- "ISO_Code"
# Combine the two datasets
combined_data <- rbind(SCT_DataLong, LOINC_DataLong)
```
## Graph
```{r}
p <- ggplot(combined_data, aes(y = variable, x = proportion, fill = ISO_Code)) +
geom_bar(stat = "identity") +
# Customize axes and labels
ylab("") +
labs(
fill = "ISO Score",
title = "ISO Scores for SNOMED and LOINC Codes by DIMIS Domain"
) +
# Add color palette
scale_fill_brewer(palette = "BuPu") +
# Adjust faceting to make the plots wider
facet_wrap(~ Source, scales = "free_x", ncol = 2) +
# Enhance plot aesthetics
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.text.y = element_text(size = 8), # Reduce y-axis text size
strip.text = element_text(size = 14), # Adjust facet label size
legend.title = element_text(size = 12),
legend.text = element_text(size = 10),
panel.spacing = unit(1, "lines") # Add spacing between facets
)
```
```{r}
print(p)
```
```{r}
ggsave("~/Library/CloudStorage/OneDrive-Charité-UniversitätsmedizinBerlin/BIH/Repositories/DIMIS/graphPaper.png", plot = p, width = 10, height = 6, dpi = 300)
```
***
# Krippendorf
## SCT
```{r}
library(irr)
# Step 1: Remove rows where either NH_SCT or MM_SCT has the value -1
Kripp_clean_SCT <- Master %>%
filter(NH_SCT != -1, MM_SCT != -1)
# Step 2: Calculate the sum of matches and non-matches
sum_matches <- sum(Kripp_clean_SCT$NH_SCT == Kripp_clean_SCT$MM_SCT)
sum_non_matches <- sum(Kripp_clean_SCT$NH_SCT != Kripp_clean_SCT$MM_SCT)
# Step 3: Calculate proportions
total_rows <- nrow(Kripp_clean_SCT)
proportion_matches <- sum_matches / total_rows
proportion_non_matches <- sum_non_matches / total_rows
# Step 4: Prepare data for Krippendorff's alpha
agreement_matrix_SCT <- as.matrix(Kripp_clean_SCT[, c("NH_SCT", "MM_SCT")])
# Step 5: Calculate Krippendorff's alpha
result_SCT <- kripp.alpha(agreement_matrix_SCT, method = "nominal") # Use "interval" for numeric data
# Step 6: Create a summary table
summary_table <- data.frame(
Metric = c("Matches", "Non-Matches", "Krippendorff Alpha"),
Count = c(sum_matches, sum_non_matches, NA), # NA for alpha, as it's not a count
Proportion = c(proportion_matches, proportion_non_matches, result_SCT$value)
)
# Print the summary table
print(summary_table)
print(result_SCT)
```
## LOINC
```{r}
# Step 1: Remove rows where either NH_SCT or MM_SCT has the value -1
Kripp_clean_LOINC <- Master %>%
filter(NH_LOINC != -1, MM_LOINC != -1)
# Step 2: Prepare data for Krippendorff's alpha
# Select only the two relevant columns and convert to a matrix
agreement_matrix_LOINC <- as.matrix(Kripp_clean_LOINC[, c("NH_SCT", "MM_SCT")])
# Step 3: Calculate Krippendorff's alpha (nominal or interval, depending on data type)
result_LOINC <- kripp.alpha(agreement_matrix_LOINC, method = "nominal") # Use "interval" for numeric data
# Print the result
print(result_LOINC)
```
```{r}
library(irr)
# Step 1: Remove rows where either NH_SCT or MM_SCT has the value -1
Kripp_clean_LOINC <- Master %>%
filter(NH_LOINC != -1, MM_LOINC != -1)
# Step 2: Calculate the sum of matches and non-matches
sum_matches <- sum(Kripp_clean_LOINC$NH_LOINC == Kripp_clean_LOINC$MM_LOINC)
sum_non_matches <- sum(Kripp_clean_LOINC$NH_LOINC != Kripp_clean_LOINC$MM_LOINC)
# Step 3: Calculate proportions
total_rows <- nrow(Kripp_clean_LOINC)
proportion_matches <- sum_matches / total_rows
proportion_non_matches <- sum_non_matches / total_rows
# Step 4: Prepare data for Krippendorff's alpha
agreement_matrix_LOINC <- as.matrix(Kripp_clean_LOINC[, c("NH_SCT", "MM_SCT")])
# Step 5: Calculate Krippendorff's alpha
result_LOINC <- kripp.alpha(agreement_matrix_LOINC, method = "nominal") # Use "interval" for numeric data
# Step 6: Create a summary table
summary_table <- data.frame(
Metric = c("Matches", "Non-Matches", "Krippendorff Alpha"),
Count = c(sum_matches, sum_non_matches, NA), # NA for alpha, as it's not a count
Proportion = c(proportion_matches, proportion_non_matches, result_LOINC$value)
)
# Print the summary table
print(summary_table)
print(result_LOINC)
```