fitted.gkwreg | R Documentation |
Extracts the fitted mean values (predicted expected values of the response)
from a fitted Generalized Kumaraswamy (GKw) regression model object of class
"gkwreg"
. This is an S3 method for the generic
fitted.values
function.
## S3 method for class 'gkwreg'
fitted(object, family = NULL, ...)
object |
An object of class |
family |
Character string specifying the distribution family under which
the fitted mean values should be calculated. If |
... |
Additional arguments, currently ignored by this method. |
This function retrieves or calculates the fitted values, which represent the
estimated conditional mean of the response variable given the covariates
(E(Y | X)
).
The function attempts to retrieve fitted values efficiently using the following priority:
Directly from the fitted.values
component stored in the object
,
if available and complete. It includes logic to handle potentially
incomplete stored values via interpolation (approx
) for
very large datasets where only a sample might be stored.
By recalculating the mean using stored parameter vectors for each
observation (object$parameter_vectors
) and an internal function
(calculateMeans
), if available.
From the fitted
component within the TMB report (object$tmb_object$report()
),
if available, potentially using interpolation as above.
As a fallback, by calling predict(object, type = "response", family = family)
.
Specifying a family
different from the one used to fit the model will
always force recalculation using the predict
method (step 4).
A numeric vector containing the fitted mean values. These values are
typically bounded between 0 and 1, corresponding to the scale of the original
response variable. The length of the vector corresponds to the number of
observations used in the model fit (considering subset
and na.action
).
Lopes, J. E.
gkwreg
, predict.gkwreg
,
residuals.gkwreg
, fitted.values
# Assume 'mydata' exists with response 'y' and predictors 'x1', 'x2'
# and that rgkw() is available and data is appropriate (0 < y < 1).
set.seed(456)
n <- 100
x1 <- runif(n, -1, 1)
x2 <- rnorm(n)
alpha <- exp(0.5 + 0.2 * x1)
beta <- exp(0.8 - 0.3 * x1 + 0.1 * x2)
gamma <- exp(0.6)
delta <- plogis(0.0 + 0.2 * x1)
lambda <- exp(-0.2 + 0.1 * x2)
# Use stats::rbeta as placeholder if rgkw is not available
y <- stats::rbeta(n, shape1 = gamma * alpha, shape2 = delta * beta) # Approximation
y <- pmax(pmin(y, 1 - 1e-7), 1e-7)
mydata <- data.frame(y = y, x1 = x1, x2 = x2)
# Fit a GKw model
model <- gkwreg(y ~ x1 | x1 + x2 | 1 | x1 | x2, data = mydata, family = "gkw")
# Extract fitted values (using the original 'gkw' family)
fitted_vals_gkw <- fitted(model)
# Extract fitted values recalculated as if it were a Beta model
# (using the fitted gamma and delta coefficients)
fitted_vals_beta <- fitted(model, family = "beta")
# Plot observed vs. fitted (using original family)
response_y <- model$y # Get the response variable used in the fit
if (!is.null(response_y)) {
plot(response_y, fitted_vals_gkw,
xlab = "Observed Response", ylab = "Fitted Mean Value",
main = "Observed vs Fitted Values (GKw Family)",
pch = 1, col = "blue"
)
abline(0, 1, col = "red", lty = 2) # Line y = x
} else {
print("Response variable not found in model object to create plot.")
}
# Compare fitted values under different family assumptions
head(data.frame(GKw_Fitted = fitted_vals_gkw, Beta_Fitted = fitted_vals_beta))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.