/
duncan.test.R
211 lines (211 loc) · 6.88 KB
/
duncan.test.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
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
#' Duncan's new multiple range test
#'
#' This test is adapted from the Newman-Keuls method. Duncan's test does not
#' control family wise error rate at the specified alpha level. It has more
#' power than the other post tests, but only because it doesn't control the
#' error rate properly. The Experimentwise Error Rate at: 1-(1-alpha)^(a-1);
#' where "a" is the number of means and is the Per-Comparison Error Rate.
#' Duncan's procedure is only very slightly more conservative than LSD. The
#' level by alpha default is 0.05.
#'
#' It is necessary first makes a analysis of variance.\cr
#'
#' if y = model, then to apply the instruction:\cr duncan.test(model, "trt",
#' alpha = 0.05, group = TRUE, main = NULL, console = FALSE)\cr where the model
#' class is aov or lm.
#'
#' @param y model(aov or lm) or answer of the experimental unit
#' @param trt Constant( only y=model) or vector treatment applied to each
#' experimental unit
#' @param DFerror Degree free
#' @param MSerror Mean Square Error
#' @param alpha Significant level
#' @param group TRUE or FALSE
#' @param main Title
#' @param console logical, print output
#' @return \item{statistics}{Statistics of the model} \item{parameters}{Design
#' parameters} \item{duncan}{Critical Range Table} \item{means}{Statistical
#' summary of the study variable} \item{comparison}{Comparison between
#' treatments} \item{groups}{Formation of treatment groups}
#' @author Felipe de Mendiburu
#' @seealso \code{\link{BIB.test}}, \code{\link{DAU.test}},
#' \code{\link{durbin.test}}, \code{\link{friedman}}, \code{\link{HSD.test}},
#' \code{\link{kruskal}}, \code{\link{LSD.test}}, \code{\link{Median.test}},
#' \code{\link{PBIB.test}}, \code{\link{REGW.test}},
#' \code{\link{scheffe.test}}, \code{\link{SNK.test}},
#' \code{\link{waerden.test}}, \code{\link{waller.test}},
#' \code{\link{plot.group}}
#' @references 1. Principles and procedures of statistics a biometrical
#' approach Steel & Torry & Dickey. Third Edition 1997\cr 2. Multiple
#' comparisons theory and methods. Departament of statistics the Ohio State
#' University. USA, 1996. Jason C. Hsu. Chapman Hall/CRC.\cr
#' @keywords htest
#' @importFrom stats quantile qtukey ptukey uniroot
#' @export
#' @examples
#'
#' library(agricolae)
#' data(sweetpotato)
#' model<-aov(yield~virus,data=sweetpotato)
#' out <- duncan.test(model,"virus",
#' main="Yield of sweetpotato. Dealt with different virus")
#' plot(out,variation="IQR")
#' duncan.test(model,"virus",alpha=0.01,console=TRUE)
#' # version old duncan.test()
#' df<-df.residual(model)
#' MSerror<-deviance(model)/df
#' out <- with(sweetpotato,duncan.test(yield,virus,df,MSerror, group=TRUE))
#' plot(out,horiz=TRUE,las=1)
#' print(out$groups)
#'
duncan.test <-
function (y, trt, DFerror, MSerror, alpha=0.05, group=TRUE,main = NULL,console=FALSE)
{
name.y <- paste(deparse(substitute(y)))
name.t <- paste(deparse(substitute(trt)))
if(is.null(main))main<-paste(name.y,"~", name.t)
clase<-c("aov","lm")
if("aov"%in%class(y) | "lm"%in%class(y)){
if(is.null(main))main<-y$call
A<-y$model
DFerror<-df.residual(y)
MSerror<-deviance(y)/DFerror
y<-A[,1]
ipch<-pmatch(trt,names(A))
nipch<- length(ipch)
for(i in 1:nipch){
if (is.na(ipch[i]))
return(if(console)cat("Name: ", trt, "\n", names(A)[-1], "\n"))
}
name.t<- names(A)[ipch][1]
trt <- A[, ipch]
if (nipch > 1){
trt <- A[, ipch[1]]
for(i in 2:nipch){
name.t <- paste(name.t,names(A)[ipch][i],sep=":")
trt <- paste(trt,A[,ipch[i]],sep=":")
}}
name.y <- names(A)[1]
}
junto <- subset(data.frame(y, trt), is.na(y) == FALSE)
Mean<-mean(junto[,1])
CV<-sqrt(MSerror)*100/Mean
medians<-tapply.stat(junto[,1],junto[,2],stat="median")
for(i in c(1,5,2:4)) {
x <- tapply.stat(junto[,1],junto[,2],function(x)quantile(x)[i])
medians<-cbind(medians,x[,2])
}
medians<-medians[,3:7]
names(medians)<-c("Min","Max","Q25","Q50","Q75")
means <- tapply.stat(junto[,1],junto[,2],stat="mean") # change
sds <- tapply.stat(junto[,1],junto[,2],stat="sd") #change
nn <- tapply.stat(junto[,1],junto[,2],stat="length") # change
means<-data.frame(means,std=sds[,2],r=nn[,2],medians)
names(means)[1:2]<-c(name.t,name.y)
ntr<-nrow(means)
Tprob<-NULL
k<-0
for(i in 2:ntr){
k<-k+1
x <- suppressWarnings(warning(qtukey((1-alpha)^(i-1), i, DFerror)))
if(x=="NaN")break
else Tprob[k]<-x
}
if(k<(ntr-1)){
for(i in k:(ntr-1)){
f <- Vectorize(function(x)ptukey(x,i+1,DFerror)-(1-alpha)^i)
Tprob[i]<-uniroot(f, c(0,100))$root
}
}
Tprob<-as.numeric(Tprob)
nr <- unique(nn[,2])
# Critical Value of Studentized Range
if(console){
cat("\nStudy:", main)
cat("\n\nDuncan's new multiple range test\nfor",name.y,"\n")
cat("\nMean Square Error: ",MSerror,"\n\n")
cat(paste(name.t,",",sep="")," means\n\n")
print(data.frame(row.names = means[,1], means[,2:6]))
}
if(length(nr) == 1 ) sdtdif <- sqrt(MSerror/nr)
else {
nr1 <- 1/mean(1/nn[,2])
sdtdif <- sqrt(MSerror/nr1)
}
DUNCAN <- Tprob * sdtdif
names(DUNCAN)<-2:ntr
duncan<-data.frame(Table=Tprob,CriticalRange=DUNCAN)
if ( group & length(nr) == 1 & console){
cat("\nAlpha:",alpha,"; DF Error:",DFerror,"\n")
cat("\nCritical Range\n")
print(DUNCAN)
}
if ( group & length(nr) != 1 & console) cat("\nGroups according to probability of means differences and alpha level(",alpha,")\n")
if ( length(nr) != 1) duncan<-NULL
Omeans<-order(means[,2],decreasing = TRUE) #correccion 2019, 1 abril.
Ordindex<-order(Omeans)
comb <-utils::combn(ntr,2)
nn<-ncol(comb)
dif<-rep(0,nn)
DIF<-dif
LCL<-dif
UCL<-dif
pvalue<-dif
odif<-dif
sig<-NULL
for (k in 1:nn) {
i<-comb[1,k]
j<-comb[2,k]
dif[k]<-means[i,2]-means[j,2]
DIF[k]<-abs(dif[k])
nx<-abs(i-j)+1
odif[k] <- abs(Ordindex[i]- Ordindex[j])+1
pvalue[k]<- round(1-ptukey(DIF[k]/sdtdif,odif[k],DFerror)^(1/(odif[k]-1)),4)
LCL[k] <- dif[k] - DUNCAN[odif[k]-1]
UCL[k] <- dif[k] + DUNCAN[odif[k]-1]
sig[k]<-" "
if (pvalue[k] <= 0.001) sig[k]<-"***"
else if (pvalue[k] <= 0.01) sig[k]<-"**"
else if (pvalue[k] <= 0.05) sig[k]<-"*"
else if (pvalue[k] <= 0.1) sig[k]<-"."
}
if(!group){
tr.i <- means[comb[1, ],1]
tr.j <- means[comb[2, ],1]
comparison<-data.frame("difference" = dif, pvalue=pvalue,"signif."=sig,LCL,UCL)
rownames(comparison)<-paste(tr.i,tr.j,sep=" - ")
if(console){cat("\nComparison between treatments means\n\n")
print(comparison)}
groups=NULL
}
if (group) {
comparison=NULL
# The probability matrix
Q<-matrix(1,ncol=ntr,nrow=ntr)
p<-pvalue
k<-0
for(i in 1:(ntr-1)){
for(j in (i+1):ntr){
k<-k+1
Q[i,j]<-p[k]
Q[j,i]<-p[k]
}
}
groups <- orderPvalue(means[, 1], means[, 2],alpha, Q,console)
names(groups)[1]<-name.y
if(console) {
cat("\nMeans with the same letter are not significantly different.\n\n")
print(groups)
}
}
parameters<-data.frame(test="Duncan",name.t=name.t,ntr = ntr,alpha=alpha)
statistics<-data.frame(MSerror=MSerror,Df=DFerror,Mean=Mean,CV=CV)
rownames(parameters)<-" "
rownames(statistics)<-" "
rownames(means)<-means[,1]
means<-means[,-1]
output<-list(statistics=statistics,parameters=parameters, duncan=duncan,
means=means,comparison=comparison,groups=groups)
class(output)<-"group"
invisible(output)
}