-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfRegressArgCheck.R
160 lines (115 loc) · 4.53 KB
/
fRegressArgCheck.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
fRegressArgCheck <- function(yfd, xfdlist, betalist, wt=NULL) {
# FREGRESS_ARGCHECK checks the first four arguments for the functions
# for function regression, including FREGRESS.
# Last modified 16 December 2020 by Jim Ramsay
# -------------------- Check classes of arguments --------------------
# check that YFD is of class either 'fd' or 'numeric' and compute sample size N
if (!(is.fdPar(yfd) || is.fd(yfd) || is.numeric(yfd) || is.matrix(yfd))) stop(
"First argument is not of class 'fdPar', 'fd', 'numeric' or 'matrix'.")
# As of 2020, if yfd 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 (is.fdPar(yfd)) yfd <- yfd$fd
if (inherits(yfd, "fd")) {
ycoef <- yfd$coefs
N <- dim(ycoef)[2]
} else {
N <- length(yfd)
}
# check that xfdlist is a list object and compute number of covariates p
# check XFDLIST
if (inherits(xfdlist, "fd") || inherits(xfdlist, "numeric"))
xfdlist <- list(xfdlist)
if (!inherits(xfdlist, "list")) stop(
"Argument XFDLIST is not a list object.")
# get number of independent variables p
p <- length(xfdlist)
# check BETALIST
if (inherits(betalist, "fd")) betalist <- list(betalist)
if (!inherits(betalist, "list")) stop(
"Argument BETALIST is not a list object.")
if (length(betalist) != p) {
cat(paste("\nNumber of regression coefficients does not match\n",
"number of independent variables."))
stop("")
}
# extract the range if YFD is functional
if (inherits(yfd, "fd")) {
rangeval <- yfd$basis$rangeval
} else {
rangeval = c(0,1)
# allscalar <- TRUE
# for (j in 1:p) {
# if (inherits(xfdlist[[j]], "fd")) {
# rangeval <- xfdlist[[j]]$basis$rangeval
# allscalar <- FALSE
# break
# }
# }
# if (allscalar) stop(
# paste("The dependent variable and all the independent",
# "variables are scalar."))
}
# -------------------- check contents of BETALIST -------------------
berror <- FALSE
for (j in 1:p) {
betafdParj <- betalist[[j]]
if (inherits(betafdParj, "fd") || inherits(betafdParj, "basisfd")) {
betafdParj <- fdPar(betafdParj)
betalist[[j]] <- betafdParj
}
if (!inherits(betafdParj, "fdPar")) {
print(paste("BETALIST[[",j,"]] is not a FDPAR object."))
berror <- TRUE
}
}
# -------------------- check contents of XFDLIST -------------------
# If the object is a vector of length N,
# it is converted to a functional data object with a
# constant basis
xerror <- FALSE
for (j in 1:p) {
xfdj <- xfdlist[[j]]
if (inherits(xfdj, "fd")) {
xcoef <- xfdj$coefs
if (length(dim(xcoef)) > 2) stop(
paste("Covariate",j,"is not univariate."))
# check size of coefficient array
Nj <- dim(xcoef)[2]
if (Nj != N) {
print(
paste("Incorrect number of replications in XFDLIST",
"for covariate",j))
xerror = TRUE
}
}
if (inherits(xfdj, "numeric")) {
if (!is.matrix(xfdj)) xfdj = as.matrix(xfdj)
Zdimj <- dim(xfdj)
if (Zdimj[1] != N && Zdimj != 1) {
print(paste("Vector in XFDLIST[[",j,"]] has wrong length."))
xerror = TRUE
}
if (Zdimj[2] != 1) {
print(paste("Matrix in XFDLIST[[",j,"]] has more than one column."))
xerror = TRUE
}
xfdlist[[j]] <- fd(matrix(xfdj,1,N), create.constant.basis(betalist[[j]]$fd$basis$rangeval))
}
if (!(inherits(xfdlist[[j]], "fd" ) ||
inherits(xfdlist[[j]], "numeric") ||
inherits(xfdlist[[j]], "matrix" ))) {
print(paste("XFDLIST[[",j,"]] is not an FD or numeric or matrix object."))
xerror = TRUE
}
}
if (xerror || berror) stop(
"An error has been found in either XFDLIST or BETALIST.")
# -------------------- check contents of WEIGHTS -------------------
if (is.null(wt)) wt = rep(1,N)
if (length(wt) != N) stop("Number of weights not equal to N.")
if (any(wt < 0)) stop("Negative weights found.")
# --------------------- return the argument list --------------------
# The older versions of fda package used yfdPar as the name for the first member.
return(list(yfd=yfd, xfdlist=xfdlist, betalist=betalist, wt=wt))
}