-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpute_data_processing.R
More file actions
173 lines (135 loc) · 4.63 KB
/
impute_data_processing.R
File metadata and controls
173 lines (135 loc) · 4.63 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
# =============================================
# MULTIPLE IMPUTATION & EXPLORATORY ANALYSIS
# =============================================
# ----------------------------
# 1. LOAD LIBRARIES
# ----------------------------
required_packages <- c("mice", "MASS", "VIM", "lattice", "dplyr",
"ggplot2", "rlang")
missing <- required_packages[!required_packages %in% installed.packages()[,"Package"]]
if (length(missing) > 0) install.packages(missing)
suppressPackageStartupMessages({
lapply(required_packages, library, character.only = TRUE)
})
# ----------------------------
# 2. LOAD & CLEAN DATA
# ----------------------------
df <- read.csv("/Users/martinnwadiugwu/Downloads/RAC_data_sheet1.csv",
stringsAsFactors = FALSE, na.strings = c("", "NA", "NaN", ".", " "))
df <- df %>%
mutate(
Age = as.numeric(gsub("\\+", "", Age)),
Gender = factor(Gender),
APOE_Status = factor(APOE_Status),
Pathology = factor(Pathology),
Diagnosis = factor(Diagnosis),
Highest_Education_Level = factor(Highest_Education_Level),
Braak = factor(Braak, levels = 0:6, ordered = TRUE),
Years_Education = factor(Years_Education, levels = 9:25, ordered = TRUE),
last_MMSE = factor(last_MMSE, levels = 0:30, ordered = TRUE)
)
# ----------------------------
# 3. MISSINGNESS DIAGNOSTICS
# ----------------------------
cat("Missing data pattern:\n")
md.pattern(df)
cat("Missing data summary:\n")
aggr(df, numbers = TRUE, prop = FALSE)
# ----------------------------
# 4. IMPUTATION SETUP
# ----------------------------
meth <- make.method(df)
pred <- make.predictorMatrix(df)
if ("ID" %in% names(df)) {
meth["ID"] <- ""
pred[, "ID"] <- 0
pred["ID", ] <- 0
}
# Assign methods
meth["Age"] <- "pmm"
meth["PMI.h."] <- "pmm"
meth["KCNH8"] <- "pmm"
meth["ZEB2"] <- "pmm"
meth["ST18"] <- "pmm"
meth["ZNF536"] <- "pmm"
meth["Gender"] <- "logreg"
meth["Diagnosis"] <- "polyreg"
meth["Pathology"] <- "polyreg"
meth["Highest_Education_Level"] <- "polyreg"
meth["APOE_Status"] <- "polyreg"
meth["structure"] <- "polyreg"
meth["Braak"] <- "polr"
meth["Years_Education"] <- "polr"
meth["last_MMSE"] <- "polr"
# ----------------------------
# 5. RUN MULTIPLE IMPUTATION
# ----------------------------
set.seed(123)
imp <- mice(df, m = 5, method = meth, predictorMatrix = pred)
#colSums(is.na(df)) == nrow(df)
# Just the numerical summary, no plot
#aggr(df, plot = FALSE, numbers = TRUE, prop = FALSE)
#table(df$APOE_Status, useNA = "ifany")
#table(df$Diagnosis, useNA = "ifany")
#df <- df[, colSums(is.na(df)) < nrow(df)]
#meth <- make.method(df)
#pred <- make.predictorMatrix(df)
#imp <- mice(df, m = 5, method = meth, predictorMatrix = pred)
# ----------------------------
# 6. VISUALIZATION
# ----------------------------
# Density plots for numeric variables
numeric_vars <- c("Age", "PMI.h.")
for (v in numeric_vars) {
for (i in 1:imp$m) {
imp_data <- complete(imp, i)
vals <- imp_data[[v]]
if (sum(!is.na(vals)) >= 2) {
print(
ggplot(imp_data, aes(x = !!sym(v))) +
geom_density(fill = "skyblue", alpha = 0.5) +
labs(title = paste("Density plot of", v, "- Imputation", i),
x = v, y = "Density") +
theme_minimal()
)
} else {
message(paste("Skipping", v, "in imputation", i, "- not enough data"))
}
}
}
# Bar plots for categorical variables
categorical_vars <- c("APOE_Status", "Braak", "Diagnosis", "Gender", "Pathology", "last_MMSE", "Highest_Education_Level", "Years_Education", "structure")
for (v in categorical_vars) {
for (i in 1:imp$m) {
imp_data <- complete(imp, i)
vals <- imp_data[[v]]
if (length(unique(na.omit(vals))) >= 2) {
print(
ggplot(imp_data, aes(x = !!sym(v))) +
geom_bar(fill = "lightgreen") +
labs(title = paste("Count plot of", v, "- Imputation", i),
x = v, y = "Count") +
theme_minimal()
)
} else {
message(paste("Skipping", v, "in imputation", i, "- not enough levels"))
}
}
}
# ----------------------------
# 7. SAVE IMPUTED DATA
# ----------------------------
completed_data <- complete(imp, 1)
write.csv(completed_data, "/Users/martinnwadiugwu/Downloads/RAC_data_imputed_new_new11.csv", row.names = FALSE)
# ----------------------------
# 8. OPTIONAL: MODEL FITTING
# ----------------------------
# Check Braak levels before modeling
if (nlevels(df$Braak) >= 2) {
fit <- with(imp, lm(PMI.h. ~ Age + Gender + Diagnosis + APOE_Status + Braak))
} else {
warning("⚠️ Braak has fewer than 2 levels. Excluding from model.")
fit <- with(imp, lm(PMI.h. ~ Age + Gender + Diagnosis + APOE_Status))
}
pooled <- pool(fit)
summary(pooled)