File: example.R

package info (click to toggle)
ohcount 3.0.0-6.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,828 kB
  • sloc: ansic: 6,534; ruby: 2,560; perl: 2,041; erlang: 350; lisp: 272; sh: 244; pascal: 196; vhdl: 150; haskell: 149; asm: 127; cs: 124; awk: 98; java: 92; php: 73; tcl: 58; xml: 57; fortran: 54; makefile: 32; python: 31; ada: 30; objc: 30; jsp: 28; sql: 18; cobol: 13; ml: 9; cpp: 3
file content (200 lines) | stat: -rw-r--r-- 9,849 bytes parent folder | download | duplicates (8)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
r	comment	# Build a 'graph-like' object having 'nodes' nodes belonging to 'classes' classes.
r	comment	# Class distribution is given by 'proba', and connectivity probabilities are given
r	comment	# by 'intraproba' and 'interproba'.
r	code	generateGraph<-function(nodes,classes,proba=rep(1/classes,classes),
r	code				intraproba=0.1,crossproba=0.02)
r	code	{
r	code		mat_pi=CreateConnectivityMat(classes,intraproba,crossproba)
r	code		igraph=Fast2SimuleERMG(nodes,proba,mat_pi[1],mat_pi[2])
r	code		adjacency=get.adjacency(igraph$graph)
r	code		cmgraph=list(nodes=nodes,classes=classes,adjacency=adjacency,nodeclasses=igraph$node.classes,proba=proba,
r	code	                     intraproba=intraproba,crossproba=crossproba)
r	code		attr(cmgraph,'class')<-c('cmgraph')
r	code		cmgraph
r	code	}
r	blank	
r	comment	# Return explicit member names for the different attributes of graph objects.
r	code	labels.cmgraph<-function(object,...)
r	code	{
r	code		c("Nodes","Classes","Adjacency Matrix","Node Classification","Class Probability Distribution","Intra Class Edge Probability","Cross Class Edge Probability")
r	code	}
r	blank	
r	comment	# Override the summmary function for graph objects.
r	code	summary.cmgraph<-function(object,...) 
r	code	{
r	blank	
r	code		cat(c("Nodes                         : ",object$nodes,"\n",
r	code		      "Edges                         : ",length(which(object$adjacency!=0)),"\n",
r	code		      "Classes                       : ",object$classes,"\n",
r	code		      "Class Probability Distribution: ",object$proba,"\n"))
r	code	}
r	blank	
r	comment	# Override the plot function for graph objects.
r	code	plot.cmgraph<-function(x,...) 
r	code	{
r	code		RepresentationXGroup(x$adjacency,x$nodeclasses)
r	code	}
r	blank	
r	comment	# Generate covariable data for the graph 'g'. Covariables are associated to vertex data, and
r	comment	# their values are drawn according to 2 distributions: one for vertices joining nodes of
r	comment	# the same class, and another for vertices joining nodes of different classes.
r	comment	# The two distributions have different means but a single standard deviation.  
r	code	generateCovariablesCondZ<-function(g,sameclustermean=0,otherclustermean=2,sigma=1) 
r	code	{
r	code		mu=CreateMu(g$classes,sameclustermean,otherclustermean)
r	code		res=SimDataYcondZ(g$nodeclasses,mu,sigma)
r	code		cmcovars=list(graph=g,sameclustermean=sameclustermean,otherclustermean=otherclustermean,sigma=sigma,mu=mu,y=res)
r	code		attr(cmcovars,'class')<-c('cmcovarz','cmcovar')
r	code		cmcovars
r	code	}
r	blank	
r	comment	# Generate covariable data for the graph 'g'. Covariables are associated to vertex data, and
r	comment	# their values are drawn according to 2 distributions: one for vertices joining nodes of
r	comment	# the same class, and another for vertices joining nodes of different classes.
r	comment	# The two distributions have different means but a single standard deviation.
r	comment	# This function generates two sets of covariables.
r	code	generateCovariablesCondXZ<-function(g,sameclustermean=c(0,3),otherclustermean=c(2,5),sigma=1) 
r	code	{
r	code		mux0=CreateMu(g$classes,sameclustermean[1],otherclustermean[1])
r	code		mux1=CreateMu(g$classes,sameclustermean[2],otherclustermean[2])
r	code		res=SimDataYcondXZ(g$nodeclasses,g$adjacency,mux0,mux1,sigma)
r	code		cmcovars=list(graph=g,sameclustermean=sameclustermean,otherclustermean=otherclustermean,sigma=sigma,mu=c(mux0,mux1),y=res)
r	code		attr(cmcovars,'class')<-c('cmcovarxz','cmcovar')
r	code		cmcovars
r	code	}
r	blank	
r	blank	
r	comment	# Override the print function for a cleaner covariable output.
r	code	print.cmcovar<-function(x,...)
r	code	{
r	code		cat("Classes           : ",x$graph$classes,"\n",
r	code		    "Intra cluster mean: ",x$sameclustermean,"\n",
r	code		    "Cross cluster mean: ",x$otherclustermean,"\n",
r	code		    "Variance          : ",x$sigma,"\n",
r	code		    "Covariables       :\n",x$y,"\n")
r	code	}
r	blank	
r	blank	
r	comment	# Perform parameter estimation on 'graph' given the covariables 'covars'.
r	code	estimateCondZ<-function(graph,covars,maxiterations,initialclasses,selfloops) 
r	code	{
r	code		res=EMalgorithm(initialclasses,covars$y,graph$adjacency,maxiterations,FALSE,selfloops)
r	code		cmestimation=list(mean=res$MuEstimated,variance=res$VarianceEstimated,pi=res$PIEstimated,alpha=res$AlphaEstimated,tau=res$TauEstimated,jexpected=res$EJ,graph=graph)
r	code		attr(cmestimation,'class')<-c('cmestimationz')
r	code		cmestimation
r	code	}
r	blank	
r	comment	# Private generic estimation function used to allow various call conventions for estimation functions.
r	code	privateestimate<-function(covars,graph,maxiterations,initialclasses,selfloops,...) UseMethod("privateestimate")
r	blank	
r	comment	# Private estimation function used to allow various call conventions for estimation functions.
r	comment	# Override of generic function for single covariables. 
r	code	privateestimate.cmcovarz<-function(covars,graph,maxiterations,initialclasses,selfloops,...)
r	code	{
r	code		res=estimateCondZ(graph,covars,maxiterations,initialclasses,selfloops)
r	code		attr(res,'class')<-c(attr(res,'class'),'cmestimation')
r	code		res
r	blank	
r	code	}
r	blank	
r	comment	# Perform parameter estimation on 'graph' given the covariables 'covars'.
r	code	estimateCondXZ<-function(graph,covars,maxiterations,initialclasses,selfloops) 
r	code	{
r	comment		#resSimXZ = EMalgorithmXZ(TauIni,Y2,Adjacente,30,SelfLoop=FALSE)
r	code		res=EMalgorithmXZ(initialclasses,covars$y,graph$adjacency,maxiterations,selfloops)
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)
r	code		attr(cmestimation,'class')<-c('cmestimationxz')
r	code		cmestimation
r	code	}
r	blank	
r	comment	# Private estimation function used to allow various call conventions for estimation functions.
r	comment	# Override of generic function for multiple covariables.
r	code	privateestimate.cmcovarxz<-function(covars,graph,maxiterations,initialclasses,selfloops,...)
r	code	{
r	code		res=estimateCondXZ(graph,covars,maxiterations,initialclasses,selfloops)
r	code		attr(res,'class')<-c(attr(res,'class'),'cmestimation')
r	code		res
r	code	}
r	blank	
r	comment	# Generic estimation function applicable to graphs with covariables.
r	code	estimate<-function(graph,covars,...) UseMethod("estimate")
r	blank	
r	comment	# Override of the generic estimation function. Performs the actual function dispatch depending on the class of covariables.
r	code	estimate.cmgraph<-function(graph,covars,maxiterations=20,initialclasses=t(rmultinom(size=1,prob=graph$proba,n=graph$nodes)),selfloops=FALSE,method=NULL,...)
r	code	{
r	code		if (length(method)  == 0) {
r	code			res=privateestimate(covars,graph,maxiterations,initialclasses,selfloops,...)
r	code		} else {
r	code			res=method(graph,covars,maxiterations,initialclasses,selfloops)
r	code			attr(res,'class')<-c(attr(res,'class'),'cmestimation')
r	code		}
r	code		res
r	code	}
r	blank	
r	comment	# Override of the generic pliot function for estimation results.
r	code	plot.cmestimation<-function(x,...)
r	code	{
r	code		par(mfrow = c(1,2))
r	code		plot(x$jexpected)
r	code		title("Expected value of J: Convergence criterion")
r	blank	
r	code		map=MAP(x$tau)
r	code		gplot(x$graph$adjacency,vertex.col=map$node.classes+2)
r	code		title("Network with estimated classes")
r	blank	
r	code	}
r	blank	
r	comment	# Generic private ICL computation function for graphs and covariables.
r	code	privatecomputeICL<-function(covars,graph,qmin,qmax,loops,maxiterations,selfloops) UseMethod("privatecomputeICL")
r	blank	
r	blank	
r	comment	# Private ICL computation function for graphs with single covariables.
r	code	privatecomputeICL.cmcovarz<-function(covars,graph,qmin,qmax,loops,maxiterations,selfloops)
r	code	{
r	code		res=ICL(X=graph$adjacency,Y=covars$y,Qmin=qmin,Qmax=qmax,loop=loops,NbIteration=maxiterations,SelfLoop=selfloops,Plot=FALSE)
r	code		attr(res,'class')<-c('cmiclz')
r	code		res
r	blank	
r	code	}
r	blank	
r	comment	# Private ICL computation function for graphs with multiple covariables.
r	code	privatecomputeICL.cmcovarxz<-function(covars,graph,qmin,qmax,loops,maxiterations,selfloops)
r	code	{
r	code		res=ICL(X=graph$adjacency,Y=covars$y,Qmin=qmin,Qmax=qmax,loop=loops,NbIteration=maxiterations,SelfLoop=selfloops,Plot=FALSE)
r	code		attr(res,'class')<-c('cmiclxz')
r	code		res
r	code	}
r	blank	
r	blank	
r	comment	# Generic public ICL computation function applicable to graph objects.
r	code	computeICL<-function(graph,covars,qmin,qmax,...) UseMethod("computeICL")
r	blank	
r	comment	# Override of ICL computation function applicable to graph objects.
r	comment	# Performs the actual method dispatch to private functions depending on the type of covariables.
r	code	computeICL.cmgraph<-function(graph,covars,qmin,qmax,loops=10,maxiterations=20,selfloops=FALSE,...)
r	code	{
r	code		res=privatecomputeICL(covars,graph,qmin,qmax,loops,maxiterations,selfloops)
r	code		res$qmin=qmin
r	code		res$qmax=qmax
r	code		res$graph=graph
r	code		res$covars=covars
r	code		attr(res,'class')<-c(attr(res,'class'),'cmicl')
r	code		res
r	code	}
r	blank	
r	comment	# Override of the plot function for results of ICL computation.
r	code	plot.cmicl<-function(x,...)
r	code	{
r	code		par(mfrow = c(1,2))
r	code		result=x$iclvalues	
r	code		maxi=which(max(result)==result)
r	code		plot(seq(x$qmin,x$qmax),result,type="b",xlab="Number of classes",ylab="ICL value")
r	code		points(maxi+x$qmin-1,result[maxi],col="red")
r	code		title("ICL curve")
r	code		best=x$EMestimation[[maxi+x$qmin-1]]
r	code		tau=best$TauEstimated
r	code		map=MAP(tau)
r	code		gplot(x$graph$adjacency,vertex.col=map$node.classes+2)
r	code		title("Network with estimated classes")
r	code	}
r	blank