Longitudinal data can be viewed as a special case of the multilevel data where time is nested within individual participants. All longitudinal data share at least three features: (1) the same entities are repeatedly observed over time; (2) the same measurements (including parallel tests) are used; and (3) the timing for each measurement is known (Baltes & Nesselroade, 1979). To study phenomena in their time-related patterns of constancy and change is a primary reason for collecting longitudinal data. Figure 1 show the plot of 50 participants from the ACTIVE study on the variable EPT for 6 times. Clearly, each line represents a participant. From it, we can see how an individual changes over time.
1. A plot of longitudinal data
Growth curve model
Growth curve models (GCM; e.g., McArdle \& Nesselroade, 2003; Meredith & Tisak, 1990) exemplify a widely used technique with a direct match to the objectives of longitudinal research described by Baltes and Nesselroade (1979) to analyze explicitly intra-individual change and inter-individual differences in change. In the past decades, growth curve models have evolved from fitting a single curve for only one individual to fitting multilevel or mixed-effects models and from linear to nonlinear models (e.g., McArdle, 2001; McArdle & Nesselroade, 2003; Meredith & Tisak, 1990; Tucker, 1958; Wishart, 1938).
A typical linear growth curve model can be written as
where $y_{it}$ is data for participant $i$ at time $t$. For each individual $i$, a linear regression model can be fitted with its own intercept $\beta_{0i}$ and slope $\beta_{1i}$. On average, there is an intercept $\gamma_{0}$ and slope $\gamma_{1}$ for all individuals. The variation of $\beta_{0i}$ and $\beta_{1i}$ represents individual differences.
Individual difference can be further explained by other factors, for example, education level and age. Then the model is
A GCM can first be fitted as a multilevel model or mixed-effects model using the python library statsmodels.
To use the library, we would need to rewrite the growth curve model as a mixed-effect model, like in multilevel regression. For the model without second level predictor, we have
For demonstration, we investigate the growth of word set test (ws in the ACTIVE data set). In the current data set, we have the data in wide format, in which the 6 measures of ws are 6 variables. To use the python library, long format data are needed. For the long-format data, we need to stack the data from all waves into a long variable. The python code below reformats the data and plot them.
Unconditional model (model without second level predictors)
Fitting the model is actually straightforward once rewriting the model in the mixed-effects format as in Equation (1).
\begin{equation*} y_{it}=\gamma_{0}+v_{i0}+\gamma_{1}*time_{it}+v_{1i}*time_{it}+e_{it}. \end{equation*}
The input and output are given below. Based on the output, the fixed effects for time (.214, t-value=11.59) is significant, therefore, there is a linear growth trend. The average intercept is 11.93 and is also significant.
>>> import numpy as np
>>> ## data
>>> import pandas as pd
>>> active = pd.read_csv('https://advstats.psychstat.org/data/active.full.csv')
>>>
>>> ## reshape the data to long format
>>> df_long = pd.melt(active, id_vars=['edu'],
... value_vars=['ws1', 'ws2', 'ws3', 'ws4', 'ws5', 'ws6'],
... var_name='occ', value_name='score')
>>> df_long['time'] = np.repeat([1, 2, 3, 4, 5, 6], active.shape[0])
>>> df_long['id'] = np.tile(range(active.shape[0]), 6)
>>> df_long
edu occ score time id
0 13 ws1 4 1 0
1 12 ws1 11 1 1
2 13 ws1 7 1 2
3 12 ws1 16 1 3
4 13 ws1 9 1 4
... ... ... ... ... ...
6679 12 ws6 21 6 1109
6680 12 ws6 13 6 1110
6681 13 ws6 15 6 1111
6682 17 ws6 12 6 1112
6683 13 ws6 3 6 1113
[6684 rows x 5 columns]
>>>
>>> ## use the statsmodels libray
>>> import statsmodels.api as sm
>>> import statsmodels.formula.api as smf
>>>
>>> ## specify the model
>>> md = smf.mixedlm("score ~ time",
... data=df_long, groups=df_long["id"],
... re_formula="~1 + time")
>>>
>>> ## fit the model
>>> results = md.fit()
>>>
>>> ## display the results
>>> print(results.summary())
Mixed Linear Model Regression Results
===========================================================
Model: MixedLM Dependent Variable: score
No. Observations: 6684 Method: REML
No. Groups: 1114 Scale: 5.3609
Min. group size: 6 Log-Likelihood: -17044.1509
Max. group size: 6 Converged: Yes
Mean group size: 6.0
-----------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
-----------------------------------------------------------
Intercept 11.926 0.152 78.424 0.000 11.628 12.224
time 0.214 0.018 11.590 0.000 0.178 0.250
Group Var 21.115 0.527
Group x time Cov 0.181 0.043
time Var 0.074 0.008
===========================================================
It is useful to test whether random-effects parameters such as the variances of intercept and slope are significance or not to evaluate individual differences. This can be done by comparing the current model with a model without random intercept or slope. The method is likelihood ratio test.
For example, to test the individual differences in slope for time. The random effects for time is .07. Based on the difference in the likelihood, which follows a chi-squared distribution, it is significant with p-value about 0. Therefore, there is significant individual difference in the growth rate (slope). This indicates that everyone has a different change rate. Note that in the alternative model m2, the random effect for time was not used.
>>> import numpy as np
>>> ## data
>>> import pandas as pd
>>> active = pd.read_csv('https://advstats.psychstat.org/data/active.full.csv')
>>>
>>> ## reshape the data to long format
>>> df_long = pd.melt(active, id_vars=['edu'],
... value_vars=['ws1', 'ws2', 'ws3', 'ws4', 'ws5', 'ws6'],
... var_name='occ', value_name='score')
>>> df_long['time'] = np.repeat([1, 2, 3, 4, 5, 6], active.shape[0])
>>> df_long['id'] = np.tile(range(active.shape[0]), 6)
>>> #df_long
>>>
>>> ## use the statsmodels libray
>>> import statsmodels.api as sm
>>> import statsmodels.formula.api as smf
>>>
>>> ## specify the model with both covariance
>>> m1 = smf.mixedlm("score ~ time",
... data=df_long, groups=df_long["id"],
... re_formula="~1 + time")
>>>
>>> ## fit the model
>>> results1 = m1.fit()
>>>
>>> ## display the results
>>> print(results1.summary())
Mixed Linear Model Regression Results
===========================================================
Model: MixedLM Dependent Variable: score
No. Observations: 6684 Method: REML
No. Groups: 1114 Scale: 5.3609
Min. group size: 6 Log-Likelihood: -17044.1509
Max. group size: 6 Converged: Yes
Mean group size: 6.0
-----------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
-----------------------------------------------------------
Intercept 11.926 0.152 78.424 0.000 11.628 12.224
time 0.214 0.018 11.590 0.000 0.178 0.250
Group Var 21.115 0.527
Group x time Cov 0.181 0.043
time Var 0.074 0.008
===========================================================
>>>
>>> ## specify an alternative model
>>> m2 = smf.mixedlm("score ~ time",
... data=df_long, groups=df_long["id"],
... re_formula="~1")
>>>
>>> ## fit the model
>>> results2 = m2.fit()
>>>
>>> ## display the results
>>> print(results2.summary())
Mixed Linear Model Regression Results
=========================================================
Model: MixedLM Dependent Variable: score
No. Observations: 6684 Method: REML
No. Groups: 1114 Scale: 5.6185
Min. group size: 6 Log-Likelihood: -17066.7246
Max. group size: 6 Converged: Yes
Mean group size: 6.0
----------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
----------------------------------------------------------
Intercept 11.926 0.159 75.075 0.000 11.615 12.237
time 0.214 0.017 12.609 0.000 0.181 0.247
Group Var 23.241 0.474
=========================================================
>>>
>>> ## difference in the likilihood
>>> delta_ll = -2*(results2.llf - results1.llf)
>>> delta_ll
np.float64(45.14731834300619)
>>>
>>> delta_df = 2
>>>
>>> ## get the p-value
>>> from scipy.stats import chi2
>>> 1 - chi2.cdf(delta_ll, delta_df)
np.float64(1.5717538381920804e-10)
To test the individual differences in intercept. The random effects for intercept is 21.11. Based on ANOVA analysis below, it is significant. Therefore, there is individual difference or individuals have different intercepts.
>>> import numpy as np
>>> ## data
>>> import pandas as pd
>>> active = pd.read_csv('https://advstats.psychstat.org/data/active.full.csv')
>>>
>>> ## reshape the data to long format
>>> df_long = pd.melt(active, id_vars=['edu'],
... value_vars=['ws1', 'ws2', 'ws3', 'ws4', 'ws5', 'ws6'],
... var_name='occ', value_name='score')
>>> df_long['time'] = np.repeat([1, 2, 3, 4, 5, 6], active.shape[0])
>>> df_long['id'] = np.tile(range(active.shape[0]), 6)
>>> #df_long
>>>
>>> ## use the statsmodels libray
>>> import statsmodels.api as sm
>>> import statsmodels.formula.api as smf
>>>
>>> ## specify the model with both covariance
>>> m1 = smf.mixedlm("score ~ time",
... data=df_long, groups=df_long["id"],
... re_formula="~1 + time")
>>>
>>> ## fit the model
>>> results1 = m1.fit()
>>>
>>> ## display the results
>>> print(results1.summary())
Mixed Linear Model Regression Results
===========================================================
Model: MixedLM Dependent Variable: score
No. Observations: 6684 Method: REML
No. Groups: 1114 Scale: 5.3609
Min. group size: 6 Log-Likelihood: -17044.1509
Max. group size: 6 Converged: Yes
Mean group size: 6.0
-----------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
-----------------------------------------------------------
Intercept 11.926 0.152 78.424 0.000 11.628 12.224
time 0.214 0.018 11.590 0.000 0.178 0.250
Group Var 21.115 0.527
Group x time Cov 0.181 0.043
time Var 0.074 0.008
===========================================================
>>>
>>> ## specify an alternative model
>>> m2 = smf.mixedlm("score ~ time",
... data=df_long, groups=df_long["id"],
... re_formula="~ 0 + time")
>>>
>>> ## fit the model
>>> results2 = m2.fit()
>>>
>>> ## display the results
>>> print(results2.summary())
Mixed Linear Model Regression Results
=========================================================
Model: MixedLM Dependent Variable: score
No. Observations: 6684 Method: REML
No. Groups: 1114 Scale: 10.2300
Min. group size: 6 Log-Likelihood: -18639.0075
Max. group size: 6 Converged: Yes
Mean group size: 6.0
---------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
---------------------------------------------------------
Intercept 11.926 0.089 133.680 0.000 11.751 12.101
time 0.214 0.040 5.306 0.000 0.135 0.293
time Var 1.228 0.019
=========================================================
>>>
>>> ## difference in the likilihood
>>> delta_ll = -2*(results2.llf - results1.llf)
>>> delta_ll
np.float64(3189.7131050783937)
>>>
>>> delta_df = 2
>>>
>>> ## get the p-value
>>> from scipy.stats import chi2
>>> 1 - chi2.cdf(delta_ll, delta_df)
np.float64(0.0)
Conditional model (model with second level predictors)
Using the same set of data, we now investigate whether education is a predictor of random intercept and slope. The model is given below:
\begin{equation} y_{it}=\gamma_{0}+\gamma_{1}*edu+v_{0i}+\gamma_{2}*time_{it} +\gamma_{3}*edu_{i}*time_{it}+v_{1i}*time_{it}+e_{it}.\end{equation}
Given there is individual differences in intercept and slope, we want to explain why. So, we use Edu as a explanatory variable. From the output, we can see that the parameter \(\gamma_{1} = .78\) is significant. Higher education relates to bigger intercept. In addition, the parameter $\gamma_{3} = -.022$ is significant. Higher education relates to lower growth rate of ws.
>>> import numpy as np
>>> ## data
>>> import pandas as pd
>>> active = pd.read_csv('https://advstats.psychstat.org/data/active.full.csv')
>>>
>>> ## reshape the data to long format
>>> df_long = pd.melt(active, id_vars=['edu'],
... value_vars=['ws1', 'ws2', 'ws3', 'ws4', 'ws5', 'ws6'],
... var_name='occ', value_name='score')
>>> df_long['time'] = np.repeat([1, 2, 3, 4, 5, 6], active.shape[0])
>>> df_long['id'] = np.tile(range(active.shape[0]), 6)
>>> df_long
edu occ score time id
0 13 ws1 4 1 0
1 12 ws1 11 1 1
2 13 ws1 7 1 2
3 12 ws1 16 1 3
4 13 ws1 9 1 4
... ... ... ... ... ...
6679 12 ws6 21 6 1109
6680 12 ws6 13 6 1110
6681 13 ws6 15 6 1111
6682 17 ws6 12 6 1112
6683 13 ws6 3 6 1113
[6684 rows x 5 columns]
>>>
>>> ## use the statsmodels libray
>>> import statsmodels.api as sm
>>> import statsmodels.formula.api as smf
>>>
>>> ## specify the model
>>> md = smf.mixedlm("score ~ time + edu + time*edu",
... data=df_long, groups=df_long["id"],
... re_formula="~1 + time")
>>>
>>> ## fit the model
>>> results = md.fit()
>>>
>>> ## display the results
>>> print(results.summary())
Mixed Linear Model Regression Results
===========================================================
Model: MixedLM Dependent Variable: score
No. Observations: 6684 Method: REML
No. Groups: 1114 Scale: 5.5542
Min. group size: 6 Log-Likelihood: -16960.1403
Max. group size: 6 Converged: Yes
Mean group size: 6.0
-----------------------------------------------------------
Coef. Std.Err. z P>|z| [0.025 0.975]
-----------------------------------------------------------
Intercept 1.240 0.741 1.674 0.094 -0.212 2.692
time 0.528 0.093 5.676 0.000 0.345 0.710
edu 0.778 0.053 14.679 0.000 0.674 0.882
time:edu -0.023 0.007 -3.433 0.001 -0.036 -0.010
Group Var 16.316 0.416
Group x time Cov 0.497 0.032
time Var 0.015 0.006
===========================================================
GCM as a SEM
In addition to estimating a GCM as a multilevel or mixed-effects model, we can also estimate it as a SEM. To illustrate this, we consider a linear growth curve model. If a group of participants all have linear change trend, for each individual, we can fit a regression model such as \[ y_{it}=\beta_{i0}+\beta_{i1}t+e_{it} \] where $\beta_{i0}$ and $\beta_{i1}$ are intercept and slope, respectively. Note that here we let $time_{it} = t$. If each individual has different time of data collection, it can still be done in the SEM framework but would be more complex. By writing the time out, we would have
\[ \left(\begin{array}{c} y_{i1}\\ y_{i2}\\ \vdots\\ y_{iT} \end{array}\right)=\left(\begin{array}{cc} 1 & 1\\ 1 & 2\\ 1 & \vdots\\ 1 & T \end{array}\right)\left(\begin{array}{c} \beta_{i0}\\ \beta_{i1} \end{array}\right)+\left(\begin{array}{c} e_{i1}\\ e_{i2}\\ \vdots\\ e_{iT} \end{array}\right) \]
Note that the above equation resembles a factor model with two factors - $b_{0}$ and $b_{1}$ and a factor loading matrix with known factor loading matrix. The individual intercept and slope can be viewed as factor scores to be estimated. Furthermore, we are interested in model with mean structure because the means of $\beta_{0}$ and $\beta_{1}$ have their meaning as average intercept and slope (rate of change). The variances of the factors can be estimated - they indicate the variations of intercept and slope. Using path diagram, the model is shown in the figure below.
2. Path diagram for a growth curve model
With the model, we can estimate it using the semopy package. Unlike the statsmodels package, in using SEM, the wide format of data is directly used. The python input and output for the unconditional model is given below.
Using this method, each parameter in the model can be directly tested using a z-test. In addition, we can use the fit statistics for SEM to test the fit of the growth curve model. Note the fit statistics from semopy need to be evaluated first before using.
Note that to specify the mean structure, we use ~1 to indicate the mean of the factor. For example, to specify the mean for the level factor, we use level ~ 1. At the time of writing (Nov 30, 2024), there is bug with the starting values for the latent factor means. Therefore, we need to specify the starting values for the latent factor means. To do it, we use code like below
level ~ a*1
slope ~ b*1
START(12.0) a
START(0.16) b
To cite the book, use:
Zhang, Z. & Wang, L. (2017-2025). Advanced statistics using R. Granger, IN: ISDSA Press. https://doi.org/10.35566/advstats. ISBN: 978-1-946728-01-2. To take the full advantage of the book such as running analysis within your web browser, please subscribe.