-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patheval.monfd.R
188 lines (158 loc) · 5.68 KB
/
eval.monfd.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
predict.monfd <- function(object, newdata=NULL, Lfdobj=0,
returnMatrix=FALSE, ...) {
if(is.null(newdata))newdata <- object$argvals
##
## 1. eval.monfd
##
evalMon <- eval.monfd(newdata, object$Wfdobj, Lfdobj, returnMatrix)
##
## 2. beta
##
beta <- object$beta
{
if(length(dim(beta))<2){
if(length(dim(evalMon))<2){
be <- beta[2]*evalMon
if(Lfdobj<1)
be <- beta[1]+be
return(be)
}
else
stop('beta does not match eval.monfd(...)')
}
else {
nem <- dim(evalMon)
if(length(dim(beta)<3)) {
if(length(nem)==2){
be <- (evalMon*rep(beta[2,], each=nem[1]))
if(Lfdobj<1)
be <- (be+rep(beta[1,], each=nem[1]))
return(be)
}
else
stop('beta does not match eval.monfd(...)')
}
else {
if(length(nem)==3){
be <- (evalMon*rep(beta[2,,], each=nem[1]))
if(Lfdobj<1)
be <- (be+rep(beta[1,,], each=nem[1]))
return(be)
}
else
stop('beta does not match eval.monfd(...)')
}
}
}
}
fitted.monfd <- function(object, ...){
predict(object)
}
residuals.monfd <- function(object, ...){
pred <- predict(object)
object$y-pred
}
eval.monfd <- function(evalarg, Wfdobj, Lfdobj=int2Lfd(0), returnMatrix=FALSE) {
# Evaluates a monotone functional data observation, or the value of a linear
# differential operator LFD applied to the object,
# at argument values in an array EVALARGS.
# Functional data object LFD, if an integer, defines NDERIV, the
# order of derivative to be evaluated.
# Functional data object LFD, if a fd object, defines weight
# functions for computing the value of a linear differential operator
# applied to the functions that are evaluated.
# A monotone functional data object h is in the form
# h(x) = [D^{-1} exp Wfdobj](x)
# where D^{-1} means taking the indefinite integral.
# The interval over which the integration takes places is defined in
# the basisfd object in WFD.
# RETURNMATRIX ... If False, a matrix in sparse storage model can be returned
# from a call to function BsplineS. See this function for
# enabling this option.
# Last modified 9 May 2012 by Jim Ramsay
# check Wfdobj
if (!inherits(Wfdobj, "fd")) stop("Wfdobj is not a fd object.")
# extract number of variables and curves from coefficient matrix for Wfdobj
coef <- Wfdobj$coefs
if (is.vector(coef)) coef <- as.matrix(coef)
coefd <- dim(coef)
ndim <- length(coefd)
if (ndim == 2) {
ncurve <- coefd[2]
nvar <- 1
} else {
ncurve <- coefd[2]
nvar <- coefd[3]
}
# determine if LFDOBJ is an integer
Lfdobj <- int2Lfd(Lfdobj)
if (!is.integerLfd(Lfdobj)) stop(
"LFDOBJ is not an integer operator.")
nderiv <- Lfdobj$nderiv
n <- length(evalarg)
hmat <- array(0,c(n,ncurve,nvar))
if (nderiv >= 2) Dwmat <- getbasismatrix(evalarg, Wfdobj$basis, 1,
returnMatrix)
if (nderiv == 3) D2wmat <- getbasismatrix(evalarg, Wfdobj$basis, 2,
returnMatrix)
basislist = vector("list", 15)
for (ivar in 1:nvar) {
for (icurve in 1:ncurve) {
if (nderiv == 0) {
if (ndim == 2) {
if (ncurve == 1) {
hmat[,icurve,ivar] <- monfn(evalarg, Wfdobj, basislist,
returnMatrix)
} else {
hmat[,icurve,ivar] <- monfn(evalarg, Wfdobj[icurve], basislist,
returnMatrix)
}
} else {
hmat[,icurve,ivar] <- monfn(evalarg, Wfdobj[icurve,ivar], basislist,
returnMatrix)
}
}
if (nderiv == 1) {
if (ndim == 2) {
hmat[,icurve,ivar] <- exp(eval.fd(evalarg, Wfdobj[icurve], 0,
returnMatrix))
} else {
hmat[,icurve,ivar] <- exp(eval.fd(evalarg, Wfdobj[icurve,ivar], 0,
returnMatrix))
}
}
if (nderiv == 2) {
if (ndim == 2) {
temp = (Dwmat %*% coef[,icurve])*
exp(eval.fd(evalarg, Wfdobj[icurve], 0,
returnMatrix))
hmat[,icurve,ivar] <- as.vector(temp)
} else {
temp = (Dwmat %*% coef[,icurve])*
exp(eval.fd(evalarg, Wfdobj[icurve,ivar], 0,
returnMatrix))
hmat[,icurve,ivar] <- as.vector(temp)
}
}
if (nderiv == 3) {
if (ndim == 2) {
hmat[,icurve,ivar] <- as.vector(((D2wmat %*% coef[,icurve]) +
(Dwmat %*% coef[,icurve])^2)*
exp(eval.fd(evalarg, Wfdobj[icurve], 0,
returnMatrix)))
} else {
hmat[,icurve,ivar] <- as.vector(((D2wmat %*% coef[,icurve,ivar]) +
(Dwmat %*% coef[,icurve,ivar])^2)*
exp(eval.fd(evalarg, Wfdobj[icurve,ivar],
0, returnMatrix)))
}
}
if (nderiv > 3) stop ("Derivatives higher than 3 are not implemented.")
}
}
if (nvar == 1) hmat <- as.matrix(hmat[,,1])
if((!returnMatrix) && (length(dim(hmat)) == 2)){
return(as.matrix(hmat))
}
return(hmat)
}