The presence of the `signal`-column is required (see tbl_mbte
).
The idea is to fit the values of a signal via arbitrary models. Those
models are used to make predictions for the signal-values.
mbte_fit(x, ...)
x | A |
---|---|
... | It is assumed, that the ellipsis only contains named elements. The
elements are used as |
The original table gets returned with the `fits`-column added (list column
consisting of tibbles). The column names are the names of the ellipsis
(...
). Each fit is a numeric column containing the predicted
signal-values.
Fitting-quosures can make use of the following masked objects (see examples for clarification):
A tibble
with the signal to fit
(`time` and `value`-columns are present).
The name of the time column as a
symbol
.
The name of the value-column stored as a
symbol
.
The number of the processed row of x
. This masked
object may be helpful if a fitting quosure has side-effects (e.g.
a unique identifier is needed for storing parameters about the fit).
Currently, there are two ways, how a fitting-quosure may return the predicted signal-values for a signal:
by returning a numeric vector: if a quosure evaluates to a numeric vector, the values are used as is.
by returning an object compatible with predict
:
In this case, predict()
will be called on the returned object
with newdata = .signal
(see above; the original signal-table,
which has been used for fitting, gets passed). This option may come in
handy, if the actual function used for fitting (e.g.
lm
) returns an object, for which a predict-method
returning a numeric vector exists (in this case
predict.lm
).
In either case, the length of the vector containing the values of the
predicted signal has to match the number of rows of .signal
(the
signal-table used for fitting).
The tibble containing event-information contains the following columns:
The error, that occurred during processing. Errors which originate from the evaluation of a fitting quosure or prediction-related errors get wrapped.
The row-number of the original table x
, at which
the error occurred.
The signal-subtibble processed at the time the error occurred.
The name of the fitting quosure (name in ...
).
The fitting quosure being processed (element of ...
).
This function logs unusual events. A warning gets raised at the end of an
execution, if events have been logged. The event-log can be retrieved
by passing the returned object to mbte_event_log
. In this
case, a tibble containing the logged events will be returned to the user.
filtered_signals
(dataset used in examples)
fitting-helpers
# load dataset (tbl_mbte with extracted subsignals) data(filtered_signals) # fit linear models to signals (by returning a predict()-compatible object # and by returning a numeric vector of the correct length) # # NOTE: `.signal` is not defined in this scope. However, masking is used and # to provide the signal-tibble as described above. fits1 <- mbte_fit(filtered_signals, lm1 = lm(value ~ t, .signal), # rely on predict() lm2 = predict(lm(value ~ t, .signal)) # return numeric vector ) # resuting table with `fits`-column added fits1#> # A tibble: 37 x 4 #> mv signal_nr signal fits #> <chr> <int> <list> <list> #> 1 mv1 1 <tibble [22 × 2]> <tibble [22 × 2]> #> 2 mv3 2 <tibble [22 × 2]> <tibble [22 × 2]> #> 3 mv5 1 <tibble [30 × 2]> <tibble [30 × 2]> #> 4 mv6 1 <tibble [21 × 2]> <tibble [21 × 2]> #> 5 mv6 3 <tibble [30 × 2]> <tibble [30 × 2]> #> 6 mv7 1 <tibble [56 × 2]> <tibble [56 × 2]> #> 7 mv8 1 <tibble [29 × 2]> <tibble [29 × 2]> #> 8 mv8 2 <tibble [26 × 2]> <tibble [26 × 2]> #> 9 mv9 1 <tibble [63 × 2]> <tibble [63 × 2]> #> 10 mv10 1 <tibble [25 × 2]> <tibble [25 × 2]> #> # … with 27 more rows# a tibble in the `fits`-list column; the columns `fit1` and `fit2` are # equivalent (only different ways of returning the fittind signal-values have # been used). fits1$fits[[1]]#> # A tibble: 22 x 2 #> lm1 lm2 #> <dbl> <dbl> #> 1 29.5 29.5 #> 2 28.3 28.3 #> 3 27.0 27.0 #> 4 25.8 25.8 #> 5 24.5 24.5 #> 6 23.2 23.2 #> 7 22.0 22.0 #> 8 20.7 20.7 #> 9 19.5 19.5 #> 10 18.2 18.2 #> # … with 12 more rows# use tidy-dots semantics of mbte_fit() - useful if fitting quosures are # generated programmatically # define fitting quosures fitting_candidates <- rlang::quos( lm1 = lm(value ~ t, .signal), # rely on predict() lm2 = predict(lm(value ~ t, .signal)) # return numeric vector ) # use tidy-dots splicing (equivalent to call producing `fits1` above) fits2 <- mbte_fit(filtered_signals, !!!fitting_candidates) fits2#> # A tibble: 37 x 4 #> mv signal_nr signal fits #> <chr> <int> <list> <list> #> 1 mv1 1 <tibble [22 × 2]> <tibble [22 × 2]> #> 2 mv3 2 <tibble [22 × 2]> <tibble [22 × 2]> #> 3 mv5 1 <tibble [30 × 2]> <tibble [30 × 2]> #> 4 mv6 1 <tibble [21 × 2]> <tibble [21 × 2]> #> 5 mv6 3 <tibble [30 × 2]> <tibble [30 × 2]> #> 6 mv7 1 <tibble [56 × 2]> <tibble [56 × 2]> #> 7 mv8 1 <tibble [29 × 2]> <tibble [29 × 2]> #> 8 mv8 2 <tibble [26 × 2]> <tibble [26 × 2]> #> 9 mv9 1 <tibble [63 × 2]> <tibble [63 × 2]> #> 10 mv10 1 <tibble [25 × 2]> <tibble [25 × 2]> #> # … with 27 more rowsfits2$fits[[1]]#> # A tibble: 22 x 2 #> lm1 lm2 #> <dbl> <dbl> #> 1 29.5 29.5 #> 2 28.3 28.3 #> 3 27.0 27.0 #> 4 25.8 25.8 #> 5 24.5 24.5 #> 6 23.2 23.2 #> 7 22.0 22.0 #> 8 20.7 20.7 #> 9 19.5 19.5 #> 10 18.2 18.2 #> # … with 12 more rows