1 # Build a 'graph-like' object having 'nodes' nodes belonging to 'classes' classes.
2 # Class distribution is given by 'proba', and connectivity probabilities are given
3 # by 'intraproba' and 'interproba'.
4 generateGraph<-function(nodes,classes,proba=rep(1/classes,classes),
5 intraproba=0.1,crossproba=0.02)
7 mat_pi=CreateConnectivityMat(classes,intraproba,crossproba)
8 igraph=Fast2SimuleERMG(nodes,proba,mat_pi[1],mat_pi[2])
9 adjacency=get.adjacency(igraph$graph)
10 cmgraph=list(nodes=nodes,classes=classes,adjacency=adjacency,nodeclasses=igraph$node.classes,proba=proba,
11 intraproba=intraproba,crossproba=crossproba)
12 attr(cmgraph,'class')<-c('cmgraph')
16 # Return explicit member names for the different attributes of graph objects.
17 labels.cmgraph<-function(object,...)
19 c("Nodes","Classes","Adjacency Matrix","Node Classification","Class Probability Distribution","Intra Class Edge Probability","Cross Class Edge Probability")
22 # Override the summmary function for graph objects.
23 summary.cmgraph<-function(object,...)
26 cat(c("Nodes : ",object$nodes,"\n",
27 "Edges : ",length(which(object$adjacency!=0)),"\n",
28 "Classes : ",object$classes,"\n",
29 "Class Probability Distribution: ",object$proba,"\n"))
32 # Override the plot function for graph objects.
33 plot.cmgraph<-function(x,...)
35 RepresentationXGroup(x$adjacency,x$nodeclasses)
38 # Generate covariable data for the graph 'g'. Covariables are associated to vertex data, and
39 # their values are drawn according to 2 distributions: one for vertices joining nodes of
40 # the same class, and another for vertices joining nodes of different classes.
41 # The two distributions have different means but a single standard deviation.
42 generateCovariablesCondZ<-function(g,sameclustermean=0,otherclustermean=2,sigma=1)
44 mu=CreateMu(g$classes,sameclustermean,otherclustermean)
45 res=SimDataYcondZ(g$nodeclasses,mu,sigma)
46 cmcovars=list(graph=g,sameclustermean=sameclustermean,otherclustermean=otherclustermean,sigma=sigma,mu=mu,y=res)
47 attr(cmcovars,'class')<-c('cmcovarz','cmcovar')
51 # Generate covariable data for the graph 'g'. Covariables are associated to vertex data, and
52 # their values are drawn according to 2 distributions: one for vertices joining nodes of
53 # the same class, and another for vertices joining nodes of different classes.
54 # The two distributions have different means but a single standard deviation.
55 # This function generates two sets of covariables.
56 generateCovariablesCondXZ<-function(g,sameclustermean=c(0,3),otherclustermean=c(2,5),sigma=1)
58 mux0=CreateMu(g$classes,sameclustermean[1],otherclustermean[1])
59 mux1=CreateMu(g$classes,sameclustermean[2],otherclustermean[2])
60 res=SimDataYcondXZ(g$nodeclasses,g$adjacency,mux0,mux1,sigma)
61 cmcovars=list(graph=g,sameclustermean=sameclustermean,otherclustermean=otherclustermean,sigma=sigma,mu=c(mux0,mux1),y=res)
62 attr(cmcovars,'class')<-c('cmcovarxz','cmcovar')
67 # Override the print function for a cleaner covariable output.
68 print.cmcovar<-function(x,...)
70 cat("Classes : ",x$graph$classes,"\n",
71 "Intra cluster mean: ",x$sameclustermean,"\n",
72 "Cross cluster mean: ",x$otherclustermean,"\n",
73 "Variance : ",x$sigma,"\n",
74 "Covariables :\n",x$y,"\n")
78 # Perform parameter estimation on 'graph' given the covariables 'covars'.
79 estimateCondZ<-function(graph,covars,maxiterations,initialclasses,selfloops)
81 res=EMalgorithm(initialclasses,covars$y,graph$adjacency,maxiterations,FALSE,selfloops)
82 cmestimation=list(mean=res$MuEstimated,variance=res$VarianceEstimated,pi=res$PIEstimated,alpha=res$AlphaEstimated,tau=res$TauEstimated,jexpected=res$EJ,graph=graph)
83 attr(cmestimation,'class')<-c('cmestimationz')
87 # Private generic estimation function used to allow various call conventions for estimation functions.
88 privateestimate<-function(covars,graph,maxiterations,initialclasses,selfloops,...) UseMethod("privateestimate")
90 # Private estimation function used to allow various call conventions for estimation functions.
91 # Override of generic function for single covariables.
92 privateestimate.cmcovarz<-function(covars,graph,maxiterations,initialclasses,selfloops,...)
94 res=estimateCondZ(graph,covars,maxiterations,initialclasses,selfloops)
95 attr(res,'class')<-c(attr(res,'class'),'cmestimation')
100 # Perform parameter estimation on 'graph' given the covariables 'covars'.
101 estimateCondXZ<-function(graph,covars,maxiterations,initialclasses,selfloops)
103 #resSimXZ = EMalgorithmXZ(TauIni,Y2,Adjacente,30,SelfLoop=FALSE)
104 res=EMalgorithmXZ(initialclasses,covars$y,graph$adjacency,maxiterations,selfloops)
105 cmestimation=list(mean=c(res$MuEstimated1,res$MuEstimated2),variance=res$VarianceEstimated,pi=res$PIEstimated,alpha=res$AlphaEstimated,tau=res$TauEstimated,jexpected=res$EJ,graph=graph)
106 attr(cmestimation,'class')<-c('cmestimationxz')
110 # Private estimation function used to allow various call conventions for estimation functions.
111 # Override of generic function for multiple covariables.
112 privateestimate.cmcovarxz<-function(covars,graph,maxiterations,initialclasses,selfloops,...)
114 res=estimateCondXZ(graph,covars,maxiterations,initialclasses,selfloops)
115 attr(res,'class')<-c(attr(res,'class'),'cmestimation')
119 # Generic estimation function applicable to graphs with covariables.
120 estimate<-function(graph,covars,...) UseMethod("estimate")
122 # Override of the generic estimation function. Performs the actual function dispatch depending on the class of covariables.
123 estimate.cmgraph<-function(graph,covars,maxiterations=20,initialclasses=t(rmultinom(size=1,prob=graph$proba,n=graph$nodes)),selfloops=FALSE,method=NULL,...)
125 if (length(method) == 0) {
126 res=privateestimate(covars,graph,maxiterations,initialclasses,selfloops,...)
128 res=method(graph,covars,maxiterations,initialclasses,selfloops)
129 attr(res,'class')<-c(attr(res,'class'),'cmestimation')
134 # Override of the generic pliot function for estimation results.
135 plot.cmestimation<-function(x,...)
139 title("Expected value of J: Convergence criterion")
142 gplot(x$graph$adjacency,vertex.col=map$node.classes+2)
143 title("Network with estimated classes")
147 # Generic private ICL computation function for graphs and covariables.
148 privatecomputeICL<-function(covars,graph,qmin,qmax,loops,maxiterations,selfloops) UseMethod("privatecomputeICL")
151 # Private ICL computation function for graphs with single covariables.
152 privatecomputeICL.cmcovarz<-function(covars,graph,qmin,qmax,loops,maxiterations,selfloops)
154 res=ICL(X=graph$adjacency,Y=covars$y,Qmin=qmin,Qmax=qmax,loop=loops,NbIteration=maxiterations,SelfLoop=selfloops,Plot=FALSE)
155 attr(res,'class')<-c('cmiclz')
160 # Private ICL computation function for graphs with multiple covariables.
161 privatecomputeICL.cmcovarxz<-function(covars,graph,qmin,qmax,loops,maxiterations,selfloops)
163 res=ICL(X=graph$adjacency,Y=covars$y,Qmin=qmin,Qmax=qmax,loop=loops,NbIteration=maxiterations,SelfLoop=selfloops,Plot=FALSE)
164 attr(res,'class')<-c('cmiclxz')
169 # Generic public ICL computation function applicable to graph objects.
170 computeICL<-function(graph,covars,qmin,qmax,...) UseMethod("computeICL")
172 # Override of ICL computation function applicable to graph objects.
173 # Performs the actual method dispatch to private functions depending on the type of covariables.
174 computeICL.cmgraph<-function(graph,covars,qmin,qmax,loops=10,maxiterations=20,selfloops=FALSE,...)
176 res=privatecomputeICL(covars,graph,qmin,qmax,loops,maxiterations,selfloops)
181 attr(res,'class')<-c(attr(res,'class'),'cmicl')
185 # Override of the plot function for results of ICL computation.
186 plot.cmicl<-function(x,...)
190 maxi=which(max(result)==result)
191 plot(seq(x$qmin,x$qmax),result,type="b",xlab="Number of classes",ylab="ICL value")
192 points(maxi+x$qmin-1,result[maxi],col="red")
194 best=x$EMestimation[[maxi+x$qmin-1]]
195 tau=best$TauEstimated
197 gplot(x$graph$adjacency,vertex.col=map$node.classes+2)
198 title("Network with estimated classes")