Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer)
(Answer: MakeOvaDag works with igraph. Some fancy properties)
Line 28: Line 28:
 
MakeOvaDag <- function(...) {
 
MakeOvaDag <- function(...) {
 
require(igraph)
 
require(igraph)
vert <- data.frame()
 
 
edg <- data.frame()
 
edg <- data.frame()
for(i in ls(envir = .GlobalEnv)) {
+
ova <- character()
 +
dec <- character()
 +
deci <- character()
 +
for(i in ls(envir = .GlobalEnv)) { # Find all ovariables
 +
if(class(get(i)) == "ovariable") {
 +
ova <- c(ova, i)
 +
}
 +
if(class(get(i)) == "odecision") {
 +
dec <- c(dec, i)
 +
}
 +
}
 +
for(i in ova) { # Find all edges between ovariables
 
obj <- get(i)
 
obj <- get(i)
if(class(obj) == "ovariable") {
+
if(nrow(obj@dependencies) > 0) {
vert <- rbind(vert, data.frame(
+
edg <- rbind(edg, data.frame(
Name = obj@name,
+
from = obj@dependencies$Name,
Size = nrow(obj@output),
+
to = obj@name,
Indices = paste(colnames(obj@output)[obj@marginal], collapse = ", ")
+
weight = 1 # Not sure yet what should be used as weight.
 
))
 
))
if(nrow(obj@dependencies) > 0) {
 
edg <- rbind(edg, data.frame(
 
From = obj@dependencies$Name,
 
To = obj@name
 
))
 
}
 
 
}
 
}
 
}
 
}
# d$Name <- as.character(d$Name)
+
dag <- graph.data.frame(edg, directed = TRUE) # Create directed acyclic graph
# d$Dependencies <- as.character(d$Dependencies)
+
for(i in dec) {
# cov.names <- sort(unique(c(d$Name, d$Dependencies)))
+
deci <- union(deci, get(i)@dectable$Decision)
# arcs <- matrix(match(c(d$Name, d$Dependencies), cov.names), ncol = 2)
+
}
# dag <- dag.init(covs = rep(1, length(cov.names)), arcs = arcs, cov.names = cov.names)
+
for(i in ova) { #Add attributes for vertices
dag <- graph.data.frame(edg, directed = TRUE)
+
obj <- get(i)
dag <- dag + vertex(vert)
+
if(!i %in% V(dag)$name) dag <- dag + vertex(obj@name)
 +
V(dag)$Size[V(dag)$name == i] <- nrow(obj@output)
 +
ind <- colnames(obj@output)[
 +
obj@marginal &
 +
!grepl("Source$", colnames(obj@output)) &
 +
! colnames(obj@output) %in% deci
 +
]
 +
decii <- deci[deci %in% colnames(obj@output)]
 +
if(length(ind) > 0) V(dag)$Indices[V(dag)$name == i] <- paste(ind, collapse = ", ")
 +
if(length(decii) > 0) V(dag)$Decisions[V(dag)$name == i] <- paste(decii, collapse = ", ")
 +
}
 +
V(dag)$Size[is.na(V(dag)$Size)] <- 1
 +
V(dag)$Size[V(dag)$Size == 0] <- 1
 +
V(dag)$Indices[is.na(V(dag)$Indices)] <- ""
 +
V(dag)$Decisions[is.na(V(dag)$Decisions)] <- ""
 +
 +
plot(
 +
dag,
 +
vertex.label.cex = 0.8,
 +
vertex.size = log(V(dag)$Size)+2, # Vertex size is (non-linearly) relative to rows in output.
 +
vertex.color = ifelse(nchar(V(dag)$Decisions) > 0, "Red", "SkyBlue2"),
 +
layout = layout.fruchterman.reingold
 +
)
 
return(dag)
 
return(dag)
 
}
 
}

Revision as of 14:21, 21 September 2015



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer")

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>