Package {BFM}


Type: Package
Title: Beta Factor Model
Version: 3.0.0
Author: Guangbao Guo [aut, cre], Jiahui Feng [aut]
Maintainer: Guangbao Guo <ggb11111111@163.com>
Description: Provides tools for factor analysis in financial and econometric settings under Beta factor models. It includes functions to simulate factor-model data with Beta-distributed idiosyncratic components (e.g., standard Beta, scaled Beta, and truncated Beta distributions) and to conduct model diagnostic assessments such as likelihood ratio tests for factor number selection and goodness-of-fit tests for Beta distribution assumptions. Estimation routines encompass maximum likelihood estimation for finite-dimensional Beta factor models, regularized Beta factor analysis for high-dimensional datasets, and shrinkage-based estimation for robust Beta factor loading recovery in noisy or incomplete data environments. The package's methodological framework is detailed in Guo G. (2023) <doi:10.1007/s00180-022-01270-z>.
License: MIT + file LICENSE
Encoding: UTF-8
Depends: R (≥ 3.5.0)
Suggests: testthat (≥ 3.0.0), spelling, betareg, zoib
NeedsCompilation: no
LazyData: true
Imports: MASS, psych, stats, caret, matrixcalc, ggplot2, tidyr
Language: en-US
Config/roxygen2/version: 8.1.1
Packaged: 2026-11-25 12:14:02 UTC; Administrator
Repository: CRAN
Date/Publication: 2026-11-30 12:30:02 UTC

California Alcohol Use Data

Description

A county-level monthly alcohol use dataset from California students (grades 7-11, 2008-2010). The response variable Percentage is a proportion (0 < Percentage < 1), suitable for zero-inflated beta regression.

Usage

AlcoholUse

Format

A data frame with multiple rows and variables:

Percentage

numeric: percentage of students who drank alcohol

Grade

factor: student grade level

Gender

factor: student gender

MedDays

numeric: mid-point of days bucket

Days

numeric: days bucket

County

factor: county identifier

A data frame with 44 rows and 4 variables:

accuracy

numeric: proportion of correct responses in a reading task

accuracy1

numeric: transformed accuracy measure

dyslexia

factor: dyslexia status (levels: "yes", "no")

iq

numeric: IQ score

Source

http://www.kidsdata.org Reading Skills Data

A dataset from Smithson and Verkuilen (2006) on reading accuracy, dyslexia status, and IQ scores. The response variable accuracy is a proportion (0 < accuracy < 1), suitable for beta regression.

Smithson, M. & Verkuilen, J. (2006). A better lemon squeezer? Maximum-likelihood regression with beta-distributed dependent variables. https://psycnet.apa.org/doi/10.1037/1082-989X.11.1.54

Examples

data(AlcoholUse)
str(AlcoholUse)

The BFM function is to generate Beta Factor Models data.

Description

The function supports various distribution types for generating the data.

Usage

BFM(n, p, m, mub, phib, distribution_type)

Arguments

n

Sample size.

p

Sample dimensionality.

m

Number of factors.

mub

Mean parameter for Beta distribution (numeric vector or scalar, 0 < mub < 1).

phib

Precision parameter for Beta distribution (positive numeric vector or scalar).

distribution_type

Type of Beta distribution.

Value

A list containing:

data

Generated BFM data matrix (n rows, p columns).

A

A matrix representing the factor loadings.

D

Diagonal matrix of unique variances.

kmo

Kaiser-Meyer-Olkin sampling adequacy measure.

bartlett

Bartlett's test of sphericity.

Examples

n <- 1000
p <- 10
m <- 5
mub <- runif(p, 0.2, 0.8)
phib <- runif(p, 5, 30)
dist_type <- "Elliptical Distribution"
X <- BFM(n, p, m, mub, phib, dist_type)


Distributed GCP for Dynamic Tensors (DGCP)

Description

Performs distributed Gaussian process dynamic CP decomposition adapted for BFM. Each node initializes via PCA, smooths the factor series with a Beta error constrained kernel, calibrates the loadings by Beta moment matching, and the coordinator aggregates the local estimates.

Usage

DGCP(X_list, m, tau = 1e-06, max_iter = 1, tol = 1e-06)

Arguments

X_list

A list of numeric data partition matrices X_{I_k}, each of dimension n_{I_k} × p.

m

An integer specifying the number of factors.

tau

A numeric regularization parameter for the local kernel matrix (default = 1e-6).

max_iter

An integer specifying the maximum number of iterations (default = 1). Set to a larger value to repeat the smoothing and calibration steps until convergence.

tol

A numeric value specifying the convergence tolerance (default = 1e-6).

Value

A list with the following components:

Abar

Global loading matrix, average of local calibrated loadings.

Fbar

Global dynamic factor matrix, average of local calibrated factors.

Ahat

List of local calibrated loading matrices per partition.

Fhat

List of local calibrated factor matrices per partition.

iters

Number of iterations completed per partition.

converged

Logical convergence indicator per partition.

Examples

set.seed(123)
X_list <- list(
  matrix(rnorm(500), nrow = 100, ncol = 5),
  matrix(rnorm(500), nrow = 100, ncol = 5)
)
res <- DGCP(X_list = X_list, m = 3)


Distributed Iterative Residual Correction Principle (DIRCP)

Description

Performs distributed iterative residual correction for BFM with Beta moment-matching correction on each data partition, followed by coordinator aggregation of the local estimates.

Usage

DIRCP(X_list, m, max_iter = 20, tol = 1e-06)

Arguments

X_list

A list of numeric data partition matrices X_{I_k}, each of dimension n_{I_k} \times p.

m

An integer specifying the number of factors.

max_iter

An integer specifying the maximum number of iterations (default = 20).

tol

A numeric value specifying the convergence tolerance (default = 1e-6).

Value

A list with the following components:

Abar

Global loading matrix, average of local corrected loadings.

Fbar

Global factor matrix, average of local corrected factors.

Ahat

List of local corrected loading matrices per partition.

Fhat

List of local corrected factor matrices per partition.

iters

Number of iterations completed per partition.

converged

Logical convergence indicator per partition.

A_hat

Alias for Abar (for plotting compatibility).

D_hat

Estimated diagonal error covariance matrix.

Examples

set.seed(123)
X_list <- list(
  matrix(rnorm(500), nrow = 100, ncol = 5),
  matrix(rnorm(500), nrow = 100, ncol = 5)
)
res <- DIRCP(X_list = X_list, m = 3)


Distributed RCP for Dynamic Tensors (DRCP)

Description

Performs distributed regularized CP decomposition adapted for BFM. Each node initializes via PCA, sets adaptive L1/L2 penalties from the initial residual moments, alternately optimizes the loadings and factors until convergence, and the coordinator aggregates the local estimates.

Usage

DRCP(X_list, m, max_iter = 20, tol = 1e-06)

Arguments

X_list

A list of numeric data partition matrices X_{I_k}, each of dimension n_{I_k} \times p.

m

An integer specifying the number of factors.

max_iter

An integer specifying the maximum number iterations (default = 20).

tol

A numeric value specifying the convergence tolerance (default = 1e-6).

Value

A list with the following components:

Abar

Global loading matrix, average of local regularized loadings.

Fbar

Global regularized factor matrix, average of local regularized factors.

Ahat

List of local regularized loading matrices per partition.

Fhat

List of local regularized factor matrices per partition.

lambda1

Vector of adaptive L1 penalties (per partition).

lambda2

Vector of adaptive L2 penalties (per partition).

iters

Number of iterations completed per partition.

converged

Logical convergence indicator per partition.

Examples

set.seed(123)
X_list <- list(
  matrix(rnorm(500), nrow = 100, ncol = 5),
  matrix(rnorm(500), nrow = 100, ncol = 5)
)
res <- DRCP(X_list = X_list, m = 3)


Distributed SCP with Temporal Smoothing (DSCP)

Description

Performs distributed static CP decomposition with temporal smoothing adapted for BFM. Each node centers its block, partitions it into pseudo-time batches, applies static CP per batch, corrects with Beta moment matching, smooths the averaged factor series with LOESS, and the coordinator aggregates the local estimates.

Usage

DSCP(X_list, m, batch_size = 20, A_true = NULL, F_true = NULL, plot = TRUE)

Arguments

X_list

A list of numeric data partition matrices X_{I_k}, each of dimension n_{I_k} \times p.

m

An integer specifying the number of factors.

batch_size

An integer specifying the fixed pseudo-time batch size (default = 20).

A_true

True global loading matrix (p x m), for evaluation & plotting, default NULL

F_true

True global factor matrix (n x m), for evaluation & plotting, default NULL

plot

Logical, whether to generate ggplot figures (only if A_true,F_true supplied)

Value

A list with the following components:

Abar

Global loading matrix, average of local batch-averaged loadings.

Fbar

Global smoothed factor matrix, average of local calibrated factors.

Ahat

List of local loading matrices per partition.

Fhat

List of local smoothed factor matrices per partition.

r

Vector of local Beta calibration ratios (per partition).

MSEA

Mean squared error for global loading matrix (if A_true supplied)

MSED

Mean squared error for global factor matrix (if F_true supplied)

figs

List of ggplot objects: factor_global, loading_scatter, local_factor_compare

Examples

## Not run: 
set.seed(123)
n <- 200; p <- 6; m <- 3
A_true <- matrix(rnorm(p*m), nrow = p, ncol = m)
F_true <- matrix(rnorm(n*m), nrow = n, ncol = m)
epsilon <- matrix(rbeta(n*p, 2, 3)-0.5, nrow=n, ncol=p)
X <- F_true %*% t(A_true) + epsilon
# split into K=2 nodes
X_list <- list(X[1:100, ], X[101:200, ])
res <- DSCP(X_list = X_list, m = 3, batch_size = 20,
            A_true = A_true, F_true = F_true, plot = TRUE)
res$MSEA
res$MSED
res$figs$p_global_F

## End(Not run)

Household Food Expenditure Data

Description

A dataset from Griffiths, Hill, and Judge (1993) on household food expenditure, income, and household size. The response variable food is a proportion (0 < food < 1), suitable for beta regression.

Usage

FoodExpenditure

Format

A data frame with 38 rows and 3 variables:

food

numeric: proportion of household income spent on food

income

numeric: household income (in thousands of dollars)

persons

numeric: number of persons living in the household

Source

Griffiths, W. E., Hill, R. C., & Judge, G. G. (1993). Learning and Practicing Econometrics. Wiley.

Examples

data(FoodExpenditure)
str(FoodExpenditure)

Gaussian Process Dynamic CP Decomposition for BFM

Description

Performs Gaussian process dynamic CP decomposition adapted for BFM with Beta error constrained kernel function and iterative optimization.

Usage

GCP(X_tilde, m, max_iter = 20, tol = 1e-06)

Arguments

X_tilde

A centered numeric observation matrix.

m

An integer specifying the number of factors.

max_iter

An integer specifying the maximum number of iterations (default = 20).

tol

A numeric value specifying the convergence tolerance (default = 1e-6).

Value

A list with the following components:

Ahat

Estimated loading matrix with Beta residual correction.

Fhat

Estimated dynamic factor matrix with Gaussian process smoothing.

iter

Number of iterations completed.

converged

Logical indicator of convergence status.

Examples

set.seed(123)
data <- matrix(rnorm(500), nrow = 100, ncol = 5)
X_tilde <- scale(data)
GCP(X_tilde = X_tilde, m = 3)


Gasoline Yield Data from Prater (1956)

Description

A dataset containing 32 observations on gasoline yield under different experimental conditions. The response variable yield is a proportion (0 < yield < 1), making it suitable for beta regression.

Usage

GasolineYield

Format

A data frame with 32 rows and 6 variables:

yield

numeric: proportion of crude oil converted to gasoline

batch

factor: 10 unique batches of crude oil

temp

numeric: temperature (Fahrenheit)

gravity

numeric: crude oil gravity

pressure

numeric: pressure

temp10

numeric: temperature (scaled)

Source

Prater (1956), as cited in Ferrari and Cribari-Neto (2004) Beta Regression for Modelling Rates and Proportions https://www.jstor.org/stable/4110074

Examples

data(GasolineYield, package = "betareg")
str(GasolineYield)

Estimate Loadings and Factors

Description

Iteratively estimates the loading matrix A and factor matrix for a centered observation matrix using iterative correction and convergence criteria.

Usage

IRCP(X_tilde, m, max_iter = 50, tol = 1e-06)

Arguments

X_tilde

A centered numeric observation matrix.

m

An integer specifying the number of factors.

max_iter

An integer specifying the maximum number of iterations (default = 50).

tol

A numeric value specifying the convergence tolerance (default = 1e-6).

Value

A list with the following components:

Ahat

Estimated loading matrix.

Fhat

Estimated factor matrix.

beta_params

Estimated alpha and beta parameters from Beta distribution fit.

residuals

Residual matrix from the factor model estimation.

iter

Number of iterations completed.

converged

Logical indicator of convergence status.

Examples

X_tilde <- scale(matrix(rnorm(500), nrow = 100, ncol = 5))
IRCP(X_tilde = X_tilde, m = 3)


Regularized CP Decomposition for BFM

Description

Performs regularized CP decomposition adapted for BFM with adaptive L1/L2 regularization based on Beta error estimation and alternating optimization for convergence.

Usage

RCP(X_tilde, m, max_iter = 20, tol = 1e-06)

Arguments

X_tilde

A centered numeric observation matrix.

m

An integer specifying the number of factors.

max_iter

An integer specifying the maximum number of iterations (default = 20).

tol

A numeric value specifying the convergence tolerance (default = 1e-6).

Value

A list with the following components:

Ahat

Estimated loading matrix with L1 regularization.

Fhat

Estimated factor matrix with L2 regularization.

lambda1

Adaptive L1 regularization coefficient for sparsity.

lambda2

Adaptive L2 regularization coefficient for smoothness.

iter

Number of iterations completed.

converged

Logical indicator of convergence status.

Examples

set.seed(123)
data <- matrix(rnorm(500), nrow = 100, ncol = 5)
X_tilde <- scale(data)
RCP(X_tilde = X_tilde, m = 3)


Reading Skills Data

Description

A dataset from Smithson and Verkuilen (2006) on reading accuracy, dyslexia status, and IQ scores. The response variable accuracy is a proportion (0 < accuracy < 1), suitable for beta regression.

Usage

ReadingSkills

Format

A data frame with 44 rows and 4 variables:

accuracy

numeric: proportion of correct responses in a reading task

accuracy1

numeric: transformed accuracy measure

dyslexia

factor: dyslexia status (levels: "yes", "no")

iq

numeric: IQ score

Source

Smithson, M. & Verkuilen, J. (2006). A better lemon squeezer? Maximum-likelihood regression with beta-distributed dependent variables. https://psycnet.apa.org/doi/10.1037/1082-989X.11.1.54

Examples

data(ReadingSkills)
str(ReadingSkills)

Static CP + Temporal Smoothing for BFM

Description

Estimates BFM loading and factor matrices using static CP decomposition with batch processing and LOESS temporal smoothing, adjusted by Beta error constraints.

Usage

SCP(X_tilde, m)

Arguments

X_tilde

A centered numeric observation matrix.

m

An integer specifying the number of factors.

Value

A list with the following components:

Ahat

Global estimated loading matrix after batch averaging.

Fhat

Global smoothed factor matrix with temporal smoothing.

smooth_weights

Smoothed weights from LOESS smoothing with Beta correction.

Examples

set.seed(123)
data <- matrix(rnorm(500), nrow = 100, ncol = 5)
X_tilde <- scale(data)
SCP(X_tilde = X_tilde, m = 3)

Determine Factor Number m

Description

Determines the number of factors using eigenvalue ratios and Beta moment constraints on a centered observation matrix.

Usage

algorithm1_determine_m(X_tilde, alpha_level = 0.05)

Arguments

X_tilde

A centered numeric observation matrix.

alpha_level

A numeric value specifying the significance level (default = 0.05).

Value

A list with the following components:

m

Estimated number of factors.

tau

Beta moment constraint threshold value.

er

Eigenvalue ratios for all consecutive eigenvalue pairs.

eig_vals

Eigenvalues of the sample covariance matrix.

Examples

set.seed(123)
X <- matrix(rnorm(500), nrow = 100, ncol = 5)
X_tilde <- scale(X)
algorithm1_determine_m(X_tilde = X_tilde, alpha_level = 0.05)


Calculate Errors for Factor Analysis Estimates

Description

This function calculates the Mean Squared Error (MSE) and relative error for factor loadings and uniqueness estimates.

Usage

calculate_errors(data, A, D, estimation_results)

Arguments

data

Matrix of BFM data.

A

Matrix of true factor loadings.

D

Matrix of true uniquenesses (diagonal matrix).

estimation_results

A list containing A_hat (estimated loadings) and D_hat (estimated uniquenesses).

Value

A named vector containing:

MSEA

Mean Squared Error for factor loadings.

MSED

Mean Squared Error for uniqueness estimates.

LSA

Relative error for factor loadings.

LSD

Relative error for uniqueness estimates.

Examples

set.seed(123)
n <- 10
p <- 5
A <- matrix(runif(p * p, -1, 1), nrow = p)
D <- diag(runif(p, 1, 2))
data <- matrix(runif(n * p), nrow = n)
estimation_results <- list(A_hat = A, D_hat = D)
errors <- calculate_errors(data, A, D, estimation_results)
print(errors)

K-Fold Cross Validation for BFM Factor Number

Description

Determines the optimal number of factors for BFM using k-fold cross validation with relative reconstruction error (RRE) and Beta residual outlier cleaning.

Usage

cv_bfm(X_tilde, max_R = 10, k = 5)

Arguments

X_tilde

A centered numeric observation matrix.

max_R

An integer specifying the maximum number of candidate factors (default = 10).

k

An integer specifying the number of folds for cross validation (default = 5).

Value

A list with the following components:

opt_R

Optimal number of factors selected by k-fold CV.

cv_rre_values

Mean cross-validated RRE for each candidate factor number.

Examples

set.seed(123)
data <- matrix(rnorm(500), nrow = 100, ncol = 5)
X_tilde <- scale(data)
cv_bfm(X_tilde = X_tilde, max_R = 10, k = 5)


Gradient Inflection Method for BFM Factor Number

Description

Determines the optimal number of factors for BFM using gradient inflection point detection with Beta residual correction and variance threshold filtering.

Usage

inflection_bfm(X_tilde, max_R = 10)

Arguments

X_tilde

A centered numeric observation matrix.

max_R

An integer specifying the maximum number of candidate factors (default = 10).

Value

A list with the following components:

opt_R

Optimal number of factors selected by inflection point method.

rre_values

Beta-corrected relative reconstruction error for each factor number.

inflection_points

Detected inflection points from second derivative.

Examples

set.seed(123)
data <- matrix(rnorm(500), nrow = 100, ncol = 5)
X_tilde <- scale(data)
inflection_bfm(X_tilde = X_tilde, max_R = 10)

T-CPIC Method for BFM Factor Number Determination

Description

Determines the optimal number of factors for BFM using the T-CPIC criterion with Beta error variance weighting on a centered numeric observation matrix.

Usage

tcpic_bfm(X_tilde, max_R = 10)

Arguments

X_tilde

A centered numeric observation matrix.

max_R

An integer specifying the maximum number of candidate factors (default = 10).

Value

A list with the following components:

opt_R

Optimal number of factors selected by T-CPIC criterion.

t_cpic_values

T-CPIC values for all candidate factor numbers.

rre_values

Relative reconstruction error (RRE) values for all candidate factor numbers.

Examples

set.seed(123)
data <- matrix(rnorm(500), nrow = 100, ncol = 5)
X_tilde <- scale(data)
tcpic_bfm(X_tilde = X_tilde, max_R = 10)