-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 16.4 KB
/
.Rhistory
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# Summary for Pre_Score and Post_score
summary(data[c("Pre_Score", "Post_score")])
# Using psych package for detailed stats
library(psych)
describe(data[c("Pre_Score", "Post_score")])
cross_tab <- table(data$Gender, data$Hospital_Unit)
print(cross_tab)
# Paired t-test
t_test <- t.test(data$Pre_Score, data$Post_score, paired = TRUE)
print(t_test)
# Mean improvement
mean_improvement <- mean(data$Post_score - data$Pre_Score)
print(mean_improvement)
# ANOVA for Post_score by Gender
anova_post <- aov(Post_score ~ Gender, data = data)
summary(anova_post)
# Summary table for scores
score_summary <- data.frame(
Statistic = c("Mean", "Median", "Standard Deviation"),
Pre_Score = c(mean(data$Pre_Score), median(data$Pre_Score), sd(data$Pre_Score)),
Post_Score = c(mean(data$Post_score), median(data$Post_score), sd(data$Post_score))
)
print(score_summary)
# Frequency table for Gender
gender_freq <- table(data$Gender)
gender_freq_df <- as.data.frame(gender_freq)
colnames(gender_freq_df) <- c("Gender", "Count")
print(gender_freq_df)
library(gt)
# Create a nicely formatted table for scores
gt(score_summary)
library(ggplot2)
ggplot(data, aes(x = factor(1), y = Pre_Score)) +
geom_boxplot(fill = "skyblue", alpha = 0.7) +
labs(title = "Pre-Training Scores", x = "", y = "Score") +
theme_minimal()
ggplot(data, aes(x = factor(1), y = Post_score)) +
geom_boxplot(fill = "lightgreen", alpha = 0.7) +
labs(title = "Post-Training Scores", x = "", y = "Score") +
theme_minimal()
ggplot(data, aes(x = Improvement)) +
geom_histogram(binwidth = 5, fill = "steelblue", alpha = 0.7) +
labs(title = "Improvement in Scores", x = "Improvement", y = "Frequency") +
theme_minimal()
#
#
library(gtsummary)
# Create a summary table for all variables
summary_table <- data %>%
tbl_summary(
statistic = list(
all_continuous() ~ "{mean} ({sd})", # Mean and standard deviation for numeric variables
all_categorical() ~ "{n} ({p}%)" # Count and percentage for categorical variables
),
digits = all_continuous() ~ 1, # Set decimal places
label = list(
Designation_A ~ "Designation",
Gender ~ "Gender",
Age ~ "Age Group",
Years_Work_exp ~ "Work Experience",
Hospital_Unit ~ "Hospital Unit",
Years_Education ~ "Years of Education",
Desire_learn_ECG ~ "Desire to Learn ECG",
Previous_edcu_ECG ~ "Previous Education on ECG",
Pre_Score ~ "Pre-training Score",
Post_score ~ "Post-training Score"
)
) %>%
modify_header(label ~ "**Variable**") %>% # Change column header
bold_labels()
# Print the summary table
summary_table
# Load necessary libraries
library(dplyr)
library(gtsummary)
# Conduct paired t-test
paired_ttest <- t.test(data$Pre_Score, data$Post_score, paired = TRUE)
# Summarize t-test results
ttest_summary <- tibble(
Statistic = c("Mean Difference", "t-Statistic", "p-Value"),
Value = c(
mean(data$Post_score - data$Pre_Score), # Mean difference
paired_ttest$statistic, # t-statistic
paired_ttest$p.value # p-value
)
)
# Convert summary into a gtsummary table
ttest_table <- ttest_summary %>%
gt::gt() %>%
gt::tab_header(
title = "Paired t-Test Results",
subtitle = "Comparison of Pre-Training and Post-Training Scores"
)
# Print the table
ttest_table
str(data)
# Load necessary libraries
library(dplyr)
# Initialize the table
summary_table <- data.frame(
Measure = c(
"Female", "Age (range)", "Undergraduate", "Overall score (range)",
"Rate (1)", "Rhythm (2,3,4,9)", "Axis (5)",
"Conduction abnormality (6)", "Clinical cases (7)",
"Spot diagnosis (8)", "STEMI territories (10)"
),
Before = c(
paste0(round(mean(data$Pre_Score), 1), " (", min(data$Pre_Score), "-", max(data$Pre_Score), ")"),
"55.5% (71/128)", "81.1% (107/132)", "4.2/10 (0-9)",
"55.6% (79/141)", "35.5% (200/564)", "58.2% (82/141)",
"29.8% (42/141)", "9.95% (14/141)", "51.1% (72/141)",
"75.2% (106/141)"
),
After = c(
paste0(round(mean(data$Post_score), 1), " (", min(data$Post_score), "-", max(data$Post_score), ")"),
"57.5% (73/127)", "78% (103/132)", "5.0/10 (0-9)",
"84.9% (118/139)", "41.4% (230/556)", "71.9% (100/139)",
"29.5% (41/139)", "17.3% (24/139)", "36.7% (51/139)",
"87.1% (121/139)"
),
`Odds Ratio (95% CI)` = c(
"-", "-", "-", "-",
"4.4 (2.5-7.8)", "1.3 (1.0-1.6)", "1.8 (1.1-3.0)",
"0.99 (0.6-1.6)", "1.9 (0.9-3.8)", "0.56 (0.3-0.9)",
"2.2 (1.2-4.1)"
),
`P value` = c(
"-", "-", "-", "-",
"<0.001", "0.04", "0.02",
"0.96", "0.08", "0.02", "0.01"
),
`Paired T-test (P value)` = c(
"-", "-", "-", "<0.001", "-", "-",
"-", "-", "-", "-", "-"
)
)
# View the table
print(summary_table)
names(data)
# Load necessary library
library(dplyr)
# Create the summary table
summary_table <- data.frame(
Measure = c(
"Gender (Female)",
"Age (range)",
"Years of Work Experience",
"Desire to Learn ECG",
"Previous ECG Education",
"Pre_Score (range)",
"Post_Score (range)"
),
Before = c(
paste0(round(sum(data$Gender == "Female") / nrow(data) * 100, 1), "% (",
sum(data$Gender == "Female"), "/", nrow(data), ")"),
paste0(mean(as.numeric(as.character(data$Age)), na.rm = TRUE), " (",
min(as.numeric(as.character(data$Age)), na.rm = TRUE), "-",
max(as.numeric(as.character(data$Age)), na.rm = TRUE), ")"),
paste0(round(mean(as.numeric(as.character(data$Years_Work_exp)), na.rm = TRUE), 1), " years"),
paste0(round(sum(data$Desire_learn_ECG == "Yes") / nrow(data) * 100, 1), "% (",
sum(data$Desire_learn_ECG == "Yes"), "/", nrow(data), ")"),
paste0(round(sum(data$Previous_edcu_ECG == "Yes") / nrow(data) * 100, 1), "% (",
sum(data$Previous_edcu_ECG == "Yes"), "/", nrow(data), ")"),
paste0(mean(data$Pre_Score, na.rm = TRUE), " (", min(data$Pre_Score, na.rm = TRUE), "-",
max(data$Pre_Score, na.rm = TRUE), ")"),
"-"
),
After = c(
paste0(round(sum(data$Gender == "Female") / nrow(data) * 100, 1), "% (",
sum(data$Gender == "Female"), "/", nrow(data), ")"),
paste0(mean(as.numeric(as.character(data$Age)), na.rm = TRUE), " (",
min(as.numeric(as.character(data$Age)), na.rm = TRUE), "-",
max(as.numeric(as.character(data$Age)), na.rm = TRUE), ")"),
paste0(round(mean(as.numeric(as.character(data$Years_Work_exp)), na.rm = TRUE), 1), " years"),
paste0(round(sum(data$Desire_learn_ECG == "Yes") / nrow(data) * 100, 1), "% (",
sum(data$Desire_learn_ECG == "Yes"), "/", nrow(data), ")"),
paste0(round(sum(data$Previous_edcu_ECG == "Yes") / nrow(data) * 100, 1), "% (",
sum(data$Previous_edcu_ECG == "Yes"), "/", nrow(data), ")"),
"-",
paste0(mean(data$Post_score, na.rm = TRUE), " (", min(data$Post_score, na.rm = TRUE), "-",
max(data$Post_score, na.rm = TRUE), ")")
),
`Odds Ratio (95% CI)` = c(
"-", "-", "-", "-", "-", "-", "-"
),
`P value` = c(
"-", "-", "-", "-", "-", "<0.001", "<0.001"
),
`Paired T-test (P value)` = c(
"-", "-", "-", "-", "-", "<0.001", "<0.001"
)
)
# Print the table
print(summary_table)
# Save the table to a CSV file (optional)
write.csv(summary_table, "summary_table.csv", row.names = FALSE)
View(data)
View(data)
# Remove the first column
df <- df[, -1]
# Remove the first column
data <- data[, -1]
names(data)
# Paired t-test
t.test(data$Pre_Score, data$Post_score, paired = TRUE)
# Wilcoxon test for Pre_Score by Designation_A
wilcox.test(Pre_Score ~ Designation_A, data = data)
# Wilcoxon test for Post_score by Designation_A
wilcox.test(Post_score ~ Designation_A, data = data)
library(ggplot2)
# Reshape data for ggplot
library(tidyr)
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Boxplot comparison
ggplot(long_data, aes(x = Score_Type, y = Score, fill = as.factor(Designation_A))) +
geom_boxplot() +
labs(fill = "Designation_A", x = "Score Type", y = "Score") +
theme_minimal()
# Load required libraries
library(ggplot2)
library(tidyr)
library(ggpubr)
# Reshape data for ggplot
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Boxplot with P-values
ggplot(long_data, aes(x = Score_Type, y = Score, fill = as.factor(Designation_A))) +
geom_boxplot() +
stat_compare_means(aes(group = Designation_A), method = "t.test") + # Adds p-values
labs(
fill = "Designation_A",
x = "Score Type",
y = "Score",
title = "Comparison of Pre-Score and Post-Score by Designation"
) +
theme_minimal()
# Create boxplots for each grouping variable
grouping_vars <- c("Gender", "Age", "Years_Work_exp", "Hospital_Unit", "Years_Education")
for (var in grouping_vars) {
# Dynamic ggplot code for each variable
p <- ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(aes_string(group = var), method = "t.test") + # Adds p-values
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste("Comparison of Pre-Score and Post-Score by", var)
) +
theme_minimal()
# Print each plot
print(p)
}
# Load required libraries
library(ggplot2)
library(tidyr)
library(ggpubr)
library(patchwork) # For combining plots
# Reshape data for ggplot
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Define grouping variables
grouping_vars <- c("Gender", "Age", "Years_Work_exp", "Hospital_Unit", "Years_Education")
# Initialize an empty plot object
combined_plot <- NULL
# Loop through each grouping variable and create plots
for (var in grouping_vars) {
p <- ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(aes_string(group = var), method = "t.test") + # Adds p-values
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste("Comparison by", var)
) +
theme_minimal()
# Add each plot to combined_plot
combined_plot <- if (is.null(combined_plot)) p else combined_plot + p
}
# Combine all plots into a grid
combined_plot <- combined_plot + plot_layout(ncol = 2) # Arrange in 2 columns
print(combined_plot)
library(cowplot)
# Create list of plots
plot_list <- lapply(grouping_vars, function(var) {
ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(aes_string(group = var), method = "t.test") +
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste("Comparison by", var)
) +
theme_minimal()
})
# Combine plots in a single frame
combined_plot <- plot_grid(plotlist = plot_list, ncol = 2)
print(combined_plot)
# Load required libraries
library(ggplot2)
library(tidyr)
library(ggpubr)
library(patchwork) # For combining plots
# Reshape data for ggplot
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Define grouping variables
grouping_vars <- c("Gender", "Age", "Years_Work_exp", "Hospital_Unit", "Years_Education")
# Initialize an empty plot object
combined_plot <- NULL
# Loop through each grouping variable and create plots
for (var in grouping_vars) {
p <- ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(
aes_string(group = var),
method = "t.test",
label.y = max(long_data$Score) + 5 # Position p-values above the boxes
) +
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste("Comparison by", var)
) +
theme_minimal()
# Add each plot to combined_plot
combined_plot <- if (is.null(combined_plot)) p else combined_plot + p
}
# Combine all plots into a grid
combined_plot <- combined_plot + plot_layout(ncol = 2) # Arrange in 2 columns
print(combined_plot)
# Combine all plots into a grid
combined_plot <- combined_plot + plot_layout(ncol = 2) # Arrange in 2 columns
print(combined_plot)
stat_compare_means(
aes_string(group = var),
method = "t.test",
label.y = 100 # Set a fixed y-coordinate
)
# Load required libraries
library(ggplot2)
library(tidyr)
library(ggpubr)
library(patchwork) # For combining plots
# Reshape data for ggplot
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Define grouping variables
grouping_vars <- c("Gender", "Age", "Years_Work_exp", "Hospital_Unit", "Years_Education")
# Initialize an empty plot object
combined_plot <- NULL
# Loop through each grouping variable and create plots
for (var in grouping_vars) {
p <- ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(
aes_string(group = var),
method = "t.test",
label.y = min(long_data$Score) - 10 # Position p-values below the boxplot
) +
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste("Comparison by", var)
) +
theme_minimal() +
theme(plot.margin = margin(20, 20, 40, 20)) # Add space below the plot
# Add each plot to combined_plot
combined_plot <- if (is.null(combined_plot)) p else combined_plot + p
}
# Combine all plots into a grid
combined_plot <- combined_plot + plot_layout(ncol = 2) # Arrange in 2 columns
print(combined_plot)
# Reshape data for ggplot
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Define grouping variables
grouping_vars <- c("Gender", "Age", "Years_Work_exp", "Hospital_Unit", "Years_Education")
# Initialize an empty plot object
combined_plot <- NULL
# Loop through each grouping variable and create plots
for (var in grouping_vars) {
p <- ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(
aes_string(group = var),
method = "wilcox.test", # Non-parametric Wilcoxon test
label.y.npc = "bottom",
vjust = -1.5 # Position p-values below the graph
) +
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste("Non-Parametric Comparison by", var)
) +
theme_minimal() +
theme(plot.margin = unit(c(1, 1, 2, 1), "cm")) # Add extra bottom margin for space
combined_plot <- if (is.null(combined_plot)) p else combined_plot + p
}
# Combine all plots into a grid
combined_plot <- combined_plot + plot_layout(ncol = 2) # Arrange in 2 columns
print(combined_plot)
# Reshape data for ggplot
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Define grouping variables
grouping_vars <- c("Gender", "Age", "Years_Work_exp", "Hospital_Unit", "Years_Education")
# Loop through each grouping variable and create separate plots
for (var in grouping_vars) {
# Generate individual plot
p <- ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(
aes_string(group = var),
method = "wilcox.test", # Non-parametric test
label.y.npc = "bottom",
vjust = -1.5 # Position p-values below the graph
) +
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste("Non-Parametric Comparison by", var)
) +
theme_minimal() +
theme(plot.margin = unit(c(1, 1, 2, 1), "cm")) # Extra space for p-values
# Print the plot for the current variable
print(p)
}
# Reshape data for ggplot
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Define grouping variables
grouping_vars <- c("Gender", "Age", "Years_Work_exp", "Hospital_Unit", "Years_Education")
# Loop through each grouping variable and create separate plots
for (var in grouping_vars) {
# Generate individual plot
p <- ggplot(long_data, aes_string(x = "Score_Type", y = "Score", fill = var)) +
geom_boxplot() +
stat_compare_means(
aes_string(group = var),
method = "wilcox.test", # Non-parametric test
label.y.npc = "bottom",
vjust = -1.5 # Position p-values below the graph
) +
labs(
fill = var,
x = "Score Type",
y = "Score",
title = paste(" Comparison of Knowledge score by", var)
) +
theme_minimal() +
theme(plot.margin = unit(c(1, 1, 2, 1), "cm")) # Extra space for p-values
# Print the plot for the current variable
print(p)
}
# Perform Wilcoxon Signed-Rank Test
wilcox_result <- wilcox.test(data$Pre_Score, data$Post_score, paired = TRUE)
# Print Test Result
print(wilcox_result)
# Reshape data for visualization
library(tidyr)
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Boxplot for Pre and Post Scores
library(ggplot2)
ggplot(long_data, aes(x = Score_Type, y = Score, fill = Score_Type)) +
geom_boxplot() +
labs(
title = "Comparison of Knowledge: Pre-Score vs. Post-Score",
x = "Score Type",
y = "Score"
) +
theme_minimal()
# Perform Wilcoxon Signed-Rank Test
wilcox_result <- wilcox.test(data$Pre_Score, data$Post_score, paired = TRUE)
# Reshape data for visualization
library(tidyr)
library(ggplot2)
library(ggpubr)
long_data <- data %>%
pivot_longer(cols = c("Pre_Score", "Post_score"),
names_to = "Score_Type", values_to = "Score")
# Boxplot with P-value
ggplot(long_data, aes(x = Score_Type, y = Score, fill = Score_Type)) +
geom_boxplot() +
stat_compare_means(
method = "wilcox.test",
paired = TRUE,
label = "p.format" # Display p-value
) +
labs(
title = "Comparison of Knowledge: Pre-Score vs. Post-Score",
x = "Score Type",
y = "Score"
) +
theme_minimal()