Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
95c8021
Update calcZhang.R
albertchen42 Nov 14, 2023
5b62266
Update calcEpiTOC.R
albertchen42 Nov 14, 2023
ab9d787
Update calcEpiTOC2.R
albertchen42 Nov 14, 2023
46b172c
Update calcEpiTOC.R
jkasa13 Dec 7, 2024
d8482b1
change calcAlcoholMcCartney
Dec 8, 2024
281b8c7
Update calcBMIMcCartney.R
jkasa13 Dec 8, 2024
8219eb3
Update calcAlcoholMcCartney.R
jkasa13 Dec 8, 2024
8d212c7
Update calcBMIMcCartney.R
jkasa13 Dec 8, 2024
5ab94d1
Update calcBocklandt.R
jkasa13 Dec 8, 2024
0e259a3
Update calcBohlin.R
jkasa13 Dec 8, 2024
e1d969c
Update calcClockCategory.R
jkasa13 Dec 8, 2024
808d3e4
Update calcCoreClocks.R
jkasa13 Dec 8, 2024
d451919
Update calcDNAmClockCortical.R
jkasa13 Dec 8, 2024
137a1dc
Update calcDNAmTL.R
jkasa13 Dec 8, 2024
6651c8f
Update calcDunedinPoAm38.R
jkasa13 Dec 8, 2024
e55c941
Update calcEpiTOC.R
jkasa13 Dec 8, 2024
042b60c
Update calcEpiTOC2.R
jkasa13 Dec 8, 2024
170a4b8
Update calcGaragnani.R
jkasa13 Dec 8, 2024
df0e2b7
Update calcHRSInChPhenoAge.R
jkasa13 Dec 8, 2024
14c5832
Update calcHannum.R
jkasa13 Dec 8, 2024
9b22ed3
Update calcHorvath1.R
jkasa13 Dec 8, 2024
5c14f32
Update calcHorvath2.R
jkasa13 Dec 8, 2024
8bfd8cd
Update calcHypoClock.R
jkasa13 Dec 8, 2024
9dd99e1
Update calcKnight.R
jkasa13 Dec 8, 2024
d02218e
Update calcLeeControl.R
jkasa13 Dec 8, 2024
e6371dd
Update calcLeeRefinedRobust.R
jkasa13 Dec 8, 2024
67731b8
Update calcLeeRobust.R
jkasa13 Dec 8, 2024
1a331a8
Update calcLin.R
jkasa13 Dec 8, 2024
eefcc61
Update calcMayne.R
jkasa13 Dec 8, 2024
5f8cd2d
Update calcMiAge.R
jkasa13 Dec 8, 2024
6edbfe2
Update calcPEDBE.R
jkasa13 Dec 8, 2024
4d16c15
Update calcPhenoAge.R
jkasa13 Dec 8, 2024
4eaee71
Update calcSmokingMcCartney.R
jkasa13 Dec 8, 2024
70f37cb
Update calcUserClocks.R
jkasa13 Dec 8, 2024
759ce97
Update calcVidalBralo.R
jkasa13 Dec 8, 2024
9b68442
Update calcWeidner.R
jkasa13 Dec 8, 2024
2c74045
Update calcZhang.R
jkasa13 Dec 8, 2024
08c46b0
Update calcZhang2019.R
jkasa13 Dec 8, 2024
02d2406
added calcPCClocks
Dec 8, 2024
b17e8e1
Update CalcPCClocks
Dec 16, 2024
6df5f9d
Update CalcPCClocks
Dec 16, 2024
69e1c7a
Update CalcPCClocks
Dec 16, 2024
2f60355
Added GrimAgeV1 and GrimAgeV2
Apr 16, 2025
72f2449
Add GrimAgeV1 and GrimAgeV2
Apr 17, 2025
5d9c793
Update Citations
Apr 17, 2025
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
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description: Code to easily calculate nearly all of the CpG-based epigenetic clo
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.0
RoxygenNote: 7.3.2
Imports:
readr,
dplyr,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export(calcDunedinPoAm38)
export(calcEpiTOC)
export(calcEpiTOC2)
export(calcGaragnani)
export(calcGrimAgeV1)
export(calcGrimAgeV2)
export(calcHRSInChPhenoAge)
export(calcHannum)
export(calcHorvath1)
Expand All @@ -28,6 +30,7 @@ export(calcLeeRobust)
export(calcLin)
export(calcMayne)
export(calcMiAge)
export(calcPCClocks)
export(calcPEDBE)
export(calcPhenoAge)
export(calcSmokingMcCartney)
Expand Down
Binary file added R/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions R/calcAlcoholMcCartney.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcAlcoholMcCartney(exampleBetas, examplePheno, imputation = F)
calcAlcoholMcCartney <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
#' @examples calcAlcoholMcCartney(exampleBetas, examplePheno, imputation = T)
calcAlcoholMcCartney <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
### Read in the Data###
Expand Down
4 changes: 2 additions & 2 deletions R/calcBMIMcCartney.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcBMIMcCartney(exampleBetas, examplePheno, imputation = F)
calcBMIMcCartney <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
#' @examples calcBMIMcCartney(exampleBetas, examplePheno, imputation = T)
calcBMIMcCartney <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
### Read in the Data###
Expand Down
4 changes: 2 additions & 2 deletions R/calcBocklandt.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcBocklandt(exampleBetas, examplePheno, imputation = F)
calcBocklandt <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
#' @examples calcBocklandt(exampleBetas, examplePheno, imputation = T)
calcBocklandt <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
### Read in the Data###
Expand Down
4 changes: 2 additions & 2 deletions R/calcBohlin.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcBohlin(exampleBetas, examplePheno, imputation = F)
calcBohlin <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
#' @examples calcBohlin(exampleBetas, examplePheno, imputation = T)
calcBohlin <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
### Read in the Data###
Expand Down
4 changes: 2 additions & 2 deletions R/calcClockCategory.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#' @return A dataframe that has column names of the core clocks giving you multiple clocks at once in an easy to compute function. These will be appended onto the existing pheno dataframe as defined in the inputs.
#' @export
#'
#' @examples calcClockCategory(exampleBetas, examplePheno, category = "chronological", imputation = F)
#' @examples calcClockCategory(exampleBetas, examplePheno, category = "chronological", imputation = T)
calcClockCategory <- function(DNAm, pheno , category = NULL,
CpGImputation = NULL, imputation = F){
CpGImputation = NULL, imputation = T){

message("Please remember to cite all of the clocks you have used! Please refer to the README.md file for assistance.")

Expand Down
4 changes: 2 additions & 2 deletions R/calcCoreClocks.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return A dataframe that has column names of the core clocks giving you multiple clocks at once in an easy to compute function. These will be appended onto the existing pheno dataframe as defined in the inputs.
#' @export
#'
#' @examples calcCoreClocks(exampleBetas, examplePheno, imputation = F)
calcCoreClocks <- function(DNAm, pheno , CpGImputation = NULL, imputation = F){
#' @examples calcCoreClocks(exampleBetas, examplePheno, imputation = T)
calcCoreClocks <- function(DNAm, pheno , CpGImputation = NULL, imputation = T){

message("Please remember to cite the core Clocks you have used! Please refer to the README.md file for assistance.")

Expand Down
4 changes: 2 additions & 2 deletions R/calcDNAmClockCortical.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcDNAmClockCortical(exampleBetas, examplePheno, imputation = F)
#' @examples calcDNAmClockCortical(exampleBetas, examplePheno, imputation = T)
#' @examples calcDNAmClockCortical(exampleBetas, examplePheno, CpGImputation = DNAmClockCortical_imputeRef, imputation = T) # Imputation in the case that there are missing CpGs using the original Brain Imputation from authors
calcDNAmClockCortical <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
calcDNAmClockCortical <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

###################################################
### Check if all necessary CpGs are in the data ###
Expand Down
4 changes: 2 additions & 2 deletions R/calcDNAmTL.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcDNAmTL(exampleBetas, examplePheno, imputation = F)
calcDNAmTL <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
#' @examples calcDNAmTL(exampleBetas, examplePheno, imputation = T)
calcDNAmTL <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
### Read in the Data###
Expand Down
2 changes: 1 addition & 1 deletion R/calcDunedinPoAm38.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @export
#' @source <https://doi.org/10.7554/eLife.54870>
#'
#' @examples calcDunedinPoAm38(exampleBetas, examplePheno, imputation = F)
#' @examples calcDunedinPoAm38(exampleBetas, examplePheno, imputation = T)
calcDunedinPoAm38 <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

###########################
Expand Down
2 changes: 1 addition & 1 deletion R/calcEpiTOC.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @export
#' @source <https://zenodo.org/record/2632938#.YfGA3S-B2Cg>
#'
#' @examples calcEpiTOC(exampleBetas, examplePheno, imputation = F)
#' @examples calcEpiTOC(exampleBetas, examplePheno, imputation = T)
calcEpiTOC <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
Expand Down
2 changes: 1 addition & 1 deletion R/calcEpiTOC2.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @export
#' @source <https://zenodo.org/record/2632938#.YfGA3S-B2Cg>
#'
#' @examples calcEpiTOC2(exampleBetas, examplePheno, imputation = F)
#' @examples calcEpiTOC2(exampleBetas, examplePheno, imputation = T)
calcEpiTOC2 <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T, approximated = F){

#######################
Expand Down
4 changes: 2 additions & 2 deletions R/calcGaragnani.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcGaragnani(exampleBetas, examplePheno, imputation = F)
calcGaragnani <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
#' @examples calcGaragnani(exampleBetas, examplePheno, imputation = T)
calcGaragnani <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
### Read in the Data###
Expand Down
91 changes: 91 additions & 0 deletions R/calcGrimAgeV1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#' CalcGrimAgeV1
#'
#' @description A function to calculate GrimAgeV1
#'
#' @param DNAm a matrix of methylation beta values. Needs to be rows = samples and columns = CpGs, with rownames and colnames.
#' @param pheno Optional: The sample phenotype data (also with samples as rows) that the clock will be appended to.
#' @param CpGImputation An optional namesd vector for the mean value of each CpG that will be input from another dataset if such values are missing here (from sample cleaning)
#' @param imputation Logical value that will allows you to perform (T)/ skip (F) imputation of mean values for missing CpGs. Warning: when imputation = F if there are missing CpGs, it will automatically ignore these CpGs during calculation, making the clock values less accurate.
#'
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples CalcGrimAgeV1(exampleBetas, examplePheno, imputation = F)
#'
#'
#'
#'
#'


calcGrimAgeV1 <- function(datMeth, datPheno){

if(!("Age" %in% variable.names(datPheno))){
stop("Error: datPheno must have a column named Age")
}
if(!("Female" %in% variable.names(datPheno))){
stop("Error: datPheno must have a column named Female")
}
if(sum(startsWith(colnames(datMeth),"cg")) == 0){
warning("Warning: It looks like you may need to format datMeth using t(datMeth) to get samples as rows!")
}

#In datPheno, rows are samples and columns are phenotypic variables.
#One of the phenotypic variables must be "Age", and another one "Female" (coded as Female = 1, Male = 0; should be a numeric variable as this will be included in PCGrimAge calculation)
#Also ensure that the order of datMeth sample IDs matches your phenotype data sample IDs, otherwise your data will be scrambled

#load(file = paste(path_to_GrimAgeV1_directory,"CalcGrimAgeV1.RData", sep = ""))
#print("Loaded GrimAgeV1 data.")

#If needed: Fill in missing CpGs needed for calculation of PCs; use mean values from GSE40279 (Hannum 2013; blood)- note that for other tissues you might prefer to use a different one
datMeth <- as.data.frame(datMeth)
if(length(c(CpGs_GrimAge1[!(CpGs_GrimAge1 %in% colnames(datMeth))],CpGs_GrimAge1[apply(datMeth[,colnames(datMeth) %in% CpGs_GrimAge1], 2, function(x)all(is.na(x)))])) == 0){
message("GrimAgeV1 - No CpGs were NA for all samples")
} else{
missingCpGs_GrimAge1 <- c(CpGs_GrimAge1[!(CpGs_GrimAge1 %in% colnames(datMeth))])
datMeth[,missingCpGs_GrimAge1] <- NA
datMeth = datMeth[,CpGs_GrimAge1]
missingCpGs_GrimAge1 <- CpGs_GrimAge1[apply(datMeth[,CpGs_GrimAge1], 2, function(x)all(is.na(x)))]
for(i in 1:length(missingCpGs_GrimAge1)){
if (!is.na(imputeMissingCpGs_GrimAge1[missingCpGs_GrimAge1[i]])){
datMeth[,missingCpGs_GrimAge1[i]] <- imputeMissingCpGs_GrimAge1[missingCpGs_GrimAge1[i]]
#print(imputeMissingCpGs_GrimAge1[missingCpGs_GrimAge1[i]])
}
else{
datMeth[,missingCpGs_GrimAge1[i]] <- 0

}
}
message(paste0("GrimAgeV1 needed to fill in ", length(missingCpGs_GrimAge1), " CpGs..."))
}

#Prepare methylation data for calculation of PC Clocks (subset to 78,464 CpGs and perform imputation if needed)
#datMeth <- datMeth[,CpGs]
#meanimpute <- function(x) ifelse(is.na(x),mean(x,na.rm=T),x)
#datMeth <- apply(datMeth,2,meanimpute)
#Note: you may substitute another imputation method of your choice (e.g. KNN), but we have not found the method makes a significant difference.
#message("Mean imputation successfully completed for any missing CpG values")

#Initialize a data frame for PC clocks
DNAmAge <- datPheno

# Calculate GrimAge Clocks
temp <- as.matrix(cbind(datMeth, Age = as.numeric(datPheno$Age, Female=datPheno$Female)))
DNAmAge$DNAmPACKYRS <- as.numeric(temp[,names(CalcGrimAgeV1$PACKYRS.model)] %*% CalcGrimAgeV1$PACKYRS.model + CalcGrimAgeV1$PACKYRS.intercept)
DNAmAge$DNAmADM <- as.numeric(temp[,names(CalcGrimAgeV1$ADM.model)] %*% CalcGrimAgeV1$ADM.model + CalcGrimAgeV1$ADM.intercept)
DNAmAge$DNAmB2M <- as.numeric(temp[,names(CalcGrimAgeV1$B2M.model)] %*% CalcGrimAgeV1$B2M.model + CalcGrimAgeV1$B2M.intercept)
DNAmAge$DNAmCystatinC <- as.numeric(temp[,names(CalcGrimAgeV1$CystatinC.model)] %*% CalcGrimAgeV1$CystatinC.model + CalcGrimAgeV1$CystatinC.intercept)
DNAmAge$DNAmGDF15 <- as.numeric(temp[,names(CalcGrimAgeV1$GDF15.model)] %*% CalcGrimAgeV1$GDF15.model + CalcGrimAgeV1$GDF15.intercept)
DNAmAge$DNAmLeptin <- as.numeric(temp[,names(CalcGrimAgeV1$Leptin.model)] %*% CalcGrimAgeV1$Leptin.model + CalcGrimAgeV1$Leptin.intercept)
DNAmAge$DNAmPAI1 <- as.numeric(temp[,names(CalcGrimAgeV1$PAI1.model)] %*% CalcGrimAgeV1$PAI1.model + CalcGrimAgeV1$PAI1.intercept)
DNAmAge$DNAmTIMP1 <- as.numeric(temp[,names(CalcGrimAgeV1$TIMP1.model)] %*% CalcGrimAgeV1$TIMP1.model + CalcGrimAgeV1$TIMP1.intercept)
#print("Components Calculated.Calculating GrimAGE...")
DNAmAge$Age<-DNAmAge$Age
DNAmAge$Female<-DNAmAge$Female
DNAmAge$GrimAgeV1 <- as.numeric(as.matrix(DNAmAge[,CalcGrimAgeV1$components]) %*% CalcGrimAgeV1$GrimAge.model ) #+ CalcGrimAgeV1$GrimAge.intercept)
y<-DNAmAge$GrimAgeV1
DNAmAge$GrimAgeV1 <- (((y- CalcGrimAgeV1$GrimAge.transform[3])/CalcGrimAgeV1$GrimAge.transform[4])*CalcGrimAgeV1$GrimAge.transform[2]) + CalcGrimAgeV1$GrimAge.transform[1]

return(DNAmAge)

}
101 changes: 101 additions & 0 deletions R/calcGrimAgeV2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#' calcGrimAge2V2
#'
#' @description A function to calculate GrimAgeV2
#'
#' @param DNAm a matrix of methylation beta values. Needs to be rows = samples and columns = CpGs, with rownames and colnames.
#' @param pheno Optional: The sample phenotype data (also with samples as rows) that the clock will be appended to.
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcPhenoAge(exampleBetas, examplePheno)
#'
#'

calcGrimAgeV2 <- function(datMeth, datPheno){
if(!("Age" %in% variable.names(datPheno))){
stop("Error: datPheno must have a column named Age")
}
if(!("Female" %in% variable.names(datPheno))){
stop("Error: datPheno must have a column named Female")
}
if(sum(startsWith(colnames(datMeth),"cg")) == 0){
warning("Warning: It looks like you may need to format datMeth using t(datMeth) to get samples as rows!")
}

#In datPheno, rows are samples and columns are phenotypic variables.
#One of the phenotypic variables must be "Age", and another one "Female" (coded as Female = 1, Male = 0; should be a numeric variable as this will be included in PCGrimAge calculation)
#Also ensure that the order of datMeth sample IDs matches your phenotype data sample IDs, otherwise your data will be scrambled

#load(file = paste(path_to_GrimAgeV2_directory,"CalcGrimAgeV2.RData", sep = ""))
#print("Loaded GrimAgeV2 data.")

#If needed: Fill in missing CpGs needed for calculation of PCs; use mean values from GSE40279 (Hannum 2013; blood)- note that for other tissues you might prefer to use a different one
# datMeth <- as.data.frame(datMeth)
# if(length(c(CpGs[!(CpGs %in% colnames(datMeth))],CpGs[apply(datMeth[,colnames(datMeth) %in% CpGs], 2, function(x)all(is.na(x)))])) == 0){
# message("No CpGs were NA for all samples")
# } else{
# missingCpGs <- c(CpGs[!(CpGs %in% colnames(datMeth))])
# datMeth[,missingCpGs] <- NA
# datMeth = datMeth[,CpGs]
# missingCpGs <- CpGs[apply(datMeth[,CpGs], 2, function(x)all(is.na(x)))]
# for(i in 1:length(missingCpGs)){
# datMeth[,missingCpGs[i]] <- imputeMissingCpGs[missingCpGs[i]]
# }
# message("Any missing CpGs successfully filled in (see function for more details)")
# }

datMeth <- as.data.frame(datMeth)
if(length(c(CpGs_GrimAge2[!(CpGs_GrimAge2 %in% colnames(datMeth))],CpGs_GrimAge2[apply(datMeth[,colnames(datMeth) %in% CpGs_GrimAge2], 2, function(x)all(is.na(x)))])) == 0){
message("GrimAgeV2 - No CpGs were NA for all samples")
} else{
missingCpGs_GrimAge2 <- c(CpGs_GrimAge2[!(CpGs_GrimAge2 %in% colnames(datMeth))])
datMeth[,missingCpGs_GrimAge2] <- NA
datMeth = datMeth[,CpGs_GrimAge2]
missingCpGs_GrimAge2 <- CpGs_GrimAge2[apply(datMeth[,CpGs_GrimAge2], 2, function(x)all(is.na(x)))]
for(i in 1:length(missingCpGs_GrimAge2)){
if (!is.na(imputeMissingCpGs_GrimAge2[missingCpGs_GrimAge2[i]])){
datMeth[,missingCpGs_GrimAge2[i]] <- imputeMissingCpGs_GrimAge2[missingCpGs_GrimAge2[i]]
#print(imputeMissingCpGs_GrimAge2[missingCpGs_GrimAge2[i]])
}
else{
datMeth[,missingCpGs_GrimAge2[i]] <- 0
}
}
message(paste0("GrimAgeV2 needed to fill in ", length(missingCpGs_GrimAge2), " CpGs..."))
}

#Prepare methylation data for calculation of PC Clocks (subset to 78,464 CpGs and perform imputation if needed)
#datMeth <- datMeth[,CpGs]
#meanimpute <- function(x) ifelse(is.na(x),mean(x,na.rm=T),x)
#datMeth <- apply(datMeth,2,meanimpute)
#Note: you may substitute another imputation method of your choice (e.g. KNN), but we have not found the method makes a significant difference.
#message("Mean imputation successfully completed for any missing CpG values")

#Initialize a data frame for PC clocks
DNAmAge <- datPheno

# Calculate GrimAge Clocks
temp <- as.matrix(cbind(datMeth, Age = as.numeric(datPheno$Age, Female=datPheno$Female)))
DNAmAge$DNAmPACKYRS <- as.numeric(temp[,names(CalcGrimAge2$PACKYRS.model)] %*% CalcGrimAge2$PACKYRS.model + CalcGrimAge2$PACKYRS.intercept)
DNAmAge$DNAmADM <- as.numeric(temp[,names(CalcGrimAge2$ADM.model)] %*% CalcGrimAge2$ADM.model + CalcGrimAge2$ADM.intercept)
DNAmAge$DNAmB2M <- as.numeric(temp[,names(CalcGrimAge2$B2M.model)] %*% CalcGrimAge2$B2M.model + CalcGrimAge2$B2M.intercept)
DNAmAge$DNAmCystatinC <- as.numeric(temp[,names(CalcGrimAge2$CystatinC.model)] %*% CalcGrimAge2$CystatinC.model + CalcGrimAge2$CystatinC.intercept)
DNAmAge$DNAmGDF15 <- as.numeric(temp[,names(CalcGrimAge2$GDF15.model)] %*% CalcGrimAge2$GDF15.model + CalcGrimAge2$GDF15.intercept)
DNAmAge$DNAmLeptin <- as.numeric(temp[,names(CalcGrimAge2$Leptin.model)] %*% CalcGrimAge2$Leptin.model + CalcGrimAge2$Leptin.intercept)
DNAmAge$DNAmPAI1 <- as.numeric(temp[,names(CalcGrimAge2$PAI1.model)] %*% CalcGrimAge2$PAI1.model + CalcGrimAge2$PAI1.intercept)
DNAmAge$DNAmTIMP1 <- as.numeric(temp[,names(CalcGrimAge2$TIMP1.model)] %*% CalcGrimAge2$TIMP1.model + CalcGrimAge2$TIMP1.intercept)
#New Proteins
DNAmAge$DNAmlogA1C <- as.numeric(temp[,names(CalcGrimAge2$logA1C.model)] %*% CalcGrimAge2$logA1C.model + CalcGrimAge2$logA1C.intercept)
DNAmAge$DNAmlogCRP <- as.numeric(temp[,names(CalcGrimAge2$logCRP.model)] %*% CalcGrimAge2$logCRP.model + CalcGrimAge2$logCRP.intercept)

# DNAmAge$Age<-DNAmAge$cAGE
# DNAmAge$Female<-DNAmAge$cFEMALE

DNAmAge$GrimAgeV2 <- as.numeric(as.matrix(DNAmAge[,CalcGrimAge2$components]) %*% CalcGrimAge2$GrimAge.model ) #+ CalcGrimAge2$GrimAge.intercept)
y<-DNAmAge$GrimAgeV2
#print(y)
DNAmAge$GrimAgeV2 <- (((y- CalcGrimAge2$GrimAge.transform[3])/CalcGrimAge2$GrimAge.transform[4])*CalcGrimAge2$GrimAge.transform[2]) + CalcGrimAge2$GrimAge.transform[1]

return(DNAmAge)

}
4 changes: 2 additions & 2 deletions R/calcHRSInChPhenoAge.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @return If you added the optional pheno input (preferred) the function appends a column with the clock calculation and returns the dataframe. Otherwise, it will return a vector of calculated clock values in order of the
#' @export
#'
#' @examples calcHRSInChPhenoAge(exampleBetas, examplePheno, imputation = F)
calcHRSInChPhenoAge <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = F){
#' @examples calcHRSInChPhenoAge(exampleBetas, examplePheno, imputation = T)
calcHRSInChPhenoAge <- function(DNAm, pheno = NULL, CpGImputation = NULL, imputation = T){

#######################
### Read in the Data###
Expand Down
Loading