Latent group structures are common in panel data. Ignoring differences between groups can introduce bias, while estimating separate coefficients for every cross-sectional unit can be imprecise. Ignoring changes in slope coefficients over time can also lead to inconsistent estimates.
The package implements the pairwise adaptive group fused Lasso (PAGFL) of Mehrabani (2023) and FUSE-TIME of Haimerl et al. (2026). PAGFL estimates latent groups and their coefficients jointly. FUSE-TIME extends the method to smoothly time-varying coefficients.
The examples below show how to use both methods.
Stay up to date with the development version from GitHub:
# install.packages("devtools")
devtools::install_github("Paul-Haimerl/PAGFL")
library(PAGFL)The stable version (1.2.0) is available on CRAN:
install.packages("PAGFL")The PAGFL package includes a function that automatically
simulates a panel data set with a group structure in the slope
coefficients:
# Simulate a simple panel with three distinct groups and two exogenous explanatory variables
set.seed(1)
sim <- sim_DGP(N = 20, n_periods = 150, p = 2, n_groups = 3)
data <- sim$data\[y_{it} = \beta_i^{0 \prime} x_{it} + \gamma_i + u_{it}, \quad i = 1, \dots, N, \quad t = 1, \dots, T,\]
where \(y_{it}\) is a scalar dependent variable, \(x_{it}\) a \(p \times 1\) vector of explanatory variables, and \(\gamma_i\) reflects an individual fixed effect. The \(p\)-dimensional vector of slope coefficients \(\beta_i^0\) follows the (latent) group structure
\[\beta_{i}^0 = \sum_{k = 1}^K \alpha_k^0 \mathbb{1} \{i \in G_k \},\]
with \(\cup_{k = 1}^K G_k = \{1, \dots, N \}\), and \(G_k \cap G_j = \emptyset\) as well as \(\| \alpha_k^0 - \alpha_j^0 \| \neq 0\) for any \(k \neq j\), \(k,j = 1, \dots, K\) (see Mehrabani 2023, sec. 2).
sim_DGP() also nests, among others, all DGPs employed in
the simulation study of Mehrabani (2023,
sec. 6).
To execute the PAGFL procedure, pass the dependent and independent variables, the number of time periods, and a penalisation parameter \(\lambda\).
estim <- pagfl(y ~ ., data = data, n_periods = 150, lambda = 20, verbose = FALSE)
summary(estim)
#> Call:
#> pagfl(formula = y ~ ., data = data, n_periods = 150, lambda = 20,
#> verbose = FALSE)
#>
#> Balanced panel: N = 20, T = 150, obs = 3000
#>
#> Convergence reached:
#> TRUE (49 iterations)
#>
#> Information criterion:
#> IC lambda
#> 1.354 20.000
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.47230 -0.72086 -0.00120 0.76214 4.31838
#>
#> 2 groups:
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#> 1 1 2 1 1 1 1 2 1 1 2 2 2 1 1 1 1 1 2 1
#>
#> Coefficients:
#> X1 X2
#> Group 1 -0.36838 1.61275
#> Group 2 -0.49489 -1.23534
#>
#> Residual standard error: 1.15012 on 2978 degrees of freedom
#> Mean squared error: 1.31307
#> Multiple R-squared: 0.65845, Adjusted R-squared: 0.65605pagfl() returns an object of class pagfl,
which holds
model: A data.frame containing the
dependent and explanatory variables as well as individual and time
indices (if provided).coefficients: A \(K \times
p\) matrix of the post-Lasso group-specific parameter
estimates.groups: A list containing (i) the total
number of groups \(\hat{K}\) and (ii) a
vector of estimated group memberships \((\hat{g}_1, \dots, \hat{g}_N)\), where
\(\hat{g}_i = k\) if \(i\) is assigned to group \(k\).residuals: A vector of residuals of the demeaned
model.fitted: A vector of fitted values of the demeaned
model.args: A list of additional arguments.IC: A list containing (i) the value of the
IC, (ii) the employed tuning parameter \(\lambda\), (iii) the mean squared error,
and (iv) lambda_grid, a table of the information criterion,
mean squared error and number of groups for each candidate penalty
value.convergence: A list containing (i) a
logical variable indicating if convergence was achieved and (ii) the
number of executed ADMM algorithm iterations.call: The function call.[!TIP]
pagflobjects support a variety of useful generic functions likesummary(),fitted(),resid(),df.residual(),formula(), andcoef().
estim_fit <- fitted(estim)[!IMPORTANT] Selecting a \(\lambda\) value a priori can be tricky. For instance, it seems like
lambda = 20is too high since the number of groups \(K\) is underestimated.
We suggest iterating over a comprehensive range of candidate values to trace out the correct model. To specify a suitable grid, create a logarithmic sequence ranging from a penalty parameter that induces complete slope heterogeneity to complete slope homogeneity (i.e., \(\widehat{K} = 1\)). The resulting \(\lambda\) grid vector can be passed in place of any specific value, and a BIC-type IC selects the best-fitting parameter.
You can also supply a data.frame with named variables
and use a formula to select them, as in lm(). Named
explanatory variables retain their names in the output.
colnames(data)[-1] <- c("a", "b")
lambda_set <- exp(log(10) * seq(log10(1e-4), log10(10), length.out = 10))
estim_set <- pagfl(y ~ a + b, data = data, n_periods = 150, lambda = lambda_set, verbose = FALSE)
summary(estim_set)
#> Call:
#> pagfl(formula = y ~ a + b, data = data, n_periods = 150, lambda = lambda_set,
#> verbose = FALSE)
#>
#> Balanced panel: N = 20, T = 150, obs = 3000
#>
#> Convergence reached:
#> TRUE (48 iterations)
#>
#> Information criterion:
#> IC lambda
#> 1.12877 0.21544
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -3.47858 -0.66283 -0.02688 0.72880 3.77812
#>
#> 3 groups:
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#> 1 1 2 3 1 3 3 2 3 3 2 2 2 1 1 1 3 1 2 3
#>
#> Coefficients:
#> a b
#> Group 1 -0.95114 1.61719
#> Group 2 -0.49489 -1.23534
#> Group 3 0.24172 1.61613
#>
#> Residual standard error: 1.03695 on 2978 degrees of freedom
#> Mean squared error: 1.06738
#> Multiple R-squared: 0.72236, Adjusted R-squared: 0.7204By default, pagfl() uses penalised least squares
(PLS, method = "PLS"), which requires weakly
exogenous regressors (Mehrabani, 2023,
sec. 2.2). For endogenous regressors, use penalised generalised method
of moments (PGMM) with exogenous instruments \(Z\).
The next example uses an endogenous panel and PGMM. For dynamic panels, we recommend the split-panel jackknife bias correction of Dhaene and Jochmans (2015).
# Generate a panel where the predictors X correlate with the cross-sectional innovation,
# but can be instrumented with q = 2 variables in Z. Furthermore, include GARCH(1,1)
# innovations and specific group sizes
sim_endo <- sim_DGP(
N = 20, n_periods = 200, p = 2, n_groups = 3, group_proportions = c(0.3, 0.3, 0.4),
error_spec = "GARCH", q = 2
)
data_endo <- sim_endo$data
Z <- sim_endo$Z
# Note that the method PGMM and the instrument matrix Z need to be passed
estim_endo <- pagfl(y ~ ., data = data_endo, n_periods = 200, lambda = 2, method = "PGMM", Z = Z, bias_correc = TRUE, max_iter = 50e3, verbose = FALSE)
summary(estim_endo)
#> Call:
#> pagfl(formula = y ~ ., data = data_endo, n_periods = 200, lambda = 2,
#> method = "PGMM", Z = Z, bias_correc = TRUE, max_iter = 50000,
#> verbose = FALSE)
#>
#> Balanced panel: N = 20, T = 200, obs = 3980
#>
#> Convergence reached:
#> TRUE (14632 iterations)
#>
#> Information criterion:
#> IC lambda
#> 1.97129 2.00000
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.87011 -0.90055 0.01193 0.90767 5.54203
#>
#> 3 groups:
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#> 1 2 3 3 3 2 2 3 1 3 2 2 1 1 2 1 3 1 3 3
#>
#> Coefficients:
#> X1 X2
#> Group 1 0.55337 -1.22836
#> Group 2 -0.88484 -0.89231
#> Group 3 1.60547 -1.43718
#>
#> Residual standard error: 1.38812 on 3958 degrees of freedom
#> Mean squared error: 1.91621
#> Multiple R-squared: 0.87079, Adjusted R-squared: 0.8701[!TIP] See
?pagflfor the minimum group size and other settings.
The functions sim_tv_DGP() and fuse_time()
simulate and estimate panels with smoothly time-varying coefficients
\(\beta_i (t/T)\). As in Haimerl et
al. (2026), the coefficients follow the group structure
\[\beta_{i} (t/T) = \sum_{k = 1}^K \alpha_k (t/T) \mathbb{1} \{i \in G_k \}.\]
FUSE-TIME uses polynomial B-splines and a fusion penalty to obtain a penalised sieve estimator (PSE) (Haimerl et al., 2026, sec. 2.2, eqs. 2.4–2.6). The package returns post-Lasso coefficient estimates obtained by refitting the estimated groups (sec. 2.2).
The argument M counts interior knots, denoted by \(M^*\) in the paper; the basis has \(M+d+1\) functions. The default knot
heuristic follows Section 4.1, and the information criterion follows
Section 3.3, equation (3.1). Dynamic models can retain Nickell bias; the
package does not apply a time-varying bias correction (Section 3.2).
# Simulate a time-varying panel with a trend and a group pattern
N <- 20
n_periods <- 100
tv_sim <- sim_tv_DGP(N = N, n_periods = n_periods, sd_error = 1, intercept = TRUE, p = 1)
tv_data <- tv_sim$data
tv_estim <- fuse_time(y ~ 1, data = tv_data, n_periods = n_periods, lambda = 5, verbose = FALSE)
summary(tv_estim)
#> Call:
#> fuse_time(formula = y ~ 1, data = tv_data, n_periods = n_periods,
#> lambda = 5, verbose = FALSE)
#>
#> Balanced panel: N = 20, T = 100, obs = 2000
#>
#> Convergence reached:
#> TRUE (212 iterations)
#>
#> Information criterion:
#> IC lambda
#> 0.16648 5.00000
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -3.57761 -0.68826 0.00820 0.70118 3.40708
#>
#> 3 groups:
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#> 1 1 1 2 2 2 1 3 3 3 2 2 3 1 3 1 2 3 2 3
#>
#> Residual standard error: 1.02901 on 1974 degrees of freedom
#> Mean squared error: 1.04509
#> Multiple R-squared: 0.74213, Adjusted R-squared: 0.73886
fuse_time() returns an object of class
fusetime, which contains
model: A data.frame containing the
dependent and explanatory variables as well as individual and time
indices (if provided).coefficients: A list holding (i) a \(T \times p^{(1)} \times \hat{K}\) array of
the post-Lasso group-specific functional coefficients and (ii) a \(\hat{K} \times p^{(2)}\) matrix of
time-constant parameter estimates (when running a mixed time-varying
panel data model).groups: A list containing (i) the total
number of groups \(\hat{K}\) and (ii) a
vector of estimated group memberships \((\hat{g}_1, \dots, \hat{g}_N)\), where
\(\hat{g}_i = k\) if \(i\) is assigned to group \(k\).residuals: A vector of residuals of the demeaned
model.fitted: A vector of fitted values on the original
outcome scale, including the unit means removed by the within
transformation.args: A list of additional arguments.IC: A list containing (i) the value of the
IC, (ii) the employed tuning parameter \(\lambda\), (iii) the mean squared error,
and (iv) lambda_grid, a table of the information criterion,
mean squared error and number of groups for each candidate penalty
value.convergence: A list containing (i) a
logical variable indicating if convergence was achieved and (ii) the
number of executed ADMM algorithm iterations.call: The function call.[!TIP]
fusetimeobjects supportsummary(),fitted(),resid(),df.residual(),formula(), andcoef().
Unbalanced panels are common in applications. FUSE-TIME accommodates missing observations through spline interpolation and pooling within groups (Haimerl et al., 2026, Supplement S4.3). Identification still requires sufficient information in the observed design. Unbalanced panels require variables identifying the unit and time period of each observation; group-specific curves are reported only between each group’s earliest and latest observations.
Let’s delete 30% of observations, add indicator variables, and run
fuse_time() again.
# Draw some observations to be omitted
delete_index <- as.logical(rbinom(n = N * n_periods, prob = 0.7, size = 1))
# Construct cross-sectional and time indicator variables
tv_data$i_index <- rep(1:N, each = n_periods)
tv_data$t_index <- rep(1:n_periods, N)
# Delete some observations
tv_data <- tv_data[delete_index, ]
# Apply FUSE-TIME to an unbalanced panel
tv_estim_unbalanced <- fuse_time(y ~ 1, data = tv_data, index = c("i_index", "t_index"), lambda = 5, verbose = FALSE)
summary(tv_estim_unbalanced)
#> Call:
#> fuse_time(formula = y ~ 1, data = tv_data, index = c("i_index",
#> "t_index"), lambda = 5, verbose = FALSE)
#>
#> Unbalanced panel: N = 20, T = 64-75, obs = 1379
#>
#> Convergence reached:
#> TRUE (950 iterations)
#>
#> Information criterion:
#> IC lambda
#> 0.18921 5.00000
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -3.43491 -0.69055 -0.00812 0.68488 3.63894
#>
#> 3 groups:
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#> 1 1 1 2 2 2 1 2 3 3 2 2 3 1 3 1 2 3 2 2
#>
#> Residual standard error: 1.04387 on 1353 degrees of freedom
#> Mean squared error: 1.06912
#> Multiple R-squared: 0.73683, Adjusted R-squared: 0.73197
[!TIP] See
?fuse_timefor spline settings and models combining time-varying and time-constant coefficients.
Not every regressor needs a group-specific coefficient. Variables
named in global_coef receive a coefficient common to all
units, and variables named in ind_coef a coefficient
specific to each unit. Neither enters the fusion penalty, so the groups
are estimated from the remaining regressors, of which at least one is
required. All four estimators accept both arguments
(pagfl() and grouped_plm() only with
method = "PLS"). In fuse_time() and
grouped_tv_plm(), also naming a variable in
const_coef makes its coefficient time-constant.
# x1: group-specific, x2: common to all units, x3: unit-specific
fuse_time(y ~ x1 + x2 + x3, data = df, n_periods = 100, lambda = 5, global_coef = "x2", ind_coef = "x3")If group membership is known, grouped_plm() and
grouped_tv_plm() estimate static and time-varying panel
models using those groups.
groups <- sim$groups
estim_oracle <- grouped_plm(y ~ ., data = data, n_periods = 150, groups = groups, method = "PLS")
summary(estim_oracle)
#> Call:
#> grouped_plm(formula = y ~ ., data = data, groups = groups, n_periods = 150,
#> method = "PLS")
#>
#> Balanced panel: N = 20, T = 150, obs = 3000
#>
#> Information criterion: 1.12877
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -3.47858 -0.66283 -0.02688 0.72880 3.77812
#>
#> 3 groups:
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#> 1 1 2 3 1 3 3 2 3 3 2 2 2 1 1 1 3 1 2 3
#>
#> Coefficients:
#> a b
#> Group 1 -0.95114 1.61719
#> Group 2 -0.49489 -1.23534
#> Group 3 0.24172 1.61613
#>
#> Residual standard error: 1.03695 on 2978 degrees of freedom
#> Mean squared error: 1.06738
#> Multiple R-squared: 0.72236, Adjusted R-squared: 0.7204grouped_plm() and grouped_tv_plm() estimate
coefficients for the supplied groups; pagfl() and
fuse_time() estimate the groups as well. The known-group
estimators therefore omit the fusion penalty and its tuning and
convergence arguments, but grouped_tv_plm() retains the
smoothness penalty described next.
A spline basis with many knots can overfit the coefficient curves.
The argument lambda_smooth penalises their curvature and
pulls them towards straight lines as it grows (requires
d >= 2). It is available in
grouped_tv_plm() and fuse_time();
scale_smooth = TRUE makes the penalty independent of the
regressors’ units. Below, the time-varying panel from above is fitted
with known groups and M = 20 interior knots, first without
and then with the penalty.
tv_data <- tv_sim$data
# Without a smoothness penalty
tv_rough <- grouped_tv_plm(y ~ 1, data = tv_data, n_periods = n_periods, groups = tv_sim$groups, M = 20, verbose = FALSE)
summary(tv_rough)
# With a smoothness penalty
tv_smooth <- grouped_tv_plm(y ~ 1, data = tv_data, n_periods = n_periods, groups = tv_sim$groups, M = 20, lambda_smooth = 0.005, verbose = FALSE)
summary(tv_smooth)
Dhaene, G., & Jochmans, K. (2015). Split-panel jackknife estimation of fixed-effect models. The Review of Economic Studies, 82(3), 991-1030. DOI: doi.org/10.1093/restud/rdv007
Haimerl, P., Smeekes, S., & Wilms, I. (2026). Estimation of latent group structures in time-varying panel data models. The Econometrics Journal. DOI: doi.org/10.1093/ectj/utag034
Mehrabani, A. (2023). Estimation and identification of latent group structures in panel data. Journal of Econometrics, 235(2), 1464-1482. DOI: doi.org/10.1016/j.jeconom.2022.12.002