# fitQ: Fits a moving window quadratic (or simple linear) regression... In pnnl/qFeature: Extract Features from Continuous or Discrete Time Series

## Description

Fits a moving quadratic (or simple linear) regression model over a series using a 2-sided window.

## Usage

 ```1 2 3 4 5 6``` ```fitQ(y, x1 = -10:10, min.window = 5, start = 1, skip = 1, linear.only = FALSE) ## S3 method for class 'fitQ' summary(object, ..., stats = c("min", "q1", "mean", "med", "q3", "max", "sd", "count")) ```

## Arguments

 `y` A numeric vector `x1` The predictor of the regression model, whose length must be odd, and it must be monotonic (increasing or decreasing). Typically this would be an evenly spaced, increasing vector. `min.window` The minimum number of non-missing data points in a window that are required to fit the regression model. `start` The index of the center of the first window `skip` The number of indexes to advance the center of the moving window each time the model is fit. `linear.only` `=TRUE` fits a simple linear regression model with `x1` as the single predictor, instead of a quadratic regression model. `object` An object of class `fit` returned by `fitQ`. `stats` A character vector of summary statistics that are valid for `summaryStats`. Alternatively, the object returned by `summaryStats` may also be supplied for this argument. `...` Additional (unused) arguments required for consistency of S3 methods

## Details

This function dynamically accounts for the incomplete windows which are caused by missing values and which occur at the beginning and end of the series. A quadratic regression model is used if `linear.only = FALSE`:

`y = a + b*x1 + c*x1^2 + e`

and a simple linear regression model is used if `linear.only = TRUE`:

`y = a + b*x1 + e`

where `a`, `b`, and `c` are regression coefficients, and `e` represents the residual error. The regression model is fit repeatedly over a two-sided moving window across the values of `y`. The middle element of `x1` is aligned with an element of `y` and then the regression model coefficients are calculated for that element of `y`. Consequently, the size of the moving window is determined by the the number of elements in `x1`. For example, if `x1 = -3:3`, the size of the moving window is 7.

For the quadratic model, `fitQ` rapidly calculates the least squares estimates by centering the predictors so that they are orthogonal (or nearly orthogonal, if data in the window are missing). Then the orthogonal coefficients are 'backtransformed' to the original, non-orthogonal parameterization. Centering and orthogonality are not required to quickly fit the simple linear regression model.

The handling of the moving windows and missing values in this function is very similar to `Smisc::smartFilter`.

Instead of a numeric vector, the `x1` argument can be a `valid_fitQ_args` object (returned by `check_fitQ_args`), in which case all the subsequent arguments to `fitQ` are ignored (because the `valid_fitQ_args` object contains all those arguments). This is useful because `fitQ` is called repeatedly by `getFeatures` over the same set of argument values.

## Value

An object of class `fitQ`, which is list with the 4 vectors that contain the results of the quadratic model fits:

 `a` The estimated intercepts `b` The estimated linear coefficients `c` The estimated quadratic coefficients. These are `NA` is `linear.only = TRUE` `d` The root mean squared error (RMSE)

The first element of the vectors `a`, `b`, `c`, and `d` contains the model coefficients corresponding to the first data point in `y`. The second element in the vectors `a`, `b`, `c`, and `d` contains the model coefficients corresponding to the second data point in `y`, etc.

## Methods (by generic)

• `summary`: Calculates summary statistics for a `fitQ` object, returning a named vector with summary statistics. The names take the form [coefficient].[stat], where the coefficient is one of "a", "b", "c", or "d", and the stat is the statistic required in the `stats` argument of the `summary` method.

Landon Sego

## Examples

 ```1 2 3 4 5 6 7``` ```# Calculate the rolling window quadratic fit z <- fitQ(rnorm(25), -3:3) z summary(z) # Or we can request our own summary stats summary(z, stats = c("count", "mean", "kurt")) ```

pnnl/qFeature documentation built on May 25, 2019, 10:22 a.m.