Skip to content

Commit 08ff834

Browse files
committed
update by Jim
1 parent 5478b3c commit 08ff834

File tree

8 files changed

+164
-138
lines changed

8 files changed

+164
-138
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author: J. O. Ramsay <ramsay@psych.mcgill.ca> [aut,cre],
66
Spencer Graves <spencer.graves@effectivedefense.org> [ctb],
77
Giles Hooker <gjh27@cornell.edu> [ctb]
88
Maintainer: J. O. Ramsay <ramsay@psych.mcgill.ca>
9-
Depends: R (>= 3.5), Matrix, fds
9+
Depends: R (>= 3.5), splines, Matrix, fds
1010
Suggests: deSolve, lattice
1111
Description: These functions were developed to support functional data
1212
analysis as described in Ramsay, J. O. and Silverman, B. W.

NAMESPACE

-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export(AmpPhaseDecomp,
4040
deriv.fd,
4141
df2lambda,
4242
dirs,
43-
eigchk,
4443
Eigen,
4544
eigen.pda,
4645
eval.basis,
@@ -62,12 +61,9 @@ export(AmpPhaseDecomp,
6261
Fperm.fd,
6362
fRegress,
6463
fRegress.fd,
65-
fRegress.fdPar,
6664
fRegress.CV,
6765
fRegress.stderr,
68-
fRegressArgCheck,
6966
fRegress.formula,
70-
fRegress.character,
7167
Fstat.fd,
7268
geigen,
7369
getbasismatrix,
@@ -215,7 +211,6 @@ S3method(predict, monfd)
215211
S3method(as.fd, 'function')
216212
S3method(as.fd, smooth.spline)
217213
S3method(fRegress, fd)
218-
S3method(fRegress, fdPar)
219214
S3method(fRegress, numeric)
220215
S3method(fRegress, double)
221216
S3method(fRegress, formula)

R/bsplineS.R

+61-61
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
bsplineS <- function (x, breaks, norder=4, nderiv=0, returnMatrix=FALSE)
2-
{
3-
# This is a wrapper function for the S-PLUS spline.des function.
4-
# The number of spline functions is equal to the number of
5-
# discrete break points, length(BREAKVALUES), plus the order, NORDER,
6-
# minus 2.
7-
# Arguments are as follows:
8-
# X ... array of values at which the spline functions are to
9-
# evaluated
10-
# BREAKS ... a STRICTLY INCREASING sequence of break points or knot
11-
# values. It must contain all the values of X within its
12-
# range.
13-
# NORDER ... order of spline (1 more than degree), so that 1 gives a
14-
# step function, 2 gives triangle functions,
15-
# and 4 gives cubic splines
16-
# NDERIV ... highest order derivative. 0 means only function values
17-
# are returned.
18-
# Return is a matrix with length(X) rows and number of columns equal to
19-
# number of b-splines
20-
21-
# last modified 6 May 2012 by Spencer Graves
22-
# previously modified 2 April 2012 by Jim Ramsay
23-
24-
x <- as.vector(x)
25-
n <- length(x)
26-
tol <- 1e-14
27-
nbreaks <- length(breaks)
28-
if (nbreaks < 2) stop('Number of knots less than 2.')
29-
if (min(diff(breaks)) < 0 ) stop('Knots are not increasing')
30-
31-
if ( max(x) > max(breaks) + tol ||
32-
min(x) < min(breaks) - tol )
33-
stop('Knots do not span the values of X')
34-
if ( x[n] > breaks[nbreaks]) breaks[nbreaks] <- x[n]
35-
if ( x[1] < breaks[1] ) breaks[1] <- x[1]
36-
37-
if (norder > 20) stop('NORDER exceeds 20.')
38-
if (norder < 1) stop('NORDER less than 1.')
39-
if (nderiv > 19) stop('NDERIV exceeds 19.')
40-
if (nderiv < 0) stop('NDERIV is negative.')
41-
if (nderiv >= norder) stop (
42-
'NDERIV cannot be as large as order of B-spline.')
43-
44-
knots <- c(rep(breaks[1 ],norder-1), breaks,
45-
rep(breaks[nbreaks],norder-1) )
46-
derivs <- rep(nderiv,n)
47-
nbasis <- nbreaks + norder - 2
48-
if (nbasis >= norder) {
49-
if (nbasis > 1) {
50-
basismat <- Matrix(spline.des(knots, x, norder, derivs)$design)
51-
} else {
52-
basismat <- as.matrix(spline.des(knots, x, norder, derivs)$design)
53-
}
54-
if((!returnMatrix) && (length(dim(basismat)) == 2)){
55-
return(as.matrix(basismat))
56-
}
57-
return(basismat)
58-
} else {
59-
stop("NBASIS is less than NORDER.")
60-
}
61-
}
1+
bsplineS <- function (x, breaks, norder=4, nderiv=0, returnMatrix=FALSE)
2+
{
3+
# This is a wrapper function for the S-PLUS spline.des function.
4+
# The number of spline functions is equal to the number of
5+
# discrete break points, length(BREAKVALUES), plus the order, NORDER,
6+
# minus 2.
7+
# Arguments are as follows:
8+
# X ... array of values at which the spline functions are to
9+
# evaluated
10+
# BREAKS ... a STRICTLY INCREASING sequence of break points or knot
11+
# values. It must contain all the values of X within its
12+
# range.
13+
# NORDER ... order of spline (1 more than degree), so that 1 gives a
14+
# step function, 2 gives triangle functions,
15+
# and 4 gives cubic splines
16+
# NDERIV ... highest order derivative. 0 means only function values
17+
# are returned.
18+
# Return is a matrix with length(X) rows and number of columns equal to
19+
# number of b-splines
20+
21+
# last modified 6 May 2012 by Spencer Graves
22+
# previously modified 2 April 2012 by Jim Ramsay
23+
24+
x <- as.vector(x)
25+
n <- length(x)
26+
tol <- 1e-14
27+
nbreaks <- length(breaks)
28+
if (nbreaks < 2) stop('Number of knots less than 2.')
29+
if (min(diff(breaks)) < 0 ) stop('Knots are not increasing')
30+
31+
if ( max(x) > max(breaks) + tol ||
32+
min(x) < min(breaks) - tol )
33+
stop('Knots do not span the values of X')
34+
if ( x[n] > breaks[nbreaks]) breaks[nbreaks] <- x[n]
35+
if ( x[1] < breaks[1] ) breaks[1] <- x[1]
36+
37+
if (norder > 20) stop('NORDER exceeds 20.')
38+
if (norder < 1) stop('NORDER less than 1.')
39+
if (nderiv > 19) stop('NDERIV exceeds 19.')
40+
if (nderiv < 0) stop('NDERIV is negative.')
41+
if (nderiv >= norder) stop (
42+
'NDERIV cannot be as large as order of B-spline.')
43+
44+
knots <- c(rep(breaks[1 ],norder-1), breaks,
45+
rep(breaks[nbreaks],norder-1) )
46+
derivs <- rep(nderiv,n)
47+
nbasis <- nbreaks + norder - 2
48+
if (nbasis >= norder) {
49+
if (nbasis > 1) {
50+
basismat <- Matrix(splines::spline.des(knots, x, norder, derivs)$design)
51+
} else {
52+
basismat <- as.matrix(splines::spline.des(knots, x, norder, derivs)$design)
53+
}
54+
if((!returnMatrix) && (length(dim(basismat)) == 2)){
55+
return(as.matrix(basismat))
56+
}
57+
return(basismat)
58+
} else {
59+
stop("NBASIS is less than NORDER.")
60+
}
61+
}

R/fRegress.R

+53-13
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ fRegress <- function(y, ...) {
33
UseMethod("fRegress")
44
}
55

6-
fRegress.fdPar <- function(y, xfdlist, betalist, wt=NULL,
7-
y2cMap=NULL, SigmaE=NULL, returnMatrix=FALSE, ...){
8-
stop("inside fRegress.fdPar")
9-
y = y$fd
6+
#fRegress.fdPar <- function(y, xfdlist, betalist, wt=NULL,
7+
# y2cMap=NULL, SigmaE=NULL, returnMatrix=FALSE, ...){
8+
# print("inside fRegress.fdPar")
9+
# y = y$fd
1010

11-
fRegress.fd(y, xfdlist, betalist, wt=wt,
12-
y2cMap=y2cMap, SigmaE=SigmaE, returnMatrix=FALSE, ...)
13-
}
11+
# fRegress.fd(y, xfdlist, betalist, wt=wt,
12+
# y2cMap=y2cMap, SigmaE=SigmaE, returnMatrix=FALSE, ...)
13+
#}
1414

1515
fRegress.fd <- function(y, xfdlist, betalist, wt=NULL,
1616
y2cMap=NULL, SigmaE=NULL, returnMatrix=FALSE,
@@ -27,9 +27,7 @@ fRegress.fd <- function(y, xfdlist, betalist, wt=NULL,
2727
# Arguments:
2828
# Y ... an object for the dependent variable,
2929
# which may be:
30-
# a functional data object,
31-
# a functional parameter (fdPar) object, or
32-
# a vector
30+
# a functional data object or a numerical vector
3331
# XFDLIST ... a list object of length p with each list
3432
# containing an object for an independent variable.
3533
# the object may be:
@@ -57,7 +55,7 @@ fRegress.fd <- function(y, xfdlist, betalist, wt=NULL,
5755
# enabling this option.
5856
#
5957
# Returns FREGRESSLIST ... A list containing seven members with names:
60-
# yfdPar ... first argument of FREGRESS
58+
# yfdobj ... first argument of FREGRESS
6159
# xfdlist ... second argument of FREGRESS
6260
# betalist ... third argument of FREGRESS
6361
# betaestlist ... estimated regression functions
@@ -75,8 +73,11 @@ fRegress.fd <- function(y, xfdlist, betalist, wt=NULL,
7573
# as predict(fRegressList). In this call fRegressList can be any object of the
7674
# "fRegress".
7775

78-
# Last modified 7 September 2020 by Jim Ramsay
76+
# Last modified 28 October 2020 by Jim Ramsay
77+
78+
if (is.fdPar(y)) y <- y$fd
7979

80+
print(class(y))
8081
print("inside fRegress.fd")
8182

8283
print("calling fRegressArgCheck")
@@ -113,7 +114,7 @@ fRegress.fd <- function(y, xfdlist, betalist, wt=NULL,
113114
onesbasis <- create.constant.basis(rangeval)
114115
onesfd <- fd(1,onesbasis)
115116

116-
if (length(ycoefdim) > 2) stop("YFDOBJ from YFDPAR is not univariate.")
117+
if (length(ycoefdim) > 2) stop("YFDOBJ from YFD is not univariate.")
117118

118119
# -------- set up the linear equations for the solution -----------
119120

@@ -377,3 +378,42 @@ fRegress.fd <- function(y, xfdlist, betalist, wt=NULL,
377378
return(fRegressList)
378379

379380
}
381+
382+
# -------------------------------------------------------------------------------------
383+
384+
eigchk <- function(Cmat) {
385+
386+
# Last modified 25 August 2020 by Jim Ramsay
387+
388+
# Cmat for NA's
389+
390+
if (any(is.na(Cmat))) stop("Cmat has NA values.")
391+
392+
# check Cmat for Cmatmetry
393+
394+
if (max(abs(Cmat-t(Cmat)))/max(abs(Cmat)) > 1e-10) {
395+
stop('CMAT is not symmetric.')
396+
} else {
397+
Cmat <- (Cmat + t(Cmat))/2
398+
}
399+
400+
# check Cmat for singularity
401+
402+
eigval <- eigen(Cmat)$values
403+
ncoef <- length(eigval)
404+
if (eigval[ncoef] < 0) {
405+
neig <- min(length(eigval),10)
406+
cat("\nSmallest eigenvalues:\n")
407+
print(eigval[(ncoef-neig+1):ncoef])
408+
cat("\nLargest eigenvalues:\n")
409+
print(eigval[1:neig])
410+
stop("Negative eigenvalue of coefficient matrix.")
411+
}
412+
if (eigval[ncoef] == 0) stop("Zero eigenvalue of coefficient matrix.")
413+
logcondition <- log10(eigval[1]) - log10(eigval[ncoef])
414+
if (logcondition > 12) {
415+
warning("Near singularity in coefficient matrix.")
416+
cat(paste("\nLog10 Eigenvalues range from\n",
417+
log10(eigval[ncoef])," to ",log10(eigval[1]),"\n"))
418+
}
419+
}

0 commit comments

Comments
 (0)