1 |
x |
|
family |
|
vars |
|
adjacency |
|
icfun |
|
... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (x, family, vars, adjacency, icfun = BIC, ...)
{
if (missing(x))
stop("'x' must be assigned")
x <- as.matrix(x)
Ni <- ncol(x)
Nt <- nrow(x)
if (missing(vars))
vars <- 1:Ni
No <- length(vars)
if (missing(adjacency))
adjacency <- matrix(1, Ni, No)
if (is.vector(adjacency))
adjacency <- as.matrix(adjacency)
if (!is.matrix(adjacency) && ncol(adjacency) != No && nrow(adjacency) !=
Ni)
stop("'adjacency' must be square matrix with a row for each predictor and column for each outcome variable.")
if (missing(family)) {
if (identical(c(0, 1), sort(unique(c(x)))))
family <- rep("binomial", No)
else family <- rep("gaussian", No)
}
if (length(family) == 1) {
family <- list(family)
if (No > 1)
for (i in 2:No) family[[i]] <- family[[1]]
}
if (length(family) != No)
stop("Length of family is not equal to number of outcome variables.")
Out <- list()
Out$graph <- matrix(0, Ni, No)
Out$IC <- 0
for (i in 1:No) {
if (is.function(family[[i]]))
fam <- family[[i]]
else fam <- get(family[[i]])
if (any(as.logical(adjacency[, i]))) {
Res <- glm(x[-1, vars[i]] ~ x[-nrow(x), as.logical(adjacency[,
i])], family = fam)
}
else {
Res <- glm(x[-1, vars[i]] ~ NULL, family = fam)
}
Out$graph[as.logical(adjacency[, i]), i] <- coef(Res)[-1]
Out$IC <- Out$IC + icfun(Res, ...)
}
return(Out)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.