1
+ # Fperm.fd(annualprec, xfdlist, betalist, nperm=10)
2
+
1
3
Fperm.fd <- function (yfdPar , xfdlist , betalist , wt = NULL ,
2
4
nperm = 200 ,argvals = NULL ,q = 0.05 ,plotres = TRUE ,... )
3
5
{
@@ -7,96 +9,135 @@ Fperm.fd <- function(yfdPar, xfdlist, betalist, wt=NULL,
7
9
# q = quantile to compare
8
10
# plotres: Do we plot the results?
9
11
12
+ # set up containers for null results
13
+
10
14
Fnull = rep(0 ,nperm )
11
15
Fnullvals = c()
12
-
13
- q = 1 - q
14
-
16
+
17
+ # switch q from residual to main body
18
+
19
+ q <- 1 - q
20
+
21
+ # keep track of processing time
22
+
15
23
begin <- proc.time()
16
24
fRegressList <- fRegress(yfdPar , xfdlist , betalist )
17
25
elapsed.time <- max(proc.time()- begin ,na.rm = TRUE )
18
-
19
- if ( elapsed.time > 30 / nperm ){
26
+ if ( elapsed.time > 30 / nperm ) {
20
27
print(paste(' Estimated Computing time =' ,
21
28
round(nperm * elapsed.time ),' seconds.' ))
22
29
}
23
-
24
- yhat <- fRegressList $ yhatfdobj
25
- if (is.null(yhat )){ yhat = fRegressList $ yhat }
30
+
31
+ # . obtain fit vector yhat from fRegressList
32
+
33
+ yhat <- fRegressList $ yhatfdobj. # . the fitted values
34
+ if (is.null(yhat )) yhat = fRegressList $ yhat
26
35
if (is.list(yhat ) && (' fd' %in% names(yhat ))) yhat <- yhat $ fd
27
-
36
+
37
+ # . initial invokation of Fstat.fd
38
+
28
39
tFstat <- Fstat.fd(yfdPar ,yhat ,argvals )
29
-
40
+
41
+ # two sets of results
42
+
30
43
Fvals <- tFstat $ F
31
- Fobs = max(Fvals )
32
-
33
- argvals = tFstat $ argvals
34
-
44
+ Fobs <- max(Fvals )
45
+
46
+ # . obtain argument values
47
+
48
+ argvals <- tFstat $ argvals
49
+
50
+ # . determine length of data vector
51
+
35
52
if (is.vector(yfdPar )){
36
- n = length(yfdPar )
53
+ n <- length(yfdPar )
37
54
} else {
38
- n = ncol(yfdPar $ coefs )
55
+ n <- ncol(yfdPar $ coefs )
39
56
}
40
-
57
+
58
+ # . --------------------------------------------------------------------------
59
+ # Loop through permutations
60
+ # . --------------------------------------------------------------------------
61
+
41
62
for (i in 1 : nperm ){
42
-
43
- tyfdPar = yfdPar [sample(n )]
44
-
63
+
64
+ tyfdPar <- yfdPar [sample(n )]
65
+
66
+ # analyze of a vector of observations
67
+
45
68
fRegressList <- fRegress(tyfdPar , xfdlist , betalist )
46
-
47
- if (is.fd(yhat )){
48
- yhat <- fRegressList $ yhatfdobj
49
- if (is.list(yhat ) && (' fd' %in% names(yhat ))) yhat <- yhat $ fd
50
- } else { yhat = fRegressList $ yhat }
51
-
52
- tFstat = Fstat.fd(tyfdPar ,yhat ,argvals )
53
-
69
+
70
+ # extract fit vector from fRegressList
71
+
72
+ if (is.fd(yhat )) {
73
+ yhat <- fRegressList $ yhatfdobj
74
+ if (is.list(yhat ) && (' fd' %in% names(yhat ))) yhat <- yhat $ fd
75
+ } else {
76
+ yhat <- fRegressList $ yhat
77
+ }
78
+
79
+ # ith analysis by Fstat.fd
80
+
81
+ tFstat <- Fstat.fd(tyfdPar ,yhat ,argvals )
82
+
83
+ # add Fnullvals to container Fnullvals
84
+
54
85
Fnullvals <- cbind(Fnullvals ,tFstat $ F )
55
-
56
- Fnull [i ] = max(Fnullvals [,i ])
86
+
87
+ # .
88
+ Fnull [i ] <- max(Fnullvals [,i ])
57
89
}
58
-
59
- pval = mean( Fobs < Fnull )
60
- qval = quantile(Fnull ,q )
61
-
62
- pvals.pts = apply(Fvals < Fnullvals ,1 ,mean )
63
- qvals.pts = apply(Fnullvals ,1 ,quantile ,q )
64
-
65
- if (plotres ){
66
- if (is.fd(yfdPar )){
67
- ylims = c(min(c(Fvals ,qval ,qvals.pts )),max(c(Fobs ,qval )))
68
-
69
- if ( is.null(names(yhat $ fdnames )) ){ xlab = ' argvals' }
70
- else { xlab = names(yhat $ fdnames )[1 ] }
71
-
72
- plot(argvals ,Fvals ,type = " l" ,ylim = ylims ,col = 2 ,lwd = 2 ,
73
- xlab = xlab ,ylab = ' F-statistic' ,main = ' Permutation F-Test' ,... )
74
- lines(argvals ,qvals.pts ,lty = 3 ,col = 4 ,lwd = 2 )
75
- abline(h = qval ,lty = 2 ,col = 4 ,lwd = 2 )
76
-
77
- legendstr = c(' Observed Statistic' ,
78
- paste(' pointwise' ,1 - q ,' critical value' ),
79
- paste(' maximum' ,1 - q ,' critical value' ))
80
-
81
- legend(argvals [1 ],ylims [2 ],legend = legendstr ,col = c(2 ,4 ,4 ),
82
- lty = c(1 ,3 ,2 ),lwd = c(2 ,2 ,2 ))
83
- }
84
- else {
85
- xlims = c(min(c(Fnull ,Fobs )),max(c(Fnull ,Fobs )))
86
- hstat = hist(Fnull ,xlim = xlims ,lwd = 2 ,xlab = ' F-value' ,
87
- main = ' Permutation F-Test' )
88
- abline(v = Fobs ,col = 2 ,lwd = 2 )
89
- abline(v = qval ,col = 4 ,lty = 2 ,lwd = 2 )
90
-
91
- legendstr = c(' Observed Statistic' ,
92
- paste(' Permutation' ,1 - q ,' critical value' ))
93
-
94
- legend(xlims [1 ],max(hstat $ counts ),legend = legendstr ,col = c(2 ,4 ),
95
- lty = c(1 ,2 ),lwd = c(2 ,2 ))
96
- }
90
+
91
+ # . --------------------------------------------------------------------------
92
+ # Display results
93
+ # . --------------------------------------------------------------------------
94
+
95
+ pval <- mean( Fobs < Fnull )
96
+ qval <- quantile(Fnull ,q )
97
+
98
+ pvals.pts <- apply(Fvals < Fnullvals ,1 ,mean )
99
+ qvals.pts <- apply(Fnullvals ,1 ,quantile ,q )
100
+
101
+ if (plotres ) {
102
+ # . dispslay results using scattter plots
103
+ if (is.fd(yfdPar )){
104
+ ylims <- c(min(c(Fvals ,qval ,qvals.pts )),max(c(Fobs ,qval )))
105
+
106
+ if ( is.null(names(yhat $ fdnames )) ){ xlab <- ' argvals' }
107
+ else { xlab <- names(yhat $ fdnames )[1 ] }
108
+
109
+ plot(argvals ,Fvals ,type = " l" ,ylim = ylims ,col = 2 ,lwd = 2 ,
110
+ xlab = xlab ,ylab = ' F-statistic' ,main = ' Permutation F-Test' ,... )
111
+ lines(argvals ,qvals.pts ,lty = 3 ,col = 4 ,lwd = 2 )
112
+ abline(h = qval ,lty = 2 ,col = 4 ,lwd = 2 )
113
+
114
+ legendstr <- c(' Observed Statistic' ,
115
+ paste(' pointwise' ,1 - q ,' critical value' ),
116
+ paste(' maximum' ,1 - q ,' critical value' ))
117
+
118
+ legend(argvals [1 ],ylims [2 ],legend = legendstr ,col = c(2 ,4 ,4 ),
119
+ lty = c(1 ,3 ,2 ),lwd = c(2 ,2 ,2 ))
120
+ } else {
121
+ # . display results with histogram
122
+
123
+ xlims <- c(min(c(Fnull ,Fobs )),max(c(Fnull ,Fobs )))
124
+ hstat <- hist(Fnull ,xlim = xlims ,lwd = 2 ,xlab = ' F-value' ,
125
+ main = ' Permutation F-Test' )
126
+ abline(v = Fobs ,col = 2 ,lwd = 2 )
127
+ abline(v = qval ,col = 4 ,lty = 2 ,lwd = 2 )
128
+
129
+ legendstr <- c(' Observed Statistic' ,
130
+ paste(' Permutation' ,1 - q ,' critical value' ))
131
+
132
+ legend(xlims [1 ],max(hstat $ counts ),legend = legendstr ,col = c(2 ,4 ),
133
+ lty = c(1 ,2 ),lwd = c(2 ,2 ))
97
134
}
98
-
99
- return (list (pval = pval ,qval = qval ,Fobs = Fobs ,Fnull = Fnull ,
100
- Fvals = Fvals ,Fnullvals = Fnullvals ,pvals.pts = pvals.pts ,qvals.pts = qvals.pts ,
101
- fRegressList = fRegressList ,argvals = argvals ))
135
+ }
136
+
137
+ # . return results
138
+
139
+ return (list (pval = pval , qval = qval , Fobs = Fobs , Fnull = Fnull ,
140
+ Fvals = Fvals , Fnullvals = Fnullvals , pvals.pts = pvals.pts ,
141
+ qvals.pts = qvals.pts ,
142
+ fRegressList = fRegressList , argvals = argvals ))
102
143
}
0 commit comments