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