-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneralfisher.R
147 lines (112 loc) · 3.56 KB
/
generalfisher.R
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
rm(list=ls())
library(CorrBin)
data(shelltox)
shelltox=shelltox
#####################################################################################
####### Dose group and High #####################################
xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)
d1=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)[,,1]
d4=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)[,,4]
datan=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)[,,c(1,4)]
both=d1+d4
#d=rbind(d1,d4)
d=datan
probdist=function(d){
d1=d[,,1]
d4=d[,,2]
m1=apply(d4,2,sum)
m2=apply(d1,2,sum)
M=m1+m2
b=d1+d4
p=sum(lfactorial(m1))+sum(lfactorial(m2))+
sum(apply(lfactorial(b),2,sum))-sum(lfactorial(M))-
sum(apply(lfactorial(d4),2,sum))-sum(apply(lfactorial(d1),2,sum))
return((p))
}
pobs=probdist(d)
pobs
c=datan
t=array(0,c(dim(c)[1],dim(c)[2],dim(c)[3]))
ps=c()
nsim=100000
for (j in 1:nsim){
for (i in 1:dim(c)[3]){
t[,,i]=r2dtable(1, rowSums(c[,,i]), colSums(c[,,i]))[[1]]
}
ps[j]=probdist(t)
}
###### two sided ##
##0.103
p.value=(length(((ps[pobs<=ps])))+1)/(nsim+1)
p.value #####0.6914231
se=sqrt((p.value*(1-p.value))/(nsim-1))
up=p.value+2.575*se
lo=p.value-2.575*se
c(lo,up) ####0.6876618 0.6951843
### mid p-value ##########
pval=0.5*(length(((ps[ps==pobs]))))/(nsim)+(length(((ps[ps>pobs]))))/(nsim)
pval
###0.597 new 0.68694
#par(mfrow=c(1,1))
#hist(ps, breaks="fd", col="gray",main="Distribution of of Simulated Values ",xlab="",prob=TRUE)
#abline(v =pobs,col="blue")
#box(lty = 'solid', col = 'black')
#png("~/Documents/memphisclassesbooks/RESEARCH/Trend/gfch.png")
#hist(ps, breaks="fd", col="gray",main="Distribution of of Simulated Values ",xlab="",prob=TRUE,ylab="P(T=t)")
#abline(v =pobs,col="blue")
#box(lty = 'solid', col = 'black')
#dev.off();
#####################################################################################
####### Dose group control,low,medium and High #####################################
c=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)
control=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)[,,1]
low=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)[,,2]
medium=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)[,,3]
high=xtabs(Freq~NResp+ClusterSize+Trt,data=shelltox)[,,4]
both=control+high+medium+low
d=c
probdist=function(d){
control=d[,,1]
low=d[,,2]
medium=d[,,3]
high=d[,,4]
m1=apply(control,2,sum)
m2=apply(low,2,sum)
m3=apply(medium,2,sum)
m4=apply(high,2,sum)
M=m1+m2+m3+m4
b=control+high+low+medium
p=sum(lfactorial(m1))+sum(lfactorial(m2))+sum(lfactorial(m3))+sum(lfactorial(m4))+
sum(apply(lfactorial(b),2,sum))-sum(lfactorial(M))-
sum(apply(lfactorial(high),2,sum))-sum(apply(lfactorial(control),2,sum))-
sum(apply(lfactorial(low),2,sum))-sum(apply(lfactorial(medium),2,sum))
return((p))
}
pobs=probdist(d)
pobs
t=array(0,c(dim(c)[1],dim(c)[2],dim(c)[3]))
ps=c()
nsim=100000
for (j in 1:nsim){
for (i in 1:dim(c)[3]){
t[,,i]=r2dtable(1, rowSums(c[,,i]), colSums(c[,,i]))[[1]]
}
ps[j]=probdist(t)
}
###0.182 new 0.3479965
p.value=(length(((ps[pobs<=ps])))+1)/(nsim+1)
p.value
se=sqrt((p.value*(1-p.value))/(nsim-1))
up=p.value+2.575*se
lo=p.value-2.575*se
c(lo,up)
### 0.3424222 0.3501708
### mid p-value ##########
pval=0.5*(length(((ps[ps==pobs]))))/(nsim)+(length(((ps[ps>pobs]))))/(nsim)
pval
###0.67 ##new 0.346165
#png("~/Documents/memphisclassesbooks/RESEARCH/Trend/gfclmh.png")
#hist(ps, breaks="fd", col="gray",main="Distribution of of Simulated Values ",xlab="",prob=TRUE,ylab="P(T=t)")
#abline(v =pobs,col="blue")
#box(lty = 'solid', col = 'black')
dev.off();