-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFperm.fd.R
143 lines (101 loc) · 4.07 KB
/
Fperm.fd.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
#Fperm.fd(annualprec, xfdlist, betalist, nperm=10)
Fperm.fd <- function(yfdPar, xfdlist, betalist, wt=NULL,
nperm=200,argvals=NULL,q=0.05,plotres=TRUE,...)
{
# yfdpar, xfdList, betalist, wt = standard inputs to fRegress
# nperm = number of permutations,
# argvals = where to evaluate functional responses,
# q = quantile to compare
# plotres: Do we plot the results?
# set up containers for null results
Fnull = rep(0,nperm)
Fnullvals = c()
# switch q from residual to main body
q <- 1-q
# keep track of processing time
begin <- proc.time()
fRegressList <- fRegress(yfdPar, xfdlist, betalist)
elapsed.time <- max(proc.time()-begin,na.rm=TRUE)
if( elapsed.time > 30/nperm ) {
print(paste('Estimated Computing time =',
round(nperm*elapsed.time),'seconds.'))
}
#. obtain fit vector yhat from fRegressList
yhat <- fRegressList$yhatfdobj. #. the fitted values
if(is.null(yhat)) yhat = fRegressList$yhat
if(is.list(yhat) && ('fd' %in% names(yhat))) yhat <- yhat$fd
#. initial invokation of Fstat.fd
tFstat <- Fstat.fd(yfdPar,yhat,argvals)
# two sets of results
Fvals <- tFstat$F
Fobs <- max(Fvals)
#. obtain argument values
argvals <- tFstat$argvals
#. determine length of data vector
if(is.vector(yfdPar)){
n <- length(yfdPar)
} else {
n <- ncol(yfdPar$coefs)
}
#. --------------------------------------------------------------------------
# Loop through permutations
#. --------------------------------------------------------------------------
for(i in 1:nperm){
tyfdPar <- yfdPar[sample(n)]
# analyze of a vector of observations
fRegressList <- fRegress(tyfdPar, xfdlist, betalist)
# extract fit vector from fRegressList
if(is.fd(yhat)) {
yhat <- fRegressList$yhatfdobj
if(is.list(yhat) && ('fd' %in% names(yhat))) yhat <- yhat$fd
} else {
yhat <- fRegressList$yhat
}
# ith analysis by Fstat.fd
tFstat <- Fstat.fd(tyfdPar,yhat,argvals)
# add Fnullvals to container Fnullvals
Fnullvals <- cbind(Fnullvals,tFstat$F)
#.
Fnull[i] <- max(Fnullvals[,i])
}
#. --------------------------------------------------------------------------
# Display results
#. --------------------------------------------------------------------------
pval <- mean( Fobs < Fnull )
qval <- quantile(Fnull,q)
pvals.pts <- apply(Fvals<Fnullvals,1,mean)
qvals.pts <- apply(Fnullvals,1,quantile,q)
if(plotres) {
#. dispslay results using scattter plots
if(is.fd(yfdPar)){
ylims <- c(min(c(Fvals,qval,qvals.pts)),max(c(Fobs,qval)))
if( is.null(names(yhat$fdnames)) ){ xlab <- 'argvals' }
else{ xlab <- names(yhat$fdnames)[1] }
plot(argvals,Fvals,type="l",ylim=ylims,col=2,lwd=2,
xlab=xlab,ylab='F-statistic',main='Permutation F-Test',...)
lines(argvals,qvals.pts,lty=3,col=4,lwd=2)
abline(h=qval,lty=2,col=4,lwd=2)
legendstr <- c('Observed Statistic',
paste('pointwise',1-q,'critical value'),
paste('maximum',1-q,'critical value'))
legend(argvals[1],ylims[2],legend=legendstr,col=c(2,4,4),
lty=c(1,3,2),lwd=c(2,2,2))
} else {
#. display results with histogram
xlims <- c(min(c(Fnull,Fobs)),max(c(Fnull,Fobs)))
hstat <- hist(Fnull,xlim=xlims,lwd=2,xlab='F-value',
main = 'Permutation F-Test')
abline(v = Fobs,col=2,lwd=2)
abline(v = qval,col=4,lty=2,lwd=2)
legendstr <- c('Observed Statistic',
paste('Permutation',1-q,'critical value'))
legend(xlims[1],max(hstat$counts),legend=legendstr,col=c(2,4),
lty=c(1,2),lwd=c(2,2))
}
}
#. return results
return(list(pval=pval, qval=qval, Fobs=Fobs, Fnull=Fnull,
Fvals=Fvals, Fnullvals=Fnullvals, pvals.pts=pvals.pts,
qvals.pts=qvals.pts,
fRegressList=fRegressList, argvals=argvals))
}