The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

first-order

library(doremi)

FIRST ORDER DIFFERENTIAL EQUATIONS

The linear first order differential equation with constant coefficients model is given by the following equation: \[\tau\frac{dy}{dt} + (y-y_{eq}) = ku(t)\] or \[\frac{dy}{dt} + \gamma (y-y_{eq}) = \gamma ku(t)\] with the initial condition: \[y(t=0)=y_{0}\] Where:

And regarding the coefficients:

It is common to find this equation with the excitation term set to 0, \(u(t)=0\):

\[\frac{dy}{dt} + \gamma(y-y_{eq}) = 0\]

The dynamics in this case follow and exponential decay of the form \(y(t)=(y_0-yeq) e^{-\gamma t} + y_{eq}\) and are provoked either by a previous excitation that is no longer present or by the displacement of the system from its equilibrium position (i.e. an initial condition different from 0)

Two steps analysis procedure

The estimation of the fixed effect coefficients of the differential equation is performed in two steps. The first step consists in estimating the first derivative, using one of the three methods proposed.

The second step consists in performing the following linear mixed-effects regression:

\[\begin{equation} \dot{y}_{ij} \sim (b_{0}+b_{0j})+ (b_1 + b_{1j})y_{ij}+ (b_2 + b_{2j}) U_{ij}+e_{ij} \label{eq2} \end{equation}\]

where:

Note that random effects are estimated for the intercept (\(b_{0} + b_{0j}\)), variable (\(b_{1} + b_{1j}\)) and excitation terms (\(b_{2} + b_{2j}\)), so that individuals can have different coefficients (initial condition, damping time, gain and equilibrium value).

The fixed effect coefficients estimated from the regression are:

The coefficients of the differential equation can thus be calculated as:

The estimation is performed using the function lmer if there are several individuals or lm if there is a single individual.

First order differential equation with no excitation term

Simulating data

Two functions are available to simulate data in the package: generate.1order simulates the solution of the differential equation for a given vector of time, and the parameters tau = \(\tau\), y0 = \(y(t=0)\),yeq = \(y_{eq}\), k = \(k\) and the vector of excitation \(u(t)\). The function create a data.table with a column t for the time, y for the signal, and exc for the excitation.

The function generate.panel.1order uses generate.1order to generate a panel of nind individuals, with measurement noise and inter-individual noise.

Example 1a - Generating signals with no noise

In this example we will generate data for 4 individuals, with a decay time of 10s. That is, the signal follows exactly the differential equation (no measurement noise) with no variation of the decay time, the gain and the equilibrium value across individuals (no interindividual noise). As we are considering no excitation, then, as mentioned, the system should be out of its equilibrium value to observe the dynamics of return to equilibrium.

It can be specified to the function whether there is an excitation or not by either setting the input to NULL or directly ignoring it in the input arguments. The function returns a data.table with the class ‘doremidata’. As any data.table, it can be visualized using the str or head functions or entering the variable name.

Where:

  • id is the identifier of the individual

  • time is the time vector introduced as input

  • signalraw is the signal without noise

  • signal is the signal with noise (in this case, the values of this column are the same as those of the signalraw column and in the plot, and thus both lines are overlapped in the plot below).

Plotting the data with the plot method available in the package for doremidata objects is quite straight forward:

Example 1b - Changing initial condition, gain and equilibrium value

In this example, we show the flexibility of the functions to add changes on the mentioned parameters. For this, we simplify the form of the exitation to a simple step:

Example 2a - Generating signals with noise

The call to the function remains almost the same, with two additional arguments: a noise to signal ratio, set at 0.2, and an interindividual noise set at 40%. We can then visualize the different trajectories for the different individuals:

Analyzing data

Example 3 - Analyzing data when the excitation is unknown, with some inter- and intraindividual noise

We will test how the analysis functions fit a decreasing exponential, as is the case when there is no excitation, when it is unknown, or when it is constant over time.

Note that the input parameter has been omitted when calling the function. The result display the summary of the fixed effect coefficients:

The data.table result_id gives the coefficients estimated for each individual:

It is possible to use the plot function directly on the result of the analysis:

First order differential equation with an excitation term

Analyzing the signals with noise generated above, we can verify that the parameters were the one introduced in the simulation function and that the estimated signals generated match the simulated ones.

Simulating data

Example 1a - Generating signals with no noise

To generate the excitation process perturbing the system we want to simulate, we can use the generate.excitation function, which generate a sequence of square pulses of a given length randomly distributed:

Note that the time vector and excitation vector can be of any form and created by the user.

In this example we will use the excitation vector we just generated and the generate.panel.1order function to generate the variable following the first order differential equation for 4 individuals with a decay time of 10s. The signal follows exactly the differential equation (no measurement noise) with no variation of the decay time, the gain and the equilibrium value across individuals (no interindividual noise).

Example 1b - Changing initial condition, gain and equilibrium value

Using the same excitation process, but changing the initial condition, the gain the the equilibrium value:

And it can be observed how the system tends to equilibrium (\(t_{eq} = 2\)) from the initial condition (\(y_0 = 3\)) after being perturbed by the excitations.

Example 2 - Generating signals with noise

The call to the function remains almost the same, this time we have added a Noise to Signal ratio of 2 (the variance of noise is twice the variance of the underlying signal) and a 20% inter-individual noise (that is the parameters gain, equilibrium value, the initial value \(y_0\) and \(\tau\) of each individual are sampled on a gaussian centered on the parameter value with a standard deviation of 20% its values) to visualize the different trajectories for the different individuals:

Analyzing data

Example 3 - Analyzing data from a single individual

In this case, we can reuse one of the signals from the previous example

As the table contains data from a single individual, the id parameter has been omitted when calling the function.

Example 3a - Analyzing data with several individuals and some inter and intra-individual noise

Analyzing the data generated in the example 2 above (table res2a), the user must specify the name of the columns containing the id of the participants, the excitation, and the signal. As several methods are available for the estimation of the derivatives, the user needs to specify which method to use (gold is the default) and the embedding dimension by modifying the parameter derparam (see the package pdf manual for more details).

Now let’s take a look at the result. When calling the variable, the fixed effect coefficients of the regression with their associated standard error, the derived coefficient characterizing the signal shape (the decay time tau, the equilibrium value yeq and the gain k), and the R2 are displayed:

The signal analyzed was generated with tau = 10, yeq = 0, and k = 1.

This is a simplified view of the results. The analysis function supplies an object of class “doremi” that contains in fact several lists. It is possible to explore the full result values by using the function “summary” for doremi objects (see the section on methods created for doremi objects at the end of this vignette).

summary(res3a) 
#> Derivative and mean calculation ($data):
#>      id excitation time   signal signal_rollmean signal_derivate1 time_derivate
#>   1:  1          1    0 1.548851        1.834129      0.181351993             1
#>   2:  1          1    1 2.041981        2.114180      0.173511265             2
#>   3:  1          1    2 1.911555        2.170934      0.150343864             3
#>   4:  1          1    3 2.389003        2.335365      0.007922849             4
#>   5:  1          1    4 2.212243        2.300707      0.036393743             5
#>  ---                                                                           
#> 400:  4          0   96 1.487556        1.477797     -0.106773663            97
#> 401:  4          0   97 1.671826        1.412553     -0.189999930            98
#> 402:  4          0   98 1.274009        1.502412      0.333695804            99
#> 403:  4          0   99 1.291826              NA               NA            NA
#> 404:  4          0  100 1.941400              NA               NA            NA
#>      excitation_rollmean totalexc totalexcroll signal_estimated
#>   1:                   1 10.22945     10.22945         1.785409
#>   2:                   1 10.22945     10.22945         1.834129
#>   3:                   1 10.22945     10.22945         1.882530
#>   4:                   1 10.22945     10.22945         1.930615
#>   5:                   1 10.22945     10.22945         1.978385
#>  ---                                                           
#> 400:                   0  0.00000      0.00000         1.948274
#> 401:                   0  0.00000      0.00000         1.929023
#> 402:                   0  0.00000      0.00000         1.909898
#> 403:                  NA  0.00000           NA         1.890899
#> 404:                  NA  0.00000           NA         1.872023
#> 
#>  Mixed-effects regression results ($regression):
#> [[1]]
#> Linear mixed model fit by REML. t-tests use Satterthwaite's method [
#> lmerModLmerTest]
#> Formula: paste0("signal_derivate1 ~ signal_rollmean + (1 +", paste(doremiexc,  
#>     "rollmean ", collapse = "+", sep = "_"), " + signal_rollmean |id) + ",  
#>     paste(doremiexc, "rollmean ", collapse = "+", sep = "_"))
#>    Data: intdata
#> Control: lmerControl(calc.derivs = FALSE, optimizer = "nloptwrap")
#> 
#> REML criterion at convergence: -138.8
#> 
#> Scaled residuals: 
#>     Min      1Q  Median      3Q     Max 
#> -3.1701 -0.6832 -0.0177  0.6163  4.2287 
#> 
#> Random effects:
#>  Groups   Name            Variance  Std.Dev.  Corr       
#>  id       (Intercept)     1.718e-09 4.145e-05            
#>           input1_rollmean 1.013e-09 3.183e-05 -0.26      
#>           signal_rollmean 2.207e-10 1.486e-05 -0.99  0.10
#>  Residual                 3.963e-02 1.991e-01            
#> Number of obs: 396, groups:  id, 4
#> 
#> Fixed effects:
#>                   Estimate Std. Error         df t value Pr(>|t|)   
#> (Intercept)      -0.006530   0.057785 392.960102  -0.113  0.91009   
#> signal_rollmean  -0.006562   0.023984 392.971440  -0.274  0.78454   
#> input1_rollmean   0.067125   0.022717 392.940144   2.955  0.00332 **
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Correlation of Fixed Effects:
#>             (Intr) sgnl_r
#> signl_rllmn -0.977       
#> inpt1_rllmn -0.018 -0.110
#> 
#> [[2]]
#> $id
#>     (Intercept) input1_rollmean signal_rollmean
#> 1  4.579499e-10    1.196365e-08   -1.078989e-09
#> 2  3.937595e-09   -7.368405e-09   -8.931788e-10
#> 3 -1.325579e-10   -9.584872e-09    7.779417e-10
#> 4 -4.262987e-09    4.989627e-09    1.194226e-09
#> 
#> with conditional variances for "id" 
#> 
#> 
#>  Mean coefficients of the differential equation ($resultmean):
#>     id       gamma gamma_stde     yeqgamma yeqgamma_stde      tau        yeq
#> 1: All 0.006561924  0.0239843 -0.006529568    0.05778508 152.3943 -0.9950691
#>    excitation_kgamma excitation_kgamma_stde excitation_k R2
#> 1:        0.06712488              0.0227174     10.22945  0
#> 
#>  Coefficients per individual ($resultid):
#>    id       gamma     yeqgamma      tau        yeq excitation_kgamma
#> 1:  1 0.006561923 -0.006529568 152.3943 -0.9950689        0.06712489
#> 2:  2 0.006561924 -0.006529564 152.3943 -0.9950684        0.06712487
#> 3:  3 0.006561925 -0.006529568 152.3943 -0.9950692        0.06712487
#> 4:  4 0.006561926 -0.006529573 152.3943 -0.9950699        0.06712488
#>    excitation_k
#> 1:     10.22945
#> 2:     10.22945
#> 3:     10.22945
#> 4:     10.22945
#> 
#>  Derivative estimation method used ($dermethod):
#> [1] "calculate.gold"
#> 
#>  Embedding number/smoothing parameter ($derparam):
#> [1] 3

The first object of the output contains the original data with some columns added. These columns contain intermediate variables necessary for the mixed-effect regression:

Where:

If we want to visualize the summary of the mixed-effect regression:

res3a$regression
#> [[1]]
#> Linear mixed model fit by REML. t-tests use Satterthwaite's method [
#> lmerModLmerTest]
#> Formula: paste0("signal_derivate1 ~ signal_rollmean + (1 +", paste(doremiexc,  
#>     "rollmean ", collapse = "+", sep = "_"), " + signal_rollmean |id) + ",  
#>     paste(doremiexc, "rollmean ", collapse = "+", sep = "_"))
#>    Data: intdata
#> Control: lmerControl(calc.derivs = FALSE, optimizer = "nloptwrap")
#> 
#> REML criterion at convergence: -138.8
#> 
#> Scaled residuals: 
#>     Min      1Q  Median      3Q     Max 
#> -3.1701 -0.6832 -0.0177  0.6163  4.2287 
#> 
#> Random effects:
#>  Groups   Name            Variance  Std.Dev.  Corr       
#>  id       (Intercept)     1.718e-09 4.145e-05            
#>           input1_rollmean 1.013e-09 3.183e-05 -0.26      
#>           signal_rollmean 2.207e-10 1.486e-05 -0.99  0.10
#>  Residual                 3.963e-02 1.991e-01            
#> Number of obs: 396, groups:  id, 4
#> 
#> Fixed effects:
#>                   Estimate Std. Error         df t value Pr(>|t|)   
#> (Intercept)      -0.006530   0.057785 392.960102  -0.113  0.91009   
#> signal_rollmean  -0.006562   0.023984 392.971440  -0.274  0.78454   
#> input1_rollmean   0.067125   0.022717 392.940144   2.955  0.00332 **
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Correlation of Fixed Effects:
#>             (Intr) sgnl_r
#> signl_rllmn -0.977       
#> inpt1_rllmn -0.018 -0.110
#> 
#> [[2]]
#> $id
#>     (Intercept) input1_rollmean signal_rollmean
#> 1  4.579499e-10    1.196365e-08   -1.078989e-09
#> 2  3.937595e-09   -7.368405e-09   -8.931788e-10
#> 3 -1.325579e-10   -9.584872e-09    7.779417e-10
#> 4 -4.262987e-09    4.989627e-09    1.194226e-09
#> 
#> with conditional variances for "id"

Where we have the random and fixed effects and the residuals calculated by the function lmer or lm depending on if the sample had several or one individual respectively.

Beware that, as known in most two-steps procedures, the estimation of derivatives reduces variance, and thus the error terms provided by the regression are not final.

The following table contains the fixed effect coefficients (result displayed by default when calling a doremi variable, as mentioned before):

It can be observed that the decay time is close to the value introduced to the simulation function (6), the equilibrium value close to its true value (0), and the gain close to its true value (1). For each individual we have:

Where:

If we graphically wish to verify how the estimated signal fits the initial signal, we can once again call the function plot, that has been adapted to handle doremi objects as well:

Similarly to the print function, plot applied to a doremi object plots by default the first six individuals contained in the result. If we wish to visualize a single individual or a specific set of individuals, we can specify them by changing the “id” input parameter of the function:

Finally, the results contain the derivative method used and the embedding number/smoothing parameter used for further reference.

Example 3b - Enhancing the fit by changing the embedding number/ smoothing parameter.

As mentioned before, the estimation of derivatives before model fit is a source of error underestimation. It is possible to enhance the quality of the fit in three ways:

In the following example, we will use the function optimum_param to find the embedding number that provides the closest \(R^2\) to 1. The other ways can be tested “manually” by calling the other functions, for instance, in a simulation study. Let us reuse the data from res2a:

And can be seen that from the range provided, an embedding number of {r} res3b$d produces the best fit and the coefficients are closer to their true values than the ones estimated in the previous example.

The optimum_param function generates an object of class “doremiparam”. If we want to graphically see the evolution of the coefficients according to the embedding number, we can easily plot the results with the plot function too:

Example 4 - Analyzing data when the signal is subject to several excitations

In this example, we construct a signal having different gain for different excitation sources. For the sake of simplicity, we generate this example signal without noise.

We have in data4 a signal, whose dynamics was created by three excitations with different gains: k = 1, 3 and 5 for id = 1, k = 1, 2.5 and 4 for id = 2. We can see in the graph below that each excitation is identical but generates a different change of the signal:

It is possible in analyze.1order to give a vector of column as input. In this case, a gain will be estimated for each excitation column.

Here, we recover the estimated gains for each individual in the resultid table. They are a good approximation of the coefficients introduced, using this small sample of 2 individuals.

Example 5 - Analyzing data with missing values in the signal

We examine how the analysis function still manages to retrieve an accurate fit from signals with missing measurements:

In the next plot we show the selection of random points made on the full data set, and also represent the signal without noise. The signal with noise will be used to carry out the analysis.

Then analyzing and visualizing the random points selection:

C - Applications

Heart rate during effort tests

Using the cardio dataset present in this package, we will apply the model to real heart frequency measurements. The data fields are described with detail in the pdf manual of the package, together with the source from which the data was obtained. The data consists in heart rate measurements from 21 individuals carrying out an effort test on a cycle ergometer. Participants pedal on a bicycle with increasing resistance. The resistance load is measured in watts, with higher load forcing the individual to greater effort. His/her heart frequency will then vary according to the effort supplied and the participant’s fitness. According to this, the cardio data includes, for each individual, the time since the beginning of the test (seconds), the bicycle load (Watts) and the heart rate (1/min). Here is an example of these data:

ggplot2::ggplot(cardio[id == 2],
                ggplot2::aes(x = time))+
  ggplot2::geom_point(ggplot2::aes(y = hr,color = "heart rate (beat/s)"))+
  ggplot2::geom_line(ggplot2::aes(y = load,color = "effort load (W)"))

We can perform the analysis using analyze.1order:

And use the plot function to plot the analysis results for all 21 participants. Note that omitting the argument id would lead to only the first 6 participants being plotted by default.

As can be seen, the model reproduces reasonably well the variation of the heart rate, and can be used to fully characterize the heart rate dynamics with three simple parameters: the resting heart rate (the equilibrium value), the characteristic time of heart rate change (the decay time), and the increase of heart rate for a given effort (the gain).

Such analysis has been used in (Mongin et al., 2020a) and Mongin et al., 2020b.

Measuring heart frequency during effort tests by considering several excitations

In the previous example, each one of the steps in the resistance load can be considered as a separated perturbation having an effect on heart rate. In this case, the functions will estimate different gains as explained, to account for each one of the contributions of the load to the total heart rate dynamics. Let us first create a new column for each step of effort load:

To consider each effort step as an independent excitation associated with its own gain, we can use analyze.1order and give the vector of the excitation column as id argument:

Here we will estimate a gain per excitation column

In the plot, each workload step of the exercise test appear in one different column.

This analysis has been the main analysis in Mongin et al.,2020c and used in Mongin et al.,2020d.

Measurements of response time of individuals when carrying out mental rotation tasks

Using the rotation dataset present in this package, we will apply the model to a case in which the excitation signal is not clearly defined. In brief, the data is from 17 individuals that carried out mental rotation tasks (identify if two figures, one of which is rotated, are the same or a mirror image) and the response time (in milliseconds) was measured (Courvoisier et al., 2013). Each individual was measured every day for 60 days, though there can be missing data. To account for this, the time has been represented by the number of days since the beginning of the experiment (variable days). The signal is the mean response time (variable meanRT) over the ~200 stimuli done every day. The purpose of the study was to compare the performance between men and women.

dermethod<- "fda"
pmin = 0.1
pmax = 1
pstep = 0.1

restemp <- optimum_param(data = rotation,
                          id = "id",
                          time ="days",
                          signal = "meanRT",
                          dermethod = dermethod,
                          model = "1order",
                          pmin = pmin,
                          pmax = pmax,
                          pstep = pstep)
#> WARN [2021-01-19 14:28:31] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:31] Some of the decay times calculated were negative and thus, the estimated signal was not generated for these.
#>                   decay times can be negative for some individuals for the following reasons: 1. The signal of
#>                   the individual doesn't go back to equilibrium. 2.The linear mixed-effects model estimating the random
#>                   effect showed some error messages/warnings. 3.Model misspecification.
#> WARN [2021-01-19 14:28:31] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:31] Some of the decay times calculated were negative and thus, the estimated signal was not generated for these.
#>                   decay times can be negative for some individuals for the following reasons: 1. The signal of
#>                   the individual doesn't go back to equilibrium. 2.The linear mixed-effects model estimating the random
#>                   effect showed some error messages/warnings. 3.Model misspecification.
#> WARN [2021-01-19 14:28:31] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:31] Some of the decay times calculated were negative and thus, the estimated signal was not generated for these.
#>                   decay times can be negative for some individuals for the following reasons: 1. The signal of
#>                   the individual doesn't go back to equilibrium. 2.The linear mixed-effects model estimating the random
#>                   effect showed some error messages/warnings. 3.Model misspecification.
#> WARN [2021-01-19 14:28:31] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:32] Some of the decay times calculated were negative and thus, the estimated signal was not generated for these.
#>                   decay times can be negative for some individuals for the following reasons: 1. The signal of
#>                   the individual doesn't go back to equilibrium. 2.The linear mixed-effects model estimating the random
#>                   effect showed some error messages/warnings. 3.Model misspecification.
#> WARN [2021-01-19 14:28:32] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:32] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:32] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:32] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:33] No excitation signal introduced as input. Input was set to 0.
#> WARN [2021-01-19 14:28:33] No excitation signal introduced as input. Input was set to 0.
restemp$summary_opt
resc2a <- analyze.1order(data = rotation,
                 id = "id",
                 time ="days",
                 signal = "meanRT",
                 dermethod = dermethod,
                 derparam = restemp$d)
#> WARN [2021-01-19 14:28:33] No excitation signal introduced as input. Input was set to 0.

Plotting analysis results:

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.