diff --git a/RMarkovBlanket.r b/RMarkovBlanket.r index 475422c..2f80f27 100644 --- a/RMarkovBlanket.r +++ b/RMarkovBlanket.r @@ -76,7 +76,7 @@ for(i in 1:sizeDotP1){ NewDotP2_2[chngreq,1] <- gsub(paste0("^",NewDotP1[i,2],"$"),NewDotP1[i,1],NewDotP2_2[chngreq,1]) } } - i <- i + 1 + #i <- i + 1 } NewDotP2_2 for(j in 1:sizeDotP1){ @@ -87,11 +87,11 @@ for(j in 1:sizeDotP1){ NewDotP2_2[chngreq,2] <- gsub(paste0("^",NewDotP1[j,2],"$"),NewDotP1[j,1],NewDotP2_2[chngreq,2]) } } - j <- j + 1 + #j <- j + 1 } lrgMarkov <- dim(NewDotP2_2)[1] -Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", VariableEndName = "Age"){ +Blanky <- function(MarkovDegree = 20, VariableStartName = "Alzheimer", VariableEndName = "GRIN2A"){ #Finding the Parents and Children d <- 1 AllNamList1 <- vector("list",length = 3) @@ -100,20 +100,25 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va ##A list of lists ##outer set by the degree of the Markov blanket AllVarList <- vector("list",length = 3) + varfound <- 0 for(d in 1:MarkovDegree){ colnames(NewDotP2_2) <- NULL ##Which variable are you looking for? ##This is the VariableEndName if(d == 1){ ##Finding the Parents for the variable - LocPofVar <- grep(VariableStartName,NewDotP2_2[,2]) + LocPofVar <- grep(paste0("^",VariableStartName,"$"),NewDotP2_2[,2]) PofVar <- NewDotP2_2[LocPofVar,1] AllNamList1[[1]] <- PofVar + AllNamList1[[1]] <- AllNamList1[[1]][!duplicated(AllNamList1[[1]])] + varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[1]])) ##Finding the Children for the variable - LocCofVar <- grep(VariableStartName,NewDotP2_2[,1]) + LocCofVar <- grep(paste0("^",VariableStartName,"$"),NewDotP2_2[,1]) CofVar <- NewDotP2_2[LocCofVar,2] AllNamList1[[2]] <- CofVar + AllNamList1[[2]] <- AllNamList1[[2]][!duplicated(AllNamList1[[2]])] + varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[2]])) ##Finding the Co-Parents of the Children for the variable NumofChild <- length(CofVar) @@ -123,25 +128,29 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va COPlist <- vector("character", length = 0) nc <- 1 for(nc in 1:NumofChild){ - LocCOPofVar <- grep(CofVar[nc],NewDotP2_2[,2]) + LocCOPofVar <- grep(paste0("^",CofVar[nc],"$"),NewDotP2_2[,2]) COPofVar <- NewDotP2_2[LocCOPofVar,1] if(sum(grepl(VariableStartName,COPofVar)) >= 1){ #positions of variable start name within the vector of co parents - posoforig <- grep(VariableStartName,COPofVar) + posoforig <- grep(paste0("^",VariableStartName,"$"),COPofVar) COPofVar <- COPofVar[-posoforig] COPlist <- append(COPlist,COPofVar) } else{ #COPlist[[nc]] <- COPofVar[COPofVar!=VariableStartName] COPlist <- append(COPlist,COPofVar) } - nc <- nc + 1 + #nc <- nc + 1 } } else { ##Making COPlist empty COPlist <- vector("character",length = 0) } AllNamList1[[3]] <- COPlist + AllNamList1[[3]] <- AllNamList1[[3]][!duplicated(AllNamList1[[3]])] + varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[3]])) + AllVarList[[1]] <- AllNamList1 + } else if(d > 1){ ##inner set by the length of the previous AllVarlist we are working on lPreVList <- length(AllVarList[[d-1]]) @@ -162,16 +171,19 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va PofVlist <- vector("character", length = 0) np <- 1 for(np in 1:NumofVars){ - LocPofVar <- grep(AllVarList[[d-1]][[ef]][np],NewDotP2_2[,2]) + LocPofVar <- grep(paste0("^",AllVarList[[d-1]][[ef]][np],"$"),NewDotP2_2[,2]) PofVar <- NewDotP2_2[LocPofVar,1] PofVlist <- append(PofVlist,PofVar) - np <- np + 1 + #np <- np + 1 } } else { ##Making COPlist empty PofVlist <- vector("character",length = 0) } AllVarList[[d]][[PCCP]] <- PofVlist + AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] + ##Have you found the VariableEndName? + varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) PCCP <- PCCP + 1 ##Finding the Children @@ -181,7 +193,7 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va CofVlist <- vector("character", length = 0) np <- 1 for(np in 1:NumofVars){ - LocCofVar <- grep(AllVarList[[d-1]][[ef]][np],NewDotP2_2[,1]) + LocCofVar <- grep(paste0("^",AllVarList[[d-1]][[ef]][np],"$"),NewDotP2_2[,1]) CofVar <- NewDotP2_2[LocCofVar,2] #if(sum(grepl(VariableStartName,CofVar)) >= 1){ # #positions of variable start name within the vector of co parents @@ -191,13 +203,16 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va #} else{ CofVlist <- append(CofVlist,CofVar) #} - np <- np + 1 + #np <- np + 1 } } else { ##Making CofPlist empty CofVlist <- vector("character",length = 0) } AllVarList[[d]][[PCCP]] <- CofVlist + AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] + ##Have you found the VariableEndName yet? + varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) PCCP <- PCCP + 1 ##Finding the Co-Parents @@ -206,7 +221,7 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va ncp <- 1 CPofClist <- vector("character",length = 0) for(ncp in 1:NumofCVars){ - LocCPofCVar <- grep(CofVlist[ncp],NewDotP2_2[,2]) + LocCPofCVar <- grep(paste0("^",CofVlist[ncp],"$"),NewDotP2_2[,2]) CPofCVar <- NewDotP2_2[LocCPofCVar,1] #if(sum(grepl(,CPofCVar)) >= 1){ # #positions of variable start name within the vector of co parents @@ -216,7 +231,7 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va #} else{ CPofClist <- append(CPofClist,CPofCVar) #} - ncp <- ncp + 1 + #ncp <- ncp + 1 } } else { @@ -224,15 +239,19 @@ Blanky <- function(MarkovDegree = lrgMarkov, VariableStartName = "Alzheimer", Va CPofClist <- vector("character",length = 0) } AllVarList[[d]][[PCCP]] <- CPofClist + AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] + ##Have you found VariableEndName now? + varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) PCCP <- PCCP + 1 - ef <- ef + 1 + #ef <- ef + 1 } } + ##Stop if you have found the VariableEndName value - if(sum(grepl(VariableEndName,AllVarList)) > 0){ + if(varfound > 0){ break } - d <- d + 1 + #d <- d + 1 } ##The Markov Degree is that found below d