-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfRegress.stderr.R
283 lines (249 loc) · 9.68 KB
/
fRegress.stderr.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
fRegress.stderr <- function (y, y2cMap, SigmaE, returnMatrix = FALSE, ...)
{
# FREGRESS.STDERR computes standard error estimates for regression
# coefficient functions estimated by function FREGRESS.
#
# Arguments:
#
# Y ... a list object produced by function FREGRESS with class name
# 'fRegress".
# This is indicated by Y in the arguments since R syntax
# requires all of tghe fRegress family of functions to
# use this notation.
# Y2CMAP ... the matrix mapping from the vector of observed values
# to the coefficients for the dependent variable.
# This is output by function SMOOTH_BASIS. If this is
# supplied, confidence limits are computed, otherwise not.
# SIGMAE ... Estimate of the covariances among the residuals. This
# can only be estimated after a preliminary analysis
# with FREGRESS.
# 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.
#
# Returns:
#
# BETASTDERRLIST ... a list object, each list containing a fdPar object
# for the standard error of a regression function.
# BVAR ... the symmetric matrix of sampling variances and
# covariances for the matrix of regression coefficients
# for the regression functions. These are stored
# column-wise in defining BVARIANCE.
# C2BMAP ... the matrix mapping from response variable coefficients
# to coefficients for regression coefficients
# Last modified 16 December 2020
# retrieve objects from y
yfdobj <- y$yfdobj
xfdlist <- y$xfdlist
betalist <- y$betalist
betaestlist <- y$betaestlist
yhatfdobj <- y$yhatfdobj
Cmat <- y$Cmat
Dmat <- y$Dmat
Cmatinv <- y$Cmatinv
wt <- y$wt
df <- y$df
betastderrlist <- y$betastderrlist
YhatStderr <- y$YhatStderr
Bvar <- y$Bvar
c2bMap <- y$c2bMap
# get number of independent variables
p <- length(xfdlist)
# compute number of coefficients
ncoef <- 0
for (j in 1:p) {
betaParfdj <- betalist[[j]]
ncoefj <- betaParfdj$fd$basis$nbasis
ncoef <- ncoef + ncoefj
}
if (inherits(yfdobj, "fdPar") || inherits(yfdobj, "fd")) {
# ----------------------------------------------------------------
# yfdobj is functional data object
# ----------------------------------------------------------------
# As of 2020, if yfdobj is an fdPar object, it is converted to an fd object.
# The added structure of the fdPar class is not used in any of the fRegress codes.
# The older versions of fda package used yfdPar as the name for the first member.
if (inherits(yfdobj, "fdPar")) yfdobj <- yfdobj$fd
# get number of replications and basis information for YFDPAR
N <- dim(yfdobj$coefs)[2]
ybasisobj <- yfdobj$basis
rangeval <- ybasisobj$rangeval
ynbasis <- ybasisobj$nbasis
ninteg <- max(501, 10 * ynbasis + 1)
tinteg <- seq(rangeval[1], rangeval[2], len = ninteg)
deltat <- tinteg[2] - tinteg[1]
ybasismat <- eval.basis(tinteg, ybasisobj, 0, returnMatrix)
# compute BASISPRODMAT
basisprodmat <- matrix(0, ncoef, ynbasis * N)
mj2 <- 0
for (j in 1:p) {
betafdParj <- betalist[[j]]
betabasisj <- betafdParj$fd$basis
ncoefj <- betabasisj$nbasis
bbasismatj <- eval.basis(tinteg, betabasisj, 0, returnMatrix)
xfdj <- xfdlist[[j]]
tempj <- eval.fd(tinteg, xfdj, 0, returnMatrix)
mj1 <- mj2 + 1
mj2 <- mj2 + ncoefj
indexj <- mj1:mj2
mk2 <- 0
for (k in 1:ynbasis) {
mk1 <- mk2 + 1
mk2 <- mk2 + N
indexk <- mk1:mk2
tempk <- bbasismatj * ybasismat[, k]
basisprodmat[indexj, indexk] <- deltat * crossprod(tempk,
tempj)
}
}
# check dimensions of Y2CMAP
y2cdim <- dim(y2cMap)
if (y2cdim[1] != ynbasis || y2cdim[2] != dim(SigmaE)[1])
stop("Dimensions of Y2CMAP not correct.")
# compute variances of regression coefficient function values
Varc <- y2cMap %*% SigmaE %*% t(y2cMap)
CVar <- kronecker(Varc, diag(rep(1, N)))
C2BMap <- Cmatinv %*% basisprodmat
Bvar <- C2BMap %*% CVar %*% t(C2BMap)
nplot <- max(51, 10 * ynbasis + 1)
tplot <- seq(rangeval[1], rangeval[2], len = nplot)
betastderrlist <- vector("list", p)
PsiMatlist <- vector("list", p)
mj2 <- 0
for (j in 1:p) {
betafdParj <- betalist[[j]]
betabasisj <- betafdParj$fd$basis
ncoefj <- betabasisj$nbasis
mj1 <- mj2 + 1
mj2 <- mj2 + ncoefj
indexj <- mj1:mj2
bbasismat <- eval.basis(tplot, betabasisj, 0, returnMatrix)
PsiMatlist <- bbasismat
bvarj <- Bvar[indexj, indexj]
bstderrj <- sqrt(diag(bbasismat %*% bvarj %*% t(bbasismat)))
bstderrfdj <- smooth.basis(tplot, bstderrj, betabasisj)$fd
betastderrlist[[j]] <- bstderrfdj
}
# compute estimated variance-covariance matrix over plotting grid
# of fitted values
YhatStderr <- matrix(0, nplot, N)
B2YhatList <- vector("list", p)
for (iplot in 1:nplot) {
YhatVari <- matrix(0, N, N)
tval <- tplot[iplot]
for (j in 1:p) {
Zmat <- eval.fd(tval, xfdlist[[j]])
betabasisj <- betalist[[j]]$fd$basis
PsiMatj <- eval.basis(tval, betabasisj)
B2YhatMapij <- t(Zmat) %*% PsiMatj
B2YhatList[[j]] <- B2YhatMapij
}
m2j <- 0
for (j in 1:p) {
m1j <- m2j + 1
m2j <- m2j + betalist[[j]]$fd$basis$nbasis
B2YhatMapij <- B2YhatList[[j]]
m2k <- 0
for (k in 1:p) {
m1k <- m2k + 1
m2k <- m2k + betalist[[k]]$fd$basis$nbasis
B2YhatMapik <- B2YhatList[[k]]
YhatVari <- YhatVari +
B2YhatMapij %*% Bvar[m1j:m2j,m1k:m2k] %*% t(B2YhatMapik)
}
}
YhatStderr[iplot, ] <- matrix(sqrt(diag(YhatVari)), 1, N)
}
}
else {
# ----------------------------------------------------------------
# yfdobj is scalar or multivariate
# ----------------------------------------------------------------
ymat <- as.matrix(yfdobj)
N <- dim(ymat)[1]
Zmat <- NULL
for (j in 1:p) {
xfdj <- xfdlist[[j]]
if (inherits(xfdj, "fd")) {
xcoef <- xfdj$coefs
xbasis <- xfdj$basis
betafdParj <- betalist[[j]]
bbasis <- betafdParj$fd$basis
Jpsithetaj <- inprod(xbasis, bbasis)
Zmat <- cbind(Zmat, t(xcoef) %*% Jpsithetaj)
}
else if (inherits(xfdj, "numeric")) {
Zmatj <- xfdj
Zmat <- cbind(Zmat, Zmatj)
}
}
# compute linear mapping c2bMap takinging coefficients for
# response into coefficients for regression functions
c2bMap <- Cmatinv %*% t(Zmat)
y2bmap <- c2bMap
Bvar <- y2bmap %*% as.matrix(SigmaE) %*% t(y2bmap)
betastderrlist <- vector("list", p)
mj2 <- 0
for (j in 1:p) {
betafdParj <- betalist[[j]]
betabasisj <- betafdParj$fd$basis
ncoefj <- betabasisj$nbasis
mj1 <- mj2 + 1
mj2 <- mj2 + ncoefj
indexj <- mj1:mj2
bvarj <- Bvar[indexj, indexj]
xfdj <- xfdlist[[j]]
if (inherits(xfdj, "fd")) {
betarng <- betabasisj$rangeval
ninteg <- max(c(501, 10 * ncoefj + 1))
tinteg <- seq(betarng[1], betarng[2], len = ninteg)
bbasismat <- eval.basis(tinteg, betabasisj, 0,
returnMatrix)
bstderrj <- sqrt(diag(bbasismat %*% bvarj %*%
t(bbasismat)))
bstderrfdj <- smooth.basis(tinteg, bstderrj,
betabasisj)$fd
}
else {
bsterrj <- sqrt(diag(bvarj))
onebasis <- create.constant.basis(betabasisj$rangeval)
bstderrfdj <- fd(t(bstderrj), onebasis)
}
betastderrlist[[j]] <- bstderrfdj
}
# compute estimated variance-covariance matrix for fitted values
B2YhatList <- vector("list", p)
YhatVari <- matrix(0, N, N)
for (j in 1:p) {
betabasisj <- betalist[[j]]$fd$basis
Xfdj <- xfdlist[[j]]
B2YhatMapij <- inprod(Xfdj, betabasisj)
B2YhatList[[j]] <- B2YhatMapij
}
m2j <- 0
for (j in 1:p) {
m1j <- m2j + 1
m2j <- m2j + betalist[[j]]$fd$basis$nbasis
B2YhatMapij <- B2YhatList[[j]]
m2k <- 0
for (k in 1:p) {
m1k <- m2k + 1
m2k <- m2k + betalist[[k]]$fd$basis$nbasis
B2YhatMapik <- B2YhatList[[k]]
YhatVari <- YhatVari + B2YhatMapij %*% Bvar[m1j:m2j,
m1k:m2k] %*% t(B2YhatMapik)
}
}
YhatStderr <- matrix(sqrt(diag(YhatVari)), N, 1)
}
# return output object of class fRegress
fRegressList <- list(yfdobj = y$yfdobj, xfdlist = y$xfdlist,
betalist = y$betalist, betaestlist = y$betaestlist,
yhatfdobj = y$yhatfdobj, Cmat = y$Cmat, Dmat = y$Dmat,
Cmatinv = y$Cmatinv, wt = y$wt,
df = y$df, y2cMap = y2cMap, SigmaE = SigmaE,
betastderrlist = betastderrlist, YhatStderr = YhatStderr,
Bvar = Bvar, c2bMap = c2bMap)
class(fRegressList) = "fRegress"
return(fRegressList)
}