mcmcabn-utiles.R 5.69 KB
Newer Older
Gilles Kratzer's avatar
Gilles Kratzer committed
1
2
3
4
5
6
###############################################################################
## abn-internal.R ---
## Author          : Gilles Kratzer
## Document created   : 13/02/2018
## Last modification  :
###############################################################################
Gilles Kratzer's avatar
Gilles Kratzer committed
7

Gilles Kratzer's avatar
Gilles Kratzer committed
8
9
10
##-------------------------------------------------------------------------
##Internal function that call multiple times strsplit() and remove space
##-------------------------------------------------------------------------
Gilles Kratzer's avatar
Gilles Kratzer committed
11

Gilles Kratzer's avatar
Gilles Kratzer committed
12
13
14
15
16
17
18
19
20
strsplits <- function(x, splits, ...)
{
  for (split in splits)
  {
    x <- unlist(strsplit(x, split, ...))
  }
  x<-gsub(" ", "", x, fixed = TRUE) #remove space
  return(x[!x == ""]) # Remove empty values
}
Gilles Kratzer's avatar
Gilles Kratzer committed
21

Gilles Kratzer's avatar
Gilles Kratzer committed
22
23
24
25
26
27
28
29
30
31
##-------------------------------------------------------------------------
##Internal function that produce a square matrix length(name) with {0,1}
##depending on f.
##f have to start with ~
##terms are entries of name
##terms are separated by +
## term1 | term2 indicates col(term1) row(term2) puts a 1
## term1 | term2:term3: ... : is used as a sep
## . = all terms in name
##-------------------------------------------------------------------------
Gilles Kratzer's avatar
Gilles Kratzer committed
32

Gilles Kratzer's avatar
Gilles Kratzer committed
33
formula.mcmcabn<-function(f, name){
Gilles Kratzer's avatar
Gilles Kratzer committed
34

Gilles Kratzer's avatar
Gilles Kratzer committed
35
  name_orignial<-name
Gilles Kratzer's avatar
Gilles Kratzer committed
36

Gilles Kratzer's avatar
Gilles Kratzer committed
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
  f<-as.character(f)

  ##tests for consistence ----------------------------------------------------------------------
  ##start as a formula
  if(!grepl('~',f[1],fixed = T)){stop("DAG specifications should start with a ~")}

  ##transformation name + or | or : or . or name to name_name
  if(sum((c("+", "|", ":", ".") %in% unlist(strsplit(name,split = c("")))))!=0){
    for(i in 1:length(name)){
      if(sum(unlist(strsplit(name[i],split = c(""))) %in% c("+"))!=0){
        f[[2]]<-gsub(name[i],gsub("+", "_", name[i],fixed = TRUE),f[[2]],fixed = TRUE)
        name[i]<-gsub("+", "_", name[i],fixed = TRUE)
      }
      if(sum(unlist(strsplit(name[i],split = c(""))) %in% c("|"))!=0){
        f[[2]]<-gsub(name[i],gsub("|", "_", name[i],fixed = TRUE),f[[2]],fixed = TRUE)
        name[i]<-gsub("|", "_", name[i],fixed = TRUE)
      }
      if(sum(unlist(strsplit(name[i],split = c(""))) %in% c(":"))!=0){
        f[[2]]<-gsub(name[i],gsub(":", "_", name[i],fixed = TRUE),f[[2]],fixed = TRUE)
        name[i]<-gsub(":", "_", name[i],fixed = TRUE)
      }
      if(sum(unlist(strsplit(name[i],split = c(""))) %in% c("."))!=0){
        f[[2]]<-gsub(name[i],gsub(".", "_", name[i],fixed = TRUE),f[[2]],fixed = TRUE)
        name[i]<-gsub(".", "_", name[i],fixed = TRUE)
      }
    }
  }

  ##collapse name
  name.c<-paste(name, collapse=':')
  ##Split by terms
  f.p<-strsplit(x = f[[2]],split = "+", fixed=TRUE)

  ##nothing more than name variable in the dag formula
  tmp.test<-strsplits(x = f[[2]],splits = c("+", "|", ":", "."), fixed=TRUE)
  if(sum(!(tmp.test %in% name))!=0){stop("DAG formulation contains some variables not in provided names")}
  ##End of tests for consistence ----------------------------------------------------------------

  ##creat the void matrix
  out<-matrix(data = 0,nrow = length(name),ncol = length(name))

  ##delete all spaces
  f.p<-gsub(" ", "", f.p[[1]], fixed = TRUE)

  ##replace "." by all names
  f.p.completed<-gsub(".",name.c,f.p, fixed = TRUE)

  ##atomization of left term


  ##contruction of the output matrix
  for(i in 1:length(f.p)){
    tmp<-f.p.completed[i]

    ##forget unique terms -> test for |
    if(grepl('|',tmp,fixed = TRUE)){

      ##split wrt |
      tmp.p<-strsplit(x = tmp,split = "|", fixed=TRUE)

      ##test for multiple terms and contruction of the list first term
      if(grepl(':',tmp.p[[1]][1])){tmp.p.p.1<-strsplit(x = tmp.p[[1]][1],split = ":", fixed=TRUE)}
      if(!grepl(':',tmp.p[[1]][1])){tmp.p.p.1<-tmp.p[[1]][1]}

      ##test for multiple terms and contruction of the list second term
      if(grepl(':',tmp.p[[1]][2])){tmp.p.p.2<-strsplit(x = tmp.p[[1]][2],split = ":", fixed=TRUE)}
      if(!grepl(':',tmp.p[[1]][2])){tmp.p.p.2<-tmp.p[[1]][2]}

      ##loop over the
      for(j in 1:length(tmp.p.p.1[[1]])){
        for(k in 1:length(tmp.p.p.2[[1]])){
          ##update of matrix
          out[grep(tmp.p.p.1[[1]][j],name),grep(tmp.p.p.2[[1]][k],name)]<-1

        }
      }
    }

  }

  ##avoid auto dependance
  diag(out)<-0

  ##only 0 and 1
  out[out>1] <- 1

  ##naming
  colnames(out)<-name_orignial
  rownames(out)<-name_orignial
  ##output
  return(out)
}
Gilles Kratzer's avatar
Gilles Kratzer committed
129
130
131
132
133
134
135
136
137
138

##-------------------------------------------------------------------------
##Ancestor function
##-------------------------------------------------------------------------

is.integer0 <- function(x)
{
  is.integer(x) && length(x) == 0L
}

Gilles Kratzer's avatar
updates    
Gilles Kratzer committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
##-------------------------------------------------------------------------
##Ancestor function
##-------------------------------------------------------------------------


#
# ancestors<-function(nodes, dag){
#   diag(dag)<-0
#    if(!is.integer0(which(x = dag[nodes,]==1,arr.ind = TRUE))){
#      tmp <- which(x = dag[nodes,]==1,arr.ind = TRUE)
#     return(unname(c(tmp,ancestors(nodes = tmp,dag = dag))))
#     }else{
#     return(NULL)
#   }
# }
Gilles Kratzer's avatar
Gilles Kratzer committed
154
155
156
157
158
159

##-------------------------------------------------------------------------
##Descendant function
##-------------------------------------------------------------------------

descendants<-function(nodes, dag){
Gilles Kratzer's avatar
updates    
Gilles Kratzer committed
160
  if(!identical(topoSortMAT(amat = dag,index = FALSE),character(0))){
Gilles Kratzer's avatar
Gilles Kratzer committed
161
162
163
164
165
166
167
168
169
  if(!is.integer0(which(x = dag[,nodes]==1,arr.ind = TRUE)) && diag(dag)==0){
    if(length(nodes)==1){
    tmp <- unname(which(x = dag[,nodes]==1,arr.ind = TRUE))}else{
      tmp <- t(unname(which(x = dag[,nodes]==1,arr.ind = TRUE)))[1,]
    }
    return(unique(unname(c(tmp,descendants(nodes = (tmp),dag = dag)))))
  }else{
    return(NULL)
  }
Gilles Kratzer's avatar
updates    
Gilles Kratzer committed
170
171
172
  }
  return("Not a DAG")
  }
Gilles Kratzer's avatar
Gilles Kratzer committed
173
174

range01 <- function(x){(x-min(x))/(max(x)-min(x))}
Gilles Kratzer's avatar
updates    
Gilles Kratzer committed
175
176

'%!in%' <- function(x,y)!('%in%'(x,y))