% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fracregpd.R
\name{fracregpd}
\alias{fracregpd}
\title{Fitting Panel Data Fractional Response Regressions}
\usage{
fracregpd(
  id,
  time,
  y,
  x,
  z,
  var.endog,
  x.exogenous = TRUE,
  lags,
  start,
  type,
  GMMww.cor = TRUE,
  link = "logit",
  intercept = TRUE,
  table = FALSE,
  variance = TRUE,
  var.type = "cluster",
  tdummies = FALSE,
  bootstrap = FALSE,
  B = 200,
  offset = NULL,
  or = FALSE,
  level = 0.95,
  na.action = stats::na.omit,
  ...
)
}
\arguments{
\item{id}{a numeric vector identifying the cross-sectional units.}

\item{time}{a numeric vector identifying the time periods in which the cross-sectional units were observed.}

\item{y}{a numeric vector containing the values of the response variable.}

\item{x}{a numeric matrix, with column names, containing the values of all covariates (exogenous and endogenous).}

\item{z}{a numeric matrix, with column names, containing the values of all exogenous variables (covariates and external instrumental variables). Only required in case of endogenous explanatory variables.}

\item{var.endog}{a numeric vector containing the values of the endogenous covariate (or of some transformation of it), which will be used as dependent variable in the linear reduced form assumed for application of the \code{QMLcre} estimator. Only required for this estimator.}

\item{x.exogenous}{a logical value indicating whether all explanatory variables are assumed to be exogenous or not.}

\item{lags}{a logical value indicating whether the first lags of \code{x} or  \code{z} should be used as instruments for \code{x}. Defaults to \code{TRUE} for the GMMww and GMMc estimators and to \code{FALSE} for the remaining estimators. The \code{GMMcre} and \code{QMLcre} estimators do not admit lagged instruments.}

\item{start}{a numeric vector containing the initial values for the parameters to be optimised. Optional.}

\item{type}{a description of the estimator to compute: \code{GMMww}, \code{GMMc}, \code{GMMbgw}, \code{GMMpfe}, \code{GMMcre}, \code{GMMpre} or \code{QMLcre}.}

\item{GMMww.cor}{a logical value indicating whether each explanatory variable should be transformed in deviations from its overall mean before computing the \code{GMMww} estimator.}

\item{link}{a description of the link function to use. Available options for all GMM estimators: \code{logit} and \code{cloglog}. Only option for the \code{QMLcre} estimator: \code{probit}.}

\item{intercept}{a logical value indicating whether the model should include a constant term or not. Only relevant for the  \code{GMMpre} estimator.}

\item{table}{a logical value indicating whether a summary table with the regression results should be printed.}

\item{variance}{a logical value indicating whether the variance of the estimated parameters should be calculated. Defaults to  \code{TRUE} whenever \code{table = FALSERUE}.}

\item{var.type}{a description of the type of variance of the estimated parameters to be calculated. Options are \code{cluster}, the default, and \code{robust}. In overidentified models, it also affects the parameter estimates via the GMM weighting matrix.}

\item{tdummies}{a logical value indicating whether time dummies should be included among the model explanatory variables.}

\item{bootstrap}{a logical value indicating whether bootstrap should be used in the estimation of the parameter standard errors.}

\item{B}{the number of bootstrap replications.}

\item{offset}{an optional numeric vector containing an offset. It must be of the same dimension as the response variable. It specifies that the variable should be included in the model with its coefficient constrained to 1.}

\item{or}{a logical value indicating whether to report odds ratios. Only valid when the link function is \code{"logit"}. Defaults to \code{FALSE}.}

\item{level}{a numeric value between 0 and 1 indicating the confidence level for the confidence intervals. Defaults to \code{0.95}.}

\item{na.action}{A function specifying how to handle missing values, default is \code{stats::na.omit}. If \code{NULL}, no action is taken.}

\item{\dots}{Arguments to pass to \link[stats]{nlminb}.}
}
\value{
\code{fracregpd} returns a list with the following elements:
  \item{type}{the name of the estimator computed.
}
  \item{link}{the name of the specified link.
}
  \item{p}{a named vector of coefficients.
}
  \item{Hy}{the transformed values of the response variable when GMM estimators are computed or the 
   values of the response variable in the QML case.
}
  \item{converged}{logical. Was the algorithm judged to have converged?
}

In case of an overidentifying model, the following element is also returned:
  \item{J}{the result of Hansen's J test of overidentifying moment conditions.
}

If \code{variance = TRUE} or \code{table = FALSERUE} and the algorithm converged successfully, the previous list also contains the following elements:
  \item{p.var}{a named covariance matrix.
}
  \item{var.type}{covariance matrix type.
}
}
\description{
\code{fracregpd} is used to fit panel data regression models when the dependent variable has a bounded, fractional nature.
}
\details{
\code{fracregpd} computes the GMM estimators proposed in Ramalho, Ramalho and Coelho (2018) for panel data fractional response models with both time-variant and time-invariant unobserved heterogeneity and endogeneous covariates: GMMww, GMMc, GMMbgw, GMMpfe, GMMcre and GMMpre. In addition, \code{fracregpd} also computes QMLcre, which was proposed by Papke and Wooldridge (2008) and Wooldridge (2019). 

\strong{Correlated Random Effects (CRE) - QMLcre:}
In panel data, unobserved individual-specific heterogeneity \eqn{c_i} may be correlated with the covariates \eqn{x_{it}}. The CRE approach (Papke and Wooldridge, 2008) models this dependence by projecting \eqn{c_i} onto the time averages of the strictly exogenous covariates \eqn{\bar{x}_i}:
\deqn{c_i = \psi + \bar{x}_i \xi + a_i}
where \eqn{a_i} is an error term independent of \eqn{x_i}. Assuming \eqn{a_i | x_i \sim N(0, \sigma_a^2)} and a probit link, integrating out \eqn{a_i} yields the "population-averaged" or scaled conditional mean:
\deqn{E(y_{it} | x_i) = G(x_{it} \beta_a + \psi_a + \bar{x}_i \xi_a)}
where the parameters with subscript \eqn{a} are scaled by \eqn{(1 + \sigma_a^2)^{-1/2}}. This equation is estimated via pooled Bernoulli QML.

\strong{Generalised Method of Moments (GMM):}
For models where strict exogeneity fails or the link function is an exponential-type link, Ramalho et al. (2018) propose GMM estimators based on the following general moment conditions:
\deqn{E[Z_{it} (H(y_{it}) - \exp(x_{it}\beta + c_i))] = 0}
where \eqn{H(\cdot)} is a transformation function and \eqn{Z_{it}} is a matrix of valid instruments. Estimators such as GMMww, GMMc, and GMMbgw use different transformations to eliminate the unobserved fixed effect \eqn{c_i} before applying GMM.

For overidentified models, \code{fracregpd} calculates Hansen's J statistic to test the validity of the overidentifying restrictions.
}
\section{Odds Ratios}{

When \code{or=TRUE} and the fractional link function (\code{linkfrac} or \code{link}) is \code{"logit"}, the model additionally computes odds ratios for the coefficients. 
Odds Ratios are exponentiated coefficients.
The corresponding standard errors for the odds ratios are calculated using the Delta method.
The confidence intervals for the odds ratios are calculated using the adjusted standard errors and the specified \code{level} (defaulting to 95\%).
Odds ratios are particularly useful in fractional logit models as they provide a direct multiplicative interpretation of the independent variable on the odds of the fractional outcome.
}

\examples{
### Empirical 401(k) Examples
data("fracreg_k401k")
y <- fracreg_k401k$prate
X <- cbind(mrate = fracreg_k401k$mrate, age = fracreg_k401k$age, 
           totemp = fracreg_k401k$totemp, sole = fracreg_k401k$sole)

# Artificial panel data structure for demonstration
N_emp <- nrow(X)
id_emp <- rep(1:(N_emp/2), each=2)
time_emp <- rep(1:2, times=N_emp/2)
mod <- fracregpd(id_emp, time_emp, y, X, type="QMLcre", link="probit")
summary(mod)

### Simulated Examples

set.seed(123)
# Simulating Panel Data
N <- 100
T_periods <- 5
id <- rep(1:N, each = T_periods)
time <- rep(1:T_periods, times = N)
x_panel <- rnorm(N * T_periods)

# Unobserved individual effect (CRE)
c_i <- rep(rnorm(N), each = T_periods) 
y_panel <- exp(x_panel + c_i) / (1 + exp(x_panel + c_i))

X <- cbind(x_panel = x_panel)

# Endogenous variable and instrument simulation
z_panel <- rnorm(N * T_periods)
u_panel <- 0.5 * z_panel + rnorm(N * T_periods)
var_endog <- 0.8 * z_panel + u_panel
y_endog <- exp(x_panel + 1.2 * var_endog + c_i + u_panel) / 
             (1 + exp(x_panel + 1.2 * var_endog + c_i + u_panel))

X_endog <- cbind(x_panel = x_panel, var_endog = var_endog)
Z_inst <- cbind(x_panel = x_panel, z_panel = z_panel)

\donttest{
# Estimate a Correlated Random Effects (CRE) Model
mod <- fracregpd(id=id, time=time, y=y_panel, x=X, type="QMLcre", link="probit")
summary(mod)

# Compute Partial Effects
pe_res <- fracregpd.pe(mod)
summary(pe_res)

# Exogeneity, no lags, no time dummies, clustered standard errors, GMMbgw estimator
mod <- fracregpd(id=id, time=time, y=y_panel, x=X, type="GMMbgw")
summary(mod)

# Estimate the GMMww estimator with odds ratios and 99\% confidence intervals
mod <- fracregpd(id=id, time=time, y=y_panel, x=X, type="GMMww", or=TRUE, level=0.99)
summary(mod)

# Lagged covariates and instruments, robust standard errors, GMMww estimator
mod <- fracregpd(id=id, time=time, y=y_panel, x=X, lags=TRUE, type="GMMww", var.type="robust")
summary(mod)

# Endogeneity, time dummies, GMMpfe estimator
mod <- fracregpd(id=id, time=time, y=y_endog, x=X_endog, z=Z_inst,
                 x.exogenous=FALSE, type="GMMpfe", tdummies=TRUE)
summary(mod)
}
}
\references{
Papke, L. and Wooldridge, J.M. (2008), "Panel data methods for fractional response variables with an application to test pass rates", \emph{Journal of Econometrics}, 145(1-2), 121-133.

Ramalho, E. A., Ramalho, J. J. S., & Coelho, L. M. S. (2018), "Exponential Regression of Fractional-Response Fixed-Effects Models with an Application to Firm Capital Structure", \emph{Journal of Econometric Methods}, 7(1), 20150019.

Wooldridge, J. M. (2019). Correlated random effects models with unbalanced panels. \emph{Journal of Econometrics}, 211(1), 137-150.
}
\seealso{
\code{\link{fracreg}}, for fitting standard cross-sectional fractional response models.\cr
\code{\link{fracreghet}}, for fitting cross-sectional fractional response models with unobserved heterogeneity.
}
\author{
Sulman Olieko Owili <oliekosulman@gmail.com>
}
