@@ -2,38 +2,26 @@ Data2fd <- function(argvals=NULL, y=NULL, basisobj=NULL, nderiv=NULL,
2
2
lambda = 3e-8 / diff(as.numeric(range(argvals ))),
3
3
fdnames = NULL , covariates = NULL , method = " chol" ) {
4
4
5
- # Last modified 5 February 2024
5
+ # Last modified 1 March 2024
6
6
7
- # Changes proposed by Spencer Graves 2010.12.08 ...
8
- # if(is.null(lambda))
9
- # lambda <- 1e-9*sd(argChk$y)/diff(range(argChk$argvals))
10
- #
11
- # Error in smooth.basis ... argvals is not numeric
12
- # in R CMD check, cannot replicate line by line.
13
-
14
- # . Tests six situations requiring modification or termination.
15
-
16
- argChk <- argvalsySwap(argvals , y , basisobj )
17
-
18
- # another rest with messages
7
+ # . Tests five situations requiring modification or termination.
19
8
20
9
argChk <- argvalsySwap(argvals , y , basisobj )
21
10
22
11
if (! is.numeric(AV <- argChk $ argvals )){
23
- print(AV )
24
12
if (is.null(AV ))
25
13
stop(' is.null(argChk$argvals); should be numeric' )
26
14
cat(' argChk$argvals is not numeric.\n ' )
27
15
cat(' class(argChk$argvals) = ' , class(AV ), ' \n ' )
28
- print(AV )
29
16
}
30
17
31
18
# . Success and smoothing ... S3 object of class fdSmooth is returned
32
19
33
- fdSmoothobj <- smooth.basisPar(argChk $ argvals , argChk $ y ,
20
+ smBasis <- smooth.basisPar(argChk $ argvals , argChk $ y ,
34
21
fdobj = basisobj , Lfdobj = nderiv , lambda = lambda ,
35
22
fdnames = fdnames ,
36
23
covariates = covariates , method = " chol" )
24
+ return (smBasis $ fd )
37
25
38
26
}
39
27
@@ -48,19 +36,19 @@ argvalsySwap = function(argvals=NULL, y=NULL, basisobj=NULL) {
48
36
if (inherits(basisobj , ' basisfd' )) rangeval <- basisobj $ rangeval
49
37
50
38
# #. --------------------------------------------------------------------------
51
- # # 1. if(is.null(y)) use argvals for y
39
+ # # Section 1. if(is.null(y)) use argvals for y
52
40
# #. --------------------------------------------------------------------------
53
41
54
42
if (is.null(y )){
55
43
if (is.null(argvals )) stop(" 'y' is missing with no default" )
56
44
# Store argvals as y and alert
57
45
cat(" 'y' is missing, using 'argvals'\n " )
58
- y <- argvals
46
+ y <- argvals
59
47
argvals <- NULL
60
48
}
61
49
62
50
# #. --------------------------------------------------------------------------
63
- # # 2. test for missing argvals, if so construct a sequence
51
+ # # Section 2. test for missing argvals, if so construct a sequence
64
52
# #. --------------------------------------------------------------------------
65
53
66
54
dimy <- dim(as.array(y ))
@@ -92,63 +80,60 @@ argvalsySwap = function(argvals=NULL, y=NULL, basisobj=NULL) {
92
80
if (is.null(a01 ))
93
81
stop(' basisobj does not have a required rangeval component.' )
94
82
n <- dimy [1 ]
95
- # construct the argval sequence
96
- argvals <- seq(a01 [1 ], a01 [2 ], length = n )
97
- # warning message about the swap
98
83
cat(paste(" 'argvals' is missing; using seq(" , a01 [1 ],
99
84
" , " , a01 [2 ], " , length=" , n , " )\n " ))
100
- # . return
85
+ # construct the argval sequence
86
+ argvals <- seq(a01 [1 ], a01 [2 ], length = n )
87
+
101
88
return (list (argvals = argvals , y = y , basisobj = basisobj ))
89
+
102
90
}
103
91
104
92
# #. --------------------------------------------------------------------------
105
93
# # 3. swapping y and argvals
106
94
# #. --------------------------------------------------------------------------
107
95
108
- dima <- dim(as.array(argvals ))
109
- {
110
- if (length(dimy ) == length(dima )){
96
+ dima <- dim(as.array(argvals ))
97
+ { # First line in code block
98
+ if (length(dimy ) == length(dima )) {
111
99
if (any(dimy != dima ))
112
- # . terminal message
113
100
stop(" dimensions of 'argvals' and 'y' must be compatible;\n " ,
114
101
" dim(argvals) = " , paste(dima , collapse = ' x ' ),
115
102
" ; dim(y) = " , paste(dimy , collapse = ' x ' ) )
116
103
# Check basisobj
117
- {
104
+ { # First line in code block
118
105
if (inherits(basisobj , ' fd' )) basisobj <- basisobj $ basis
119
106
else {
120
107
if (inherits(basisobj , ' fdPar' ))
121
108
basisobj <- basisobj $ fd $ basis
122
109
else {
123
- if (inherits(basisobj , ' array' )){
124
- fd. <- fd(basisobj )
110
+ if (inherits(basisobj , ' array' )) {
111
+ fd. <- fd(basisobj )
125
112
basisobj <- fd. $ basis
126
- }
127
- else {
113
+ } else {
128
114
if (inherits(basisobj , ' integer' ))
129
115
basisobj <- create.bspline.basis(argvals , norder = basisobj )
130
116
else {
131
117
if (is.null(basisobj ))
132
118
basisobj <- create.bspline.basis(argvals )
133
119
else
134
120
if (! inherits(basisobj , ' basisfd' ))
135
- # . terminal message
136
121
stop(" 'basisobj' is NOT a functional basis" ,
137
122
" object (class 'basisfd'); class = " ,
138
123
class(basisobj )[1 ])
139
124
}
140
125
}
141
126
}
142
127
}
143
- }
144
- arng <- range(argvals )
128
+ } # Last line in code block
129
+ a01 <- basisobj $ rangeval
130
+ arng <- range(argvals )
145
131
if ((rangeval [1 ]< = arng [1 ]) && (arng [2 ]< = rangeval [2 ])) {
146
132
return (list (argvals = argvals , y = y , basisobj = basisobj ))
147
133
}
148
134
#
149
135
yrng <- range(y )
150
- if ((rangeval [1 ]< = yrng [1 ]) && (yrng [2 ]< = rangeval [2 ])) {
151
- # . alert message
136
+ if ((a01 [1 ]< = yrng [1 ]) && (yrng [2 ]< = a01 [2 ])) {
152
137
cat(paste(" 'argvals' is NOT contained in basisobj$rangeval" ,
153
138
" , but 'y' is; swapping 'argvals' and 'y'.\n " ))
154
139
return (list (argvals = y , y = argvals , basisobj = basisobj ))
@@ -157,7 +142,7 @@ argvalsySwap = function(argvals=NULL, y=NULL, basisobj=NULL) {
157
142
stop(" Neither 'argvals' nor 'y' are contained in " ,
158
143
" basisobj$rangeval" )
159
144
}
160
- }
145
+ } # Last line in code block
161
146
162
147
# #. --------------------------------------------------------------------------
163
148
# # 4. If(length(dimy) < length(dima)) swap argvals and y
@@ -176,9 +161,8 @@ argvalsySwap = function(argvals=NULL, y=NULL, basisobj=NULL) {
176
161
dima <- dimy
177
162
dimy <- d.
178
163
}
179
- # error message if argvals and y are inconsistent
164
+
180
165
if (any(dima != dimy [1 : length(dima )]))
181
- # terminal message
182
166
stop(" A dimension of 'argvals' does not match 'y':\n " ,
183
167
" dim(argvals) = " , paste(dima , collapse = " x " ),
184
168
" ; dim(y) = " , paste(dimy , collapse = " x " ) )
@@ -187,7 +171,7 @@ argvalsySwap = function(argvals=NULL, y=NULL, basisobj=NULL) {
187
171
# # 5. Check compatibility of argvals with basisobj
188
172
# #. --------------------------------------------------------------------------
189
173
190
- {
174
+ { # First line in code block
191
175
if (inherits(basisobj , ' fd' ))basisobj <- basisobj $ basis
192
176
else {
193
177
if (inherits(basisobj , ' fdPar' ))
@@ -205,37 +189,20 @@ argvalsySwap = function(argvals=NULL, y=NULL, basisobj=NULL) {
205
189
basisobj <- create.bspline.basis(argvals )
206
190
else
207
191
if (! inherits(basisobj , ' basisfd' ))
208
- # . error message if basisobj incorrect class
209
192
stop(" 'basisobj' is NOT a functional basis" ,
210
193
" object (class 'basisfd'); class = " ,
211
194
class(basisobj )[1 ])
212
195
}
213
196
}
214
197
}
215
198
}
199
+ } # Last line in code block
200
+ a01 <- basisobj $ rangeval
201
+ arng <- range(argvals )
202
+ if ((a01 [1 ]< = arng [1 ]) && (arng [2 ]< = a01 [2 ])) {
203
+ return (list (argvals = argvals , y = y , basisobj = basisobj ))
216
204
}
217
-
218
- # #. --------------------------------------------------------------------------
219
- # # 6. Check compatibility of argvals with basisobj$rangeval
220
- # #. --------------------------------------------------------------------------
221
-
222
- # set up a safety zone for argvals out of range by a tiny amount
223
- delta <- 1e-7 * (rangeval [2 ]- rangeval [1 ]) # the tiny amount
224
- for (i in 1 : length(argvals )) {
225
- argi <- argvals [i ]
226
- # print(argi)
227
- if (argi < rangeval [1 ] && argi > = rangeval [1 ]- delta ) {
228
- argi <- rangeval [1 ]
229
- }
230
- if (argi > rangeval [2 ] && argi < = rangeval [2 ]+ delta ) {
231
- argi <- rangeval [2 ]
232
- }
233
- }
234
- if (any(argvals < rangeval [1 ]) || any(argvals > rangeval [2 ])) {
235
- # error message
236
- stop(" There are argvals not contained within interval basisobj$rangeval" )
237
- }
238
- return (list (argvals = argvals , y = y , basisobj = basisobj ))
239
-
205
+ #
206
+ stop(" 'argvals' are not contained in basisobj$rangeval" )
240
207
}
241
208
0 commit comments