Type: Package
Title: Interactive Fixed Effects Estimator for Balanced Panel Data
Version: 0.1.3
Date: 2026-04-21
Description: Implements the interactive fixed effects ('IFE') panel estimator of Bai (2009) <doi:10.3982/ECTA6135> with analytical standard errors ('homoskedastic', 'HC1' robust, and cluster-robust by unit). Supports asymptotic bias correction for large panels (Bai 2009) and a dynamic extension for predetermined regressors (Moon and Weidner 2017 <doi:10.1017/S0266466615000328>). Includes information-criterion-based factor number selection (Bai and Ng 2002 <doi:10.1111/1468-0262.00273>). All computations use base R only with no external dependencies.
License: GPL-2 | GPL-3
Encoding: UTF-8
Language: en-US
LazyData: true
RoxygenNote: 7.3.3
Depends: R (≥ 3.5.0)
Imports: stats
Suggests: testthat (≥ 3.0.0), knitr, rmarkdown
VignetteBuilder: knitr
URL: https://github.com/Rickchen0910/xtife
BugReports: https://github.com/Rickchen0910/xtife/issues
NeedsCompilation: no
Packaged: 2026-04-21 11:35:09 UTC; apple
Author: Binzhi Chen [aut, cre]
Maintainer: Binzhi Chen <Binzhi.Chen9@gmail.com>
Repository: CRAN
Date/Publication: 2026-04-21 21:10:02 UTC

Compute Bias-Corrected IFE Coefficients (Bai 2009)

Description

Applies the two-term asymptotic bias correction from Bai (2009) Theorems 7.1 and 7.2 to the raw IFE coefficient vector:

\hat{\beta}^\dagger = \hat{\beta} - \hat{B}/N - \hat{C}/T

where \hat{B} corrects for cross-sectional heteroskedasticity (Equation 17) and \hat{C} corrects for time-varying heteroskedasticity (Equation 19). Both terms require T/N^2 \to 0 and N/T^2 \to 0 respectively (Theorem 7.2). For panels of the scale used in the package examples (N \approx 50, T \approx 30) both conditions hold approximately.

Usage

.bias_correct(beta, F_hat, Lambda_hat, X_dm_arr, X_tilde, e_mat, N, TT, p, r)

Arguments

beta

p-vector of uncorrected IFE coefficients

F_hat

T x r factor matrix (F'F/T = I_r enforced)

Lambda_hat

N x r loading matrix

X_dm_arr

T x N x p array of demeaned covariates (after additive FE)

X_tilde

T x N x p array of factor-projected demeaned X

e_mat

T x N matrix of full-model residuals (from .ife_fit)

N, TT, p, r

panel dimensions

Value

list with elements: beta_bc p-vector: bias-corrected coefficients B_hat p-vector: estimated cross-section bias term (pre-scaling by 1/N) C_hat p-vector: estimated time-heteroskedasticity bias term (pre-scaling by 1/T)


Compute Bias-Corrected Coefficients for the Dynamic IFE Estimator

Description

Applies the three-term asymptotic bias correction from Moon and Weidner (2017) Corollary 4.5 to the raw dynamic IFE coefficient vector:

\hat{\beta}^* = \hat{\beta} + W^{-1}\!\left(\hat{B}_1/T + \hat{B}_2/N + \hat{B}_3/T\right)

where \hat{B}_1 corrects for the Nickell-type bias arising from predetermined (lagged) regressors using a lag-truncation bandwidth M1, and \hat{B}_2, \hat{B}_3 correct for cross-sectional and time-series heteroskedasticity respectively. The latter two terms are algebraically equivalent to the Bai (2009) \hat{B} and \hat{C} terms.

Usage

.bias_correct_mw(
  beta,
  F_hat,
  Lambda_hat,
  X_dm_arr,
  X_tilde,
  e_mat,
  N,
  TT,
  p,
  r,
  M1 = 1L
)

Arguments

beta

p-vector of uncorrected IFE coefficients

F_hat

T x r factor matrix

Lambda_hat

N x r loading matrix

X_dm_arr

T x N x p array of demeaned covariates (after additive FE)

X_tilde

T x N x p double-projected covariates (M_Lambda M_F applied)

e_mat

T x N matrix of full-model residuals

N, TT, p, r

panel dimensions

M1

lag bandwidth for B1 (number of lags to include; default 1)

Value

list: beta_bc p-vector: bias-corrected coefficients B1_hat p-vector: dynamic bias term (pre-multiplied by D0_inv; scaled by 1/(NT)) B2_hat p-vector: cross-section heteroscedasticity bias (same as Bai B_hat) B3_hat p-vector: time heteroscedasticity bias (same as Bai C_hat)


Compute Information Criteria for a Given Number of Factors (Internal)

Description

Evaluates five information criteria for a fitted IFE model with r factors, given the mean squared residual V_r. Returns IC1, IC2, and IC3 from Bai and Ng (2002) Proposition 1 (ICp1/ICp2/ICp3), applied to IFE residuals as suggested by Bai (2009) Section 9.4, plus a BIC-style criterion (IC_bic) and a small-sample-corrected prediction criterion (PC) as implemented in the fect package (C++ source, lines 196–224). Called once per candidate r inside ife_select_r().

Usage

.compute_ic(V_r, r, N, TT, p, force)

Arguments

V_r

scalar: mean squared residual = mean(u_mat^2) (not df-adjusted)

r

integer: number of factors

N, TT

panel dimensions

p

number of covariates

force

additive FE spec (for np calculation)

Value

named list: IC1, IC2, IC3 (Bai & Ng 2002), IC_bic and PC (Bai 2009/fect)


Demean Panel Matrices for Additive Fixed Effects

Description

Removes additive unit and/or time fixed effects from a T x N outcome matrix and a T x N x p covariate array using the within-group transformation. Supports four specifications: no demeaning ("none"), unit-only ("unit"), time-only ("time"), and two-way ("two-way").

Usage

.demean_panel(Y_mat, X_arr, force)

Arguments

Y_mat

T x N outcome matrix

X_arr

T x N x p covariate array (NULL if p = 0)

force

character: "none" | "unit" | "time" | "two-way"

Value

list with: Y_dm T x N demeaned outcome X_dm T x N x p demeaned covariate array (or NULL) mu_Y grand mean of Y alpha_Y N x 1 unit means of Y (after grand mean removal) xi_Y T x 1 time means of Y (after grand mean removal)


Core IFE Estimation via SVD-Based Alternating Projections

Description

Estimates the Interactive Fixed Effects model by iterating between an OLS step for the regression coefficients (given current factor estimates) and an SVD step for the factor matrix (given current coefficients). Convergence is declared when the maximum absolute change in the coefficient vector falls below tol. Handles the degenerate case r = 0 (standard OLS on demeaned data) as a special case. Supports both the strictly-exogenous ("static", Bai 2009) and the predetermined-regressor ("dynamic", Moon and Weidner 2017) projection schemes.

Usage

.ife_fit(Y_dm, X_dm, r, tol = 1e-09, max_iter = 10000L, method = "static")

Arguments

Y_dm

T x N demeaned outcome

X_dm

T x N x p demeaned covariate array (or NULL if p = 0)

r

integer, number of factors (>= 0)

tol

convergence tolerance on max |beta_new - beta_old|

max_iter

maximum iterations

method

character: "static" (Bai 2009) or "dynamic" (Moon and Weidner 2017)

Value

list: beta p x 1 coefficient vector (numeric(0) if p = 0) F_hat T x r factor matrix (normalized F'F/T = I_r) Lambda_hat N x r loading matrix X_tilde T x N x p factor-projected covariates (for SE computation) e_mat T x N residual matrix E = Y_dm - X_dm * beta - F Lambda' n_iter number of iterations used converged logical


Compute Standard Errors for the IFE Estimator

Description

Constructs the sandwich variance-covariance matrix for the IFE coefficient vector using the Frisch-Waugh-Lovell principle. The effective regressors are the factor-projected covariates \tilde{X}_{it} (i.e., demeaned X after removing the factor space). Three estimators are supported: homoskedastic ("standard"), HC1 heteroskedasticity-robust ("robust"), and cluster-robust by unit ("cluster"), following Cameron, Gelbach and Miller (2011). Degrees of freedom account for regression coefficients, interactive FE parameters, and additive FE parameters.

Usage

.ife_se(beta, X_tilde, u_mat, N, TT, r, force, se_type)

Arguments

beta

p x 1 coefficient vector

X_tilde

T x N x p projected covariate array

u_mat

T x N residual matrix from the full model

N, TT

panel dimensions

r

number of factors

force

additive FE specification (for df computation)

se_type

"standard" | "robust" | "cluster"

Value

list: vcov_mat p x p estimated variance-covariance matrix df residual degrees of freedom


Dataset on US Cigarette Demand Panel

Description

Balanced panel of cigarette sales and prices across 46 US states for 30 years (1963–1992). Originally used in Baltagi (1995) and widely used as a benchmark dataset for panel estimators.

Usage

cigar

Format

A data frame with 1,380 rows and 9 variables:

state

US state identifier (integer, 1–46)

year

year (integer, 1963–1992)

price

cigarette price index

pop

state population

pop16

population aged 16 and over

cpi

consumer price index

ndi

per-capita disposable income

sales

per-capita cigarette sales (packs per person per year)

pimin

minimum cigarette price in adjoining states

Source

Baltagi, B.H. (1995) Econometric Analysis of Panel Data. Wiley. Distributed with the plm R package (Croissant and Millo 2008).

References

Baltagi, B.H. (1995). Econometric Analysis of Panel Data. Wiley.

Croissant, Y. and Millo, G. (2008). Panel data econometrics in R: the plm package. Journal of Statistical Software, 27(2), 1–43. doi:10.18637/jss.v027.i02


Estimate Interactive Fixed Effects Model (Bai 2009)

Description

Fits the panel model

y_{it} = \alpha_i + \xi_t + X_{it}'\beta + \lambda_i'F_t + u_{it}

for balanced panel data with analytical standard errors.

Usage

ife(
  formula,
  data,
  index,
  r = 1L,
  force = "two-way",
  se = "standard",
  bias_corr = FALSE,
  method = "static",
  M1 = 1L,
  tol = 1e-09,
  max_iter = 10000L
)

Arguments

formula

R formula: outcome ~ covariate1 + covariate2 + ...

data

data.frame in long format (one row per unit-time observation)

index

character(2): c("unit_id_column", "time_id_column")

r

integer >= 0, number of interactive factors (default 1)

force

additive FE specification: "none" | "unit" | "time" | "two-way" (default "two-way")

se

SE type: "standard" | "robust" | "cluster" (default "standard"; "cluster" clusters by unit id)

bias_corr

logical; if TRUE apply bias correction. For method = "static" uses the two-term Bai (2009) Sec. 7 correction (B/N + C/T). For method = "dynamic" uses the three-term Moon and Weidner (2017) correction (B1/T + B2/N + B3/T). Requires r > 0 and at least one covariate. (default FALSE)

method

"static" (default) for Bai (2009) strictly-exogenous regressors; "dynamic" for Moon and Weidner (2017) predetermined regressors (e.g. lagged dependent variable). The dynamic estimator uses double projection M_Lambda M_F on X in the SVD loop.

M1

integer; lag bandwidth for the B1 dynamic bias term (default 1L). Only used when method = "dynamic" and bias_corr = TRUE.

tol

convergence tolerance (default 1e-9)

max_iter

maximum iterations (default 10000L)

Value

An S3 object of class "ife" with the following components:

References

Bai, J. (2009). Panel data models with interactive fixed effects. Econometrica, 77(4), 1229–1279. doi:10.3982/ECTA6135

Moon, H.R. and Weidner, M. (2017). Dynamic linear panel regression models with interactive fixed effects. Econometric Theory, 33, 158–195. doi:10.1017/S0266466615000328

Bai, J. and Ng, S. (2002). Determining the number of factors in approximate factor models. Econometrica, 70(1), 191–221. doi:10.1111/1468-0262.00273

Examples

data(cigar, package = "xtife")
fit <- ife(sales ~ price, data = cigar, index = c("state", "year"),
           r = 2, force = "two-way", se = "standard")
print(fit)

Select the Number of Factors via Information Criteria

Description

Fits the IFE model for r = 0, 1, ..., r_max and evaluates five information criteria at each value of r. Returns IC1, IC2, and IC3 from Bai and Ng (2002) Proposition 1, applied to IFE residuals per Bai (2009) Section 9.4, plus a BIC-style penalty (IC_bic) and a small-sample-corrected prediction criterion (PC) from Bai (2009). The criterion-minimising r for each IC is flagged with "*" in the printed table, and a data-driven recommendation (favouring IC_bic when the Bai-Ng criteria decrease monotonically) is displayed.

Usage

ife_select_r(
  formula,
  data,
  index,
  r_max = NULL,
  force = "two-way",
  verbose = TRUE,
  tol = 1e-09,
  max_iter = 10000L
)

Arguments

formula

R formula passed to ife()

data

long-format data.frame

index

character(2): c("unit_id", "time_id")

r_max

maximum r to consider (default: min(8, floor(min(N,T)/2)))

force

additive FE type (default "two-way")

verbose

logical; if TRUE (default) print progress and results table to the console. Set to FALSE for silent operation.

tol

convergence tolerance (default 1e-9)

max_iter

maximum iterations (default 10000L)

Value

(invisibly) a data.frame with columns r, V_r, IC1, IC2, IC3, IC_bic, PC, converged, and attribute "suggested" (named integer vector giving the IC-minimising r for each criterion).

References

Bai, J. (2009). Panel data models with interactive fixed effects. Econometrica, 77(4), 1229–1279. doi:10.3982/ECTA6135

Bai, J. and Ng, S. (2002). Determining the number of factors in approximate factor models. Econometrica, 70(1), 191–221. doi:10.1111/1468-0262.00273

Examples


  data(cigar, package = "xtife")
  sel <- ife_select_r(sales ~ price, data = cigar,
                      index = c("state", "year"), r_max = 4)


Print an IFE Model Summary

Description

Prints a formatted summary of an object of class "ife", including panel dimensions, number of factors, additive fixed effect specification, SE type, and a coefficient table with standard errors, t-statistics, p-values, and 95% confidence intervals. If bias correction was applied, bias terms are also reported. Information criteria are printed when the object contains them (i.e., when called from ife_select_r()).

Usage

## S3 method for class 'ife'
print(x, digits = 4, ...)

Arguments

x

an object of class "ife"

digits

number of significant digits (default 4)

...

unused

Value

x invisibly.

Examples

data(cigar, package = "xtife")
fit <- ife(sales ~ price, data = cigar, index = c("state", "year"),
           r = 2, force = "two-way", se = "standard")
print(fit)