bw <- llscoefbw(
y ~ x | z1 + z2,
data = dat,
max.search = 2L,
grid_scan = 2^c(-2:5)
)
bw$bw
#> z1 z2
#> 1.0112281 0.1098348
bw$lscv
#> [1] 0.4131019
bw$n_feval
#> [1] 81
bw$scan_factor
#> [1] 1
The returned object stores the selected bandwidth vector in bw$bw, the LSCV function value in bw$lscv, and the number of criterion evaluations in bw$n_feval. bw$scan_factor identifies the multiplier used to initialize the Nelder–Mead search that produced the selected bandwidths. It is 1 when the search from the original starting values gives the best result; otherwise it is the corresponding element of grid_scan. It identifies the starting point of the successful search, not a multiplier that must be applied to the final optimized bandwidths.
Estimate smooth coefficients with llscoef()
The bandwidth object can be passed directly to llscoef(). Set betas, residuals, and gradients to TRUE to request the corresponding results.
ll_fit <- llscoef(
bw,
betas = TRUE,
residuals = TRUE,
gradients = TRUE
)
head(coef(ll_fit))
#> Intercept x
#> [1,] -0.7549730 0.24808914
#> [2,] -0.4051996 -0.10563703
#> [3,] 0.6618143 0.76958556
#> [4,] -0.1828546 0.05444311
#> [5,] -0.1943742 -0.76117380
#> [6,] -0.6984837 -0.46875480
head(residuals(ll_fit))
#> [1] 0.945025684 -0.014871922 -0.001088696 0.267423581 0.427518363
#> [6] 0.706623478
head(ll_fit$grad)
#> d(Intercept)/d(z1) d(Intercept)/d(z2) d(x)/d(z1) d(x)/d(z2)
#> [1,] -0.7718702 -1.687306 1.1671360 -2.1132186
#> [2,] -0.7343571 -1.782581 1.3137553 -1.9057324
#> [3,] 0.9309584 -3.112874 0.6570694 -1.0728161
#> [4,] -0.8324892 -1.406925 1.7780919 -2.3290642
#> [5,] -1.4660867 -1.526035 1.1738315 -0.5401165
#> [6,] -1.2565855 -1.922726 1.0837268 -1.0616251
The coef() method returns the estimated smooth intercept and slope coefficients. The residuals() method returns the training residuals. The grad component contains derivatives of the smooth coefficients with respect to the continuous z variables, ordered by coefficient and then by continuous variable.
The training sample is retained in the bandwidth object. The newdata argument specifies where the smooth coefficients are evaluated; it must contain the regressors in the x and z parts of the formula. The response is not required for evaluation.
eval_dat <- data.frame(
x = mean(dat$x),
z1 = seq(min(dat$z1), max(dat$z1), length.out = 50),
z2 = mean(dat$z2)
)
ll_grid <- llscoef(bw, newdata = eval_dat, betas = TRUE)
head(coef(ll_grid))
#> Intercept x
#> [1,] -0.06046866 -0.7005078
#> [2,] -0.07845649 -0.6828119
#> [3,] -0.09640097 -0.6649886
#> [4,] -0.11430255 -0.6470389
#> [5,] -0.13216169 -0.6289637
#> [6,] -0.14997885 -0.6107639
Here x is held at its sample mean, z2 is held at its sample mean, and the coefficient functions are evaluated over a grid of z1 values. Residuals, when requested, remain training-sample residuals even if newdata is used.
Use the bandwidths in the stochastic frontier model
The bandwidth object can also be supplied to spscsfa() with spscreg = "ll". The model then uses the local-linear smooth-coefficient estimator in its first step and the maximum-likelihood step for the inefficiency parameters.
fit_ll <- spscsfa(
y ~ x | z1 + z2,
data = dat,
spscreg = "ll",
bandwidth = bw,
gradients = TRUE,
verbose = FALSE
)
summary(fit_ll)
#>
#> ==================== SPSC-SFA RESULTS ====================
#>
#> Formula: y ~ x | z1 + z2
#> Number of observations: 100
#>
#> --- Data Information ---
#> Data frame supplied: YES
#> Rows used: 100 out of 100
#> Variables from data: y, x, z1, z2
#>
#> Data source: from formula and data
#>
#> --- Parametric variables (x) ---
#> Count: 1
#> x
#>
#> --- Nonparametric variables (z) ---
#> Count: 2
#> z1 (continuous)
#> z2 (continuous)
#>
#> --- STEP 1: SMOOTH COEFFICIENTS ---
#> Smooth-coefficient estimator: local-linear (LL)
#> Bandwidth source: user-supplied
#> Training observations: 100
#> Evaluation observations: 100
#> Coefficients estimated on: training data
#>
#> Smooth Coefficients Summary (over the training observations):
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> Intercept (theta) -1.5796 -0.6422 -0.2267 -0.3455 -0.02446 0.6618
#> x -0.9505 -0.5428 -0.1333 -0.1086 0.24211 1.1887
#>
#> --- STEP 2: MLE RESULTS ---
#> Inefficiency variables: ALL z variables from formula's z part (no third part)
#> Number of inefficiency variable slope coefficients, delta_1: 2
#>
#> Intercept (alpha) Summary (over the training observations):
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> Intercept (alpha) -0.9045 0.02688 0.3892 0.294 0.5804 1.238
#>
#> Inefficiency determinants (sigma_u = exp(delta_0 + delta_1'z)):
#>
#> Estimate Std. Error z value Pr(>|z|)
#> Intercept -0.36183 0.28387 -1.275 0.202
#> z1 0.02467 0.32559 0.076 0.940
#> z2 0.25196 0.32644 0.772 0.440
#>
#> --- Variance Parameters ---
#> Estimate Std. Error z value Pr(>|z|)
#> sigma2_v 0.06201 0.04038 1.536 0.125
#>
#> Log-likelihood: -75.84 (optimizer: optim_BFGS )
#>
#> --- TECHNICAL EFFICIENCY (BC Method) ---
#> Confidence level for TE intervals: 95%
#> Mean SD Min Max Median
#> 0.5807 0.2053 0.1519 0.8937 0.6115
#>
#> --- INEFFICIENCY (JLMS Method) ---
#> Mean SD Min Max Median
#> 0.6414 0.4226 0.1173 1.9130 0.5169
#>
#> --- GRADIENTS ---
#>
#> Smooth coefficients, theta (intercept) and beta (local-linear estimates): gradients with respect to each continuous z variable
#> Summary statistics over the training observations.
#>
#> With respect to z1:
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> d(Intercept (theta))/d(z1) -1.9883 -1.3076 -0.8432 -0.7967 -0.7379 1.111
#> d(x)/d(z1) 0.4921 0.8434 1.1249 1.1393 1.3146 1.879
#>
#> With respect to z2:
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> d(Intercept (theta))/d(z2) -3.625 -1.915 -1.508 -1.424 -0.8959 0.1810
#> d(x)/d(z2) -2.442 -1.997 -1.442 -1.364 -1.0464 0.3357
#>
#> Summary statistics over the training observations.
#> Intercept alpha: gradients with respect to each continuous z variable
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> d(Intercept (alpha))/d(z1) -1.971 -1.290 -0.8281 -0.7809 -0.7224 1.1249
#> d(Intercept (alpha))/d(z2) -3.484 -1.737 -1.3335 -1.2629 -0.7468 0.3478
#>
#> JLMS inefficiency: gradients with respect to the inefficiency determinants
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> z1 0.0001431 0.000927 0.002191 0.002628 0.004333 0.007831
#> z2 0.0014609 0.009466 0.022377 0.026834 0.044250 0.079967
Extract and inspect gradients
With gradients = TRUE, spscsfa() returns gradients of the smooth coefficients with respect to the continuous z variables, gradients of the recovered intercept alpha, and analytic gradients of the JLMS inefficiency estimates with respect to the inefficiency determinants. grad() extracts these components, optionally for selected variables.
g_all <- grad(fit_ll)
names(g_all)
#> [1] "theta_beta_grad" "alpha_grad" "inefficiency_grad"
# Gradients with respect to z2
g_z2 <- grad(fit_ll, zvar = "z2")
head(g_z2$theta_beta_grad$z2)
#> d(Intercept (theta))/d(z2) d(x)/d(z2)
#> [1,] -1.687306 -2.1132186
#> [2,] -1.782581 -1.9057324
#> [3,] -3.112874 -1.0728161
#> [4,] -1.406925 -2.3290642
#> [5,] -1.526035 -0.5401165
#> [6,] -1.922726 -1.0616251
head(g_z2$alpha_grad)
#> d(Intercept (alpha))/d(z2)
#> [1,] -1.525666
#> [2,] -1.623976
#> [3,] -2.967708
#> [4,] -1.253212
#> [5,] -1.350671
#> [6,] -1.743789
head(g_z2$inefficiency_grad)
#> z2
#> [1,] 0.001486344
#> [2,] 0.026221023
#> [3,] 0.026733529
#> [4,] 0.013944770
#> [5,] 0.008979255
#> [6,] 0.003543902
The smooth-coefficient gradients are evaluated at the points used for the first-step estimates. The alpha_grad and inefficiency_grad components are computed at the training observations. A categorical variable in the z part has no smooth-coefficient derivative with respect to its levels. If it also enters the inefficiency equation, its converted binary variables can have corresponding columns in inefficiency_grad.
Plot local-linear smooth coefficients
The plot.spscsfa() method uses the estimator and bandwidths of the fitted model. It evaluates each smooth coefficient over a grid of one continuous z variable at a time; other continuous z variables are held at their sample means.
plot(fit_ll, zvar = "z2", n_grid = 60, rug = TRUE, ask = FALSE)