Package Deal': R Topics Documented
Package Deal': R Topics Documented
R topics documented:
autosearch . . drawnetwork genlatex . . . insert . . . . jointprior . . ksl . . . . . . learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 . 4 . 6 . 7 . 9 . 11 . 11
2 makesimprob . . . . maketrylist . . . . . network . . . . . . . Network tools . . . . networkfamily . . . . node . . . . . . . . . numbermixed . . . . nwfsort . . . . . . . perturb . . . . . . . . prob . . . . . . . . . rats . . . . . . . . . . readnet . . . . . . . . rnetwork . . . . . . . score . . . . . . . . . unique.networkfamily Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
autosearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 14 16 18 19 21 23 24 24 26 27 28 29 30 31 32
autosearch
Greedy search
Description From initial network, does local perturbations to increase network score. Usage autosearch(initnw,data,prior=jointprior(network(data)),maxiter=5 , trylist= vector("list",size(initnw)),trace=TRUE, timetrace=TRUE,showban=FALSE,removecycles=FALSE) heuristic(initnw,data,prior=jointprior(network(data)), maxiter=1 ,restart=1 ,degree=size(initnw), trylist= vector("list",size(initnw)),trace=TRUE, timetrace=TRUE,removecycles=FALSE) gettable(x) Arguments initnw data prior maxiter restart degree an object of class network, from which the search is started. a data frame used for learning the network, see network. a list containing parameter priors, generated by jointprior. an integer, which gives the maximum number of steps in the search algorithm. an integer, which gives the number of times to perturb initnw and rerun the search. an integer, which gives the degree of perturbation, see perturb.
autosearch trylist trace timetrace showban removecycles a list used internally for reusing learning of nodes, see maketrylist. a logical. If TRUE, plots the accepted networks during search. a logical. If TRUE, prints some timing information on the screen.
a logical passed to the plot method for network objects. If FALSE, the banned arrows are not shown in the plots (if trace is TRUE). a logical. If TRUE, all networks explored in the search is returned, except for networks containing a cycle. If FALSE, all networks are returned, including cyclic networks. an output object from a search.
x Details
In autosearch, a list of networks is in each step created with either one arrow added, one arrow deleted or one arrow turned (if a cycle is not generated). The network scores of all the proposal networks are calculated and the network with the highest score is chosen for the next step in the search. If no proposed network has a higher network score than the previous network, the search is terminated. The network with the highest network score is returned, along with a list containing all tried networks (depending on the value of removecycles). heuristic restarts by perturbing initnw degree times and calling autosearch again. The number of restarts is given by the option restart. Value autosearch and heuristic returns a list with three elements, that may be accessed using getnetwork, gettable and gettrylist. The elements are nw table an object of class network, which gives the network with the highest score. a table with all tried networks. If removecycles is FALSE, the networks may contain cycles. The table contains two columns: model with a string representation of the model and score with the corresponding log network score. The table can be translated to a networkfamily using makenw. an updated list used internally for reusing learning of nodes, see maketrylist.
trylist Author(s)
Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also perturb
4 Examples
drawnetwork
data(rats) fit <- network(rats) fit.prior <- jointprior(fit,12) fit <- getnetwork(learn(fit,rats,fit.prior)) fit <- getnetwork(insert(fit,2,1,rats,fit.prior)) fit <- getnetwork(insert(fit,1,3,rats,fit.prior)) hisc <- autosearch(fit,rats,fit.prior,trace=FALSE) hisc <- autosearch(fit,rats,fit.prior,trace=FALSE,removecycles=TRUE) # slower plot(getnetwork(hisc)) hisc2 <- heuristic(fit,rats,fit.prior,restart=1 ,trace=FALSE) plot(getnetwork(hisc2)) print(modelstring(getnetwork(hisc2))) plot(makenw(gettable(hisc2),fit))
drawnetwork
Description drawnetwork allows the user to specify a Bayesian network through a point and click interface. Usage drawnetwork(nw,df,prior,trylist=vector("list",size(nw)), unitscale=2 ,cexscale=8, arrowlength=.25,nocalc=FALSE, yr=c( ,35 ),xr=yr,...)
Arguments nw df prior trylist cexscale arrowlength nocalc unitscale an object of class network to be edited. a data frame used for learning the network, see network. a list containing parameter priors, generated by jointprior. a list used internally for reusing learning of nodes, see maketrylist. a numeric passed to the plot method for network objects. Measures the scaled size of text and symbols. a numeric passed to the plot method for network objects. Measures the length of the edges of the arrowheads. a logical. If TRUE, no learning procedure is called, see eg. rnetwork. a numeric passed to the plot method for network objects. Scale parameter for chopping off arrow heads.
drawnetwork xr yr ... Details a numeric vector with two components containing the range on x-axis. a numeric vector with two components containing the range on y-axis. additional plot arguments, passed to the plot method for network objects.
To insert an arrow from node A to node B, rst click node A and then click node B. When the graph is nished, click stop. To specify that an arrow must not be present, press ban (a toggle) and draw the arrow. This is shown as a red dashed arrow. It is possible to ban both directions between nodes. The ban list is stored with the network in the property banlist. It is a matrix with two columns. Each row is the from node index and the to node index, where the indices are the column number in the data frame. Note that the network score changes as the network is re-learned whenever a change is made (unless nocalc is TRUE). Value A list with two elements that may be accessed using getnetwork and gettrylist. The elements are nw trylist Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also network Examples
data(rats) rats.nw <- network(rats) rats.prior <- jointprior(rats.nw,12) rats.nw <- getnetwork(learn(rats.nw,rats,rats.prior)) ## Not run: newrat <- getnetwork(drawnetwork(rats.nw,rats,rats.prior))
an object of class network with the nal network. an updated list used internally for reusing learning of nodes, see maketrylist.
genlatex
genlatex
Description The networks in a network family is arranged as pictex-graphs in a LaTeX-table. Usage genlatex(nwl,outdir="pic/",prefix="scoretable",picdir="",picpre="pic", ncol=5,nrow=7,width=12/ncol,vadjust=-1.8) genpicfile (nwl,outdir="pic/",prefix="pic",w=1.6,h=1.6,bigscale=3) Arguments nwl outdir prefix picdir picpre ncol nrow width vadjust w h bigscale Value Files: {outdir}{picpre}xx.tex one pictex le for each network in the network family, indexed by xx. {outdir}{prefix}.tex LaTeX le with table including all pictex les. {outdir}{picpre}nice.tex pictex le with the best network. Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. object of class networkfamily containing a list of objects of class network. character string, the directory for storing output. character string, the lename (without extension) of the LaTeX le. The lenames of the picles begin with the given prex. character string, the directory where pic-les are stored. character string, prex for pic-les. integer, the number of columns in LaTeX table. integer, the number of rows in LaTeX table. numeric, the width of each cell in the LaTeX table. numeric, the vertical adjustment in LaTeX table. numeric, the width of pictex objects numeric, the height of pictex objects numeric, the scaling of the best network, which is output in nice.tex
insert
Description Inserts/removes one arrow in a network (if legal) Usage insert (nw,j,i,df,prior,nocalc=FALSE,trylist=vector("list",size(nw))) remover(nw,j,i,df,prior,nocalc=FALSE,trylist=vector("list",size(nw)))
8 Arguments nw j i df prior nocalc trylist Details Examines if the arrow from j to i is legal according to the following criteria Arrows from/to the same node are not legal. Arrows from continous nodes to discrete nodes are not legal. Arrows banned in ban list are not legal, see drawnetwork. Arrows already existing in the network are not legal. an object of class network. integer, giving the index of the from node. integer, giving the index of the to node. a data frame used for learning the network, see network. a list describing parameter priors, generated by jointprior. a logical. If TRUE, learning is not called. a list, used internally for reusing learning of nodes, see maketrylist.
insert
If the arrow is not legal, a NULL network is returned. Otherwise, the arrow is inserted/removed, the network is re-learned (if nocalc is FALSE). The trylist is updated. Value A list with two elements nw trylist Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. Examples
data(rats) rats.nw <- network(rats) rats.nw <- getnetwork(insert(rats.nw,2,1,nocalc=TRUE)) rats.prior <- jointprior(rats.nw,12) rats.nw2 rats.nw2 <- network(rats) <- getnetwork(learn(rats.nw2,rats,rats.prior))
an object of class network with the arrow added/removed if this is possible. If not, NULL is returned. an updated list, used internally for reusing learning of nodes, see maketrylist.
jointprior
rats.nw2 rats.nw3 <- getnetwork(insert(rats.nw2,1,2,rats,rats.prior)) <- getnetwork(remover(rats.nw2,1,2,rats,rats.prior))
jointprior
Description Given a network with a prob property for each node, derives the joint probability distribution. Then the quantities needed in the local master procedure for nding the local parameter priors are deduced. Usage jointprior(nw,N=NA,phiprior="bottcher",timetrace=FALSE) Arguments nw an object of class network. Each node must have a prob property to describe the local probability distribution. The prob property is created using prob method for network objects, which is called by the network function. an integer, which gives the size of the imaginary data base. If this is too small, NAs may be created in the output, resulting in errors in learn. If no N is given, the procedure tries to set a value as low as possible. a string, which species how the prior for phi is calculated. Either phiprior="bottcher" or phiprior="heckerman" can be used. a logical. If TRUE, prints some timing information on the screen.
For the discrete part of the network, the joint probability distribution is calculated by multiplying together the local probability distributions. Then, jointalpha is determined by multiplying each entry in the joint probability distribution by the size of the imaginary data base N. For the mixed part of the network, for each conguration of the discrete variables, the joint Gaussian distribution of the continuous variables is constructed and represented by jointmu (one row for each conguration of the discrete parents) and jointsigma (a list of matrices one for each conguration of the discrete parents). The congurations of the discrete parents are ordered according to findex. The algorithm for constructing the joint distribution of the continuous variables is described in Shachter and Kenley (1989). Then, jointalpha, jointnu, jointrho, mu and jointphi are deduced. These quantities are later used for deriving local parameter priors. For each conguration i of the discrete variables, i = i = i
10 and i = (i 1)i if phiprior="bottcher", see Bottcher(2001) and i = i (i 2)i /(i + 1) if phiprior="heckerman", see Heckerman, Geiger and Chickering (1995). Value A list with the following elements, jointalpha jointnu jointrho jointmu jointsigma jointphi a table used in the local master procedure for discrete variables. a table used in the local master procedure for continuous variables. a table used in the local master procedure for continuous variables.
jointprior
a numeric matrix used in the local master procedure for continuous variables. a list of numeric matrices (not used in further calculations). a list of numeric matrices used in the local master procedure for continuous variables.
Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Bottcher, S.G. (2001). Learning Bayesian Networks with Mixed Variables, Articial Intelligence and Statistics 2001, Morgan Kaufmann, San Francisco, CA, USA, 149-156. Heckerman, D., Geiger, D. and Chickering, D. (1995). Learning Bayesian networks: The combination of knowledge and statistical data. Machine Learning, 20: 197-243. Shachter, R.D. and Kenley, C.R. (1989), Gaussian inuence diagrams. Management Science, 35:527-550. Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also network, prob Examples
data(rats) rats.nw <- network(rats) rats.prior <- jointprior(rats.nw,12) ## Not run: savenet(rats.nw,file("rats.net")) ## Not run: rats.nw <- readnet(file("rats.net"))
ksl
## Not run: rats.nw <- prob(rats.nw,rats) ## Not run: rats.prior <- jointprior(rats.nw,12)
11
ksl
Description Data from a study measuring health and social characteristics of representative samples of Danish 70 year olds, taken in 1967 and 1984. Format A data frame with variables of both discrete and continuous types. FEV Forced ejection volume Kol Cholesterol Hyp Hypertension (no/yes) logBMI Logarithm of Body Mass Index Smok Smoking (no/yes) Alc Alcohol consumption (seldom/frequently) Work Working (yes/no) Sex male/female Year Survey year (1967/1984) References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal.
learn
Description Updates the distributions of the parameters in the network, based on a prior network and data. Also, the network score is calculated. Usage learn (nw, df, prior=jointprior(nw), nodelist=1:size(nw), trylist=vector("list",size(nw)), timetrace=FALSE)
12 Arguments nw df prior nodelist trylist timetrace Details an object of class network. a data frame used for learning the network, see network. a list containing parameter priors, generated by jointprior. a numeric vector of indices of nodes to be learned. a list used internally for reusing learning of nodes, see maketrylist. a logical. If TRUE, prints some timing information on the screen.
learn
The procedure learn determines the master prior, local parameter priors and local parameter posteriors, see Bottcher (2001). It may be called on all nodes (default) or just a single node. From the joint prior distribution, the marginal distribution of all parameters in the family consisting of the node and its parents can be determined. This is the master prior, see localmaster. The local parameter priors are now determined by conditioning in the master prior distribution, see conditional. The hyperparameters associated with the local parameter prior distribution is attached to each node in the property condprior. Finally, the local parameter posterior distributions are calculated (see post) and attached to each node in the property condposterior. A so-called trylist is maintained to speedup the learning process. The trylist consists of a list of matrices for each node. The matrix for a given node holds previously evaluated parent congurations and the corresponding log-likelihood contribution. If a node with a certain parent conguration needs to be learned, it is checked, whether the node has already been learned. The previously learned nodes are given as input in the trylist parameter and is updated in the learning procedure. When one or more nodes in a network have been learned, the network score is updated and attached to the network in the property score. The learning procedure is called from various functions using the principle, that networks should always be updated with their score. Thus, e.g.\ drawnetwork keeps the network updated when the graph is altered. Value A list with two elements that may be accessed using getnetwork and gettrylist. The elements are nw an object of class network, with the condposterior properties updated for the nodes. Also, the property score is updated and contains the network score. The contribution to the network score for each node is contained in the property loglik for each node. an updated list used internally for reusing learning of nodes, see maketrylist.
trylist Author(s)
makesimprob References
13
Bottcher, S.G. (2001). Learning Bayesian Networks with Mixed Variables, Articial Intelligence and Statistics 2001, Morgan Kaufmann, San Francisco, CA, USA, 149-156. Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also networkfamily, jointprior, maketrylist, network Examples
data(rats) fit <fit.prior <fit.learn <fit.nw <fit.learn2<network(rats) jointprior(fit,12) learn(fit,rats,fit.prior,timetrace=TRUE) getnetwork(fit.learn) learn(fit,rats,fit.prior,trylist=gettrylist(fit.learn),timetrace=TRUE)
makesimprob
Description Creates local probability distributions reecting the graph of the network. These are attached as a simprob property to each node in the network and can be edited and used for rnetwork. Usage makesimprob(nw, s2=function(idx,cf) { cf <- as.vector(cf) xs <- (1:length(cf)) log(xs%*%cf+1) }, m =function(idx,cf) { cf <- as.vector(cf) xs <- (1:length(cf))^2 .69*(xs%*%cf) }, m1=function(idx,cf) { cf <- as.vector(cf) xs <- (1:length(cf))*1 idx*(cf%*%xs) })
maketrylist
function that returns the variance as a function of the node index and the conguration of the discrete variables. function that returns the intercept as a function of the node index and the conguration of the discrete variables. function that returns the regression coefcients as a function of the node index and the conguration of the discrete variables.
Details For each node, the local simprob is determined. If the node is discrete, the probability distribution is uniform (and thus not reecting the dependence in the graph, as it should). If the node is continuous, one mean and variance is attached per conguration of the discrete parents. The mean depends on the continuos parents and is the regression coefcients determined by the functions m (intercept) and m1 (regression coefcients). The variance is determined by the function s2. Value The network object nw, where each node has attached the property simprob. Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also rnetwork
maketrylist
Description For faster learning, a trylist is maintained as a lookup table for a given parent conguration of a node. Usage maketrylist(initnw,data,prior=jointprior(network(data)),timetrace=FALSE)
maketrylist Arguments initnw data prior timetrace Details an object of class network, from which the search is started. a data frame used for learning the network, see network. a list containing parameter priors, generated by jointprior. a logical. If TRUE, prints some timing information on the screen.
15
This procedure is included for illustrative purposes. For each node in the network, all possible parent congurations are created and learned. The result is called a trylist. To create the full trylist is very time-consuming, and a better choice is to maintain a trylist while searching and indeed this is automatically done. The trylist is given as output to all functions that call the learning procedure and can be given as an argument. Value A list with one element per node in the network. In the list, element i is a matrix with two columns: a string with the indices of the parent nodes, separated by ":", and a numeric with the log-likelihood contribution of the node given the parent conguration. Whenever learning is performed of a node given a parent conguration, the trylist is consulted to yield faster learning, especially useful when using autosearch or heuristic. Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about Deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also networkfamily, autosearch heuristic Examples
data(rats) rats.nw <rats.pr <rats.nw <rats.tr <network(rats) jointprior(rats.nw,12) getnetwork(learn(rats.nw,rats,rats.pr)) maketrylist(rats.nw,rats,rats.pr)
16
network
network
Description A Bayesian network is represented as an object of class network. Methods for printing and plotting are dened. Usage network(df,specifygraph=FALSE,inspectprob=FALSE, doprob=TRUE,yr=c( ,35 ),xr=yr) ## S3 method for class network print(x,filename=NA,condposterior=FALSE, condprior=FALSE,...) ## S3 method for class network plot(x,arrowlength=.25, notext=FALSE, sscale=7,showban=TRUE,yr=c( ,35 ),xr=yr, unitscale=2 ,cexscale=8,...)
Arguments df specifygraph inspectprob doprob x filename condprior condposterior sscale unitscale cexscale arrowlength xr yr a data frame, where the columns dene the variables. A continuous variable should have type numeric and discrete varibles should have type factor. a logical. If TRUE, provides a call to drawnetwork to interactively specify a directed acyclic graph and possibly a ban list (see below). a logical. If TRUE, provides a plot of the graph and possibility to inspect the calculated probability distribution by clicking on the nodes. a logical. If TRUE, do not calculate a probability distribution. Used for example in rnetwork. an object of class network. a string or NA. If not NA, output is printed to a le. a logical. If TRUE, the conditional prior is printed, see conditional. a logical. If TRUE, the conditional posterior is printed, see learn. a numeric. The nodes are initially placed on a circle with radius sscale. a numeric. Scale parameter for chopping off arrow heads. a numeric. Scale parameter to set the size of the nodes. a numeric containing the length of the arrow heads. a numeric vector with two components containing the range on x-axis. a numeric vector with two components containing the range on y-axis.
network notext showban ... Value a logical. If TRUE, no text is displayed in the nodes on the plot. a logical. If TRUE, banned arrows are shown in red. additional plot arguments, passed to plot.node.
17
The netork creator function returns an object of class network, which is a list with the following elements (properties), nodes n discrete continuous banlist score relscore a list of objects of class node. If doprob is TRUE, the nodes are given the property prob which is the initial probability distribution used by jointprior. an integer containing the number of nodes in the network. a numeric vector of indices of discrete nodes. a numeric vector of indices of continuous nodes. a numeric matrix with two columns. Each row contains the indices i -> j of arrows that may not be allowed in the directed acyclic graph. a numeric added by learn and is the log network score. a numeric added by nwfsort and is the relative network score compared with the best network in a network family.
Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also networkfamily, node, rnetwork, learn, drawnetwork, jointprior, heuristic, nwequal Examples
A <- factor(rep(c("A1","A2"),5 )) B <- factor(rep(rep(c("B1","B2"),25),2)) thisnet <- network( data.frame(A,B) ) set.seed(1 sex <age <yield <weight <mydata <mynw <9) gl(2,4,label=c("male","female")) gl(2,2,8) rnorm(length(sex)) rnorm(length(sex)) data.frame(sex,age,yield,weight) network(mydata)
18
localprob(mynw,"sex") <localprob(mynw,"age") <localprob(mynw,"yield") <localprob(mynw,"weight")<print(mynw) plot(mynw) prior <- jointprior(mynw) mynw <- getnetwork(learn(mynw,mydata,prior)) thebest <- getnetwork(autosearch(mynw,mydata,prior)) print(mynw,condposterior=TRUE) ## Not run: savenet(mynw,file("yield.net")) c( .4, .6) c( .6, .4) c(2, ) c(1, )
Network tools
Network tools
Description Various extraction/replacement functions for networks Usage modelstring(x) makenw(tb,template) as.network(nwstring,template) size(x) banlist(x) banlist(x) <- value getnetwork(x) gettrylist(x) Arguments x tb template nwstring value an object of class network. a table output from autosearch or heuristic in the list property table. Can be translated into a networkfamily. an object of class network with the same nodes as the networks described in the table tb. a string representing the network. a numeric matrix with two columns. Each row contains the indices i -> j of arrows that may not be allowed in the directed acyclic graph.
networkfamily Details
19
The string representation of a network is a minimal size representation to speed up calculations. The functions modelstring, as.network and makenw converts between the string represention and network objects. size extracts the number of nodes in a network object. banlist extracts the banlist from a network object. getnetwork and gettrylist are accessor function that extracts a network object or trylist from the result from autosearch, heuristic, learn, perturb, networkfamily, drawnetwork.
networkfamily
Description Method for generating and learning all networks that are possible for a given set of variables. These may be plotted or printed. Also, functions for sorting according to the network score (see nwfsort) and for making a network family unique (see the unique method for networkfamily objects) are available. Usage networkfamily(data,nw=network(data), prior=jointprior(nw), trylist=vector("list",size(nw)), timetrace=TRUE) ## S3 method for class networkfamily print(x,...) ## S3 method for class networkfamily plot(x,layout=, cexscale=5,arrowlength= .1,sscale=7,...) Arguments nw data prior trylist timetrace x layout cexscale arrowlength sscale ... an object of class network. This should be the empty network for the set of variables. a data frame used for learning the network, see network. a list containing parameter priors, generated by jointprior. a list used internally for reusing learning of nodes, see maketrylist. a logical. If TRUE, prints some timing information on the screen. an object of class networkfamily. a numeric two dimensional vector with the number of plots in the rows and columns of each plotting page. Default set to rep(min(1+floor(sqrt(length(x))),5),2). a numeric. A scaling parameter to set the size of the nodes. a numeric, which gives the length of the arrow heads. a numeric. The nodes are initially placed on a circle with radius sscale. additional plot arguments passed to the plot method for network objects.
20 Details
networkfamily
networkfamily generates and learns all possible networks with the nodes given as in the initial network nw. This is done by successively trying to generate the networks with all possible arrows to/from each node (see addarrows). If there is a ban list present in nw (see network), then this is respected, as are the restrictions described in insert. After generation of all possible networks, a test for cycles (see cycletest) is performed and only networks with directed acyclic graphs are returned.
Value The function networkfamily returns a list with two components, nw trylist an object of class networkfamily. an updated list used internally for reusing learning of nodes, see maketrylist.
See Also network, genlatex, heuristic, nwfsort, unique.networkfamily, elementin, addarrows, cycletest
Examples
data(rats) allrats <- getnetwork(networkfamily(rats)) plot(allrats) print(allrats)
node
21
node
Representation of nodes
Description An important part of a network is the list of nodes. The nodes summarize the local properties of a node, given the parents of the node. Usage node (idx,parents,type="discrete",name=paste(idx), levels=2,levelnames=paste(1:levels),position=c( , )) ## S3 method for class node print(x,filename=NA,condposterior=TRUE,condprior=TRUE,...) ## S3 method for class node plot(x,cexscale=1 ,notext=FALSE,...) nodes(nw) nodes(nw) <- value Arguments x parents idx type name levels levelnames position nw value filename condprior condposterior cexscale notext ... an object of class node. a numeric vector with indices of the parents of the node. an integer, which gives the index of the node (the column number of the corresponding data frame). a string, which gives the type of the node. Either "discrete" (for factors) or "continuous" (for numeric). a string, which gives the name used when plotting and printing. Defaults to the column name in the data frame. an integer. If type is "discrete", this is the number of levels for the discrete variable. if type is "discrete", this is a vector of strings (same length as levels) with the names of the levels. If type is "continuous", the argument is ignored. a numeric vector with coordinates where the node should appear in the plot. Usually set by network and drawnetwork. an object of class network. a list of elements of class node. a string or NA. If not NA, output is printed to a le. a logical. If TRUE, the conditional prior is printed, see conditional. a logical. If TRUE, the conditional posterior is printed, see learn. a numeric. Scale parameter to set the size of the nodes. a logical. If TRUE, no text is displayed in the nodes on the plot. additional plot arguments.
22 Details
node
The operations on a node are typically done when operating on a network, so these functions are not to be called directly. When a network is created with network, the nodes in the nodelist are created using the node procedure. Local probability distributions are added as the property prob to each node using prob.node. If the node is continuous, this is a numeric vector with the conditional variance and the conditional regression coefcients arising from a regression on the continuous parents, using data. If the node has discrete parents, prob is a matrix with a row for each conguration of the discrete parents. If the node is discrete, prob is a multiway array which gives the conditional probability distribution for each conguration of the discrete parents. The generated prob can be replaced to match the prior information available. nodes gives the list of nodes of a network. localprob gives the probability distribution for each node in the network. Value The node creator function returns an object of class node, which is a list with the following elements (properties), idx name type levels levelnames an integer. A unique index for this node. It MUST correspond to the column index of the variable in the data frame. a string. The printed name of the node. a string. Either "continuous" or "discrete". an integer. If the node is of type "discrete", this integer is the number of levels of the node. if type is "discrete", this is a vector of strings (same length as levels) with the names of the levels. If type is "continuous", the node does not have this property. a vector of indices of the parents to this node. It is best to manage this vector using the insert function. a numeric vector, matrix or multiway array, giving the initial probability distribution. If the node is discrete, prob is a multiway array. If the node is continuous, prob is a matrix with one row for each conguration of the discrete parents, reducing to a vector if the node has no discrete parents. a list, generated by conditional giving the parameter priors deduced from jointprior using the master prior procedure (see localmaster). a list, which gives the parameter posteriors obtained from learnnode. a numeric giving the log likelihood contribution for this node, calculated in learnnode. a numeric vector, matrix or multiway array similar to prob, added by makesimprob and used by rnetwork.
parents prob
numbermixed Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal.
23
numbermixed
Description Calculates the number of different directed acyclic graphs for a set of discrete and continuous nodes. Usage numbermixed(nd,nc) Arguments nd nc Details No arrows are allowed from continuous nodes to discrete nodes. Cycles are not allowed. The number of networks is given by Bottcher (2003), using the result in Robinson (1977). When nd+nc>15, the procedure is quite slow. Value A numeric containing the number of directed acyclic graphs with the given node conguration. Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Bottcher, S.G. (2003). Learning Conditional Gaussian Networks. http://www.math.aau.dk/ ~alma. Aalborg University, 2003. Robinson, R.W. (1977). Counting unlabeled acyclic digraphs, Lecture Notes in Mathematics, 622: Combinatorial Mathematics. Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. an integer, which gives the number of discrete nodes. an integer, which gives the number of continuous nodes.
24 Examples
numbermixed(2,2) ## Not run: numbermixed(5,1 )
perturb
nwfsort
Description According to the score property of the networks in a network family, the networks are sorted and the relative score, i.e.\ the score of a network relative to the highest score, is attached to each network as the relscore property. Usage nwfsort(nwf) Arguments nwf Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. an object of class networkfamily.
perturb
Perturbs a network
Description Randomly insert/delete/turn arrows to obtain another network. Usage perturb(nw,data,prior,degree=size(nw),trylist=vector("list",size(nw)), nocalc=FALSE,timetrace=TRUE)
perturb Arguments nw data prior degree trylist nocalc timetrace Details an object of class network, from which arrows are added/removed/turned. a data frame used for learning the network, see network. a list containing parameter priors, generated by jointprior.
25
an integer, which gives the number of attempts to randomly insert/remove/turn an arrow. a list used internally for reusing learning of nodes, see maketrylist. a logical. If TRUE no learning procedure is called, see eg. rnetwork. a logical. If TRUE, prints some timing information on the screen.
Given the initial network, a new network is constructed by randomly choosing an action: remove, turn, add. After the action is chosen, we choose randomly among all possibilities of that action. If there are no possibilites, the unchanged network is returned. Value A list with two elements that may be accessed using getnetwork and gettrylist. The elements are nw trylist Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. Examples
set.seed(2 ) data(rats) fit <- network(rats) fit.prior <- jointprior(fit) fit <- getnetwork(learn(fit,rats,fit.prior)) fit.new <- getnetwork(perturb(fit,rats,fit.prior,degree=1 )) data(ksl) ksl.nw <- network(ksl) ksl.rand <- getnetwork(perturb(ksl.nw,nocalc=TRUE,degree=1 )) plot(ksl.rand)
an object of class network with the generated network. an updated list used internally for reusing learning of nodes, see maketrylist.
26
prob
prob
Description Methods for accessing or changing the local probability distributions and for accessing the local prior and posterior distributions Usage prob(x,df,...) ## S3 method for class node prob(x,df,nw,...) ## S3 method for class network prob(x,df,...) localprob(nw) localprob(nw,name) <- value localprior(node) localposterior(node)
Arguments x df nw node name ... value an object of class node or network. a data frame, where the columns dene the variables. A continuous variable should have type numeric and discrete varibles should have type factor. an object of class network. an object of class node. a string, which gives the node name. additional arguments for specic methods. If the node is continuous, this is a numeric vector with the conditional variance and the conditional regression coefcients arising from a regression on the continuous parents, using data. If the node has discrete parents, it is a matrix with a row for each conguration of the discrete parents. If the node is discrete, it is a multiway array which gives the conditional probability distribution for each conguration of the discrete parents.
Details The prob methods add local probability distributions to each node. If the node is continuous, this is a numeric vector with the conditional variance and the conditional regression coefcients arising from a regression on the continuous parents, using data. If the node has discrete parents, prob is
rats
27 a matrix with a row for each conguration of the discrete parents. If the node is discrete, prob is a multiway array which gives the conditional probability distribution for each conguration of the discrete parents. The generated prob can be replaced to match the prior information available. localprob returns the probability distribution for each node in the network. In a learned network, the local prior and posterior can be accessed for each node using localprior and localposterior.
Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal.
rats
Weightloss of rats
Description An articial data set. 24 rats (12 female, 12 male) have been randomized to use one of three drugs (products for loosing weight). The weightloss for each rat is noted after one and two weeks. Format A data frame with 4 variables. Sex a factor with two levels: "M" (male), "F" (female) Drug a factor with three levels: "D1", "D2", "D3" (three types) W1 a numeric: weightloss, week one. W2 a numeric: weightloss, week 2. References Morrison, D.F. (1976). Multivariate Statistical Methods. McGraw-Hill, USA. Edwards, D. (1995). Introduction to Graphical Modelling, Springer-Verlag. New York. Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal.
28
readnet
readnet
Reads/saves .net le
Description Reads/saves a Bayesian network specication in the .net language (see http://developer.hugin. com/documentation/net/). Usage readnet(con=file("default.net")) savenet(nw, con=file("default.net")) Arguments con nw Details readnet reads only the structure of a network, i.e.\ the directed acyclic graph. savenet exports the prob property for each node in the network object along with the network structure dened by the parents of each node. Value readnet creates an object of class network with the nodes specied as in the .net connection. The network has not been learned and the nodes do not have prob properties (see prob.network). savenet writes the object to the connection. Note The call to readnet(savenet(network)) is not the identity function as information is thrown away in both savenet and readnet. Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. See Also network a connection. an object of class network.
rnetwork Examples
data(rats) nw <- network(rats) ## Not run: savenet(nw,file("default.net")) ## Not run: nw2 <- readnet(file("default.net")) ## Not run: nw2 <- prob(nw2,rats)
29
rnetwork
Description Given a network with nodes having the simprob property, rnetwork simulates a data set. Usage rnetwork(nw, n=24, file="") Arguments nw n file Details The variables are simulated one at a time in an order that ensures that the parents of the node have already been simulated. For discrete variables a multinomial distribution is used and for continuous variables, a Gaussian distribution is used, according to the simprob property in each node. Value A data frame with one row per case. If a le name is given, a le is created with the data set. Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. an object of class network, where each node has the property simprob (see makesimprob). an integer, which gives the number of cases to simulate. a string. If non-empty, the data set is stored there.
30 Examples
A B c1 c2 df <<<<<factor(NA,levels=paste("A",1:2,sep="")) factor(NA,levels=paste("B",1:3,sep="")) NA NA data.frame(A,B,c1,c2)
score
nw <- network(df,doprob=FALSE) # doprob must be FALSE nw <- makesimprob(nw) # create simprob properties set.seed(944) sim <- rnetwork(nw,n=1
score
Network score
Description Accessor for the score from a node or network Usage score(x,...) ## S3 method for class node score(x,...) ## S3 method for class network score(x,...)
Arguments x ... Value For networks, the log network score is returned. For nodes, the contribution to the log network score is returned. Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. an object of class node or network. additional arguments for specic methods.
unique.networkfamily
31
unique.networkfamily
Description Removes networks that are equal or equivalent to networks already in the network family. Usage ## S3 method for class networkfamily unique(x,incomparables=FALSE,equi=FALSE,timetrace=FALSE,epsilon=1e-12,...) Arguments x incomparables equi timetrace epsilon ... Author(s) Susanne Gammelgaard Bottcher <[email protected]>, Claus Dethlefsen <[email protected]>. References Further information about deal can be found at: http://www.math.aau.dk/~dethlef/novo/deal. Examples
data(rats) rats.nwf <- networkfamily(rats) rats.nwf2<- unique(getnetwork(rats.nwf),equi=TRUE)
an object of class networkfamily. a logical, but has no effect. a logical. If TRUE, also equivalent networks are thrown out (i.e. if their score is within epsilon from another network). a logical. If TRUE, prints some timing information on the screen. a numeric, which measures how close network scores are allowed to be from each other to be equivalent. further arguments (no effect)
Index
Topic datasets ksl, 11 rats, 27 Topic iplot genlatex, 6 insert, 7 learn, 11 networkfamily, 19 nwfsort, 24 perturb, 24 readnet, 28 unique.networkfamily, 31 Topic models autosearch, 2 drawnetwork, 4 jointprior, 9 makesimprob, 13 maketrylist, 14 network, 16 Network tools, 18 node, 21 numbermixed, 23 prob, 26 rnetwork, 29 score, 30 addarrows, 20 as.network (Network tools), 18 autosearch, 2, 15, 18, 19 banlist (Network tools), 18 banlist<- (Network tools), 18 conditional, 12, 16, 21, 22 cycletest, 20 drawnetwork, 4, 8, 12, 16, 17, 19, 21 elementin, 20 factor, 16, 26 32 findex, 9 genlatex, 6, 20 genpicfile (genlatex), 6 getnetwork, 3, 5, 12, 25 getnetwork (Network tools), 18 gettable, 3 gettable (autosearch), 2 gettrylist, 3, 5, 12, 25 gettrylist (Network tools), 18 heuristic, 15, 1720 heuristic (autosearch), 2 insert, 7, 20, 22 jointprior, 2, 4, 8, 9, 12, 13, 15, 17, 19, 22, 25 ksl, 11 learn, 9, 11, 16, 17, 19, 21 learnnode, 22 localmaster, 12, 22 localposterior (prob), 26 localprior (prob), 26 localprob (prob), 26 localprob<- (prob), 26 makenw, 3 makenw (Network tools), 18 makesimprob, 13, 22, 29 maketrylist, 35, 8, 12, 13, 14, 19, 20, 25 modelstring (Network tools), 18 network, 25, 810, 1216, 16, 1822, 25, 26, 28, 29 Network tools, 18 networkfamily, 3, 7, 13, 15, 1719, 19 node, 17, 21, 21, 26 nodes (node), 21
INDEX nodes<- (node), 21 numbermixed, 23 nwequal, 17 nwfsort, 17, 19, 20, 24 perturb, 2, 3, 19, 24 pictex, 7 plot.network (network), 16 plot.networkfamily (networkfamily), 19 plot.node, 17 plot.node (node), 21 post, 12 print.network (network), 16 print.networkfamily (networkfamily), 19 print.node (node), 21 prob, 9, 10, 26 prob.network, 28 rats, 27 readnet, 28 remover (insert), 7 rnetwork, 4, 13, 14, 16, 17, 22, 25, 29 savenet (readnet), 28 score, 30 size (Network tools), 18 unique.networkfamily, 20, 31
33