| Title: | Semiparametric Smooth-Coefficient Stochastic Frontier Analysis |
| Version: | 0.2.0 |
| Maintainer: | Kai Sun <ksun1@shu.edu.cn> |
| Description: | Provides semiparametric smooth-coefficient stochastic frontier analysis following Sun and Kumbhakar (2013) <doi:10.1016/j.econlet.2013.05.001> where the coefficients of the parametric part vary smoothly with a set of nonparametric variables. Inefficiency term is allowed to depend on a set of determinants through heteroskedasticity. Smooth coefficients are estimated using nonparametric regression and the remaining frontier parameters are estimated by maximum likelihood. Technical efficiency and inefficiency are computed using the Battese and Coelli (1988) <doi:10.1016/0304-4076(88)90053-X> and Jondrow et al. (1982) <doi:10.1016/0304-4076(82)90004-5> (JLMS) methods, respectively. Confidence intervals for technical efficiency are computed using the approach of Horrace and Schmidt (1996) <doi:10.1007/BF00157044>. Marginal effects of inefficiency determinants on the JLMS inefficiency estimator are computed following Kumbhakar and Sun (2013) <doi:10.1016/j.econlet.2013.04.041>. By-products of the package include least-squares cross-validation bandwidth selection for the local-linear semiparametric smooth-coefficient estimator, local-linear semiparametric smooth-coefficient estimation, and analytic gradients of the local-constant semiparametric smooth-coefficient estimator with respect to the continuous z variables following Geng and Sun (2019) <doi:10.1016/j.econlet.2019.108684>. |
| License: | AGPL (≥ 3) |
| Encoding: | UTF-8 |
| Depends: | R (≥ 3.6) |
| Imports: | Formula, graphics, grDevices, np, progress (≥ 1.2.1), stats |
| Config/roxygen2/version: | 8.1.1 |
| Suggests: | testthat (≥ 3.0.0) |
| Config/testthat/edition: | 3 |
| LazyData: | true |
| NeedsCompilation: | no |
| Author: | Kai Sun [aut, cre] |
| Packaged: | 2026-11-19 05:02:58 UTC; Administrator |
| Repository: | CRAN |
| Date/Publication: | 2026-11-19 11:20:15 UTC |
Extract gradient estimates
Description
Extracts the gradients estimated by spscsfa with
gradients = TRUE: the gradients of the smooth coefficients
\theta and \beta and of the intercept \alpha with
respect to the continuous nonparametric variables z, and the gradients
of the JLMS inefficiency estimates with respect to the inefficiency
determinants. The smooth coefficients are local-constant if the model was
estimated with spscreg = "lc" and local-linear if it was estimated
with spscreg = "ll".
Usage
grad(x, zvar = NULL)
Arguments
x |
An object of class |
zvar |
Optional character vector with the names of the variables whose
gradients are extracted, as they are written in If |
Value
A list with three elements:
theta_beta_grad |
The gradients of the smooth coefficients: a list
with one matrix for each continuous |
alpha_grad |
The gradients of |
inefficiency_grad |
The gradients of the JLMS inefficiency estimates
with respect to the inefficiency determinants: a matrix with one column
for each determinant (the binary variables for a categorical one), or
|
With zvar, the elements are restricted to the variables chosen. An
element that is not available for them is NULL.
See Also
Examples
set.seed(1)
n <- 100
x <- rnorm(n)
z1 <- runif(n)
z2 <- factor(sample(c("A", "B", "C"), n, replace = TRUE))
y <- cos(z1) + sin(z1) * x + rnorm(n, 0, 0.1) -
exp(0.1 + 0.1 * z1) * abs(rnorm(n, 0, 1))
dat <- data.frame(y, x, z1, z2)
result <- spscsfa(y ~ x | z1 + z2, data = dat, gradients = TRUE,
verbose = FALSE)
# all the gradients
g <- grad(result)
names(g)
# the gradients with respect to z1
g1 <- grad(result, zvar = "z1")
head(g1$theta_beta_grad$z1)
# z2 is not continuous: only inefficiency_grad can be extracted, and a note
# is printed
g2 <- grad(result, zvar = "z2")
head(g2$inefficiency_grad)
Extract JLMS inefficiency estimates
Description
Extract JLMS inefficiency estimates
Usage
inefficiency(x)
Arguments
x |
An object of class 'spscsfa'. |
Value
A numeric vector of JLMS conditional inefficiency estimates.
Gradients of the local-constant smooth coefficient estimator
Description
Computes the analytic gradients, with respect to the continuous z
variables, of the smooth coefficients of the local-constant (LC)
semiparametric smooth coefficient (SPSC) model
y_i = x_i'\beta(z_i) + u_i following Geng and Sun (2019). It is called by
spscsfa or can be used independently. An np::npscoefbw
bandwidth object with its default LC regression type and kernels can be passed
into the bws argument of this function.
Usage
lcscoefgrad(bws, txdat, tydat, tzdat, ezdat = NULL)
Arguments
bws |
The bandwidths: the bandwidth object returned by
|
txdat |
A data frame or matrix with the regressors |
tydat |
A numeric vector with the dependent variable. |
tzdat |
A data frame with the variables the coefficients depend on: numeric (continuous), factors (unordered) or ordered factors. |
ezdat |
Optional data frame with the evaluation points, with the same
variables as |
Details
The LC estimator is
\hat\beta(z) = \Big(\sum_i x_i x_i' K_i(z)\Big)^{-1}
\sum_i x_i y_i K_i(z).
Differentiating the locally weighted moment condition
\sum_i x_i \hat u_i(z) K_i(z) = 0, where
\hat u_i(z) = y_i - x_i'\hat\beta(z), with respect to the lth
continuous z variable gives
D_l\hat\beta(z) = \Big(\sum_i x_i x_i' K_i(z)\Big)^{-1}
\sum_i x_i \hat u^*_{li}(z) K_i(z),
that is, another LC regression, of the transformed residuals
\hat u^*_{li}(z) = \hat u_i(z)(z_{li} - z_l)/h_l^2, on the original
regressors. This is the result for a Gaussian kernel for the continuous
variables: k\left(\frac{z_{li}-z_l}{h_l}\right) = \frac{1}{\sqrt{2\pi}}
\exp\left(-\frac{1}{2} \left(\frac{z_{li}-z_l}{h_l}\right)^2 \right).
Factors enter the product kernel K_i(z) through kernels that
do not depend on the continuous variables, so they do not change the formula.
The kernel weights are those of np::npksum. A
bandwidth object from np::npscoefbw must therefore be for
regtype = "lc", with fixed bandwidths (not scale factors) and a
second-order Gaussian kernel without bounds for the continuous variables;
its kernels for factors are used as they are. If the weighted cross-product
matrix at an evaluation point is singular, a ridge is added to it and a warning
is given.
Value
A named list with one element for each continuous variable in
tzdat, in the order of its columns and named after them. The
element for the lth continuous variable is a matrix with one row for
each evaluation point and one column for each smooth coefficient, in the
order of the coefficients (the constant first, then the columns of
txdat). Its columns are the gradients of all smooth coefficients
with respect to z_l and are named as llscoef names its gradients:
d(Intercept)/d(z_l), d(x1)/d(z_l), .... The list is empty if
there is no continuous variable in tzdat.
References
Xin Geng and Kai Sun (2019). Gradient estimation of the local-constant semiparametric smooth coefficient model. Economics Letters, 185, 108684.
See Also
llscoef, whose gradients are those of the
local-linear estimator.
Examples
set.seed(1)
n <- 200
z <- runif(n)
x <- rnorm(n)
y <- sin(2 * pi * z) - cos(2 * pi * z) * x + rnorm(n, sd = 0.5)
dat <- data.frame(y, x, z)
bw <- np::npscoefbw(y ~ x | z, data = dat)
g <- lcscoefgrad(bw, txdat = dat["x"], tydat = dat$y, tzdat = dat["z"])
# g$z has the columns d(Intercept)/d(z) and d(x)/d(z); the true gradients
# are 2 * pi * cos(2 * pi * z) and 2 * pi * sin(2 * pi * z)
head(g$z)
## Not run:
# gradients on a grid of z, through 'ezdat': plotted against z, with the
# true gradients (dashed)
ez <- data.frame(z = seq(0, 1, length.out = 100))
g.ez <- lcscoefgrad(bw, txdat = dat["x"], tydat = dat$y, tzdat = dat["z"],
ezdat = ez)
op <- par(mfrow = c(1, 2))
plot(ez$z, g.ez$z[, "d(Intercept)/d(z)"], type = "l",
xlab = expression(z),
ylab = expression(partialdiff * beta[0](z) / partialdiff * z))
lines(ez$z, 2 * pi * cos(2 * pi * ez$z), lty = 2)
plot(ez$z, g.ez$z[, "d(x)/d(z)"], type = "l",
xlab = expression(z),
ylab = expression(partialdiff * beta[1](z) / partialdiff * z))
lines(ez$z, 2 * pi * sin(2 * pi * ez$z), lty = 2)
par(op)
## End(Not run)
Local-linear semiparametric smooth coefficient estimation
Description
Estimates the semiparametric smooth coefficient (SPSC) model by local-linear
kernel regression, at the training data or at new evaluation points. The bandwidth
object of llscoefbw can be passed as bws. It is used
by spscsfa for the estimation of the smooth coefficients.
Usage
llscoef(bws, ...)
## S3 method for class 'llsmoothcoefficientbw'
llscoef(
bws,
formula = NULL,
data = NULL,
newdata = NULL,
betas = FALSE,
residuals = FALSE,
gradients = FALSE,
...
)
## S3 method for class 'formula'
llscoef(
bws,
data = NULL,
newdata = NULL,
betas = FALSE,
residuals = FALSE,
gradients = FALSE,
na.action = stats::na.omit,
...
)
## Default S3 method:
llscoef(
bws,
formula,
data = NULL,
newdata = NULL,
betas = FALSE,
residuals = FALSE,
gradients = FALSE,
na.action = stats::na.omit,
...
)
## S3 method for class 'llsmoothcoefficient'
print(x, ...)
## S3 method for class 'llsmoothcoefficient'
coef(object, ...)
## S3 method for class 'llsmoothcoefficient'
residuals(object, ...)
## S3 method for class 'llsmoothcoefficient'
fitted(object, ...)
Arguments
bws |
A bandwidth specification: an object of class
|
... |
Further arguments, not used. |
formula |
A formula |
data |
Optional data frame containing the variables in |
newdata |
Optional data frame with the evaluation points. If omitted, the training data are used. See ‘Details’. |
betas |
Logical; if |
residuals |
Logical; if |
gradients |
Logical; if |
na.action |
As in |
object, x |
An object of class |
Details
The model is y_i = x_i'\beta(z_i) + u_i, where x_i consists of a
constant followed by the variables in the first part of formula and
z_i holds the variables in the second part. A third part, as accepted
by spscsfa for the inefficiency determinants, is ignored.
Observations with missing values are removed.
For a given z, the local-linear estimator following Geng and Sun (2019) is
\hat\gamma(z) = \Big(\sum_i S_i S_i' K_i(z)\Big)^{-1}
\sum_i S_i y_i K_i(z),
where S_i = (x_i', (x_i \otimes (z_i - z))')' involves the continuous
variables in z only and K_i(z) is a generalized product kernel
(Second-order Gaussian for continuous, Aitchison-Aitken for unordered and Li-Racine for
ordered variables, the defaults of np::npksum). The first elements of
\hat\gamma(z) estimate the intercept and slope smooth coefficients and the
remaining ones their gradients. Variables of the second part of
formula are classified by their class: numeric variables are
continuous, factors are unordered categorical and ordered factors are
ordered categorical.
- Formula and bandwidths
bwsis the first argument, and what it is decides which of the other arguments are needed. The formula is given once, tollscoefbw:-
bwsis the object returned byllscoefbwwithLSCV = TRUE:formulamust not be given, anddatais not needed. The object keeps the formula and the call tollscoefbw, whosedataare evaluated again in the environment the call was made from.datareplaces the training data,newdatagives the evaluation points. -
bwsis a formula: the bandwidths are selected first, byllscoefbw(bws, data = data), and used in the same call. -
bwsis a numeric vector of local-linear bandwidths, such as the result ofllscoefbwwithLSCV = FALSE:formulais required, anddataunless the variables are in the environment offormula. A bandwidth object ofnp::npscoefbwis accepted too, and its bandwidths are used, if it was made withregtype = "ll", fixed bandwidths (bwtype = "fixed",bwscaling = FALSE) and the default kernels (ckertype,ckerorder,ckerbound,ukertypeandokertypearguments). However, one made withregtype = "lc", the defaultregtypeargument ofnp::npscoefbw, has bandwidths for the local-constant estimator, and gives an error like the other objects that do not qualify. A user-supplied vector that does not qualify cannot be told apart from other vectors, but such a vector is not suitable, either.
A bandwidth object returned by
llscoefbwcannot be used together with aformula. This is because an object of class"llsmoothcoefficientbw"stores the formula and data used to select the bandwidths. Therefore, formula is not allowed to appear again to guarantee internal consistency.-
- Evaluation points
The estimation always uses the training data: those of the call to
llscoefbwfor a bandwidth object, otherwise those given byformulaanddata.newdataonly decides where the smooth coefficients are evaluated. It needs the variables of thexandzparts offormula(the response is not needed and is ignored). Thexvariables matter for the fitted values only, and any values, such as the training means, can be used when only the smooth coefficients are of interest. Factors innewdataare matched to the levels of the training data, and rows ofnewdatawith missing values are dropped.- Residuals
The residuals are those of the training data, that is,
yminus the fitted values at the training points, also whennewdatais supplied.- Gradients
The gradients of the smooth coefficients with respect to the continuous variables in
z, one column for each coefficient and variable, in X-major order: all the gradients of the first coefficient (the intercept) come first, then those of the second one, and so on, each in the order of the continuouszvariables.
Value
llscoef returns an object of class
"llsmoothcoefficient", a list with components
mean |
The fitted values at the evaluation points. |
beta |
If |
resid |
If |
grad |
If |
Together with bw, x_names, z_names, n_train,
n_eval, eval_train (evaluation and training data are the same),
omit (the rows with missing values that were dropped from the
evaluation data: from newdata if it is given, and otherwise from the training data) and
call. With na.action = na.exclude, mean,
beta and grad are padded with NA at the rows of the
evaluation data that were dropped (those in omit), and
resid at the dropped rows of the training data, so that they line
up with newdata or with the data. n_train and n_eval
still count the rows that were used.
Note
For an object of class "llsmoothcoefficient", the smooth coefficients and residuals can be extracted with
coef and residuals, respectively, and the fitted values with
fitted.
References
Xin Geng and Kai Sun (2019). Gradient estimation of the local-constant semiparametric smooth coefficient model. Economics Letters, 185, 108684.
Tristen Hayfield and Jeffrey S. Racine (2008). Nonparametric econometrics: The np package. Journal of Statistical Software, 27(5), 1–32.
Qi Li, Cliff J. Huang, Dong Li and Tsu-Tan Fu (2002). Semiparametric smooth coefficient models. Journal of Business & Economic Statistics, 20(3), 412–422.
See Also
llscoefbw, lcscoefgrad, spscsfa
Examples
set.seed(1)
n <- 200
x <- rnorm(n)
z1 <- runif(n)
z2 <- runif(n)
z3 <- factor(sample(c("A", "B", "C"), n, replace = TRUE))
y <- cos(z1 + z2) + sin(z1 - z2) * x + rnorm(n, 0, 0.1)
dat <- data.frame(y, x, z1, z2, z3)
bw <- llscoefbw(y ~ x | z1 + z2 + z3, data = dat)
# estimation at the training data: the formula and the data
# are those of llscoefbw()
fit <- llscoef(bw, betas = TRUE, residuals = TRUE, gradients = TRUE)
fit
head(coef(fit))
head(residuals(fit))
head(fit$grad)
# smooth coefficients on a grid of z1, with z2 and z3 fixed
grid <- data.frame(x = mean(x), z1 = seq(0, 1, length.out = 50), z2 = 0.5,
z3 = factor("A", levels = levels(dat$z3)))
fit.grid <- llscoef(bw, newdata = grid, betas = TRUE)
plot(grid$z1, coef(fit.grid)[, "x"], xlab=expression(z[1]), ylab=expression(beta(z[1])),
type = "l")
# bandwidths selected inside llscoef(), from a formula
fit.f <- llscoef(y ~ x | z1 + z2 + z3, data = dat, betas = TRUE)
# given a bandwidth vector that is not an object of class
# "llsmoothcoefficientbw": the formula is needed
fit.b <- llscoef(bw$bw, formula = y ~ x | z1 + z2 + z3, data = dat, betas = TRUE)
Least-squares cross-validation bandwidths for the local-linear SPSC estimator
Description
Selects the bandwidths of the local-linear semiparametric smooth-coefficient
(SPSC) estimator by least-squares cross-validation (LSCV). The result can be
passed to llscoef through its bws argument
or to spscsfa through its bandwidth argument.
Usage
llscoefbw(formula, bws, data, LSCV, na.action, max.search, grid_scan)
## Default S3 method:
llscoefbw(
formula,
bws = NULL,
data = NULL,
LSCV = TRUE,
na.action = stats::na.omit,
max.search = 2L,
grid_scan = 2^c(-2:5)
)
## S3 method for class 'formula'
llscoefbw(
formula,
bws = NULL,
data = NULL,
LSCV = TRUE,
na.action = stats::na.omit,
max.search = 2L,
grid_scan = 2^c(-2:5)
)
## S3 method for class 'llsmoothcoefficientbw'
print(x, ...)
Arguments
formula |
A formula |
bws |
Optional numeric vector of local-linear bandwidths, one for each
variable in the |
data |
Optional data frame containing the variables in |
LSCV |
Logical; if |
na.action |
A function, or the name of a function, that decides what to do
with rows that have missing values in any part of |
max.search |
Maximum number of Nelder-Mead searches, a positive integer
(default 2): the one from the starting values and up to
|
grid_scan |
A strictly increasing numeric vector of finite positive
multipliers for the coarse scan (default |
x |
An object of class |
... |
Further arguments, ignored. |
Details
The model is y_i = x_i'\beta(z_i) + u_i, where x_i consists of a
constant followed by the variables in the first part of formula and
z_i holds the variables in the second part. A third part, as accepted
by spscsfa for the inefficiency determinants, is ignored.
Observations with missing values are removed.
For a given z, the local-linear estimator following Geng and Sun (2019) is
\hat\gamma(z) = \Big(\sum_i S_i S_i' K_i(z)\Big)^{-1}
\sum_i S_i y_i K_i(z),
where S_i = (x_i', (x_i \otimes (z_i - z))')' involves the continuous
variables in z only and K_i(z) is a generalized product kernel
(Second-order Gaussian for continuous, Aitchison-Aitken for unordered and Li-Racine for
ordered variables, the defaults of np::npksum). The first elements of
\hat\gamma(z) estimate the smooth coefficients. The LSCV criterion is the
mean of the squared leave-one-out residuals.
Variables of the second part of formula are classified by their
class: numeric variables are continuous, factors are unordered categorical
and ordered factors are ordered categorical.
- Starting values
Rule-of-thumb bandwidths:
1.059224\, sd(z)\, n^{-1/(4+p)}for a continuous variable and1.059224\, n^{-2/(4+p)}for a categorical variable, wherenis the number of observations andpthe number of continuous variables inz. A categorical starting value outside the admissible range (see below) is replaced by half of the upper limit.- Admissible bandwidths
The criterion takes a very large value for inadmissible bandwidths. Continuous bandwidths must be positive, unordered categorical bandwidths must lie in
[0, (c-1)/c]withcthe number of levels, and ordered categorical bandwidths in[0, 1]. Singular local systems are ridged.- Search
Nelder-Mead, that is
optimwithmethod = "Nelder-Mead"and its default settings, started from the starting values.- Scan and further searches
The LSCV criterion is not a smooth function of the bandwidths: with few observations it can have local minima, for example at small bandwidths, where a Nelder-Mead search that starts nearby stops. Unless
max.search = 1, the criterion is therefore also evaluated on a coarse scan: the starting values with every continuous bandwidth multiplied by each value ingrid_scan(categorical bandwidths are kept at their starting values). By default,grid_scan = 2^c(-2:5), which takes 8 evaluations. The scan moves all the continuous bandwidths together, and is skipped if no variable in thezpart is continuous.- Progress
Progress reporting during bandwidth selection is provided by the progress package.
Value
With LSCV = FALSE, a numeric vector of
bandwidths, one for each variable in the z part of formula,
in that order and named after those variables.
With LSCV = TRUE, an object of class
"llsmoothcoefficientbw": a list with components
bwthe bandwidths, a numeric vector named after the variables in the
zpart offormula.formulathe
formulathat was supplied.n_obsthe number of observations used.
n_dimthe number of variables in the
zpart.lscvthe value of the LSCV criterion at
bw.n_fevalthe number of evaluations of the criterion, those of the scan and of every search made.
scan_factorthe multiplier used to initialize the Nelder-Mead search that produced the selected bandwidths. It is
1when the search from the original starting values gives the best result; otherwise it is the corresponding value ingrid_scan.n_conti,n_unord,n_ordthe numbers of continuous, unordered categorical and ordered categorical variables in the
zpart.callthe call to
llscoefbw, with the environment it was made from.obs_omit,n_obs_omitthe rows of the training data that were dropped because of missing values in any part of
formula, and their number. The row numbers refer todata, or to the variables in the environment offormula.na_actionthe
na.actionfunction used.
References
Xin Geng and Kai Sun (2019). Gradient estimation of the local-constant semiparametric smooth coefficient model. Economics Letters, 185, 108684.
Tristen Hayfield and Jeffrey S. Racine (2008). Nonparametric econometrics: The np package. Journal of Statistical Software, 27(5), 1–32.
Qi Li, Cliff J. Huang, Dong Li and Tsu-Tan Fu (2002). Semiparametric smooth coefficient models. Journal of Business & Economic Statistics, 20(3), 412–422.
See Also
Examples
## Not run:
set.seed(1)
n <- 200
x <- rnorm(n)
z1 <- runif(n)
z2 <- runif(n)
z3 <- factor(sample(c("A", "B", "C"), n, replace = TRUE))
y <- cos(z1 + z2) + sin(z1 - z2) * x + rnorm(n, 0, 0.1)
dat <- data.frame(y, x, z1, z2, z3)
bw <- llscoefbw(y ~ x | z1 + z2 + z3, data = dat)
bw
bw$bw
# the formula and the data are those of llscoefbw()
fit <- llscoef(bw, betas = TRUE)
# a single search from the starting values, without the scan
llscoefbw(y ~ x | z1 + z2 + z3, data = dat, max.search = 1)
# check and return given bandwidths without a search
llscoefbw(y ~ x | z1 + z2 + z3, bws = bw$bw, data = dat, LSCV = FALSE)
## End(Not run)
Plot smooth coefficients of an SPSC-SFA model
Description
Plot method for objects of class "spscsfa". Each estimated smooth
coefficient, i.e. the intercept \theta(Z) and the slope coefficients
\beta(Z), is plotted against one continuous nonparametric variable
Z at a time, over a grid running from min(Z) to max(Z).
The smooth coefficients are those of the estimator used to fit the model:
local-constant if spscreg = "lc" and local-linear if
spscreg = "ll" in spscsfa.
Usage
## S3 method for class 'spscsfa'
plot(
x,
which = NULL,
zvar = NULL,
n_grid = 100L,
rug = TRUE,
mfrow = NULL,
ask = NULL,
...
)
Arguments
x |
An object of class |
which |
Smooth coefficients to plot, given as positions or names of the
columns of |
zvar |
Continuous |
n_grid |
Number of equally spaced grid points between |
rug |
Logical; if |
mfrow |
Integer vector of length 2 giving the number of rows and
columns of panels per page. The default ( |
ask |
Logical; if |
... |
Further graphical parameters passed to
|
Details
For every continuous variable in the nonparametric part Z of the model
(the second part of the formula), the smooth coefficients are evaluated on
n_grid equally spaced points between the sample minimum and maximum
of that variable. The grid is supplied to spscsfa through its
ezdat argument, re-using the bandwidth of the fitted model, so no new
cross-validation is carried out. The same estimator (spscreg) as in
the fitted model is used for the grid: the local-constant estimator for spscreg = "lc"
and the local-linear estimator for spscreg = "ll".
If the model has more than one Z variable, the other continuous
Z variables are held fixed at their sample averages while the variable
on the horizontal axis runs over its grid. One page is drawn for each
continuous Z variable, with one panel for each smooth coefficient.
Discrete Z variables (factors and ordered factors) are ignored by the
plot method: no plots are drawn against them. The smooth coefficients still
depend on them, however, so they have to take some value when the
coefficients are evaluated; each discrete Z variable is held at its
most frequent category.
A term in the second part of the formula is a variable as it enters the
model: for log(z) the grid runs over the values of log(z),
which also labels the axis, and factor(z2) is a discrete variable.
Only the smooth coefficients of the first estimation step, i.e. the columns
of smoothcoef(x)$theta_beta, are plotted. In particular, the
intercept is \theta(Z) (Intercept (theta)) and not the
recovered intercept \alpha(Z).
Value
Invisibly, a named list with one data frame for each Z
variable plotted against. The first column of each data frame is the grid
of that Z variable and the remaining columns are the smooth
coefficients evaluated on the grid, so that the curves can be redrawn or
customized.
See Also
Examples
set.seed(1)
n <- 100
x <- rnorm(n)
z <- runif(n)
y <- cos(z) + sin(z) * x + rnorm(n, 0, 0.1) -
exp(0.1 + 0.1 * z) * abs(rnorm(n, 0, 1))
dat <- data.frame(output = y, input = x, covariate = z)
result <- spscsfa(output ~ input | covariate, data = dat, verbose = FALSE)
# the intercept and the slope coefficient plotted against 'covariate'
plot(result)
# the plotted values are returned invisibly
curves <- plot(result, n_grid = 50, col = "blue")
head(curves$covariate)
# Two continuous z variables and one discrete z variable: one page per
# continuous z variable; the other continuous z variable is held at its
# average and the factor at its most frequent category
z1 <- runif(n)
z2 <- runif(n)
z3 <- factor(rep(c("A", "B"), length.out = n))
y <- cos(z1 + z2) + sin(z1 - z2) * x + rnorm(n, 0, 0.1) -
exp(0.1 + 0.1 * z1) * abs(rnorm(n, 0, 1))
dat <- data.frame(y, x, z1, z2, z3)
result <- spscsfa(y ~ x | z1 + z2 + z3, data = dat, verbose = FALSE)
plot(result)
# the slope coefficient only, against z2 only
plot(result, which = 2, zvar = "z2")
# local-linear smooth coefficients (spscreg = "ll"), plotted the same way
result_ll <- spscsfa(y ~ x | z1 + z2 + z3, data = dat, spscreg = "ll", verbose = FALSE)
plot(result_ll)
Print SPSC-SFA results
Description
print method for objects of class "spscsfa". It formats the
fit through summary.spscsfa, so the display is identical to
that of summary(x) and responds to the same standard arguments of
print. As required for print methods, the object
itself is returned invisibly.
Usage
## S3 method for class 'spscsfa'
print(x, digits = max(3L, getOption("digits") - 3L), ...)
Arguments
x |
An object of class |
digits |
Integer; the minimum number of significant digits to use. |
... |
Further arguments passed to
|
Value
The original object of class "spscsfa", returned invisibly.
It is a list containing the components smooth_coefficients,
mle_results, technical_efficiency, inefficiency,
model_summary, formula_parsed and data_info. These
components contain the estimated smooth coefficients and recovered
intercept, maximum likelihood results, technical efficiency estimates and
confidence intervals, JLMS inefficiency estimates, model specification
information, parsed formula components, and data cleaning information. If
the model was estimated with gradients = TRUE,
smooth_coefficients_grad and inefficiency_grad are also returned.
See Also
summary.spscsfa,
print.summary.spscsfa, spscsfa
Print an SPSC-SFA summary
Description
print method for objects of class "summary.spscsfa" created by
summary.spscsfa. Numeric output is controlled by the standard
arguments of print: digits and
signif.stars are handled explicitly, and any further arguments in
... (for example na.print, print.gap, quote or
right) are passed down to printCoefmat and
print.default.
Usage
## S3 method for class 'summary.spscsfa'
print(
x,
digits = max(3L, getOption("digits") - 3L),
signif.stars = getOption("show.signif.stars"),
signif.legend = signif.stars,
...
)
Arguments
x |
An object of class |
digits |
Integer; the minimum number of significant digits. Defaults to
the value stored by |
signif.stars |
Logical; if |
signif.legend |
Logical; if |
... |
Further arguments passed to |
Value
The argument x, invisibly.
See Also
summary.spscsfa, print.spscsfa
Philippine Rice Farm Data
Description
Data used to illustrate the application of the semiparametric smooth-coefficient stochastic frontier model.
Usage
data(ricephil)
Format
A data frame with 344 observations and 17 variables.
References
Timothy J. Coelli, D.S. Prasada Rao, Christopher J. O'Donnell, and George E. Battese (2005). An Introduction to Efficiency and Productivity Analysis. Springer, New York.
S. Pandey, P. Masciat, L. Velasco, and R. Villano (1999). Risk analysis of a rainfed rice production system in Tarlac, Central Luzon, Philippines. Experimental Agriculture, 35(2), 225–237.
Extract smooth coefficients
Description
Extracts the smooth coefficients of a fitted model. smooth_coef is an alias of
smoothcoef.
Usage
smoothcoef(x)
Arguments
x |
An object of class 'spscsfa'. |
Value
A list with two components:
theta_beta |
The matrix of the smooth coefficients of the first step,
|
alpha |
A matrix with one column: the estimates of the intercept
|
Semiparametric Smooth-Coefficient Stochastic Frontier Analysis
Description
Estimates a semiparametric smooth-coefficient stochastic frontier model
y_i = \alpha(Z_i) + X_i' \beta(Z_i) + v_i - u(Z_i)
following Sun and Kumbhakar (2013). The model allows the coefficients of the
parametric part to vary smoothly with a set of nonparametric variables.
Technical efficiency and inefficiency are computed using the Battese and
Coelli (1988) (BC) and Jondrow et al. (1982) (JLMS) methods, respectively.
Confidence intervals for technical efficiency are computed using the
approach of Horrace and Schmidt (1996). Marginal effects of the inefficiency
determinants on the JLMS inefficiency are computed following Kumbhakar and Sun (2013).
Usage
spscsfa(
formula,
data = NULL,
ezdat = NULL,
spscreg = c("lc", "ll"),
bandwidth = NULL,
gradients = FALSE,
verbose = TRUE,
sig_level = 0.05,
optim_method = "optim_BFGS",
optim_control = list(maxit = 5000),
stval = "automatic",
penalty = 1e+08
)
Arguments
formula |
A formula of the form |
data |
Optional data frame containing all variables used in |
ezdat |
Optional data frame containing evaluation points for the
nonparametric variables |
spscreg |
Character string; the estimator of the smooth coefficients
in the first step. |
bandwidth |
Optional bandwidths for the smooth-coefficient estimation.
If |
gradients |
Logical; if |
verbose |
Logical; if |
sig_level |
Significance level for confidence intervals of technical efficiency (default 0.05). |
optim_method |
Character string specifying the optimizer for MLE in the second step:
|
optim_control |
List of control parameters passed to the optimizer for MLE in the second step. |
stval |
Starting values for the MLE in the second step. Use |
penalty |
To ensure positivity of estimated |
Details
The stochastic production frontier model is specified as
y_i = \alpha(Z_i) + X_i' \beta(Z_i) + v_i - u(Z_i),
where y_i can be viewed as the log of output, X_i can be viewed as a
vector of the log of inputs, and Z_i is a vector of covariates.
\alpha(\cdot) is the intercept, and \beta(\cdot) is the slope vector.
Both \alpha(\cdot) and \beta(\cdot) are unknown functions of Z_i.
The noise term v_i \sim iid N(0, \sigma_v^2).
The inefficiency term is u(Z_i) = \sigma_u(Z_i) \eta_i, where
\eta_i \sim iid N^+(0,1).
The random variables \eta_i and v_i are assumed to be independent of
each other and independent of X and Z.
To guarantee positivity of \sigma_u(Z_i),
\sigma_u(Z_i) = \exp(\delta_0 + \delta_1' Z_i).
These imply that
E(u_i \mid Z_i)
= \sigma_u(Z_i) E(\eta_i \mid Z_i)
= \sqrt{2/\pi}\,\sigma_u(Z_i)
= \sqrt{2/\pi}\,\exp(\delta_0 + \delta_1' Z_i)
and
\sigma^2_u(Z_i) = \exp[2(\delta_0 + \delta_1' Z_i)].
The estimation involves two steps. In the first step, estimate
\theta(Z_i) (Intercept (theta)), \beta(Z_i), and
\varepsilon_i from
y_i = \theta(Z_i) + X_i'\beta(Z_i) + \varepsilon_i,
where
\theta(Z_i) = \alpha(Z_i)-E(u_i\mid Z_i)
and
\varepsilon_i = v_i-[u(Z_i)-E(u_i\mid Z_i)]
= E(u_i\mid Z_i) + v_i-u(Z_i).
Then in the second step, use the estimated \varepsilon_i to estimate
\delta_0, \delta_1 and \sigma^2_v from
\varepsilon_i =
\sqrt{2/\pi}\,\exp(\delta_0+\delta_1'Z_i)
+ v_i
- \exp(\delta_0+\delta_1'Z_i)\eta_i.
Finally, compute
E(u_i \mid Z_i) = \sqrt{2/\pi}\,\exp(\delta_0 + \delta_1' Z_i)
from estimated \delta_0 and \delta_1. The intercept in the original
stochastic production frontier \alpha(Z_i)
(Intercept (alpha)) is recovered as
\alpha(Z_i)
=\theta(Z_i)+E(u_i\mid Z_i).
See Sun and Kumbhakar (2013, page 306) for more details.
The smooth coefficients of the first step can be estimated by either a local-constant
(LC) or a local-linear (LL) kernel estimator, as chosen by spscreg.
With spscreg = "lc" (default), the bandwidths are selected by
np::npscoefbw and the smooth coefficients are estimated by
np::npscoef with default arguments. With spscreg = "ll", the
bandwidths are selected by llscoefbw and the smooth
coefficients are estimated by llscoef. If bandwidth is
NULL (default), the bandwidths are selected by least-squares
cross-validation (LSCV) for the chosen estimator. The residuals of the chosen
estimator are used in the second step, the maximum likelihood estimation.
If gradients = TRUE, the gradients of the first-step smooth
coefficients with respect to the continuous variables in z are
estimated as well: by lcscoefgrad for the local-constant
estimator and by llscoef for the local-linear estimator. Since
\alpha(Z_i) = \theta(Z_i) + E(u_i \mid Z_i), the gradient of
\alpha(Z_i) with respect to the lth continuous Z variable is
\partial\alpha(Z_i)/\partial z_l = \partial\theta(Z_i)/\partial z_l
+ \sqrt{2/\pi}\,\exp(\delta_0+\delta_1'Z_i)\,\delta_{1l},
where \delta_{1l} is the coefficient of that variable in the
inefficiency equation (zero if the variable is not an inefficiency
determinant) and \partial\theta(Z_i)/\partial z_l is evaluated at the
training data.
The analytic gradients of the local-constant smooth coefficient estimator
with respect to the continuous z variables, returned in
smooth_coefficients_grad if gradients = TRUE and
spscreg = "lc", are computed following Geng and Sun (2019); see
lcscoefgrad. The marginal effects of the inefficiency determinants on the JLMS
inefficiency estimates, returned as inefficiency_grad if
gradients = TRUE, are computed following Kumbhakar and Sun (2013).
The JLMS inefficiency estimator is the conditional mean
E(u_i \mid \varepsilon_i) = \tilde\mu_i + \sigma_{*i}\, g_i, where
\tilde\mu_i = -\varepsilon_i \sigma_{ui}^2 / \sigma_i^2,
\sigma_{*i} = \sigma_{ui}\sigma_v / \sigma_i,
\sigma_i^2 = \sigma_{ui}^2 + \sigma_v^2,
m_i = \tilde\mu_i / \sigma_{*i} and
g_i = \phi(m_i) / \Phi(m_i). Since \sigma_{ui} = \exp(\delta_0 +
\delta_1' Z_i) depends on the inefficiency determinants, the first-order
analytic derivative of the JLMS estimator with respect to the l-th
inefficiency determinant (holding \varepsilon_i fixed) is
\frac{\partial E(u_i \mid \varepsilon_i)}{\partial z_{li}}
= \frac{\delta_{1l}}{\sigma_i^2}
\Bigl[\sigma_v^2 \sigma_{*i} \bigl(g_i (1 + m_i^2) + m_i g_i^2\bigr)
- 2 \sigma_{*i}^2 \varepsilon_i (1 - g_i^2 - m_i g_i)\Bigr].
This is the \sigma_{ui} channel of Equation (9) in Kumbhakar and Sun
(2013), where the coefficient on z_{li} in \sigma_{ui} was called
\gamma_l (it is \delta_{1l} here, as in Sun and Kumbhakar, 2013);
the other two channels vanish because \mu_i = 0 and \sigma_v is
constant. If gradients = TRUE, the derivatives are returned in
inefficiency_grad. A column of inefficiency_grad that belongs
to a dummy variable created from a categorical inefficiency determinant is
the analytic partial derivative with the dummy treated as continuous.
Value
A list of class "spscsfa" containing:
smooth_coefficients |
Smooth coefficient estimates (local-constant if
|
smooth_coefficients_grad |
If |
mle_results |
MLE parameter estimates, standard errors, and log-likelihood. |
technical_efficiency |
Technical efficiency scores (BC method) and confidence intervals. |
inefficiency |
Inefficiency estimates (JLMS method). |
inefficiency_grad |
If |
model_summary |
Summary information about the model specification. |
formula_parsed |
Parsed formula components. |
data_info |
Information about data cleaning and observations used. |
model_frame |
The estimation sample: the model frame, without missing
values, of the variables used in |
Note
Results can be summarized using summary.spscsfa.
Extraction functions for smooth coefficients, technical efficiency and
inefficiency are available through smoothcoef,
te, and inefficiency, respectively. If
gradients = TRUE, the gradients are extracted with
grad.
The smooth coefficients can be plotted against the continuous nonparametric
variables using plot.spscsfa.
spsc_sfa is an alias of spscsfa, smooth_coef is an alias of smoothcoef,
and the methods of the class "spscsfa" also handle fitted models and
summaries of the class "spsc_sfa".
Author(s)
Kai Sun <ksun1@shu.edu.cn>
References
George E. Battese and Tim J. Coelli (1988). Prediction of firm-level technical efficiencies with a generalized frontier production function and panel data. Journal of Econometrics, 38(3), 387–399.
Xin Geng and Kai Sun (2019). Gradient estimation of the local-constant semiparametric smooth coefficient model. Economics Letters, 185, 108684.
William C. Horrace and Peter Schmidt (1996). Confidence statements for efficiency estimates from stochastic frontier models. Journal of Productivity Analysis, 7(2), 257–282.
James Jondrow, C.A. Knox Lovell, Ivan S. Materov, and Peter Schmidt (1982). On the estimation of technical inefficiency in the stochastic frontier production function model. Journal of Econometrics, 19(2–3), 233–238.
Subal C. Kumbhakar and Kai Sun (2013). Derivation of marginal effects of determinants of technical inefficiency. Economics Letters, 120(2), 249–253.
Kai Sun and Subal C. Kumbhakar (2013). Semiparametric smooth-coefficient stochastic frontier model. Economics Letters, 120(2), 305–309.
Examples
set.seed(1)
n <- 100
x <- rnorm(n)
z <- runif(n)
ez <- seq(0, 1, length = 1000)
q <- rnorm(n)
y <- cos(z) + sin(z) * x + rnorm(n, 0, 0.1) -
exp(0.1 + 0.1 * z) * abs(rnorm(n, 0, 1))
dat <- data.frame(output = y, input = x, covariate = z, ineffvar = q)
# covariate enters the nonparametric part and is also treated as inefficiency determinant
result <- spscsfa(output ~ input | covariate, data = dat,
optim_method = "optim_BFGS", verbose = TRUE)
result$smooth_coefficients
result$mle_results
result$technical_efficiency
result$inefficiency
# This gives the same results: the inefficiency variables default to the nonparametric part
result <- spscsfa(output ~ input | covariate | NULL, data = dat,
optim_method = "optim_BFGS", verbose = TRUE)
# Use evaluation data (ezdat) for estimating the smooth coefficients
result <- spscsfa(output ~ input | covariate, data = dat,
ezdat = data.frame(covariate = ez),
optim_method = "optim_BFGS", verbose = TRUE)
nrow(result$smooth_coefficients$theta_beta)
# Gradients of the smooth coefficients, of alpha, and of the inefficiency
result <- spscsfa(output ~ input | covariate, data = dat, gradients = TRUE, verbose = TRUE)
result$smooth_coefficients_grad$theta_beta_grad
head(result$smooth_coefficients_grad$alpha_grad)
head(result$inefficiency_grad)
grad(result, zvar = "covariate")
# Three-part formula: nonparametric part differs from inefficiency determinants
result <- spscsfa(output ~ input | covariate | ineffvar, data = dat,
optim_method = "optim_BFGS", verbose = TRUE)
# Customized bandwidth for the nonparametric part
result <- spscsfa(output ~ input | covariate | ineffvar, data = dat,
optim_method = "optim_BFGS", bandwidth = c(0.1), verbose = TRUE)
# Restrict no inefficiency determinants (intercept only)
result <- spscsfa(output ~ input | covariate | 1, data = dat,
optim_method = "optim_BFGS", verbose = TRUE)
# Local-linear smooth coefficients (the default is spscreg = "lc")
result <- spscsfa(output ~ input | covariate, data = dat, spscreg = "ll", verbose = TRUE)
result$smooth_coefficients$theta_beta
# Gradients with local-linear smooth coefficients
result <- spscsfa(output ~ input | covariate, data = dat,
spscreg = "ll", gradients = TRUE, verbose = TRUE)
result
# User-provided bandwidth for the local-linear estimator: llscoefbw() with
# LSCV = FALSE returns a named vector
bw <- llscoefbw(output ~ input | covariate, bws = 0.2, data = dat, LSCV = FALSE)
result <- spscsfa(output ~ input | covariate, data = dat,
spscreg = "ll", bandwidth = bw, verbose = TRUE)
# More variables
set.seed(1)
n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n)
z1 <- runif(n)
z2 <- runif(n)
y <- cos(z1 + z2) + sin(z1 - z2) * x1 + sin(z1 + z2) * x2 +
rnorm(n, 0, 0.1) - exp(0.1 + 0.1 * z1 + 0.1 * z2) * abs(rnorm(n, 0, 1))
dat <- data.frame(y, x1, x2, z1, z2)
result <- spscsfa(y ~ x1 + x2 | z1 + z2, data = dat, spscreg = "ll", gradients = TRUE)
# z3 is an unordered categorical variable
# spscsfa() detects if z3 is a factor
dat$z3 <- factor(rep(sample(c("A", "B", "C", "D", "E")), 20))
# z1, z2, z3 enter the nonparametric part and are also inefficiency determinants
result <- spscsfa(y ~ x1 + x2 | z1 + z2 + z3, data = dat)
# z3 is preserved as an unordered categorical variable in the nonparametric part
result$smooth_coefficients$bandwidth
# z3 is converted to dummy variables as inefficiency determinants
result$mle_results$delta_table
# Same as above (inefficiency defaults to nonparametric part)
result <- spscsfa(y ~ x1 + x2 | z1 + z2 + z3 | NULL, data = dat)
# Three-part formula: use z3 only as the inefficiency determinant
result <- spscsfa(y ~ x1 + x2 | z1 + z2 + z3 | z3, data = dat)
# Use external variables as inefficiency determinants
dat$q1 <- rnorm(n)
dat$q2 <- rnorm(n)
result <- spscsfa(y ~ x1 + x2 | z1 + z2 + z3 | q1 + q2, data = dat)
# No inefficiency determinants (intercept only)
result <- spscsfa(y ~ x1 + x2 | z1 + z2 + z3 | 1, data = dat, verbose = TRUE)
# Application with real data (ricephil)
## Not run:
data(ricephil)
ricephil$FARMERCODE <- as.factor(ricephil$FARMERCODE)
# this example shows categorical variable handling
result <- spscsfa(PROD ~ AREA + LABOR + NPK | EDYRS + FARMERCODE | FARMERCODE,
data = ricephil, optim_method = "optim_BFGS", verbose = TRUE)
# FARMERCODE is preserved as an unordered categorical variable in the nonparametric part
result$smooth_coefficients$bandwidth
# FARMERCODE is converted to dummy variables as inefficiency determinants
result$mle_results$delta_table
## End(Not run)
Summarize SPSC-SFA results
Description
summary method for objects of class "spscsfa". Following the
convention of summary, it computes and returns a
summary object rather than printing one, so that the result can be assigned,
subsetted and reused programmatically. The summary is rendered by
print.summary.spscsfa, which is dispatched automatically when
the object is auto-printed at the console.
Usage
## S3 method for class 'spscsfa'
summary(object, digits = max(3L, getOption("digits") - 3L), ...)
Arguments
object |
An object of class |
digits |
Integer; the minimum number of significant digits to use when
the summary is printed. Stored in the returned object and used as the
default by |
... |
Further arguments passed to or from other methods. Accepted for
compatibility with the generic |
Value
An object of class "summary.spscsfa": a list with components
- formula, n_obs, data_supplied, data_info
Model formula, number of observations used, whether a data frame was supplied, and the data cleaning information.
- n_params_x, x_names
Number and names of the parametric variables.
- n_params_z, z_names, z_is_factor, z_is_ordered
Number, names and types of the nonparametric variables.
- categorical_conversion_warning
Note about categorical inefficiency determinants converted to binary variables, or
NULL.- spscreg
The estimator of the smooth coefficients:
"lc"(local-constant) or"ll"(local-linear).- bandwidth, bandwidth_user_supplied, n_training, n_evaluation, used_evaluation_data
Step 1 smooth-coefficient information.
- smooth_coefficients
Numeric matrix with one row for each smooth coefficient (
\theta(z), the intercept, and then each\beta(z)) and the columnsMin.,1st Qu.,Median,Mean,3rd Qu.andMax.: the summary statistics of the Step 1 estimates over the evaluation points (the rows ofezdatif it was supplied tospscsfa, the training observations otherwise), orNULLif the estimates are not available.- inefficiency_vars_null, n_inefficiency_vars, sigma_u_formula
-
Specification of the inefficiency equation.
- alpha
Numeric matrix with one row,
Intercept (alpha), and the columnsMin.,1st Qu.,Median,Mean,3rd Qu.andMax.: the summary statistics of the intercept\alpha(z) = \theta(z) + E(u|z)over the training observations, orNULLif it is not available.- coefficients
Numeric matrix of the
deltacoefficients with columnsEstimate,Std. Error,z valueandPr(>|z|).- variance
Numeric matrix, in the same layout, for
sigma2_v.- logLik, optim_method
Maximized log-likelihood and optimizer used.
- te, inefficiency
Named numeric vectors of summary statistics (
Mean,SD,Min,Max,Median) for the BC technical efficiency and JLMS inefficiency estimates.- sig_level
Significance level used for the technical efficiency confidence intervals.
- gradients
NULLunless the model was estimated withgradients = TRUE. Otherwise a list with the elementstheta_beta_grad(a list with one matrix for each continuouszvariable),alpha_gradandinefficiency_grad(matrices), each with one row for each gradient and the columnsMin.,1st Qu.,Median,Mean,3rd Qu.andMax.: the summary statistics of that gradient. Fortheta_beta_gradthey are computed over the evaluation points: the rows ofezdatif it was supplied tospscsfa, the training observations otherwise. Foralpha_gradandinefficiency_gradthey are always computed over the training observations.- digits
Default number of significant digits for printing.
See Also
print.summary.spscsfa, print.spscsfa,
spscsfa
Extract technical efficiency scores
Description
Extract technical efficiency scores
Usage
te(x)
Arguments
x |
An object of class |
Value
A numeric vector of technical efficiency estimates.