/
smooth.bibasis2.R
258 lines (212 loc) · 8.28 KB
/
smooth.bibasis2.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
# this function differs with the original one in the penalties:
# we consider
# lambdat*kronecker(penmatt,sinprod) instead of
# lambdas*kronecker(Imat,penmats) for s direction
# and, we consider
# lambdat*kronecker(penmatt,sinprod) instead of
# lambdat*kronecker(penmatt,Imat) for t direction
# the modification is according to marginal penalization: Wood 2006 (see notes)
smooth.bibasis2 <- function (sarg, targ, y, fdPars, fdPart, fdnames=NULL){
# SMOOTH_BIBASIS Smooths discrete surface values over a rectangular
# lattice to estimate a smoothing function f(s,t)
# using penalized basis functions.
#
# Arguments for this function:
#
# sarg ... A set of argument values for the row dimension s.
# targ ... A set of argument values for the col dimension t.
# Y ... an array containing surface values. If two-dimensional,
# a single surface is assumed. If three-dimensional,
# the third dimension corresponds to replications.
# FDPARS ... A functional parameter or fdPar object for
# variation along the row dimension s.
# FDPART ... A functional parameter or fdPar object for
# variation along the col dimension t.
# FDNAMES ... A cell of length 3 with names for
# 1. argument s
# 2. argument t
# 3. the function f(s,t).
# Returns a list containing:
# FDOBJ ... an object of class fd containing coefficients.
# DF ... a degrees of freedom measure.
# GCV ... a measure of lack of fit discounted for df.
# If the function is univariate, GCV is a vector
# containing the error sum of squares for each
# function, and if the function is multivariate,
# GCV is a NVAR by NCURVES matrix.
# COEF ... the coefficient matrix for the basis function
# expansion of the smoothing function
# SSE ... the error sums of squares.
# SSE is a vector or matrix of the same size as
# GCV.
# PENMAT... the penalty matrix.
# Y2CMAP... the matrix mapping the data to the coefficients.
# last modified 11 December 2008 by Jim Ramsay
# ---------------------------------------------------------------------
# Check argments
# ---------------------------------------------------------------------
# check argument values
#sarg = argcheck(sarg)
#targ = argcheck(targ)
ns = length(sarg)
nt = length(targ)
# check Y
if(!inherits(y, "matrix") && !inherits(y, "array"))
stop("'y' is not of class matrix or class array.")
ydim = dim(y)
if (ydim[1] != ns) stop(
"Number of rows of Y is not the same length as SARG.")
if (ydim[2] != nt) stop(
"Number of columns of Y is not the same length as TARG.")
if (length(ydim) == 2) {
nsurf = 1
ymat = matrix(y, ns*nt, 1)
} else {
nsurf = ydim(3)
ymat = matrix(0, ns*nt, nsurf)
for (isurf in 1:nsurf)
ymat[,isurf] = matrix(y[,,isurf], ns*nt, 1)
}
# check FDPARS, FDPART and BASES, LBFDOBJ"S and LAMBDA"S
#fdPars = fdParcheck(fdPars)
fdobjs = fdPars$fd
sbasis = fdobjs$basis
snbasis = sbasis$nbasis - length(sbasis$dropind)
lambdas = fdPars$lambda
Lfds = fdPars$Lfd
#fdPart = fdParcheck(fdPart)
fdobjt = fdPart$fd
tbasis = fdobjt$basis
tnbasis = tbasis$nbasis - length(tbasis$dropind)
lambdat = fdPart$lambda
Lfdt = fdPart$Lfd
# check LAMBDA
if (lambdas < 0) {
warning ("Value of lambdas was negative, 0 used instead.")
lambdas = 0
}
if (lambdat < 0) {
warning ("Value of lambdat was negative, 0 used instead.")
lambdat = 0
}
# set default argument values
if (is.null(fdnames)) {
fdnames = vector("list", 3)
fdnames[[1]] = "argument s"
fdnames[[2]] = "argument t"
fdnames[[3]] = "function"
}
# ----------------------------------------------------------------
# set up the linear equations for smoothing
# ----------------------------------------------------------------
sbasismat = eval.basis(sarg, sbasis)
tbasismat = eval.basis(targ, tbasis)
basismat = kronecker(tbasismat,sbasismat)
#######
#we add inprod of basis
sinprod <- inprod(sbasis, sbasis)
tinprod <- inprod(tbasis, tbasis)
# meke it symmetric
sinprod <- (sinprod + t(sinprod))/2
tinprod <- (tinprod + t(tinprod))/2
#######
if (ns*nt > snbasis*tnbasis || lambdas > 0 || lambdat > 0) {
# The following code is for the coefficients completely determined
Bmat = crossprod(basismat,basismat)
Bmat0 = Bmat
# set up right side of equations
Dmat = crossprod(basismat,ymat)
# set up regularized cross-product matrix BMAT
if (lambdas > 0){
penmats = eval.penalty(sbasis, Lfds)
Bnorm = sqrt(sum(c(Bmat0)^2))
pennorm = sqrt(sum(c(penmats)^2))
condno = pennorm/Bnorm
if (lambdas*condno > 1e12) {
lambdas = 1e12/condno
warning(paste("lambdas reduced to",lambdas,
"to prevent overflow"))
}
#Imat = diag(rep(tnbasis,1))
#Bmat = Bmat0 + lambdas*kronecker(Imat,penmats)
Bmat <- Bmat + lambdas*kronecker(tinprod,penmats)
}
if (lambdat > 0) {
penmatt = eval.penalty(tbasis, Lfdt)
Bnorm = sqrt(sum(c(Bmat0)^2))
pennorm = sqrt(sum(c(penmatt)^2))
condno = pennorm/Bnorm
if (lambdat*condno > 1e12) {
lambdat = 1e12/condno
warning(paste("lambdat reduced to",lambdat,
"to prevent overflow"))
}
#Imat = diag(rep(snbasis,1))
#Bmat = Bmat0 + lambdat*kronecker(penmatt,Imat)
Bmat <- Bmat + lambdat*kronecker(penmatt,sinprod)
}
# compute inverse of Bmat
Bmat = (Bmat+t(Bmat))/2
Lmat = chol(Bmat)
# Lmat = try(chol(Bmat), silent=TRUE) {
# if (class(Lmat)=="try-error") {
# Beig = eigen(Bmat, symmetric=TRUE)
# BgoodEig = (Beig$values>0)
# Brank = sum(BgoodEig)
# if (Brank < dim(Bmat)[1])
# warning("Matrix of basis function values has rank ",
# Brank, " < dim(fdobj$basis)[2] = ",
# length(BgoodEig), " ignoring null space")
# goodVec = Beig$vectors[, BgoodEig]
# Bmatinv = (goodVec %*% (Beig$values[BgoodEig] * t(goodVec)))
# } else {
Lmatinv = solve(Lmat)
Bmatinv = Lmatinv %*% t(Lmatinv)
# }
# }
# ----------------------------------------------------------------
# Compute the coefficients defining the smooth and
# summary properties of the smooth
# ----------------------------------------------------------------
# compute map from y to c
y2cMap = Bmatinv %*% t(basismat)
# compute degrees of freedom of smooth
BiB0 = Bmatinv %*% Bmat0
df = sum(diag(BiB0))
# solve normal equations for each observation
coef = solve(Bmat, Dmat)
if (nsurf == 1) {
coefmat = matrix(coef, snbasis, tnbasis)
} else {
coefmat = array(0, c(snbasis, tnbasis, nsurf))
for (isurf in 1:nsurf)
coefmat[,,isurf] = matrix(coef[,isurf], snbasis, tnbasis)
}
} else {
stop(paste("The number of basis functions exceeds the number of ",
"points to be smoothed."))
}
# ----------------------------------------------------------------
# compute SSE, yhat, GCV and other fit summaries
# ----------------------------------------------------------------
# compute error sum of squares
yhat = basismat %*% coef
SSE = sum((ymat - yhat)^2)
# compute GCV index
N = ns*nt*nsurf
if (df < N) {
gcv = (SSE/N)/((N - df)/N)^2
} else {
gcv = NA
}
# ------------------------------------------------------------------
# Set up the functional data objects for the smooths
# ------------------------------------------------------------------
bifdobj = bifd(coefmat, sbasis, tbasis, fdnames)
smoothlist = list(bifdobj=bifdobj, df=df, gcv=gcv,
SSE=SSE, y2cMap=y2cMap, sarg=sarg,
targ=targ, y=y, coef = coefmat)
# class(smoothlist) = "bifdSmooth"
return(smoothlist)
}
#que no regerse penmats