Computing Autocorrelations in Single-Case Experimental Design#
The autocorr
function calculates autocorrelation values for different phases and overall in a single-case dataset. It helps in analyzing the dependency between observations across time.
Analyzing Temporal Dependencies with autocorr
#
The autocorr
function computes autocorrelations within each phase and across all phases in a dataset. This is useful for understanding whether measurements at one time point influence future values.
Required Arguments:#
data
: A Pandas DataFrame containing SCED data.
Optional Arguments:#
dvar
: The column name of the dependent variable (default"values"
).pvar
: The column name of the phase variable (default"phase"
).mvar
: The column name representing measurement time (default"mt"
).lag_max
: The maximum lag for which autocorrelations should be computed (default3
).
The function processes each case separately and computes autocorrelations within each phase and across all observations.
import scia as sc
📖 scia 1.101.0.dev6 - For Documentation, visit: https://ahsankhodami.github.io/scia/intro.html
Example 1: Computing Autocorrelations in a Dataset#
This example calculates autocorrelations up to a lag of 3 for each phase and overall.
import pandas as pd
import numpy as np
# Create a sample dataset
df = pd.DataFrame({
"case": ["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"],
"phase": ["Baseline", "Baseline", "Baseline", "Intervention", "Intervention",
"Baseline", "Baseline", "Intervention", "Intervention", "Intervention"],
"values": [5, 6, 7, 8, 10, 3, 4, 6, 8, 9],
"mt": [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
})
# Compute autocorrelations
df_ac = sc.autocorr(df, lag_max=3)
print(df_ac)
Case Phase Lag 1 Lag 2 Lag 3
0 A Baseline 0.000000 -0.500000 NaN
1 A Intervention -0.500000 NaN NaN
2 A all 0.335135 -0.072973 -0.345946
3 B Baseline -0.500000 NaN NaN
4 B Intervention -0.023810 -0.476190 NaN
5 B all 0.461538 -0.153846 -0.461538
Example 2: Computing Autocorrelations with a Higher Lag#
Setting lag_max=5
allows computing autocorrelations for more time points.
df_ac = sc.autocorr(df, lag_max=5)
print(df_ac)
Case Phase Lag 1 Lag 2 Lag 3 Lag 4 Lag 5
0 A Baseline 0.000000 -0.500000 NaN NaN NaN
1 A Intervention -0.500000 NaN NaN NaN NaN
2 A all 0.335135 -0.072973 -0.345946 -0.416216 NaN
3 B Baseline -0.500000 NaN NaN NaN NaN
4 B Intervention -0.023810 -0.476190 NaN NaN NaN
5 B all 0.461538 -0.153846 -0.461538 -0.346154 NaN