forked from boukepieter/MCNA_Analysys_Iraq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcovid_functions.R
More file actions
102 lines (83 loc) · 3.4 KB
/
covid_functions.R
File metadata and controls
102 lines (83 loc) · 3.4 KB
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
add_p_value_to_summary_table<-function(result){
p.value <- result$hypothesis.test$result$p.value
result$summary.statistic$p.value <- p.value
if(is.null(p.value)){
result$summary.statistic$p.value <- NA
}
result
}
ggtitle_w_pvalue <-function(result){
title_text<-paste(result$parameters$dependent.var,": ", round(result$hypothesis.test$result$p.value,3))
ggtitle(title_text)
}
resultlist_summary_statistics_as_one_table<-function(results){
results %>%
lapply(function(x){x$summary.statistic}) %>% do.call(rbind,.)
}
result_format_numbers <- function (result){
if(is.null(result)){return(NULL)}
if (!is.list(result)) {
stop("result must be a list")
}
if(is.null(result$summary.statistic)){
return(result)
}
if(!is.data.frame(result$summary.statistic)){
stop("result$summary.statistic is not a data frame.. something went horribly wrong earlier!!")
}
# if(!is.numeric(result$summary.statistic$numbers)){stop("resultl summary statistic numbers are not numeric")}
result$summary.statistic$numbers <- as.numeric(result$summary.statistic$numbers)
result$summary.statistic$min <- as.numeric(result$summary.statistic$min)
result$summary.statistic$max <- as.numeric(result$summary.statistic$max)
is_percent <- grepl("\\_categorical\\_", result$parameters$case)
has_confints <- !all(is.na(c(result$summary.statistic$min,
result$summary.statistic$max)))
has_dependent_var_values <- !all(is.na(result$summary.statistic$dependent.var.value))
has_independent_var_values <- !all(is.na(result$summary.statistic$independent.var.value))
nums_formated <- round(as.numeric(result$summary.statistic$numbers),
2)
if (is_percent) {
nums_formated <- paste0(round(nums_formated * 100), "%")
}
if (!is_percent) {
nums_formated <- as.character(round(nums_formated, 2))
}
if (has_confints) {
if (is_percent) {
min_formated <- paste0(round(result$summary.statistic$min *
100), "%")
max_formated <- paste0(round(result$summary.statistic$max *
100), "%")
}
else {
min_formated <- paste0(round(result$summary.statistic$min,
2), "")
max_formated <- paste0(round(result$summary.statistic$max),
"")
}
interval <- paste0("(", min_formated, "-",
max_formated, ")")
}
else {
interval <- ""
}
formated_nums <- paste(nums_formated, interval)
formated_nums[formated_nums == "0 (0-0)"] <- "0"
formated_nums[formated_nums == "0% (0%-0%)"] <- "0%"
result$summary.statistic$numbers <- formated_nums
return(result)
}
#MERGING THE NAME HEADER WITH THE FACTORS IN THE FIRST ROW
reshape_summstats <- function(summs) {
summs[, c("X", "independent.var", "independent.var.value", "se",
"repeat.var")] <- NULL
summs <- as.data.frame(t(summs))
names(summs) <- as.matrix(summs[1, ])
summs <- summs[-1, ]
summs[] <- lapply(summs, function(x) type.convert(as.character(x)))
namesvar<-as.vector(sapply( summs[1,], paste0, collapse=""))
names(summs) <- ifelse(namesvar=="NA", names(summs),
paste(names(summs[1,]), namesvar, sep = "_"))
summs<-summs[-1,]
return(summs)
}