Description Usage Arguments Details Value Author(s) References See Also Examples
View source: R/fRegress.fdPar.R View source: R/fRegress.R
This function carries out a functional regression analysis, where
either the dependent variable or one or more independent variables are
functional. Non-functional variables may be used on either side
of the equation. In a simple problem where there is a single scalar
independent covariate with values z_i, i=1,…,N and a single
functional covariate with values x_i(t), the two versions of the
model fit by fRegress
are the scalar dependent variable
model
y_i = β_1 z_i + \int x_i(t) β_2(t) \, dt + e_i
and the concurrent functional dependent variable model
y_i(t) = β_1(t) z_i + β_2(t) x_i(t) + e_i(t).
In these models, the final term e_i or e_i(t) is a residual, lack of fit or error term.
In the concurrent functional linear model for a functional dependent variable, all functional variables are all evaluated at a common time or argument value $t$. That is, the fit is defined in terms of the behavior of all variables at a fixed time, or in terms of "now" behavior.
All regression coefficient functions β_j(t) are considered to be functional. In the case of a scalar dependent variable, the regression coefficient for a scalar covariate is converted to a functional variable with a constant basis. All regression coefficient functions can be forced to be smooth through the use of roughness penalties, and consequently are specified in the argument list as functional parameter objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | fRegress(y, ...)
## S3 method for class 'formula'
fRegress(y, data=NULL, betalist=NULL, wt=NULL,
y2cMap=NULL, SigmaE=NULL,
method=c('fRegress', 'model'), sep='.', ...)
## S3 method for class 'character'
fRegress(y, data=NULL, betalist=NULL, wt=NULL,
y2cMap=NULL, SigmaE=NULL,
method=c('fRegress', 'model'), sep='.', ...)
## S3 method for class 'fd'
fRegress(y, xfdlist, betalist, wt=NULL,
y2cMap=NULL, SigmaE=NULL, returnMatrix=FALSE, ...)
## S3 method for class 'fdPar'
fRegress(y, xfdlist, betalist, wt=NULL,
y2cMap=NULL, SigmaE=NULL, returnMatrix=FALSE, ...)
## S3 method for class 'numeric'
fRegress(y, xfdlist, betalist, wt=NULL,
y2cMap=NULL, SigmaE=NULL, returnMatrix=FALSE, ...)
|
y |
the dependent variable object. It may be an object of five possible classes:
|
data |
an optional |
xfdlist |
a list of length equal to the number of independent variables (including any intercept). Members of this list are the independent variables. They can be objects of either of these two classes:
In either case, the object must have the same number of replications
as the dependent variable object. That is, if it is a scalar, it
must be of the same length as the dependent variable, and if it is
functional, it must have the same number of replications as the
dependent variable. (Only univariate independent variables are
currently allowed in |
betalist |
For the For the |
wt |
weights for weighted least squares |
y2cMap |
the matrix mapping from the vector of observed values to the
coefficients for the dependent variable. This is output by function
|
SigmaE |
Estimate of the covariances among the residuals. This can only be
estimated after a preliminary analysis with |
method |
a character string matching either |
sep |
separator for creating names for multiple variables for
|
returnMatrix |
logical: If TRUE, a two-dimensional is returned using a special class from the Matrix package. |
... |
optional arguments |
Alternative forms of functional regression can be categorized with traditional least squares using the following 2 x 2 table:
explanatory | variable | |||
response | | | scalar | | | function |
| | | | |||
scalar | | | lm | | | fRegress.numeric |
| | | | |||
function | | | fRegress.fd or | | | fRegress.fd or |
| | fRegress.fdPar | | | fRegress.fdPar or linmod | |
For fRegress.numeric
, the numeric response is assumed to be the
sum of integrals of xfd * beta for all functional xfd terms.
fRegress.fd or .fdPar
produces a concurrent regression with
each beta
being also a (univariate) function.
linmod
predicts a functional response from a convolution
integral, estimating a bivariate regression function.
In the computation of regression function estimates in
fRegress
, all independent variables are treated as if they are
functional. If argument xfdlist
contains one or more vectors,
these are converted to functional data objects having the constant
basis with coefficients equal to the elements of the vector.
Needless to say, if all the variables in the model are scalar, do NOT
use this function. Instead, use either lm
or lsfit
.
These functions provide a partial implementation of Ramsay and Silverman (2005, chapters 12-20).
These functions return either a standard fRegress
fit object or
or a model specification:
fRegress fit |
a list of class
|
model specification |
The Alternatively, to see how the
|
J. O. Ramsay, Giles Hooker, and Spencer Graves
Ramsay, James O., Hooker, Giles, and Graves, Spencer (2009) Functional Data Analysis in R and Matlab, Springer, New York.
Ramsay, James O., and Silverman, Bernard W. (2005), Functional Data Analysis, 2nd ed., Springer, New York.
fRegress.formula
,
fRegress.stderr
,
fRegress.CV
,
linmod
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 | ###
###
### scalar response and explanatory variable
### ... to compare fRegress and lm
###
###
# example from help('lm')
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
fRegress.D9 <- fRegress(weight ~ group)
(lm.D9.coef <- coef(lm.D9))
(fRegress.D9.coef <- sapply(fRegress.D9$betaestlist, coef))
all.equal(as.numeric(lm.D9.coef), as.numeric(fRegress.D9.coef))
###
###
### vector response with functional explanatory variable
###
###
##
## set up
##
annualprec <- log10(apply(CanadianWeather$dailyAv[,,"Precipitation.mm"],
2,sum))
# The simplest 'fRegress' call is singular with more bases
# than observations, so we use a small basis for this example
smallbasis <- create.fourier.basis(c(0, 365), 25)
# There are other ways to handle this,
# but we will not discuss them here
tempfd <- smooth.basis(day.5,
CanadianWeather$dailyAv[,,"Temperature.C"], smallbasis)$fd
##
## formula interface
##
precip.Temp.f <- fRegress(annualprec ~ tempfd)
##
## Get the default setup and modify it
##
precip.Temp.mdl <- fRegress(annualprec ~ tempfd, method='m')
# First confirm we get the same answer as above:
precip.Temp.m <- do.call('fRegress', precip.Temp.mdl)
all.equal(precip.Temp.m, precip.Temp.f)
# set up a smaller basis than for temperature
nbetabasis <- 21
betabasis2. <- create.fourier.basis(c(0, 365), nbetabasis)
betafd2. <- fd(rep(0, nbetabasis), betabasis2.)
# add smoothing
betafdPar2. <- fdPar(betafd2., lambda=10)
precip.Temp.mdl2 <- precip.Temp.mdl
precip.Temp.mdl2[['betalist']][['tempfd']] <- betafdPar2.
# Now do it.
precip.Temp.m2 <- do.call('fRegress', precip.Temp.mdl2)
# Compare the two fits
precip.Temp.f[['df']] # 26
precip.Temp.m2[['df']]# 22 = saved 4 degrees of freedom
(var.e.f <- mean(with(precip.Temp.f, (yhatfdobj-yfdPar)^2)))
(var.e.m2 <- mean(with(precip.Temp.m2, (yhatfdobj-yfdPar)^2)))
# with a modest increase in lack of fit.
##
## Manual construction of xfdlist and betalist
##
xfdlist <- list(const=rep(1, 35), tempfd=tempfd)
# The intercept must be constant for a scalar response
betabasis1 <- create.constant.basis(c(0, 365))
betafd1 <- fd(0, betabasis1)
betafdPar1 <- fdPar(betafd1)
betafd2 <- with(tempfd, fd(basisobj=basis, fdnames=fdnames))
# convert to an fdPar object
betafdPar2 <- fdPar(betafd2)
betalist <- list(const=betafdPar1, tempfd=betafdPar2)
precip.Temp <- fRegress(annualprec, xfdlist, betalist)
all.equal(precip.Temp, precip.Temp.f)
###
###
### functional response with vector explanatory variables
###
###
##
## simplest: formula interface
##
daybasis65 <- create.fourier.basis(rangeval=c(0, 365), nbasis=65,
axes=list('axesIntervals'))
Temp.fd <- with(CanadianWeather, smooth.basisPar(day.5,
dailyAv[,,'Temperature.C'], daybasis65)$fd)
TempRgn.f <- fRegress(Temp.fd ~ region, CanadianWeather)
##
## Get the default setup and possibly modify it
##
TempRgn.mdl <- fRegress(Temp.fd ~ region, CanadianWeather, method='m')
# make desired modifications here
# then run
TempRgn.m <- do.call('fRegress', TempRgn.mdl)
# no change, so match the first run
all.equal(TempRgn.m, TempRgn.f)
##
## More detailed set up
##
region.contrasts <- model.matrix(~factor(CanadianWeather$region))
rgnContr3 <- region.contrasts
dim(rgnContr3) <- c(1, 35, 4)
dimnames(rgnContr3) <- list('', CanadianWeather$place, c('const',
paste('region', c('Atlantic', 'Continental', 'Pacific'), sep='.')) )
const365 <- create.constant.basis(c(0, 365))
region.fd.Atlantic <- fd(matrix(rgnContr3[,,2], 1), const365)
region.fd.Continental <- fd(matrix(rgnContr3[,,3], 1), const365)
region.fd.Pacific <- fd(matrix(rgnContr3[,,4], 1), const365)
region.fdlist <- list(const=rep(1, 35),
region.Atlantic=region.fd.Atlantic,
region.Continental=region.fd.Continental,
region.Pacific=region.fd.Pacific)
beta1 <- with(Temp.fd, fd(basisobj=basis, fdnames=fdnames))
beta0 <- fdPar(beta1)
betalist <- list(const=beta0, region.Atlantic=beta0,
region.Continental=beta0, region.Pacific=beta0)
TempRgn <- fRegress(Temp.fd, region.fdlist, betalist)
all.equal(TempRgn, TempRgn.f)
###
###
### functional response with
### (concurrent) functional explanatory variable
###
###
##
## predict knee angle from hip angle; from demo('gait', package='fda')
##
## formula interface
##
(gaittime <- as.numeric(dimnames(gait)[[1]])*20)
gaitrange <- c(0,20)
gaitbasis <- create.fourier.basis(gaitrange, nbasis=21)
harmaccelLfd <- vec2Lfd(c(0, (2*pi/20)^2, 0), rangeval=gaitrange)
gaitfd <- smooth.basisPar(gaittime, gait,
gaitbasis, Lfdobj=harmaccelLfd, lambda=1e-2)$fd
hipfd <- gaitfd[,1]
kneefd <- gaitfd[,2]
knee.hip.f <- fRegress(kneefd ~ hipfd)
##
## manual set-up
##
# set up the list of covariate objects
const <- rep(1, dim(kneefd$coef)[2])
xfdlist <- list(const=const, hipfd=hipfd)
beta0 <- with(kneefd, fd(basisobj=basis, fdnames=fdnames))
beta1 <- with(hipfd, fd(basisobj=basis, fdnames=fdnames))
betalist <- list(const=fdPar(beta0), hipfd=fdPar(beta1))
fRegressout <- fRegress(kneefd, xfdlist, betalist)
all.equal(fRegressout, knee.hip.f)
#See also the following demos:
#demo('canadian-weather', package='fda')
#demo('gait', package='fda')
#demo('refinery', package='fda')
#demo('weatherANOVA', package='fda')
#demo('weatherlm', package='fda')
|
Loading required package: splines
Loading required package: Matrix
Attaching package: 'fda'
The following object is masked from 'package:graphics':
matplot
Warning message:
In fRegress.formula(weight ~ group) :
No functions found; setting rangeval to 0:1
(Intercept) groupTrt
5.032 -0.371
const group.Trt
5.032 -0.371
[1] TRUE
[1] TRUE
[1] 26
[1] 21.99979
[1] 0.003313809
[1] 0.003591287
[1] TRUE
[1] TRUE
[1] TRUE
[1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 12.5 13.5 14.5
[16] 15.5 16.5 17.5 18.5 19.5
[1] TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.