@@ -44,13 +44,17 @@ $(SIGNATURES)
4444### Required arguments
4545```julia
4646* `name::AbstractString` : Name for the DAG object
47- * `d` : DAG definition as an
48- OrderedDict (see extended help)
49- AbstractString (as in ggm or dagitty)
50- AdjacencyMatrix
47+ * `d::ModelDefinition` : DAG definition
5148```
5249
53- ### Optional positional argument
50+ where
51+ ```
52+ ModelDefinition = Union{OrderedDict, AbstractString, NamedArray}
53+ ```
54+
55+ See the extended help for a usage example.
56+
57+ ### Keyword arguments
5458```julia
5559* `df::DataFrame` : DataFrame with observations
5660```
@@ -80,15 +84,15 @@ Coming from R's dagitty:
8084amat <- dagitty("dag { {X V} -> U; S1 <- U; {Y V} -> W; S2 <- W}”)
8185```julia
8286dag = DAG("my_name", "dag { {X V} -> U; S1 <- U; {Y V} -> W; S2 <- W}”)
83- display(dag.a ) # Show the adjacency_matrix
87+ display(dag) # Show the DAG
8488```
8589
8690Coming from R's ggm:
8791
8892amat <- DAG(U~X+V, S1~U, W~V+Y, S2~W, order=FALSE)
8993```julia
9094dag = DAG("my_name", "DAG(U~X+V, S1~U, W~V+Y, S2~W”)
91- display(dag.a ) # Show the adjacency_matrix
95+ display(dag) # Show the DAG
9296```
9397
9498### Acknowledgements
@@ -105,121 +109,36 @@ The Julia translation is licenced under: MIT.
105109
106110Part of API, exported.
107111"""
108- function DAG (name:: AbstractString , d:: OrderedDict , df:: DataFrame )
109-
110- vars = dag_vars (d)
111- a = adjacency_matrix (d)
112- e = edge_matrix (d)
113-
114- # Compute covariance matrix and store as NamedArray
115-
116- @assert length (names (df)) == length (vars) " DataFrame has different number of columns"
117- s = NamedArray (cov (Array (df)), (names (df), names (df)), (" Rows" , " Cols" ))
118-
119- # Create object
120-
121- DAG (name, d, a, e, s, df, vars)
122-
123- end
124-
125- """
126-
127- # `DAG`
128-
129- $(SIGNATURES)
130-
131- Part of API, exported.
132- """
133- function DAG (name:: AbstractString , d:: OrderedDict )
134-
135- vars = dag_vars (d)
136- a = adjacency_matrix (d)
137- e = edge_matrix (d)
138-
139- # Create object
140-
141- DAG (name, d, a, e, nothing , nothing , vars)
142- end
143-
144- """
145-
146- # `DAG`
147-
148- $(SIGNATURES)
149-
150- Part of API, exported.
151- """
152- function DAG (name:: AbstractString , str:: AbstractString , df:: DataFrame )
153- ds = strip (str)
154- if ds[1 : 3 ] == " DAG"
155- d = from_ggm (ds)
156- elseif ds[1 : 3 ] == " dag"
157- d = from_dagitty (ds)
158- else
159- @error " Unrecognized model string: $(ds) )"
160- end
112+ function DAG (name:: AbstractString , model:: ModelDefinition ; df:: DataFrameOrNothing = nothing )
113+
114+ local d
115+ if typeof (model) <: OrderedDict
116+ d = model
117+ elseif typeof (model) <: AbstractString
118+ ds = strip (model)
119+ if ds[1 : 3 ] == " DAG"
120+ d = from_ggm (ds)
121+ elseif ds[1 : 3 ] == " dag"
122+ d = from_dagitty (ds)
123+ else
124+ @error " Unrecognized model string: $(ds) )"
125+ end
126+ elseif typeof (model) <: NamedArray
127+ d = adjacency_matrix_to_dict (model)
128+ end
161129
162130 vars = dag_vars (d)
163131 a = adjacency_matrix (d)
164132 e = edge_matrix (d)
165133
166- # Compute covariance matrix and store as NamedArray
167-
168- @assert length (names (df)) == length (vars) " DataFrame has different number of columns"
169- s = NamedArray (cov (Array (df)), (names (df), names (df)), (" Rows" , " Cols" ))
170-
171- # Create object
172-
173- DAG (name, d, a, e, s, df, vars)
174-
175- end
176-
177- """
178-
179- # `DAG`
180-
181- $(SIGNATURES)
182-
183- Part of API, exported.
184- """
185- function DAG (name:: AbstractString , str:: AbstractString )
186- ds = strip (str)
187- if ds[1 : 3 ] == " DAG"
188- d = from_ggm (ds)
189- elseif ds[1 : 3 ] == " dag"
190- d = from_dagitty (ds)
134+ if isnothing (df)
135+ s = nothing
191136 else
192- @error " Unrecognized model string: $(ds) )"
137+ # Compute covariance matrix and store as NamedArray
138+ @assert length (names (df)) == length (vars) " DataFrame has different number of columns"
139+ s = NamedArray (cov (Array (df)), (names (df), names (df)), (" Rows" , " Cols" ))
193140 end
194141
195- vars = dag_vars (d)
196- a = adjacency_matrix (d)
197- e = edge_matrix (d)
198-
199- # Create object
200-
201- DAG (name, d, a, e, nothing , nothing , vars)
202- end
203-
204- """
205-
206- # `DAG`
207-
208- $(SIGNATURES)
209-
210- Part of API, exported.
211- """
212- function DAG (name:: AbstractString , a:: NamedArray , df:: DataFrame )
213-
214- vars = names (a, 1 )
215- d = adjacency_matrix_to_dict (a)
216- e = StructuralCausalModels. edge_matrix (a)
217-
218- # Compute covariance matrix and store as NamedArray if df is present
219-
220- @assert length (names (df)) == length (vars) " DataFrame has different number of columns"
221- s = NamedArray (cov (Array (df)), (names (df), names (df)), (" Rows" , " Cols" ))
222-
223142 # Create object
224143
225144 DAG (name, d, a, e, s, df, vars)
228147
229148"""
230149
231- # `DAG`
232-
233- $(SIGNATURES)
234-
235- Part of API, exported.
236- """
237- function DAG (name:: AbstractString , a:: NamedArray )
238-
239- vars = names (a, 1 )
240- d = adjacency_matrix_to_dict (a)
241- e = StructuralCausalModels. edge_matrix (a)
242-
243- # Create object
244-
245- DAG (name, d, a, e, nothing , nothing , vars)
246-
247- end
248-
249- """
250-
251150# `set_dag_df!`
252151
253152Set or update Dataframe associated to DAG
0 commit comments