-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheda_by_lukereding.R
180 lines (164 loc) · 5.65 KB
/
eda_by_lukereding.R
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
find_type <- function(x) {
case_when(
is.factor(x) ~ "factor",
is.character(x) ~ "character",
is.numeric(x) ~ "numeric",
TRUE ~ "not sure"
)
}
permute_icc <- function(x, y, n = 99) {
actual <- ICCbare(x, y)
nulls <- vector(length = length(n), mode = "numeric")
for(i in seq_along(1:n)) {
scrambled_x <- sample(x, length(x), replace = F)
nulls[i] <- ICCbare(scrambled_x, y)
}
(sum(abs(nulls) > ifelse(actual > 0, actual, -actual)) + 1) / (n+1)
}
permute_tau <- function(x, y, n = 99) {
actual <- GKtau(x, y)$tauxy
nulls <- vector(length = length(n), mode = "numeric")
for(i in seq_along(1:n)) {
scrambled_x <- sample(x, length(x), replace = F)
nulls[i] <- GKtau(scrambled_x, y)$tauxy
}
(sum(abs(nulls) > ifelse(actual > 0, actual, -actual)) + 1) / (n+1)
}
# to do:
## get p-values
eda <- function(x, plot = FALSE) {
x <- as.data.frame(x)
num_rows <- ncol(x)^2 - ncol(x)
df <- tibble(var1 = vector(mode = "character", length = 1),
var2 = vector(mode = "character", length = 1),
statistic = vector(mode = "character", length = 1),
value = vector(mode = "double", length = 1),
p_value = vector(mode = "double", length = 1),
n = vector(mode = "integer", length = 1))
for(i in seq_along(1:ncol(x)))
for(j in seq_along(1:ncol(x))) {
if(i < j){
# get type of columns i and j
var_1_type <- find_type(x[,i])
var_2_type <- find_type(x[,j])
#print(paste("var1 type: ", var_1_type, "\nvar2 type: ", var_2_type, "\n\n"))
x1 <- x[,i]
x2 <- x[,j]
# remove NAs for simplicity
if(any(is.na(x1))){
# get NA indicies
ind <- which(is.na(x1))
x1 <- x1[-ind]
x2 <- x2[-ind]
}
if(any(is.na(x2))){
# get NA indicies
ind <- which(is.na(x2))
x1 <- x1[-ind]
x2 <- x2[-ind]
}
# make sure x1 and x2 are the same length
stopifnot(length(x1) == length(x2))
n <- length(x1)
if(var_1_type == "numeric" & var_2_type == "numeric") {
# run a correlation
result <- cor.test(x1, x2)
df <- add_row(df,
var1 = names(x)[i],
var2 = names(x)[j],
statistic = "r",
value = result$estimate,
p_value = result$p.value,
n = n
)
} else if(var_1_type == "factor" & var_2_type == "numeric") {
# run an ANOVA or t-test, depending on number of levels
num_levels <- length(levels(x1))
require(ICC)
result <- ICCbare(x1, x2)
p <- permute_icc(x1, x2)
df <- add_row(df,
var1 = names(x)[i],
var2 = names(x)[j],
statistic = "ICC",
value = result,
p_value = p,
n = n
)
} else if(var_1_type == "numeric" & var_2_type == "factor") {
# run an ANOVA or t-test, depending on number of levels
num_levels <- length(levels(x2))
require(ICC)
result <- ICCbare(x2, x1)
p <- permute_icc(x2, x1)
df <- add_row(df,
var1 = names(x)[i],
var2 = names(x)[j],
statistic = "ICC",
value = result,
p_value = p,
n = n
)
} else if(var_1_type == "factor" & var_2_type == "factor") {
require("GoodmanKruskal")
# compute the GKtau statistic
stat1 <- GKtau(x1, x2)$tauxy
stat2 <- GKtau(x1, x2)$tauyx
p1 <- permute_tau(x1, x2)
p2 <- permute_tau(x2, x1)
df <- add_row(df,
var1 = names(x)[i],
var2 = names(x)[j],
statistic = "tau",
value = stat1,
p_value = p1,
n = n
)
df <- add_row(df,
var1 = names(x)[j],
var2 = names(x)[i],
statistic = "tau",
value = stat2,
p_value = p2,
n = n
)
} else{
# return an empty row
df <- add_row(df,
var1 = names(x)[i],
var2 = names(x)[j],
statistic = NA_character_,
value = NA_integer_,
p_value = NA_real_,
n = n
)
}
}
}
if(plot == TRUE) {
df[-1,] %>%
filter(!is.na(value)) %>%
unite(variables, var1, var2, sep = " by ") %>%
mutate(`possibly significant` = if_else(p_value < 0.05, "significant", "NS")) %>%
ggplot(aes(y = value, x = reorder(variables, value), color = `possibly significant`)) +
geom_point() +
coord_flip() +
facet_wrap(~statistic, scales = "free") +
theme_minimal() +
scale_color_manual(values = c("#37454B", "#E84F22"))
} else{
df[-1,]
}
}
# eda(iris)
#
# eda(iris) %>%
# filter(!is.na(value)) %>%
# unite(variables, var1, var2, sep = " :: ") %>%
# mutate(significant = if_else(p_value < 0.05, "significant", "NS")) %>%
# ggplot(aes(y = value, x = reorder(variables, value), color = significant)) +
# geom_point() +
# coord_flip() +
# facet_wrap(~statistic, scales = "free") +
# theme_minimal()
# ggsave("~/Desktop/out.pdf", width = 20, height = 15)