-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patharithmetic.fd.Rd
185 lines (156 loc) · 4.92 KB
/
arithmetic.fd.Rd
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
\name{arithmetic.fd}
\alias{arithmetic.fd}
\alias{+.fd}
\alias{plus.fd}
\alias{-.fd}
\alias{minus.fd}
\alias{*.fd}
\alias{times.fd}
\title{
Arithmetic on functional data ('fd') objects
}
\description{
Arithmetic on functional data objects
}
\usage{
\method{+}{fd}(e1, e2)
\method{-}{fd}(e1, e2)
\method{*}{fd}(e1, e2)
plus.fd(e1, e2, basisobj=NULL)
minus.fd(e1, e2, basisobj=NULL)
times.fd(e1, e2, basisobj=NULL)
}
\arguments{
\item{e1, e2}{
object of class 'fd' or a numeric vector. Note that 'e1+e2' will
dispatch to plus.fd(e1, e2) only if e1 has class 'fd'. Similarly,
'e1-e2' or 'e1*e2' will dispatch to minus.fd(e1, e2) or time.fd(e1,
e2), respectively, only if e1 is of class 'fd'.
}
\item{basisobj}{
reference basis; defaults to e1[['basis']] * e2[['basis']];
ignored for \code{plus.fd} and \code{minus.fd}.
}
}
\value{
A function data object corresponding to the pointwise sum, difference
or product of e1 and e2.
If both arguments are functional data objects, the bases are the same,
and the coefficient matrices are the same dims, the indicated
operation is applied to the coefficient matrices of the two objects.
In other words, e1+e2 is obtained for this case by adding the
coefficient matrices from e1 and e2.
If e1 or e2 is a numeric scalar, that scalar is applied to the
coefficient matrix of the functional data object.
If either e1 or e2 is a numeric vector, it must be the same length as
the number of replicated functional observations in the other
argument.
When both arguments are functional data objects, they need not have
the same bases. However, if they don't have the same number of
replicates, then one of them must have a single replicate. In the
second case, the singleton function is replicated to match the number
of replicates of the other function. In either case, they must have
the same number of functions. When both arguments are functional data
objects, and the bases are not the same, the basis used for the sum is
constructed to be of higher dimension than the basis for either factor
according to rules described in function TIMES for two basis objects.
}
\references{
Ramsay, James O., Hooker, Giles, and Graves, Spencer (2009),
\emph{Functional data analysis with R and Matlab}, Springer, New
York.
Ramsay, James O., and Silverman, Bernard W. (2005), \emph{Functional
Data Analysis, 2nd ed.}, Springer, New York.
Ramsay, James O., and Silverman, Bernard W. (2002), \emph{Applied
Functional Data Analysis}, Springer, New York.
}
\seealso{
\code{\link{basisfd}},
\code{\link{basisfd.product}},
\code{\link{exponentiate.fd}}
}
\examples{
oldpar <- par(no.readonly=TRUE)
##
## add a parabola to itself
##
bspl4 <- create.bspline.basis(nbasis=4)
parab4.5 <- fd(c(3, -1, -1, 3)/3, bspl4)
coef2 <- matrix(c(6, -2, -2, 6)/3, 4)
dimnames(coef2) <- list(NULL, 'reps 1')
\dontshow{stopifnot(}
all.equal(coef(parab4.5+parab4.5), coef2)
\dontshow{)}
##
## Same example with interior knots at 1/3 and 1/2
##
bspl5.3 <- create.bspline.basis(breaks=c(0, 1/3, 1))
plot(bspl5.3)
x. <- seq(0, 1, .1)
para4.5.3 <- smooth.basis(x., 4*(x.-0.5)^2, bspl5.3)$fd
plot(para4.5.3)
bspl5.2 <- create.bspline.basis(breaks=c(0, 1/2, 1))
plot(bspl5.2)
para4.5.2 <- smooth.basis(x., 4*(x.-0.5)^2, bspl5.2)$fd
plot(para4.5.2)
#str(para4.5.3+para4.5.2)
coef2. <- matrix(0, 9, 1)
dimnames(coef2.) <- list(NULL, 'rep1')
\dontshow{stopifnot(}
all.equal(coef(para4.5.3-para4.5.2), coef2.)
\dontshow{)}
##
## product
##
quart <- para4.5.3*para4.5.2
% # interior knots of the sum
% # = union(interior knots of the summands);
% # ditto for difference and product.
% \dontshow{stopifnot(}
% all.equal(knots.fd(quart), c(knots.fd(para4.5.3), knots.fd(para4.5.2)))
% \dontshow{)}
# norder(quart) = norder(para4.5.2)+norder(para4.5.3)-1 = 7
\dontshow{stopifnot(}
norder(quart) == (norder(para4.5.2)+norder(para4.5.3)-1)
\dontshow{)}
# para4.5.2 with knot at 0.5 and para4.5.3 with knot at 1/3
# both have (2 end points + 1 interior knot) + norder-2
# = 5 basis functions
# quart has (2 end points + 2 interior knots)+norder-2
# = 9 basis functions
# coefficients look strange because the knots are at
# (1/3, 1/2) and not symmetrical
\dontshow{stopifnot(}
all.equal(as.numeric(coef(quart)),
0.1*c(90, 50, 14, -10, 6, -2, -2, 30, 90)/9)
\dontshow{)}
plot(para4.5.3*para4.5.2) # quartic, not parabolic ...
##
## product with Fourier bases
##
f3 <- fd(c(0,0,1), create.fourier.basis())
f3^2 # number of basis functions = 7?
##
## fd+numeric
##
coef1 <- matrix(c(6, 2, 2, 6)/3, 4)
dimnames(coef1) <- list(NULL, 'reps 1')
\dontshow{stopifnot(}
all.equal(coef(parab4.5+1), coef1)
\dontshow{)}
\dontshow{stopifnot(}
all.equal(1+parab4.5, parab4.5+1)
\dontshow{)}
##
## fd-numeric
##
coefneg <- matrix(c(-3, 1, 1, -3)/3, 4)
dimnames(coefneg) <- list(NULL, 'reps 1')
\dontshow{stopifnot(}
all.equal(coef(-parab4.5), coefneg)
\dontshow{)}
plot(parab4.5-1)
plot(1-parab4.5)
par(oldpar)
}
\keyword{smooth}