Commit 6b8f81e7b32ee891d8fd7378852288082cb11376
1 parent
b992d1b704
Exists in
master
Fixed issue with similar variable names and added some experimental work
Showing
1 changed file
with
110 additions
and
0 deletions
Show diff stats
RMarkovBlanket.r
1 | #Efrain Gonzalez | 1 | #Efrain Gonzalez |
2 | #8/28/2017 | 2 | #8/28/2017 |
3 | #Code for Markov Blanket | 3 | #Code for Markov Blanket |
4 | 4 | ||
5 | 5 | ||
6 | #The required libraries | 6 | #The required libraries |
7 | library(pryr) | 7 | library(pryr) |
8 | library(MASS) | 8 | library(MASS) |
9 | library(dplyr) | 9 | library(dplyr) |
10 | library(tidyr) | 10 | library(tidyr) |
11 | library(readr) | 11 | library(readr) |
12 | library(stringr) | 12 | library(stringr) |
13 | 13 | ||
14 | 14 | ||
15 | #Have the user choose an original Dot file that they want to use | 15 | #Have the user choose an original Dot file that they want to use |
16 | DotFile <- file.choose() | 16 | DotFile <- file.choose() |
17 | TheDotP1 <- read_delim(DotFile,delim = "\t",col_names = FALSE) %>% | 17 | TheDotP1 <- read_delim(DotFile,delim = "\t",col_names = FALSE) %>% |
18 | dplyr::filter(!grepl("->|[{}]",X1)) %>% | 18 | dplyr::filter(!grepl("->|[{}]",X1)) %>% |
19 | dplyr::filter(!grepl("Banjo",X1)) %>% | 19 | dplyr::filter(!grepl("Banjo",X1)) %>% |
20 | dplyr::filter(!grepl("labeljust",X1)) | 20 | dplyr::filter(!grepl("labeljust",X1)) |
21 | counterP1 <- 1 | 21 | counterP1 <- 1 |
22 | sizeDotP1 <- dim(TheDotP1)[1] | 22 | sizeDotP1 <- dim(TheDotP1)[1] |
23 | NewDotP1 <- matrix("0",ncol = 2, nrow = sizeDotP1) | 23 | NewDotP1 <- matrix("0",ncol = 2, nrow = sizeDotP1) |
24 | for(counterP1 in 1:sizeDotP1){ | 24 | for(counterP1 in 1:sizeDotP1){ |
25 | coldataP1 <- str_trim(TheDotP1[counterP1,1]) %>% | 25 | coldataP1 <- str_trim(TheDotP1[counterP1,1]) %>% |
26 | as.character(.,stringsAsFactors = FALSE) | 26 | as.character(.,stringsAsFactors = FALSE) |
27 | if(grepl("Banjo|labeljust|>",coldataP1)==FALSE){ | 27 | if(grepl("Banjo|labeljust|>",coldataP1)==FALSE){ |
28 | NumberP1 <- strsplit(coldataP1," ") %>% | 28 | NumberP1 <- strsplit(coldataP1," ") %>% |
29 | .[[1]]%>% | 29 | .[[1]]%>% |
30 | .[1] | 30 | .[1] |
31 | VarNameP1 <- strsplit(coldataP1," ") %>% | 31 | VarNameP1 <- strsplit(coldataP1," ") %>% |
32 | .[[1]] %>% | 32 | .[[1]] %>% |
33 | .[2] %>% | 33 | .[2] %>% |
34 | strsplit(.,"\"") %>% | 34 | strsplit(.,"\"") %>% |
35 | .[[1]] %>% | 35 | .[[1]] %>% |
36 | .[grep("^\\w|^\\d",.)] | 36 | .[grep("^\\w|^\\d",.)] |
37 | NewDotP1[counterP1,1] <- VarNameP1 | 37 | NewDotP1[counterP1,1] <- VarNameP1 |
38 | NewDotP1[counterP1,2] <- NumberP1 | 38 | NewDotP1[counterP1,2] <- NumberP1 |
39 | } | 39 | } |
40 | if(grepl("->",coldataP1) == TRUE){ | 40 | if(grepl("->",coldataP1) == TRUE){ |
41 | break | 41 | break |
42 | } | 42 | } |
43 | } | 43 | } |
44 | 44 | ||
45 | 45 | ||
46 | TheDotP2 <- read_delim(DotFile,delim = "\t",col_names = FALSE) %>% | 46 | TheDotP2 <- read_delim(DotFile,delim = "\t",col_names = FALSE) %>% |
47 | dplyr::filter(grepl("->",X1)) | 47 | dplyr::filter(grepl("->",X1)) |
48 | counterP2 <- 1 | 48 | counterP2 <- 1 |
49 | sizeDotP2 <- dim(TheDotP2)[1] | 49 | sizeDotP2 <- dim(TheDotP2)[1] |
50 | NewDotP2 <- matrix("0",ncol = 2, nrow = sizeDotP2) | 50 | NewDotP2 <- matrix("0",ncol = 2, nrow = sizeDotP2) |
51 | for(counterP2 in 1:sizeDotP2){ | 51 | for(counterP2 in 1:sizeDotP2){ |
52 | coldataP2 <- str_trim(TheDotP2[counterP2,1]) %>% | 52 | coldataP2 <- str_trim(TheDotP2[counterP2,1]) %>% |
53 | as.character(.,stringsAsFactors = FALSE) | 53 | as.character(.,stringsAsFactors = FALSE) |
54 | ParentNumP2 <- strsplit(coldataP2,"->") %>% | 54 | ParentNumP2 <- strsplit(coldataP2,"->") %>% |
55 | .[[1]]%>% | 55 | .[[1]]%>% |
56 | .[1] | 56 | .[1] |
57 | ChildNumP2 <- strsplit(coldataP2,"->") %>% | 57 | ChildNumP2 <- strsplit(coldataP2,"->") %>% |
58 | .[[1]] %>% | 58 | .[[1]] %>% |
59 | .[2] %>% | 59 | .[2] %>% |
60 | strsplit(.,";") %>% | 60 | strsplit(.,";") %>% |
61 | .[[1]] %>% | 61 | .[[1]] %>% |
62 | .[1] | 62 | .[1] |
63 | NewDotP2[counterP2,1] <- ParentNumP2 | 63 | NewDotP2[counterP2,1] <- ParentNumP2 |
64 | NewDotP2[counterP2,2] <- ChildNumP2 | 64 | NewDotP2[counterP2,2] <- ChildNumP2 |
65 | } | 65 | } |
66 | 66 | ||
67 | colnames(NewDotP2) <- c("Parents","Children") | 67 | colnames(NewDotP2) <- c("Parents","Children") |
68 | 68 | ||
69 | #Matching numbers to variable names | 69 | #Matching numbers to variable names |
70 | NewDotP2_2 <- NewDotP2 | 70 | NewDotP2_2 <- NewDotP2 |
71 | for(i in 1:sizeDotP1){ | 71 | for(i in 1:sizeDotP1){ |
72 | #Where is the variable located within NewDotP2 (column one only)? | 72 | #Where is the variable located within NewDotP2 (column one only)? |
73 | chngreq <- grep(paste0("^",NewDotP1[i,2],"$"),NewDotP2_2[,1]) | 73 | chngreq <- grep(paste0("^",NewDotP1[i,2],"$"),NewDotP2_2[,1]) |
74 | if(is.na(sum(chngreq)) == FALSE){ | 74 | if(is.na(sum(chngreq)) == FALSE){ |
75 | if(sum(chngreq) > 0){ | 75 | if(sum(chngreq) > 0){ |
76 | NewDotP2_2[chngreq,1] <- gsub(paste0("^",NewDotP1[i,2],"$"),NewDotP1[i,1],NewDotP2_2[chngreq,1]) | 76 | NewDotP2_2[chngreq,1] <- gsub(paste0("^",NewDotP1[i,2],"$"),NewDotP1[i,1],NewDotP2_2[chngreq,1]) |
77 | } | 77 | } |
78 | } | 78 | } |
79 | #i <- i + 1 | 79 | #i <- i + 1 |
80 | } | 80 | } |
81 | NewDotP2_2 | 81 | NewDotP2_2 |
82 | for(j in 1:sizeDotP1){ | 82 | for(j in 1:sizeDotP1){ |
83 | #Where is the variable located within NewDotP2 (column two only)? | 83 | #Where is the variable located within NewDotP2 (column two only)? |
84 | chngreq <- grep(paste0("^",NewDotP1[j,2],"$"),NewDotP2_2[,2]) | 84 | chngreq <- grep(paste0("^",NewDotP1[j,2],"$"),NewDotP2_2[,2]) |
85 | if(is.na(sum(chngreq)) == FALSE){ | 85 | if(is.na(sum(chngreq)) == FALSE){ |
86 | if(sum(chngreq) > 0){ | 86 | if(sum(chngreq) > 0){ |
87 | NewDotP2_2[chngreq,2] <- gsub(paste0("^",NewDotP1[j,2],"$"),NewDotP1[j,1],NewDotP2_2[chngreq,2]) | 87 | NewDotP2_2[chngreq,2] <- gsub(paste0("^",NewDotP1[j,2],"$"),NewDotP1[j,1],NewDotP2_2[chngreq,2]) |
88 | } | 88 | } |
89 | } | 89 | } |
90 | #j <- j + 1 | 90 | #j <- j + 1 |
91 | } | 91 | } |
92 | 92 | ||
93 | lrgMarkov <- dim(NewDotP2_2)[1] | 93 | lrgMarkov <- dim(NewDotP2_2)[1] |
94 | Blanky <- function(MarkovDegree = 20, VariableStartName = "Alzheimer", VariableEndName = "GRIN2A"){ | 94 | Blanky <- function(MarkovDegree = 20, VariableStartName = "Alzheimer", VariableEndName = "GRIN2A"){ |
95 | #Finding the Parents and Children | 95 | #Finding the Parents and Children |
96 | d <- 1 | 96 | d <- 1 |
97 | AllNamList1 <- vector("list",length = 3) | 97 | AllNamList1 <- vector("list",length = 3) |
98 | #AllNamList <- vector("list", length = 3) | 98 | #AllNamList <- vector("list", length = 3) |
99 | ##Finding the Parents of the Parents | 99 | ##Finding the Parents of the Parents |
100 | ##A list of lists | 100 | ##A list of lists |
101 | ##outer set by the degree of the Markov blanket | 101 | ##outer set by the degree of the Markov blanket |
102 | AllVarList <- vector("list",length = 3) | 102 | AllVarList <- vector("list",length = 3) |
103 | varfound <- 0 | 103 | varfound <- 0 |
104 | for(d in 1:MarkovDegree){ | 104 | for(d in 1:MarkovDegree){ |
105 | colnames(NewDotP2_2) <- NULL | 105 | colnames(NewDotP2_2) <- NULL |
106 | ##Which variable are you looking for? | 106 | ##Which variable are you looking for? |
107 | ##This is the VariableEndName | 107 | ##This is the VariableEndName |
108 | if(d == 1){ | 108 | if(d == 1){ |
109 | ##Finding the Parents for the variable | 109 | ##Finding the Parents for the variable |
110 | LocPofVar <- grep(paste0("^",VariableStartName,"$"),NewDotP2_2[,2]) | 110 | LocPofVar <- grep(paste0("^",VariableStartName,"$"),NewDotP2_2[,2]) |
111 | PofVar <- NewDotP2_2[LocPofVar,1] | 111 | PofVar <- NewDotP2_2[LocPofVar,1] |
112 | AllNamList1[[1]] <- PofVar | 112 | AllNamList1[[1]] <- PofVar |
113 | AllNamList1[[1]] <- AllNamList1[[1]][!duplicated(AllNamList1[[1]])] | 113 | AllNamList1[[1]] <- AllNamList1[[1]][!duplicated(AllNamList1[[1]])] |
114 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[1]])) | 114 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[1]])) |
115 | 115 | ||
116 | ##Finding the Children for the variable | 116 | ##Finding the Children for the variable |
117 | LocCofVar <- grep(paste0("^",VariableStartName,"$"),NewDotP2_2[,1]) | 117 | LocCofVar <- grep(paste0("^",VariableStartName,"$"),NewDotP2_2[,1]) |
118 | CofVar <- NewDotP2_2[LocCofVar,2] | 118 | CofVar <- NewDotP2_2[LocCofVar,2] |
119 | AllNamList1[[2]] <- CofVar | 119 | AllNamList1[[2]] <- CofVar |
120 | AllNamList1[[2]] <- AllNamList1[[2]][!duplicated(AllNamList1[[2]])] | 120 | AllNamList1[[2]] <- AllNamList1[[2]][!duplicated(AllNamList1[[2]])] |
121 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[2]])) | 121 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[2]])) |
122 | 122 | ||
123 | ##Finding the Co-Parents of the Children for the variable | 123 | ##Finding the Co-Parents of the Children for the variable |
124 | NumofChild <- length(CofVar) | 124 | NumofChild <- length(CofVar) |
125 | if(NumofChild > 0){ | 125 | if(NumofChild > 0){ |
126 | ##Creating a list of the Co-Parents for each of the children | 126 | ##Creating a list of the Co-Parents for each of the children |
127 | ##list size is based on the amount of Children | 127 | ##list size is based on the amount of Children |
128 | COPlist <- vector("character", length = 0) | 128 | COPlist <- vector("character", length = 0) |
129 | nc <- 1 | 129 | nc <- 1 |
130 | for(nc in 1:NumofChild){ | 130 | for(nc in 1:NumofChild){ |
131 | LocCOPofVar <- grep(paste0("^",CofVar[nc],"$"),NewDotP2_2[,2]) | 131 | LocCOPofVar <- grep(paste0("^",CofVar[nc],"$"),NewDotP2_2[,2]) |
132 | COPofVar <- NewDotP2_2[LocCOPofVar,1] | 132 | COPofVar <- NewDotP2_2[LocCOPofVar,1] |
133 | if(sum(grepl(VariableStartName,COPofVar)) >= 1){ | 133 | if(sum(grepl(VariableStartName,COPofVar)) >= 1){ |
134 | #positions of variable start name within the vector of co parents | 134 | #positions of variable start name within the vector of co parents |
135 | posoforig <- grep(paste0("^",VariableStartName,"$"),COPofVar) | 135 | posoforig <- grep(paste0("^",VariableStartName,"$"),COPofVar) |
136 | COPofVar <- COPofVar[-posoforig] | 136 | COPofVar <- COPofVar[-posoforig] |
137 | COPlist <- append(COPlist,COPofVar) | 137 | COPlist <- append(COPlist,COPofVar) |
138 | } else{ | 138 | } else{ |
139 | #COPlist[[nc]] <- COPofVar[COPofVar!=VariableStartName] | 139 | #COPlist[[nc]] <- COPofVar[COPofVar!=VariableStartName] |
140 | COPlist <- append(COPlist,COPofVar) | 140 | COPlist <- append(COPlist,COPofVar) |
141 | } | 141 | } |
142 | #nc <- nc + 1 | 142 | #nc <- nc + 1 |
143 | } | 143 | } |
144 | } else { | 144 | } else { |
145 | ##Making COPlist empty | 145 | ##Making COPlist empty |
146 | COPlist <- vector("character",length = 0) | 146 | COPlist <- vector("character",length = 0) |
147 | } | 147 | } |
148 | AllNamList1[[3]] <- COPlist | 148 | AllNamList1[[3]] <- COPlist |
149 | AllNamList1[[3]] <- AllNamList1[[3]][!duplicated(AllNamList1[[3]])] | 149 | AllNamList1[[3]] <- AllNamList1[[3]][!duplicated(AllNamList1[[3]])] |
150 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[3]])) | 150 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllNamList1[[3]])) |
151 | 151 | ||
152 | AllVarList[[1]] <- AllNamList1 | 152 | AllVarList[[1]] <- AllNamList1 |
153 | 153 | ||
154 | } else if(d > 1){ | 154 | } else if(d > 1){ |
155 | ##inner set by the length of the previous AllVarlist we are working on | 155 | ##inner set by the length of the previous AllVarlist we are working on |
156 | lPreVList <- length(AllVarList[[d-1]]) | 156 | lPreVList <- length(AllVarList[[d-1]]) |
157 | ef <- 1 | 157 | ef <- 1 |
158 | ##PCCP will eventually equal the total size that we expect for the iteration (#d) | 158 | ##PCCP will eventually equal the total size that we expect for the iteration (#d) |
159 | ## which is just lPreVList * 3 | 159 | ## which is just lPreVList * 3 |
160 | PCCP <- 1 | 160 | PCCP <- 1 |
161 | newsize <- (lPreVList * 3) | 161 | newsize <- (lPreVList * 3) |
162 | if(d > 3){ | 162 | if(d > 3){ |
163 | AllVarList[[d]] <- vector("list") | 163 | AllVarList[[d]] <- vector("list") |
164 | } | 164 | } |
165 | for(ef in 1:lPreVList){ | 165 | for(ef in 1:lPreVList){ |
166 | ##Finding the Parents | 166 | ##Finding the Parents |
167 | NumofVars <- length(AllVarList[[d-1]][[ef]]) | 167 | NumofVars <- length(AllVarList[[d-1]][[ef]]) |
168 | if(NumofVars > 0){ | 168 | if(NumofVars > 0){ |
169 | ##Creating a list of the Parents for each of the Variables | 169 | ##Creating a list of the Parents for each of the Variables |
170 | ##list size is based on the amount of Previous Variables | 170 | ##list size is based on the amount of Previous Variables |
171 | PofVlist <- vector("character", length = 0) | 171 | PofVlist <- vector("character", length = 0) |
172 | np <- 1 | 172 | np <- 1 |
173 | for(np in 1:NumofVars){ | 173 | for(np in 1:NumofVars){ |
174 | LocPofVar <- grep(paste0("^",AllVarList[[d-1]][[ef]][np],"$"),NewDotP2_2[,2]) | 174 | LocPofVar <- grep(paste0("^",AllVarList[[d-1]][[ef]][np],"$"),NewDotP2_2[,2]) |
175 | PofVar <- NewDotP2_2[LocPofVar,1] | 175 | PofVar <- NewDotP2_2[LocPofVar,1] |
176 | PofVlist <- append(PofVlist,PofVar) | 176 | PofVlist <- append(PofVlist,PofVar) |
177 | #np <- np + 1 | 177 | #np <- np + 1 |
178 | } | 178 | } |
179 | } else { | 179 | } else { |
180 | ##Making COPlist empty | 180 | ##Making COPlist empty |
181 | PofVlist <- vector("character",length = 0) | 181 | PofVlist <- vector("character",length = 0) |
182 | } | 182 | } |
183 | AllVarList[[d]][[PCCP]] <- PofVlist | 183 | AllVarList[[d]][[PCCP]] <- PofVlist |
184 | AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] | 184 | AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] |
185 | ##Have you found the VariableEndName? | 185 | ##Have you found the VariableEndName? |
186 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) | 186 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) |
187 | PCCP <- PCCP + 1 | 187 | PCCP <- PCCP + 1 |
188 | 188 | ||
189 | ##Finding the Children | 189 | ##Finding the Children |
190 | if(NumofVars > 0){ | 190 | if(NumofVars > 0){ |
191 | ##Creating a list of the Children for each of the Previous Parents | 191 | ##Creating a list of the Children for each of the Previous Parents |
192 | ##list size is based on the amount of Previous Parents | 192 | ##list size is based on the amount of Previous Parents |
193 | CofVlist <- vector("character", length = 0) | 193 | CofVlist <- vector("character", length = 0) |
194 | np <- 1 | 194 | np <- 1 |
195 | for(np in 1:NumofVars){ | 195 | for(np in 1:NumofVars){ |
196 | LocCofVar <- grep(paste0("^",AllVarList[[d-1]][[ef]][np],"$"),NewDotP2_2[,1]) | 196 | LocCofVar <- grep(paste0("^",AllVarList[[d-1]][[ef]][np],"$"),NewDotP2_2[,1]) |
197 | CofVar <- NewDotP2_2[LocCofVar,2] | 197 | CofVar <- NewDotP2_2[LocCofVar,2] |
198 | #if(sum(grepl(VariableStartName,CofVar)) >= 1){ | 198 | #if(sum(grepl(VariableStartName,CofVar)) >= 1){ |
199 | # #positions of variable start name within the vector of co parents | 199 | # #positions of variable start name within the vector of co parents |
200 | # posoforig <- grep(VariableStartName,COPofVar) | 200 | # posoforig <- grep(VariableStartName,COPofVar) |
201 | # COPofVar <- COPofVar[-posoforig] | 201 | # COPofVar <- COPofVar[-posoforig] |
202 | # COPlist <- append(COPlist,COPofVar) | 202 | # COPlist <- append(COPlist,COPofVar) |
203 | #} else{ | 203 | #} else{ |
204 | CofVlist <- append(CofVlist,CofVar) | 204 | CofVlist <- append(CofVlist,CofVar) |
205 | #} | 205 | #} |
206 | #np <- np + 1 | 206 | #np <- np + 1 |
207 | } | 207 | } |
208 | } else { | 208 | } else { |
209 | ##Making CofPlist empty | 209 | ##Making CofPlist empty |
210 | CofVlist <- vector("character",length = 0) | 210 | CofVlist <- vector("character",length = 0) |
211 | } | 211 | } |
212 | AllVarList[[d]][[PCCP]] <- CofVlist | 212 | AllVarList[[d]][[PCCP]] <- CofVlist |
213 | AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] | 213 | AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] |
214 | ##Have you found the VariableEndName yet? | 214 | ##Have you found the VariableEndName yet? |
215 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) | 215 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) |
216 | PCCP <- PCCP + 1 | 216 | PCCP <- PCCP + 1 |
217 | 217 | ||
218 | ##Finding the Co-Parents | 218 | ##Finding the Co-Parents |
219 | NumofCVars <- length(CofVlist) | 219 | NumofCVars <- length(CofVlist) |
220 | if(NumofCVars > 0){ | 220 | if(NumofCVars > 0){ |
221 | ncp <- 1 | 221 | ncp <- 1 |
222 | CPofClist <- vector("character",length = 0) | 222 | CPofClist <- vector("character",length = 0) |
223 | for(ncp in 1:NumofCVars){ | 223 | for(ncp in 1:NumofCVars){ |
224 | LocCPofCVar <- grep(paste0("^",CofVlist[ncp],"$"),NewDotP2_2[,2]) | 224 | LocCPofCVar <- grep(paste0("^",CofVlist[ncp],"$"),NewDotP2_2[,2]) |
225 | CPofCVar <- NewDotP2_2[LocCPofCVar,1] | 225 | CPofCVar <- NewDotP2_2[LocCPofCVar,1] |
226 | #if(sum(grepl(,CPofCVar)) >= 1){ | 226 | #if(sum(grepl(,CPofCVar)) >= 1){ |
227 | # #positions of variable start name within the vector of co parents | 227 | # #positions of variable start name within the vector of co parents |
228 | # posoforig <- grep(VariableStartName,COPofVar) | 228 | # posoforig <- grep(VariableStartName,COPofVar) |
229 | # COPofVar <- COPofVar[-posoforig] | 229 | # COPofVar <- COPofVar[-posoforig] |
230 | # COPlist <- append(COPlist,COPofVar) | 230 | # COPlist <- append(COPlist,COPofVar) |
231 | #} else{ | 231 | #} else{ |
232 | CPofClist <- append(CPofClist,CPofCVar) | 232 | CPofClist <- append(CPofClist,CPofCVar) |
233 | #} | 233 | #} |
234 | #ncp <- ncp + 1 | 234 | #ncp <- ncp + 1 |
235 | } | 235 | } |
236 | 236 | ||
237 | } else { | 237 | } else { |
238 | ##Making COPlist empty | 238 | ##Making COPlist empty |
239 | CPofClist <- vector("character",length = 0) | 239 | CPofClist <- vector("character",length = 0) |
240 | } | 240 | } |
241 | AllVarList[[d]][[PCCP]] <- CPofClist | 241 | AllVarList[[d]][[PCCP]] <- CPofClist |
242 | AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] | 242 | AllVarList[[d]][[PCCP]] <- AllVarList[[d]][[PCCP]][!duplicated(AllVarList[[d]][[PCCP]])] |
243 | ##Have you found VariableEndName now? | 243 | ##Have you found VariableEndName now? |
244 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) | 244 | varfound <- varfound + sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[PCCP]])) |
245 | PCCP <- PCCP + 1 | 245 | PCCP <- PCCP + 1 |
246 | #ef <- ef + 1 | 246 | #ef <- ef + 1 |
247 | } | 247 | } |
248 | } | 248 | } |
249 | 249 | ||
250 | ##Stop if you have found the VariableEndName value | 250 | ##Stop if you have found the VariableEndName value |
251 | if(varfound > 0){ | 251 | if(varfound > 0){ |
252 | break | 252 | break |
253 | } | 253 | } |
254 | #d <- d + 1 | 254 | #d <- d + 1 |
255 | } | 255 | } |
256 | ##The Markov Degree is that found below | 256 | ##The Markov Degree is that found below |
257 | d | 257 | d |
258 | } | 258 | } |
259 | |||
260 | |||
261 | |||
262 | #############Methods for finding paths############# | ||
263 | ###Beyond Here Everything is still experimental### | ||
264 | #Find the path to the variable in question | ||
265 | RshipList <- vector("list",length = d) | ||
266 | l = d | ||
267 | for(l in d:1){ | ||
268 | if(l == d){ | ||
269 | RshipList[[l]] <- VariableEndName | ||
270 | for(ship in 1:length(AllVarList[[d]])){ | ||
271 | if(sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[ship]])) > 0){ | ||
272 | break | ||
273 | } | ||
274 | } | ||
275 | |||
276 | } else { | ||
277 | modship <- ship %% 3 | ||
278 | intship <- as.integer(ship/3) | ||
279 | if(modship > 0){ | ||
280 | intship <- intship + 1 | ||
281 | } | ||
282 | RshipList[[l]] <- AllVarList[[l]][[intship]] | ||
283 | ship <- intship | ||
284 | } | ||
285 | #l <- l - 1 | ||
286 | } | ||
287 | #Full actual path | ||
288 | k = d | ||
289 | prevar <- VariableEndName | ||
290 | Pathlisting <- vector("list",length = d) | ||
291 | Pathlisting[[d]] <- prevar | ||
292 | for(k in d:1){ | ||
293 | if(k == d){ | ||
294 | for(ship in 1:length(AllVarList[[d]])){ | ||
295 | if(sum(grepl(paste0("^",VariableEndName,"$"),AllVarList[[d]][[ship]])) > 0){ | ||
296 | break | ||
297 | } | ||
298 | } | ||
299 | |||
300 | } | ||
301 | modship <- ship %% 3 | ||
302 | intship <- as.integer(ship/3) | ||
303 | if(modship > 0){ | ||
304 | intship <- intship + 1 | ||
305 | } | ||
306 | if(modship == 0){ | ||
307 | ##When modship == 0 then we are refering to a CoParent | ||
308 | ##Gives how many children the full set of CoParents has | ||
309 | lenCoP <- length(AllVarList[[k]][[(ship - 1)]]) | ||
310 | ##variables that are actually children of the coparent | ||
311 | actvar <- vector("character",length = 0) | ||
312 | ##Parents of actvar | ||
313 | Pactvar <- vector("character",length =0) | ||
314 | ##variables that could have led to the previous set of variables | ||
315 | Wanvar <- vector("character",length = 0) | ||
316 | m <- 1 | ||
317 | for(m in 1:lenCoP){ | ||
318 | LCPoVar <- grep(paste0("^",AllVarList[[k]][[(ship - 1)]][m],"$"),NewDotP2_2[,2]) | ||
319 | CPoVar <- NewDotP2_2[LCPoVar,1] | ||
320 | lenprevar <- length(prevar) | ||
321 | y <- 1 | ||
322 | for(y in 1:lenprevar){ | ||
323 | |||
324 | if(sum(grepl(prevar[y],CPoVar)) >= 1){ | ||
325 | actvar <- append(actvar,AllVarList[[k]][[(ship - 1)]][m]) | ||
326 | Pactvar <- append(Pactvar,CPoVar) | ||
327 | } | ||
328 | #y <- y + 1 | ||
329 | } | ||
330 | #m <- m + 1 | ||
331 | |||
332 | } | ||
333 | Pactvar <- Pactvar[!duplicated(Pactvar)] | ||
334 | ##Searching to see if any of the parents are in the previous degree | ||
335 | ##The right children will have both the current variable and a previous degrees variable as parents | ||
336 | o <- 1 | ||
337 | for(o in 1:length(Pactvar)){ | ||
338 | if(sum(grepl(Pactvar[o],AllVarList[[(k - 1)]][[intship]])) >= 1){ | ||
339 | Wanvar <- append(Wanvar,Pactvar[o]) | ||
340 | } | ||
341 | #o <- o + 1 | ||
342 | } | ||
343 | Wanvar <- Wanvar[!duplicated(Wanvar)] | ||
344 | prevar <- Wanvar | ||
345 | |||
346 | } else if(modship == 1){ | ||
347 | #When modship == 1 then we are referring to a Parent | ||
348 | LCofVar <- grep(paste0("^",prevar,"$"),NewDotP2_2[,1]) | ||
349 | CoVar <- NewDotP2_2[LCofVar,1] | ||
350 | lenC <- length(CoVar) | ||
351 | for(o in 1:lenC){ | ||
352 | if(grepl(CoVar[o],AllVarList[[k]][[intship]]) == TRUE){ | ||
353 | |||
354 | |||
355 | } | ||
356 | |||
357 | } | ||
358 | |||
359 | |||
360 | } else if(modship == 2){ | ||
361 | #When modship == 0 then we are referring to a Child | ||
362 | |||
363 | } | ||
364 | |||
365 | ship <- intship | ||
366 | |||
367 | Pathlisting[[(k - 1)]] <- prevar | ||
368 | } | ||
259 | 369 |