linear_fe: Main function for fitting the fixed effect linear model

View source: R/linear_fe.R

linear_feR Documentation

Main function for fitting the fixed effect linear model

Description

Fit a fixed effect linear model via profile likelihood or dummy encoding.

Usage

linear_fe(
  formula = NULL,
  data = NULL,
  Y = NULL,
  Z = NULL,
  ID = NULL,
  Y.char = NULL,
  Z.char = NULL,
  ID.char = NULL,
  method = "pl"
)

Arguments

formula

a two-sided formula object describing the model to be fitted, with the response variable on the left of a ~ operator and covariates on the right, separated by + operators. The fixed effect of the provider identifier is specified using id().

data

a data frame containing the variables named in the formula, or the columns specified by Y.char, Z.char, and ID.char.

Y

a numeric vector representing the response variable.

Z

a matrix or data frame representing the covariates, which can include both numeric and categorical variables.

ID

a numeric vector representing the provider identifier.

Y.char

a character string specifying the column name of the response variable in the data.

Z.char

a character vector specifying the column names of the covariates in the data.

ID.char

a character string specifying the column name of the provider identifier in the data.

method

a character string specifying the method to fit the model.

  • "pl" (default) uses profile likelihood to fit the model.

  • "dummy" calls lm to fit the model using dummy variables for the provider identifier.

Details

This function is used to fit a fixed effect linear model of the form:

Y_{ij} = \gamma_i + \mathbf{Z}_{ij}^\top\boldsymbol\beta + \epsilon_{ij}

where Y_{ij} is the continuous outcome for individual j in provider i, \gamma_i is the provider-specific effect, \mathbf{Z}_{ij} are the covariates, and \boldsymbol\beta is the vector of coefficients for the covariates. The default method for fitting the model is profile likelihood, but dummy encoding can also be used by specifying the appropriate method. When the number of providers is very large, we recommend using the profile likelihood method, as it is computationally efficient and requires less memory usage.

The function accepts three different input formats: a formula and dataset, where the formula is of the form response ~ covariates + id(provider), with provider representing the provider identifier; a dataset along with the column names of the response, covariates, and provider identifier; or the outcome vector \boldsymbol{Y}, the covariate matrix or data frame \mathbf{Z}, and the provider identifier vector.

If issues arise during model fitting, consider using the data_check function to perform a data quality check, which can help identify missing values, low variation in covariates, high-pairwise correlation, and multicollinearity. For datasets with missing values, this function automatically removes observations (rows) with any missing values before fitting the model.

Value

A list of objects with S3 class "linear_fe":

coefficient

a list containing the estimated coefficients: beta, the fixed effects for each predictor, and gamma, the effect for each provider.

variance

a list containing the variance estimates: beta, the variance-covariance matrix of the predictor coefficients, and gamma, the variance of the provider effects.

sigma

the residual standard error.

fitted

the fitted values of each individual.

observation

the original response of each individual.

residuals

the residuals of each individual, that is response minus fitted values.

linear_pred

the linear predictor of each individual.

data_include

the data used to fit the model, sorted by the provider identifier. For categorical covariates, this includes the dummy variables created for all categories except the reference level.

char_list

a list of the character vectors representing the column names for the response variable, covariates, and provider identifier. For categorical variables, the names reflect the dummy variables created for each category.

method

the method used for model fitting, either "Profile Likelihood" or "Dummy".

Loglkd

log likelihood.

AIC

Akaike information criterion.

BIC

Bayesian information criterion.

References

Hsiao, C. (2022). Analysis of panel data (No. 64). Cambridge university press.

R Core Team (2023). The R Stats Package: lm. Available at: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/lm.html

See Also

data_check

Examples

data(ExampleDataLinear)
outcome <- ExampleDataLinear$Y
covar <- ExampleDataLinear$Z
ID <- ExampleDataLinear$ID
data <- data.frame(outcome, ID, covar)
covar.char <- colnames(covar)
outcome.char <- colnames(data)[1]
ID.char <- colnames(data)[2]
formula <- as.formula(paste("outcome ~", paste(covar.char, collapse = " + "), "+ id(ID)"))

# Fit fixed linear effect model using three input formats
fit_fe1 <- linear_fe(Y = outcome, Z = covar, ID = ID)
fit_fe2 <- linear_fe(data = data, Y.char = outcome.char, Z.char = covar.char, ID.char = ID.char)
fit_fe3 <- linear_fe(formula, data)


pprof documentation built on April 12, 2025, 1:33 a.m.