File: scanStatistic.R

package info (click to toggle)
r-cran-boolnet 2.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: ansic: 12,452; sh: 16; makefile: 2
file content (216 lines) | stat: -rw-r--r-- 3,735 bytes parent folder | download | duplicates (6)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# determination of P(k,N,w)
pval <- function(k,N,w)
{	
	
	return((k/w-N-1)*b(k,N,w)+2*Gb(k,N,w))
		
}

# helper function
b<-function(k,N,w)
{
	return(choose(N,k)*w^k*(1-w)^(N-k))	
}

# helper function
Gb<-function(k,N,w)
{	
	sum<-0
	for(i in k:N)
	{	
		sum <- sum + b(i,N,w)	
	}
	
	return(sum)
}

# If two significant overlapping windows were found, these windows are
# merged. If the windows do not overlap, two different windows are stored 
# in a list 
listadapt <- function(lcur,lnew)
{
	if(length(lcur)==0)
	{
		lcur=lnew	
		return(lcur)
	}
	else
	{		
		if(lnew[[1]][1]<=lcur[[length(lcur)]][2])
		{
			lcur[[length(lcur)]][2]<-lnew[[1]][2]
			if(lcur[[length(lcur)]][3]>lnew[[1]][3])
			{
				lcur[[length(lcur)]][3] <- lnew[[1]][3]
			}
			return(lcur)
		}	
		else
		{
			lcur<-append(lcur,lnew)
			return(lcur)
		}
			
		
	}
	
}
# This method searches for data accumulations by shifting a window with
# window size <w> across the data and deciding at each position if there 
# is a data accumulation. To test this, a scan statistic with significance
# level <sign.level> is used. 
scanStatistic <- function(vect, w=0.25, sign.level=0.1)
{	
	temp<-vect
	vect <-unlist(vect)
	vsort <- sort(vect)
	N <- length(vect)
	range <- (max(vect)) - (min(vect))
	windowsize <- range*w	
	N <- length(vect)
	binarizeddata<-temp
	res<-list()
	lcur<-list()
	
	# shift a fixed window over the data
	# the window is moved from point to point
	for(i in seq_along(vect))
	{	
		start <- vsort[i]
		stop <- vsort[i] + windowsize
		
		k <- length(vect[(vect >= start) & (vect <= stop)])
		
		p <- pval(k,N,w)
		
		if(p>1)
		{
			p=0.99	
		}
		
		if(p<=sign.level & p>0 & k >= (N*w-1) & k > 2)
		{
			res <- listadapt(res,list(c(start,stop,p)))
		}
		
	}
	
	
	# if no accumulation for a fixed <sign.level> was found, the 
	# binarization is rejected, and we search for a accumulation
	# with a higher sign.level.
	if(length(res)==0)
	{ 
		while(TRUE)
		{
			sign.level=sign.level+0.05
			
			if(sign.level>2)
			{
				binarizeddata<-(sapply(vect,function(x) 0))  
				return(list(bindata=binarizeddata,thresholds=NA,reject=TRUE))
    			
			}
		
			for(i in seq_along(vect))
			{
				start <- vsort[i]
				stop <- vsort[i] + windowsize
		
				k <- length(vect[(vect >= start) & (vect <= stop)])
		
				p <- pval(k,N,w)
				
				if(p>1)
				{
					p=0.99	
				}
				
				if(p<=sign.level & p>0 & k >= (N*w-1) & k > 2)
				{	
					#res <- append(res,list(c(start=start,stop=stop,pval=p)))
					res <- listadapt(res,list(c(start,stop,p)))
				}
		
			}
			if(length(res)!=0)
				break
				
				
			
		}
		reject<-TRUE	
		
	}
	else
	{
		reject<-FALSE
	}
	
	
	# search the window with the smallest sign.level.
	# this window is used for the binarization
	min=1000
	ind=0
	for(i in seq_along(res))
	{
		if(res[[i]][3]<min)
		{
			ind=i	
			min=res[[i]][3]
		}
	}
	
	# are more points on the left or on the right side
	# of the window? Based on this, the binarization is performed
	bigger <- length(vect[vect > res[[ind]][2]]) 
	smaller <- length(vect[vect < res[[ind]][1]])
	
	if(bigger > smaller)
	{
		threshold<-res[[ind]][2]
		
		small<-tail(vsort[vsort<=threshold],n=1)
		big<-vsort[vsort>threshold][1]
		
		thres<-(big+small)/2
		
		for(i in seq_along(vect))
		{
			if(vect[i]<=threshold)
			{
				binarizeddata[i]<-0
			}
			else
			{
				binarizeddata[i]<-1
			}
		}	
			
	}
	else
	{
		threshold<-res[[ind]][1]
		
		small<-tail(vsort[vsort<threshold],n=1)
		big<-vsort[vsort>=threshold][1]		
		
		thres<-(big+small)/2
		
		for(i in seq_along(vect))
		{
			if(vect[i]>=threshold)
			{
				binarizeddata[i]<-1
			}
			else
			{
				binarizeddata[i]<-0
			}
		}
		
	}
	
	return(list(bindata=binarizeddata,thresholds=as.numeric(thres),reject=reject))
	
}