PageRenderTime 61ms CodeModel.GetById 46ms app.highlight 3ms RepoModel.GetById 10ms app.codeStats 0ms

/packages/archive/2010/05.2010/05.02.2010/spacodi/R/randomization.test.sp.R

http://github.com/eastman/spacodiR
R | 42 lines | 35 code | 7 blank | 0 comment | 12 complexity | b6cdf3ddbf7170e0930f975bd6cd6880 MD5 | raw file
 1randomization.test.sp=function(obs=obs, exp=exp, iter=10000, return.all=FALSE, two.tailed=TRUE){
 2	
 3	obs=obs[is.finite(as.numeric(obs))]
 4	exp=exp[is.finite(as.numeric(exp))]
 5	
 6	O=sample(as.numeric(obs), size=iter, replace=TRUE)
 7	E=sample(as.numeric(exp), size=iter, replace=TRUE)
 8	
 9	rr=cbind((O-E), NA)
10	rr[,2]=ifelse(rr[,1]>0, 1, ifelse(rr[,1]<0, 0, sample(c(0,1),1)))
11	p=rr[,2]
12	
13	p.g=1-(sum(p)/iter)
14	p.l=1-p.g
15	
16	out.d=as.numeric(rr[,1])
17	
18	if(!two.tailed) {
19		out.p=list(p.g,p.l)
20	} else {
21		if(p.g!=p.l)out.p=min(c(p.g,p.l))*2 else out.p=1
22	}
23	
24	if(return.all) {
25		r.out=list(out.d, out.p)
26	} else {
27		r.out=out.p
28	}
29	if(length(r.out)==1) {									#r.all=FALSE; two=TRUE
30		names(r.out)="p.value"
31	} else if(two.tailed==FALSE){
32		if(return.all==TRUE) {
33			names(r.out)=c("differences","p.value")         #r.all=TRUE; two=FALSE
34			names(r.out[[2]])=c("greater","lesser")
35		} else {
36			names(r.out)=c("greater","lesser")				#r.all=FALSE; two=FALSE
37		}	
38	} else {												#r.all=TRUE; two=TRUE
39		names(r.out)=c("differences","p.value")  
40	}
41	return(r.out)
42}