Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions R/calcPCBrainAge.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ calcPCBrainAge <- function(DNAm, pheno = NULL, CpGImputation = NULL){
###################################################
### Check if all necessary CpGs are in the data ###
###################################################

# Debug: convert input data to data.frame if it's a matrix
if (is.matrix(DNAm)) {
DNAm <- as.data.frame(DNAm)
message("Input matrix is converted to a data frame.")
}

missingCpGs <- rownames(PCBrainAge_Model$rotation)[!(rownames(PCBrainAge_Model$rotation) %in% colnames(DNAm))]
DNAm[,missingCpGs] <- NA
if(!is.na(missingCpGs[1])){ #if there are columns of CpGs missing...
if(is.null(CpGImputation)){ #...and the user didn't specify a mean CpG vector, use the Hannum 2013 blood mean data
data("imputeMissingBrainCpGs")
for(i in 1:length(missingCpGs)){
datMeth[,missingCpGs[i]] <- imputeMissingBrainCpGs[missingCpGs[i]]
DNAm[,missingCpGs[i]] <- imputeMissingBrainCpGs[missingCpGs[i]]
}} else{ #...otherwise use the user's mean CpG vector which contains the required CpG values
for(i in 1:length(missingCpGs)){
datMeth[,missingCpGs[i]] <- CpGImputation[missingCpGs[i]]
DNAm[,missingCpGs[i]] <- CpGImputation[missingCpGs[i]]
}
}
}
Expand All @@ -54,7 +61,7 @@ calcPCBrainAge <- function(DNAm, pheno = NULL, CpGImputation = NULL){
DNAm <- DNAm[,match(rownames(PCBrainAge_Model$rotation), colnames(DNAm))]

if(any(is.na(DNAm))){
DNAm <- apply(DNAm,2,meanimpute)
DNAm <- apply(DNAm,2,meanImpute) # Debug: call the function meanImpute
message("Mean imputation successfully completed for missing CpG values")
}

Expand All @@ -73,13 +80,3 @@ calcPCBrainAge <- function(DNAm, pheno = NULL, CpGImputation = NULL){
pheno
}
}