-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEWAS_analysis_base_functions.R
More file actions
180 lines (139 loc) · 4.92 KB
/
EWAS_analysis_base_functions.R
File metadata and controls
180 lines (139 loc) · 4.92 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
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
cache_binary_or_categorical_vars <- hash()
is_binary_or_categorical_var <- function(var, df, survey_year_code, log) {
if (var %notin% names(df)){
cat(red('[Var not exists] Var: ', var), '\n')
stop('')
}
cache_key <- paste(survey_year_code, var)
if (cache_key %in% keys(cache_binary_or_categorical_vars)){
if (log) {
cat(bold('[CACHE Var Type] Cache Key: "', cache_key ,'" Type: ',
cache_binary_or_categorical_vars[[cache_key]], sep=''), '\n')
}
return(cache_binary_or_categorical_vars[[cache_key]])
}
# df <- MainTable
ret = 0
df <- df[]
############################
######## ALTER IF OTHER SURVEY YEAR COMBINATIONS ARE USED
############################
if (survey_year_code == 'all'){
} else if (survey_year_code == '234'){
df <- df[df$SDDSRVYR > 1,]
} else if (survey_year_code >= 1 && survey_year_code <= 4){
df <- df[df$SDDSRVYR == survey_year_code,]
} else{
stop('giveb surver year code is not valid')
}
unique_val <- unique(df[[var]])[!is.na(unique(df[[var]]))]
unique_val <- sort(unique_val)
if(length(unique_val) == 2 && all(unique_val == c(0, 1))){
if (log) {
cat(bold('[Var is Binary] (tested) Var: ', var ,
' Unique Vals: ', toString(unique_val), sep=''), '\n')
}
ret <- 1
}
if(length(unique_val) == 1){
# CAN NOT IDENTIFY TYPE OF THIS VAR BECAUSE IT ONLY HAS ONE UNIQUE VALUE REPORTED
ret <- 3
}
else {
if (survey_year_code == 'all'){
var_desc_patel <- VarDescription[VarDescription$var == var, ]
} else {
var_desc_patel <- VarDescription[
(VarDescription$var == var) & (VarDescription$series_num == survey_year_code), ]
if (nrow(var_desc_patel) > 1){
cat(red(bold("[WARNING] Variable ", var, " has more than one record in varDescription for series: ", survey_year_code, sep='')))
}
}
cat_levels <- var_desc_patel$categorical_levels
if (nrow(var_desc_patel) > 0){
if (1 %in% var_desc_patel$is_binary){
if (log) {
cat(bold('[Var is Binary] (Patel marked) Var: ', var ,
' Unique Vals [#', length(unique_val) ,']: ',
toStringVector(unique_val), sep=''), '\n')
}
ret <- 2
} else if (1 %in% var_desc_patel$is_ordinal){
if (log) {
cat(bold('[Var is Ordinal] (Patel marked) Var: ', var,
' Unique Vals [#', length(unique_val) ,']: ',
toStringVector(unique_val), sep=''), '\n')
}
ret <- 2
} else if (is.na(cat_levels) == FALSE && is.character(cat_levels) && nchar(cat_levels) > 0){
if (log) {
cat(bold('[Var is Categorical] (Patel marked) Var: ', var,
' Unique Vals [#', length(unique_val) ,']: ',
toStringVector(unique_val), sep=''), '\n')
}
ret <- 2
}
}
}
if (ret == 0 & log == TRUE){
cat(bold('[Var is Continues] Var: ', var ,
' Unique Vals [#', length(unique_val) ,']: ',
toStringVector(unique_val), sep=''), '... \n')
}
cache_binary_or_categorical_vars[[cache_key]] = ret
return(ret)
}
round_df <- function(x, digits) {
numeric_columns <- sapply(x, mode) == 'numeric'
x[numeric_columns] <- round(x[numeric_columns], digits)
x
}
get_searies_years <- function(searies_number) {
ret <- NULL
if (searies_number == 1){
ret <- '1999-2000'
} else if(searies_number == 2){
ret <- '2001-2002'
} else if (searies_number == 3){
ret <- '2003-2004'
} else if (searies_number == 4){
ret <- '2005-2006'
}
return(ret)
}
toStringVector <- function(v) {
slice_size = 20
if (length(v) < slice_size){
return (toString(v))
} else {
return (paste(toString(v[1:slice_size]), "..."))
}
}
boxcox_lambda <- hash()
boxcox_trans <- function(df, var_name){
lambda <- boxCox(df[[var_name]]~1, family="yjPower", plotit = FALSE)
lam_df <- data.frame(lambda$x, lambda$y)
lambda <- lam_df[with(lam_df, order(-lam_df$lambda.y)),][1,1]
print(paste("BoxCox Lambda for ", var_name , ": ", lambda))
boxcox_lambda[[var_name]] <- lambda
out <- yjPower(df[[var_name]], lambda, jacobian.adjusted=TRUE)
out <- data.frame(out)#, ncol=1)
colnames(out) <- var_name
return(out)
}
boxcox_trans_return_lambda <- function(df, var_name){
lambda <- boxCox(df[[var_name]]~1, family="yjPower", plotit = FALSE)
lam_df <- data.frame(lambda$x, lambda$y)
lambda <- lam_df[with(lam_df, order(-lam_df$lambda.y)),][1,1]
# print(paste("BoxCox Lambda for ", var_name , ": ", lambda))
boxcox_lambda[[var_name]] <- lambda
out <- yjPower(df[[var_name]], lambda, jacobian.adjusted=TRUE)
out <- data.frame(out)#, ncol=1)
colnames(out) <- var_name
return(list("out"=out, "lambda"=lambda))
}
#boxcox_trans(MainTable_subset, 'age')
#boxcox_lambda
logit_trans <- function(x){
return (log(x / (1-x)))
}