What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu...

51
MakeDynr Ou, Hunter, Chow Why Dynr Facts Modeling Framework Estimation Example 1: Linear SDE Example 2: Nonlinear Discrete-time Example 3: Regime- switching Nonlinear ODE Take-home Message References What’s for dynr: A package for linear and nonlinear DYNamic modeling in R Lu Ou 1 , Michael D. Hunter 2 , and Sy-Miin Chow 1 Pennsylvania State University: Human Development and Family Studies 1 University of Oklahoma Health Sciences Center: Pediatrics 2 [email protected] Modern Modeling Methods (M 3 ) Storrs, CT; May 25, 2016 1 / 51

Transcript of What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu...

Page 1: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

What’s for dynr: A package for linear andnonlinear DYNamic modeling in R

Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1

Pennsylvania State University: Human Development and Family Studies1

University of Oklahoma Health Sciences Center: Pediatrics2

[email protected]

Modern Modeling Methods (M3)Storrs, CT; May 25, 2016

1 / 51

Page 2: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

What’s in a name?

BicameRal Extended Kalman Filter with Iterated Smoothing(BREKFIS)

2 / 51

Page 3: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Overview

1 Why

2 Dynr Facts

3 Modeling Framework

4 Estimation

5 Example 1: Linear SDE

6 Example 2: Nonlinear Discrete-time

7 Example 3: Regime-switching Nonlinear ODE

8 Take-home Message

3 / 51

Page 4: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Why dynr?Why make a new package for this?

� OpenMx (Neale et al., in press) (linear only)

� ctsem (Driver, Oud, & Voelkle, 2015) (linear, continuoustime only)

� MATLAB (single subject)

� MKFM6 (Dolan, 2005) (linear only)

� dlm (Petris, 2010) (slow)

� SsfPack (Koopman, Shephard, & Doornik, 1999) (singlesubject)

� MPlus 8 (not available yet, linear only)

4 / 51

Page 5: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Why dynr?Why make a new package for this?

� Linear and nonlinear models

� Multiple subjects

� Intuitive interface

� Fast

� Great reporting

� Combine dynamics for continuous and categorical latentvariables

5 / 51

Page 6: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Why dynamics?

� Goal of psychology (Hockenbury & Hockenbury, 2010)� To understand behavior and mental processes

� Behavior unfolds in time.

� Mental processes are fundamentally time-oriented

� Action across time is the study of dynamics.

� Development occurs within people and across time.

6 / 51

Page 7: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Why differential equations?

� Common language of many sciences

� Hypothesize rules that govern individual systems over time� Rules: actions, reactions, forces, influences� Individual: people are generally heterogeneous

� Discrete-time methods are differential equations in discretetime.

7 / 51

Page 8: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

1 Dynr fits discrete- and continuous-time dynamicmodels to multivariate longitudinal/time-series data.

2 Dynr handles linear and nonlinear dynamic modelswith an easy-to-use interface.

� Dynr allows model specification in matrix and formulaforms.

� Dynr allows automatic differentiation.

3 Dynr deals with dynamic models withregime-switching properties.

� e.g., Markov switching autoregressive (MSAR);Markov switching Kalman Filter (MSKF)

� Caveat: Only linear measurement

4 Dynr computes in C and runs fast.

5 Dynr provides ready-to-present results through LaTexequations and plots.

What can dynr do? Dynr Facts 1-5

8 / 51

Page 9: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

� https://quantdev.ssri.psu.edu/

avada_resources/dynr/

� In the next week or so, on CRAN!

Where can I get dynr?

9 / 51

Page 10: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

� Gather data with dynr.data()

� Prepare recipes with� prep.measurement()� prep.*Dynamics()� prep.initial()� prep.noise()� prep.regimes() (optional)

� Mix recipes and data into a model withdynr.model()

� Cook model with dynr.cook()

� Serve results with� summary()� plot()� dynr.ggplot()� plotFormula()� printex()

dynr preparation

10 / 51

Page 11: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Modeling Framework

� Dynamic Model (prep.*Dynamics())� Discrete-time

xi (ti,j) = Fθf ,i (xi (ti,j−1)) + ζi (ti,j), ζi (ti,j) ∼ N(0,Σζ)� Continuous-time Ordinary and Stochastic Differential

Equation (ODE & SDE)dxi (t) = Fθf ,i (xi (t), t)dt + Gθf ,i (t)dw(t)

� Measurement Model (prep.measurement())yi (ti ,j) = µ + Λxi (ti ,j) + εi (ti ,j), εi (ti ,j) ∼ N(0,Σy )

� Initial condition (prep.initial())x1(t1,j) ∼ N(x0,P0)

11 / 51

Page 12: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

What are regime-switchingdynamic models?

� A regime–switching longitudinal model consists of severallatent (unobserved) classes–or “regimes.” Within eachclass, a submodel is used to described the distinct changepatterns associated with the class.

� Each “regime” can be thought of as one of the stages orphases of a dynamic process.

� Individuals can switch between classes or regimes overtime.

� The changes that unfold within a regime are continuous innature.

� Markov-switching, hidden markov models, latent transitionanalysis, probabilistic functions of Markov chains (Petrie,1969)

12 / 51

Page 13: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Initial regime probabilitiesTransition probabilities

Multinomial logistic regression models are used to represent theinitial regime probabilities and describe each individual i ’stransition in class membership from time t-1 to time t as

Pr(Si1 = k|xi1,θ)∆= πk,i1 =

exp(ak1 + b′k1xi1)∑Ks1=1 exp(as1 + b′s1xi1)

,

Pr(Sit = k|Si ,t−1 = j , xit ,θ)∆= πjk,it =

exp(akt + b′ktxit)∑Kst=1 exp(ast + b′stxit)

Sit = individual i ’s class membership at time t

K = the number of regimes

akt = the logit intercept for the kth regime at time t

xit = a vector of covariates for person i at time t

bkt = a vector of logit slopes for the kth regime at time t

13 / 51

Page 14: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Extended Kalman Filter

1 Time update (Prediction)

2 Measurement Update (Correction)

vk = yk −Λx̂k|k−1,Re,k = Σε + ΛP̂k|k−1ΛT

Kk = P̂k|k−1ΛTR−1

e,k

x̂k|k = x̂k|k−1 + Kkvk , P̂k|k = P̂k|k−1 −KkΛP̂k|k−1

3 Optimizationlog L(θ) = 1

2

∑ni=1

∑Tk=1(−pi,k log(2π)−log |Re,k |−v′i,kR

−1e,kvi,k)

4 Kim Smoother

14 / 51

Page 15: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

15 / 51

Page 16: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

16 / 51

Page 17: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

17 / 51

Page 18: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

18 / 51

Page 19: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

19 / 51

Page 20: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

20 / 51

Page 21: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

21 / 51

Page 22: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

22 / 51

Page 23: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

23 / 51

Page 24: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

6 8 10 12 14 16

1020

3040

5060

7080

Age

Rea

ding

Rec

ogni

tion

Kalman PredictionKalman UpdateObserved Data

24 / 51

Page 25: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Extended Kalman Filter

� Prediction (Discrete Time)

x̂t|t−1 = F (t, x̂t−1|t−1)

Pt|t−1 =∂F (t, x̂(t))

∂x̂Pt−1|t−1

∂F (t, x̂(t))

∂x̂

T

+ Q

� Prediction (Continuous Time)

d x̂

dt= F (t, x̂t−1|t−1)

DP(t) =∂F (t, x̂(t))

∂x̂P(t) + P(t)(

∂F (t, x̂(t))

∂x̂)T + Q(t)

� Solve these differential equations using Runge-Kutta oradaptive ODE solver.

25 / 51

Page 26: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Linear Harmonic Oscillator

� fit to data from broad domains within psychology

� resilience of older adults (Boker, Montpetit, Hunter, &Bergeman, 2010)

� coupling of smoking and drinking behavior in adolescents(Boker & Graham, 1998)

� response of widows to the loss of their spouse (Bisconti,Bergeman, & Boker, 2004, 2006)

� fluctuations and self-regulation of emotions (S. Chow,Ram, Boker, Fujita, & Clore, 2005)

� mother-infant synchrony during playful, face-to-faceinteractions (Zentall, Boker, & Braungart-Rieker, 2006)

� coupling of weather and mood in patients with bipolardisorder (Boker, Leibenluft, Deboeck, Virk, & Postolache,2008)

� dynamics of romantic partners (Steele & Ferrer, 2011)

26 / 51

Page 27: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Damped and Forced Harmonic Oscillator

� As a second-order system

d2x

dt2= −kx − c

dx

dt+ ζ (1)

� As a vector of first-order systems( dxdtd2xdt2

)=

(0 1−k −c

)(xdxdt

)+

(0ζ

)(2)

� The Measurement Model

y =(

1 0)( x

dxdt

)+ ε (3)

27 / 51

Page 28: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Linear Harmonic OscillatorExample code

# measurement

# this is the factor loadings matrix

#Lambda in SEM notation or C in OpenMx notation

meas <- prep.measurement(

# starting values and fixed values

values.load=matrix(c(1, 0), 1, 2),

# parameter names or fixed

params.load=matrix(c('fixed', 'fixed'), 1, 2),

state.names=c("Position","Velocity"),

obs.names=c("y1"))

28 / 51

Page 29: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Linear Harmonic OscillatorExample code

# define the differential equation

dynamics <- prep.matrixDynamics(

values.dyn=matrix(c(0, -0.1, 1, -0.2), 2, 2),

params.dyn=matrix(

c('fixed', 'spring', 'fixed', 'friction'),

2, 2), #uses params 'spring' and 'friction'

isContinuousTime=TRUE)

29 / 51

Page 30: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Linear Harmonic OscillatorExample code

# Prepare for cooking

# put all the recipes together

model <- dynr.model(dynamics=dynamics,

measurement=meas,

noise=ecov, initial=initial, data=data,

outfile="LinearSDE.c")

# Look with LaTeX

printex(model)

30 / 51

Page 31: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Linear SDE: printex {dynr}The measurement model is given by:[

y1(t)]

=[1 0

] [Position(t)Velocity(t)

]+ ε,

ε ∼ N( [

0.00],[mnoise

] )The dynamic model is given by:[dPosition(t)dVelocity(t)

]= (

[0 1

spring friction

] [Position(t)Velocity(t)

])dt + dw(t),

dw(t) ∼ N([0.00

0.00

],

[0 00 dnoise

])The initial condition of the dynamic model is given by:[

Position(0)Velocity(0)

]∼ N

([inipos1

],

[1 00 1

])31 / 51

Page 32: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Linear Harmonic OscillatorExample code

# Cook

# Estimate free parameters

res <- dynr.cook(model)

# Examine results

summary(res)

32 / 51

Page 33: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

summary

33 / 51

Page 34: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Output: plot {dynr}plot(res, model)

111111111

11111111111111111111

11111111111111111111111

11111111111111

1

11111111

1

1111111111111111111111111111111111111111111111111

1

1111111111111111111

111

1111111111111

1

1

1111

1111111111111111111111111111

1

111

1

1

11111111111111111111111111111111111111111111111111111

11111

111

11111111111111111111

1

1111111111111111111111111111111

111111111111111111111111

111

1

11

111

11111

1

1

1111111111111

11

11

1111111111111

1

1

11111111111111111111111111111

1111

11111111111111111111111111

11111111

1111

1

1

1

11111

1

1111111111111111111111111111111111111111111111111111111111111111111111111

1

1

111

11111

1111

11111111111111111111

1111111111

11111111111

1

11111111111111111

11111111111111111111

111

111111111

1

111

1

1111111111

1

11111111111111111111111111

111111111111111111111

11111111111

11111111111111111111

111111

11111

11111111111111111111111111

11

1

1

1111111111111111111111111

111

1

1

1111

11111

111111111111111111111111

11111111111111111111111111

1111

11

11111111

1111

1

11111111111111

1111111111111111111111

11111111111111111111

1

111111111111111

1

1111111111111111

11111111111111111111111122

222222

2222222222222222222222

22222222222222222222222222222222222

2222222222

22

222222222

2222

222222222222222222222222222222222222222222222222222222

2222222

22

22222222222222222222222222222222222222222222

2

2222

22222222222222

2222222222222222222

22222

2222222222

22

222

22222

2

22

2222222222222222222222222

222222222222222222222222

222222222222222222

222222222

22

2222

2222222

22222222222222222222

222222222222

222222222222222

2222222222222222222222222222222222222

22222222222222222222

222

22222222

22222222222

2222222222222

2222222222222222222222222

22222222222222

222222222

222

222222222

22222222222222222222222

22

22222222

22222222222222222

22222

222222

222222222222222222222

22222

22222222

22

222

222

222222222222222222222222222222222

2222222222222222222222222222222222

22222222222222222222222222

222222222222222222222222222222

22222222

222222222222222222222

22222

222222222

2222222222222222222222222222222222222222222222222

22222222222222

2222222222222222222

2222222222222222222222

222222222222222222222

22222222222222222222222

2222222222

22222222

22222222222222

1

−4

0

4

0 250 500 750 1000time

Sm

ooth

ed S

tate

Val

ues

variable 1 2state1 state2

Dynamic Model without Noise

d(Position(t)) = (Velocity(t))dt

d(Velocity(t)) = (−0.25 × Position(t) + −0.62 × Velocity(t))dt

Measurement Model without Noise

y1 = Position34 / 51

Page 35: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Example 2: MotivationData from the Affective Dynamics and Individual Differencesstudy (Emotions and Dynamic Systems Laboratory, 2010).Participants provided daily affect ratings 5 times a day atrandom intervals over 30 days.

−2.5

0.0

2.5

5.0

0 10 20 30time

valu

e

state Uncoupling Coupling

variable Positive Emotion Negative Emotion

Observed Positive and Negative Affects

35 / 51

Page 36: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Regime-switching NonlinearDynamic Factor Analysis Model

PEit = aPPEi ,t−1 + bPN,citNEi ,t−1 + ζPE ,it

NEit = aNNEi ,t−1 + bNP,citPEi ,t−1 + ζNE ,it (4)

bPN,cit =

{0 if cit = 0

bPN0

(exp(abs(NEi,t−1))

1+exp(abs(NEi,t−1))

)if cit = 1,

bNP,Sit =

{0 if cit = 0

bNP0

(exp(abs(PEi,t−1))

1+exp(abs(PEi,t−1))

)if cit = 1,

(5)

(S.-M. Chow & Zhang, 2013). Model adapted from an earlier model presented by(S.-M. Chow, Tang, Yuan, Song, & Zhu, 2011).

36 / 51

Page 37: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Regime-switching NonlinearDynamic Factor Analysis Model

� Dynr fits discrete-time dynamic models too.

� Dynr allows specification of nonlinear dynamicmodels in formula forms.

� Dynr deals with dynamic models withregime-switching properties.

� Dynr provides ready-to-present results throughLaTex equations and plots.

Dynr Facts

37 / 51

Page 38: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Output: dynr.ggplot {dynr}

1 2

−2

−1

0

1

2

0 100 200 300 0 100 200 300timeS

mo

oth

ed

Sta

te V

alu

es

variable PE NE

regime Coupled (nonlinear) Decoupled (linear)

Results from RS Nonlinear DFA model

38 / 51

Page 39: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Regime-switchingPredator and Prey Model

The dynamic model is given by:

Regime 1:

d(prey)

dt= a× prey − b × prey × predator ,

d(predator)

dt= −c × predator + d × prey × predator ,

Regime 2:

d(prey)

dt= a× prey − e × prey2

− b × prey × predator ,

d(predator)

dt= f × predator − c × predator2

+ d × prey × predator

39 / 51

Page 40: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

One-regime Predator-and-preyModel

1

2

3

0 1 2 3 4 5time

valu

e

variable

prey

predator

0

1

2

3

0 1 2 3 4 5time

valu

e

variable

x

y

0.5

1.0

1.5

2.0

0 1 2 3prey

pred

ator

5

10

15

20id

−1

0

1

2

3

−1 0 1 2 3 4x

y

5

10

15

20id

40 / 51

Page 41: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

One-regime extension ofthe Predator-and-prey Model

0

1

2

3

0 1 2 3 4 5time

valu

e

variable

prey

predator

−1

0

1

2

3

0 1 2 3 4 5time

valu

e

variable

x

y

0.8

1.2

1.6

2.0

0 1 2 3prey

pred

ator

5

10

15

20id

0

1

2

3

4

−2 0 2 4x

y

5

10

15

20id

41 / 51

Page 42: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Regime-switchingPredator and Prey Model

0

1

2

3

0 1 2 3 4 5time

valu

e

variable

prey

predator

−1

0

1

2

3

0 1 2 3 4 5time

valu

e

variable

x

y

1.0

1.5

2.0

0 1 2 3prey

pred

ator

5

10

15

20id

−1

0

1

2

3

−2 0 2 4x

y

5

10

15

20id

42 / 51

Page 43: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Regime-switchingPredator and Prey Model

� Dynr fits nonlinear ODE models withregime-switching properties.

� Dynr allows for automatic differentiation.

� Dynr provides ready-to-present results throughLaTex equations and plots.

Dynr Facts

43 / 51

Page 44: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

1 Dynr fits discrete- and continuous-time dynamicmodels to multivariate longitudinal/time-series data.

2 Dynr handles linear and nonlinear dynamic modelswith an easy-to-use interface.

� Dynr allows model specification in matrix and formulaforms.

� Dynr allows automatic differentiation.

3 Dynr deals with dynamic models withregime-switching properties.

� e.g., Markov switching autoregressive (MSAR);Markov switching Kalman Filter (MSKF)

� Caveat: Only linear measurement

4 Dynr computes in C and runs fast.

5 Dynr provides ready-to-present results through LaTexequations and plots.

What can dynr do? Dynr Facts 1-5

44 / 51

Page 45: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

� https://quantdev.ssri.psu.edu/

avada_resources/dynr/

� In the next week or so, on CRAN!

Where can I get dynr?

45 / 51

Page 46: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

� Gather data with dynr.data()

� Prepare recipes with� prep.measurement()� prep.dynamics()� prep.initial()� prep.noise()� prep.regimes() (optional)

� Mix recipes and data into a model withdynr.model()

� Cook model with dynr.cook()

� Serve results with� summary()� plot()� dynr.ggplot()� plotFormula()� printex()

dynr preparation

46 / 51

Page 47: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Thank you for your attention!

� NIH grants R01GM105004, R01HD07699

� NSF grant BCS-0826844

� Sukruth Reddy, Zhaohua Lu, Timothy Brick, Meng Chen, PeifengYin

� The QuantDev Group

Acknowledgements

47 / 51

Page 48: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Bisconti, T. L., Bergeman, C. S., & Boker, S. M. (2004). Emotionalwell-being in recently bereaved widows: A dynamical systemsapproach. Journal of Gerontology, 59B, 158-167.

Bisconti, T. L., Bergeman, C. S., & Boker, S. M. (2006). Socialsupport as a predictor of variability: An examination of theadjustment trajectories of recent widows. Psychology andAging, 21, 590-599.

Boker, S. M., & Graham, J. (1998). A dynamical systems analysis ofadolescent substance abuse. Multivariate BehavioralResearch, 33, 479-507. doi: 10.1207/s15327906mbr3304\ 3

Boker, S. M., Leibenluft, E., Deboeck, P. R., Virk, G., & Postolache,T. T. (2008). Mood oscillations and coupling between moodand weather in patients with rapid cycling bipolar disorder.International Journal of Child Health and HumanDevelopment, 1(2), 181-203.

48 / 51

Page 49: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Boker, S. M., Montpetit, M. A., Hunter, M. D., & Bergeman, C. S.(2010). Modeling resilience with differential equations. InP. C. M. Molenaar & K. Newell (Eds.), Individual pathwaysof change: Statistical models for analyzing learning anddevelopment (p. 183-206). Washington, D.C.: AmericanPsychological Association. doi: 10.1037/12140-011

Chow, S., Ram, N., Boker, S. M., Fujita, F., & Clore, G. (2005).Emotion as a thermostat: Representing emotion regulationusing a damped oscillator model. Emotion, 5, 208-225.

Chow, S.-M., Tang, N., Yuan, Y., Song, X., & Zhu, H. (2011).Bayesian estimation of semiparametric nonlinear dynamicfactor analysis models using the Dirichlet process prior.British Journal of Mathematical and Statistical Psychology,64(1), 69–106.

Chow, S.-M., & Zhang, G. (2013). Nonlinear regime-switchingstate-space (RSSS) models. Psychometrika, 78(4), 740–768.

Dolan, C. V. (2005). MKFM6: Multi-group, multi-subject stationarytime series modeling based on the kalman filter. Retrievedfrom http://users/fmg.uva.nl/cdolan/

49 / 51

Page 50: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Driver, C. C., Oud, J. H. L., & Voelkle, M. C. (2015). Continuoustime structural equation modelling with R package ctsem.Journal of Statistical Software.

Emotions and Dynamic Systems Laboratory. (2010). The affectivedynamics and individual differences (ADID) study:Developing non-stationary and network-based methods formodeling the perception and physiology of emotions.Unpublished manual.

Koopman, S. J., Shephard, N., & Doornik, J. A. (1999). Statisticalalgorithms for models in state space using SsfPack 2.2.Econometrics Journal, 2(1), 113-166. doi:10.1111/1368-423X.00023

Neale, M. C., Hunter, M. D., Pritikin, J. N., Zahery, M., Brick, T. R.,Kirkpatrick, R. M., . . . Boker, S. M. (in press). OpenMx2.0: Extended structural equation and statistical modeling.Psychometrika. doi: 10.1007/s11336-014-9435-8

Petrie, T. (1969). Probabilistic functions of finite state markovchains. Annals of Mathematical Statistics, 40, 97-115.

Petris, G. (2010). A R package for dynamic linear models. Journal ofStatistical Software, 36(12), 1-16.

50 / 51

Page 51: What's for dynr: A package for linear ... - modeling.uconn.edu...nonlinear DYNamic modeling in R Lu Ou1, Michael D. Hunter2, and Sy-Miin Chow1 Pennsylvania State University: Human

MakeDynr

Ou, Hunter,Chow

Why

Dynr Facts

ModelingFramework

Estimation

Example 1:Linear SDE

Example 2:NonlinearDiscrete-time

Example 3:Regime-switchingNonlinearODE

Take-homeMessage

References

Steele, J. S., & Ferrer, E. (2011). Latent differential equationmodeling of self-regulatory and coregulatory affectiveprocesses. Multivariate Behavioral Research, 46(6), 956-984.doi: 10.1080/00273171.2011.625305

Zentall, S. R., Boker, S. M., & Braungart-Rieker, J. M. (2006, June).Mother-infant synchrony: A dynamical systems approach. InProceedings of the Fifth International Conference onDevelopment and Learning.

51 / 51