# fitSpline

##### Produce the fits from a natural cubic smoothing spline applied to a response in a `data.frame`

,
and growth rates can be computed using derivatives

Uses `smooth.spline`

to fit a spline to all the values of
`response`

stored in `data`

.

The amount of smoothing can be controlled by `df`

and the
`smoothing.method`

provides for `direct`

amd
`logarithmic`

smoothing. If `df = NULL`

, the amount of
smoothing is controlled by the default arguments and those you supply
for `smooth.spline`

. The method of Huang (2001) for correcting the
fitted spline for estimation bias at the end-points will be applied if
`correctBoundaries`

is `TRUE`

.

The derivatives of the fitted spline can also be obtained, and the
Absolute and Relative Growth Rates ( AGR and RGR) computed using them, provided
`correctBoundaries`

is `FALSE`

. Otherwise, growth rates can be
obtained by difference using `splitContGRdiff`

.

By default, `smooth.spline`

will issue an error if there are not
at least four distinct x-values. On the other hand, `fitSplines`

issues a warning and sets all smoothed values and derivatives to
`NA`

. The handling of missing values in the observations is
controlled via `na.x.action`

and `na.y.action`

.

##### Usage

```
fitSpline(data, response, x, df=NULL, smoothing.method = "direct",
correctBoundaries = FALSE,
deriv=NULL, suffices.deriv=NULL, RGR=NULL, AGR=NULL,
na.x.action="exclude", na.y.action = "exclude", ...)
```

##### Arguments

- data
A

`data.frame`

containing the column to be smoothed.- response
A

`character`

giving the name of the column in`data`

that is to be smoothed.- x
A

`character`

giving the name of the column in`data`

that contains the values of the predictor variable.- df
A

`numeric`

specifying the desired equivalent number of degrees of freedom of the smooth (trace of the smoother matrix). Lower values result in more smoothing. If`df = NULL`

, the amount of smoothing is controlled by the default arguments for and those that you supply to`smooth.spline`

.- smoothing.method
A

`character`

giving the smoothing method to use. The two possibilites are (i)`"direct"`

, for directly smoothing the observed`response`

, and (ii)`"logarithmic"`

, for smoothing the`log`

-transformed`response`

and then back-transforming by taking the exponentional of the fitted values.- correctBoundaries
A

`logical`

indicating whether the fitted spline values are to have the method of Huang (2001) applied to them to correct for estimation bias at the end-points. Note that`deriv`

must be`NULL`

for`correctBoundaries`

to be set to`TRUE`

.- deriv
A

`numeric`

specifying one or more orders of derivatives that are required.- suffices.deriv
A

`character`

giving the characters to be appended to the names of the derivatives. If`NULL`

and the derivative is to be retained then`smooth.dv`

is appended.- RGR
A

`character`

giving the character to be appended to the smoothed`response`

to create the RGR name, but only when`smoothing.method`

is`direct`

and the RGR is required. When`smoothing.method`

is`direct`

and the RGR is required`RGR`

must not be`NULL`

and`deriv`

must include one so that the first derivative is available for calculating it. When`smoothing.method`

is`logarithmic`

, the RGR is the backtransformed first derivative and so, to obtain it, merely include`1`

in`deriv`

and any suffix for it in`suffices.deriv`

. Leave`RGR`

set to`NULL`

.- AGR
A

`character`

giving the character to be appended to the smoothed`response`

to create the AGR name, but only when`smoothing.method`

is`logarithmic`

and the AGR is required. When`smoothing.method`

is`logarithmic`

and the AGR is required`AGR`

must not be`NULL`

and`deriv`

must include one so that the first derivative is available for calculating it. When`smoothing.method`

is`direct`

, the AGR is the backtransformed first derivative and so, to obtain it, merely include`1`

in`deriv`

and any suffix for it in`suffices.deriv`

. Leave`AGR`

set to`NULL`

.- na.x.action
A

`character`

string that specifies the action to be taken when values of`x`

are`NA`

. The possible values are`fail`

,`exclude`

or`omit`

. For`exclude`

and`omit`

, predictions and derivatives will only be obtained for nonmissing values of`x`

. The difference between these two codes is that for`exclude`

the returned`data.frame`

will have as many rows as`data`

, the missing values have been incorporated.- na.y.action
A

`character`

string that specifies the action to be taken when values of`y`

, or the`response`

, are`NA`

. The possible values are`fail`

,`exclude`

,`omit`

,`allx`

,`trimx`

,`ltrimx`

or`rtrimx`

. For all options, except`fail`

, missing values in`y`

will be removed before smoothing. For`exclude`

and`omit`

, predictions and derivatives will be obtained only for nonmissing values of`x`

that do not have missing`y`

values. Again, the difference between these two is that, only for`exclude`

will the missing values be incorporated into the returned`data.frame`

. For`allx`

, predictions and derivatives will be obtained for all nonmissing`x`

. For`trimx`

, they will be obtained for all nonmissing`x`

between the first and last nonmissing`y`

values that have been ordered for`x`

; for`ltrimx`

and`utrimx`

either the lower or upper missing`y`

values, respectively, are trimmed.- ...
allows for arguments to be passed to

`smooth.spline`

.

##### Value

A `data.frame`

containing `x`

and the fitted smooth. The names
of the columns will be the value of `x`

and the value of `response`

with `.smooth`

appended. The number of rows in the `data.frame`

will be equal to the number of pairs that have neither a missing `x`

or
`response`

and it will have the same order of codex as `data`

.
If `deriv`

is not `NULL`

, columns
containing the values of the derivative(s) will be added to the
`data.frame`

; the name each of these columns will be the value of
`response`

with `.smooth.dvf`

appended, where
`f`

is the order of the derivative, or the value of `response`

with `.smooth.`

and the corresponding element of
`suffices.deriv`

appended. If `RGR`

is not `NULL`

, the RGR
is calculated as the ratio of value of the first derivative of the fitted
spline and the fitted value for the spline.

##### References

Huang, C. (2001). Boundary corrected cubic smoothing splines. *Journal of Statistical Computation and Simulation*, **70**, 107-121.

##### See Also

`splitSplines`

, `smooth.spline`

,
`predict.smooth.spline`

, `splitContGRdiff`

##### Examples

```
# NOT RUN {
data(exampleData)
fit <- fitSpline(longi.dat, response="Area", , x="xDays", df = 4,
deriv=c(1,2), suffices.deriv=c("AGRdv","Acc"))
# }
```

*Documentation reproduced from package growthPheno, version 1.0-13, License: GPL (>= 2)*