library(doremi)

# SECOND ORDER DIFFERENTIAL EQUATIONS

The differential equation considered in this case is the following:

$\frac{d^2y}{dt} + 2\xi\omega_{n}\frac{dy}{dt} + \omega_{n}^2 y = k\omega_{n}^2u(t)$ (1)

Where:

• $$y(t)$$ is the signal to be analyzed

• $$\frac{dy}{dt}$$ is its first derivative

• $$\frac{d^2y}{dt}$$ is its second derivative

• $$u(t)$$ is the excitation term perturbing the dynamics of $$y(t)$$

And regarding the coefficients:

• $$\omega_{n} = \frac{2\pi}{T}$$ is the system’s natural frequency, the frequency with which the system would oscillate if there were no damping. $$T$$ is the corresponding period of oscillation. The term $$\omega_{n}^2$$ represents thus the ratio between the attraction to the equilibrium and the inertia. If we considered the example of a mass attached to a spring, this term would represent the ratio of the spring constant and the object’s mass.

• $$\xi$$ is the damping ratio. It represents the friction that damps the oscillation of the system (slows the rate of change of the variable). The value of $$\xi$$ determines the shape of the system time response, which can be: $$\xi<0$$ Unstable, oscillations of increasing magnitude $$\xi=0$$ Undamped, oscillating $$0<\xi<1$$ Underdamped or simply “damped”. Most of the studies use this model, also referring to it as “Damped Linear Oscillator” (DLO). $$\xi=1$$ Critically damped $$\xi>1$$ Over-damped, no oscillations in the return to equilibrium

• $$k$$ is the gain. It represents the proportionality between the stationary increase of $$y$$ and the constant increase of $$u$$ that caused it.

• $$yeq$$ is the signal equilibrium value, i.e. the stationary value reached when the excitation term is 0.

If the excitation term $$u(t)$$ is null, then the equation reduce to

$\frac{d^2y}{dt} + 2\xi\omega_{n}\frac{dy}{dt} + \omega_{n}^2 y = 0$ (2)

Equation (2) can also be found in the social/behavioral sciences literature as: $y'' + \zeta y' + \eta y = 0$ (3) That assumes the $$y_{eq}$$ is 0.

In which: $\zeta= 2\xi\omega_{n}$ and $\eta=\omega_{n}^2$ (4)

The dynamics in this case are then provoked either by a previous excitation that is no longer present or by the displacement of the system from its equilibrium position (either due to an initial condition different from 0 or an initial “speed” or derivative at time 0 different from 0): $y(t=0)=y_{0}$ $\frac{dy}{dt}(t=0)=v_{0}$ The shape of the solution to equation (2) -also called trajectory, or system response in engineering- will change according to the values of the parameters $$\xi$$ and $$\omega_{n}^2$$ presented, specially the values of $$\xi$$, as this parameter will define if the behavior is divergent, oscillating or undamped, underdamped (oscillations decreasing exponentially) or overdamped (system going back to equilibrium without oscillations) as it can be seen in the figure below:

data11a <- data.table::rbindlist(lapply(seq(0,2,0.2),
function(eps){
generate.2order(time = 0:49,
y0 = 1,
xi = eps,
period = 20)[,xi := eps][]
}))
# plot
ggplot2::ggplot(data11a,ggplot2::aes(t,y,color = as.factor(xi)))+
ggplot2::geom_line() +
ggplot2::labs(x = "time (arb. unit)", y = "signal (arb. unit)", colour = "xi")

## Simulating data

Two functions are available to simulate data in the package: generate.2order simulate the solution of the differential equation for a given vector of time, and the parameters period = $$T$$, xi = $$\xi$$, yeq = $$y_{eq}$$, y0 = $$y(t = 0)$$, v0 = $$\frac{dy}{dt}(t=0)$$, k = $$k$$ and the vector 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.2order uses generate.2order to generate a panel of nind individuals, with measurement noise and inter-individual noise.

### Example 1 - Generating Damped Linear Oscillator signals with inter-individual variability

The parameter internoise allow the parameters of the differential equation to vary between the individuals. They are in this case distributed along a normal distribution centered on the parameter value given to the generate.panel.2order function with a standard deviation of internoiseparameter. The parameter intranoise allows to ass measurement noise. intranoise is the ratio between the measurement noise’ standard deviation and the signal’ standard deviation.

When there is no excitation, the input can be set to NULL (default value), like in the example above. As presented on the first order differential equation vignette, the result can be easily plotted through the plot command:

We see here that the period, the equilibrium value and the damping parameter vary between each individual.

### Example 2 - Using simulation functions to generate undamped, critically damped and overdamped signals

Let’s note that the damping ratio parameter allows to generate not only oscillating signals, that is when $$0<\xi<1$$, but also signals where the system can reach its equilibrium value without oscillations: these are the critically damped ($$\xi=1$$) and overdamped ($$\xi>1$$). The simulation functions of the package also allow the generation of these behaviors, as shown below:

## Analyzing data

### Example 1 - Analyzing Damped Linear Oscillator signals

Analyzing the previous dataset and as for the first order model, the user must specify the name of the columns containing the id of the participants, the excitation, and the signal. Several methods are available for the estimation of the derivatives and the user needs to specify which method to use (gold is the default) and the embedding dimension/smoothing parameter (see the package pdf manual for more details).

Now let’s take a look at the result. It is possible to plot the estimated curve from the estimated coefficients, to visually inspect the analysis:

The different parts of the resulting doremi object are the same as those for the first order:

• data: contains the original dataset and extra columns for intermediate calculations. The column “signalname_derivate2” is the only additional one with respect to the first order result.

• resultmean: contains the fixed coefficients resulting from the regression for all the individuals. It is also the part of the result displayed when one calls the variable name (that calls the print method for doremi objects):

Beware that, as known in the two-steps procedures, and as in the first order case, the estimation of derivatives is a source of bias and thus the error terms provided by the regression are not final. Nevertheless, it is possible to obtain from the summary of the regression the standard errors calculated for the coefficients estimated that will be $$\zeta$$, $$\eta$$ (see equations 3 and 4) and $$y_{eq}\zeta$$ if the equilibrium value is different from 0.

In the resultid object, the first columns are the terms resulting from the regression:

• omega2 is the term $$\omega_n^2$$ or $$\eta$$, with its standard error omega2_stde

• xi2omega is the term $$2\xi \omega_n^2$$ or $$\zeta$$, with its standard error xi2omega_stde

• yeqomega2 is $$y_{eq}\omega_n^2$$ or $$y_{eq}\zeta$$, with its standard error yeqomega2_stde

• komega2 is $$k\omega^2$$ the coefficient associated to the external excitation term in the regression, with its standard error komega2_stde. As there is no excitation on this example, it is NA.

Whereas the following are the values calculated from these first columns:

• period is the oscillation period, that can be extracted from the term $$\omega_n^2$$, were $$T=2\pi/\omega_n$$

• wn is the natural frequency, calculated also from the term omega2 = $$\omega_n^2$$

• xi is the damping factor, calculated from the term xi2omega = $$2\xi \omega_n^2$$

• yeq is the equilibrium value, calculated from yeqomega2 = $$y_{eq}\omega_n^2$$

• k is the gain, calculated from komega2

• resultid: contains the same coefficients but reconstructed for each individual. In the figure above it can be seen that for some of them, the fit wasn’t very good. This will be enhanced using the function optimum_param that will identify which embedding number produces the best estimate, as for the first order case. For each individual we then have:

• regression: contains the summary of the multilevel regression carried out to fit the coefficients

The doremi object will also contain the derivative method used and the embedding number/smoothing parameter used for further reference.

# SECOND ORDER DIFFERENTIAL EQUATION MODELS WITH AN EXCITATION TERM

## Simulating data

### Example 1 - Generating signals with no noise

In this example we will generate data for 5 individuals, that respond to a “step” excitation (an excitation that changes value abruptly, from 0 to 1 in this case). We will consider no dynamic noise, no variation of the damping factor, period, or equilibrium value across individuals (no interindividual noise).

Plotting once more the data with the plot method available in the package for doremidata objects:

### Example 2 - Generating signals with noise

The call to the function remains almost the same, this time with a noise to signal ratio of 0.3 and a 20% inter-individual noise:

And, as it can be seen in the figures, the coefficients change according to the person (damping factor, period, gain). Initial value, speed and equilibrium value could also change if their initial value was different from 0. These have been set to 0 for readability of the results but they could also be included.

### Example 3 - Using an initial condition in a time different from t0=0

The functions to generate the solution of the second order differential equation allow to specify the time for which the initial condition (y0 and v0) are given. This time must be between the minimum and the maximum value of the time vector given to the function. Below an example specifying the value, the derivative of the signal at a given time:

The function generate.2order generated for all time given the unique solution of the differential equation that has value 3 at $$t = 25$$ and a derivative of 1 at this point.

### Example 4 - studying the effect of periodical excitations

The excitation can be of any form. This package can also be used to simulate driven damped oscillators:

Here we can see that the system, which has a natural period of 20, has a steady state that oscillate with the same period as the excitation, that is a period of 10.

## Analyzing data

Analyzing the signals generated in the previous examples, we will verify that the parameters were the one introduced in the simulation function and that the estimated signals generated match the simulated ones.

### Example 1 - Analyzing data from a single individual

The simplest case is the one in which the data measured corresponds to a single individual. The call to the function is almost the same but omitting the id parameter in the call. The input parameter “verbose” as in other R functions, allows to print (using the package futile.logger) the actions carried out by the function until the calculation of the result:

### Example 2 - Analyzing data with several individuals and some inter and intra-individual noise

Analyzing the data generated in the example 2 of the simulation section, 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 (see the package pdf manual for more details).

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

As it was mentioned before, the estimation of derivatives is a source of bias. The previous fit can be enhanced in three ways:

• By changing the embedding number/smoothing parameter

• By changing the order of the derivative

• By changing the derivation method

In the following example, we will use the function optimum_param of the doremi package to find the embedding number that provides an R2 the closest to 1. The other ways can be tested “manually” by calling the other functions, for instance, in a simulation study.

res3 <- optimum_param (data=data2,
id="id",
input="excitation",
time="time",
signal="signal",
model = "2order",
dermethod = "glla",
order = 2,
pmin = 5,
pmax = 17,
pstep = 2)
#> Warning: Model failed to converge with 2 negative eigenvalues: -4.1e-02 -1.8e+00
#> Warning: Model failed to converge with 1 negative eigenvalue: -9.9e-02
res3$analysis #> omega2 omega2_stde xi2omega esp2omega_stde yeqomega2 #> 1: 0.15240859 0.017165126 0.07072646 0.050518640 -1.529673e-02 #> 2: 0.08386721 0.010183999 0.05197518 0.026363745 -7.186378e-03 #> 3: 0.06642613 0.011871053 0.04104850 0.016291128 -4.009652e-03 #> 4: 0.05545717 0.010826453 0.03585923 0.010365056 -1.362731e-03 #> 5: 0.04942667 0.009205934 0.03541548 0.008307339 -7.057166e-04 #> 6: 0.04556870 0.008059665 0.03606063 0.007272696 -6.186052e-05 #> 7: 0.04119786 0.006177849 0.04445767 0.006281961 1.671691e-03 #> yeqomega2_stde period wn xi yeq excitation_komega2 #> 1: 0.017591424 16.09441 0.3903954 0.09058310 -0.100366572 0.15579155 #> 2: 0.008042482 21.69620 0.2895984 0.08973667 -0.085687571 0.08476531 #> 3: 0.005135935 24.37869 0.2577327 0.07963387 -0.060362571 0.06569553 #> 4: 0.003473488 26.68093 0.2354935 0.07613637 -0.024572668 0.05275712 #> 5: 0.002512501 28.26176 0.2223211 0.07964938 -0.014278052 0.04620178 #> 6: 0.001855513 29.43381 0.2134683 0.08446369 -0.001357522 0.04173378 #> 7: 0.001621059 30.95584 0.2029725 0.10951645 0.040577128 0.03607719 #> excitation_komega2_stde excitation_k R2 D #> 1: 0.025741643 1.0221966 0.2853658 5 #> 2: 0.015358674 1.0107085 0.3187247 7 #> 3: 0.014804667 0.9890014 0.4880996 9 #> 4: 0.012224892 0.9513129 0.7379361 11 #> 5: 0.009979706 0.9347541 0.7653589 13 #> 6: 0.008501695 0.9158432 0.7250433 15 #> 7: 0.006484326 0.8757055 0.6831471 17 res3$summary_opt
#>        omega2 omega2_stde   xi2omega esp2omega_stde     yeqomega2
#> 1: 0.04942667 0.009205934 0.03541548    0.008307339 -0.0007057166
#>    yeqomega2_stde   period        wn         xi         yeq excitation_komega2
#> 1:    0.002512501 28.26176 0.2223211 0.07964938 -0.01427805         0.04620178
#>    excitation_komega2_stde excitation_k        R2  D method
#> 1:             0.009979706    0.9347541 0.7653589 13   glla