Description Usage Arguments Details Value References See Also Examples
A local version of logistic regression for classification that puts increased emphasis on a good model fit near the decision boundary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | dalr(X, ...)
## S3 method for class 'formula'
dalr(formula, data, weights, ...,
subset, na.action)
## S3 method for class 'data.frame'
dalr(X, ...)
## S3 method for class 'matrix'
dalr(X, Y, weights = rep(1, nrow(X)),
intercept = TRUE, ..., subset, na.action)
## Default S3 method:
dalr(X, Y, thr = 0.5,
wf = c("biweight", "cauchy", "cosine", "epanechnikov", "exponential", "gaussian", "optcosine", "rectangular", "triangular"),
bw, k, nn.only = TRUE, itr = 3, intercept = TRUE,
weights = rep(1, nrow(X)), ...)
|
formula |
A formula of the form |
data |
A |
X |
(Required if no |
Y |
(Required if no |
thr |
The threshold value used to predict class membership, defaults to 0.5. See Details. |
wf |
A window function which is used to calculate
weights that are introduced into the fitting process.
Either a character string or a function, e.g. |
bw |
(Required only if |
k |
(Required only if |
nn.only |
(Required only if |
itr |
Number of iterations for model fitting, defaults to 3. See also the Details section. |
intercept |
Should the model contain an intercept.
passed to |
weights |
Initial observation weights (defaults to a vector of 1s). |
... |
Further arguments to |
subset |
An index vector specifying the cases to be used in the training sample. (NOTE: If given, this argument must be named.) |
na.action |
The default is first, any
|
Local logistic regression (Hand and Vinciotti, 2003) is a
modification of the standard logistic regression approach
to discrimination. For discrimination a good fit of the
model is required especially near the true decision
boundary. Therefore weights are introduced into the
fitting process that reflect the proximity of training
points to the decision boundary. Let the class levels be
0 and 1. The distance of a training observation x
to the decision boundary is measured by means of the
difference P(1 | x) - thr where thr
is a
threshold in [0,1]. Since P(1 | x) is not
known in advance an iterative procedure is required. We
start by fitting an unweighted logistic regression model
to the data in order to obtain initial estimates of
P(1 | x). These are used to calculate the
observation weights. Model fitting and calculation of
weights is done several times in turn. By default, the
number of iterations is limited to 3.
The name of the window function (wf
) can be
specified as a character string. In this case the window
function is generated internally in dalr
.
Currently supported are "biweight"
,
"cauchy"
, "cosine"
, "epanechnikov"
,
"exponential"
, "gaussian"
,
"optcosine"
, "rectangular"
and
"triangular"
.
Moreover, it is possible to generate the window functions
mentioned above in advance (see
wfs
) and pass them to
dalr
.
Any other function implementing a window function can
also be used as wf
argument. This allows the user
to try own window functions. See help on
wfs
for details.
Internally, glm.fit
with family =
binomial()
is used and the weights produced using
wf
are passed to glm.fit
via its
weights
argument.
If the predictor variables include factors, the formula interface must be used in order to get a correct model matrix.
Warnings about non-integer #successes in a binomial glm are expected.
An object of class "dalr"
inheriting from class
"glm"
, a list containing at least the following
components:
Values of glm
:
coefficients |
A named vector of coefficients. |
residuals |
The working residuals,
that is the residuals in the final iteration of the IWLS
fit. Since cases with zero weights are omitted, their
working residuals are |
fitted.values |
The fitted mean values, obtained by transforming the linear predictors by the inverse of the link function. |
rank |
The numeric rank of the fitted linear model. |
family |
The
|
linear.predictor |
The linear fit on link scale. |
deviance |
Up to a constant, minus twice the maximized log-likelihood. Where sensible, the constant is chosen so that a saturated model has deviance zero. |
aic |
A version of Akaike's An Information Criterion, minus twice the maximized log-likelihood plus twice the number of parameters, computed by the aic component of the family. For binomial and Poison families the dispersion is fixed at one and the number of parameters is the number of coefficients. For gaussian, Gamma and inverse gaussian families the dispersion is estimated from the residual deviance, and the number of parameters is the number of coefficients plus one. For a gaussian family the MLE of the dispersion is used so this is a valid value of AIC, but for Gamma and inverse gaussian families it is not. For families fitted by quasi-likelihood the value is NA. |
null.deviance |
The deviance for the null model, comparable with deviance. The null model will include the offset, and an intercept if there is one in the model. Note that this will be incorrect if the link function depends on the data other than through the fitted mean: specify a zero offset to force a correct calculation. |
iter |
The number of iterations of IWLS used. |
weights |
A list of length |
prior.weights |
A
list of length |
df.residual |
The residual degrees of freedom. |
df.null |
The residual degrees of freedom for the null model. |
y |
If requested (the default) the y vector used. (It is a vector even for a binomial model.) |
x |
If requested, the model matrix. |
model |
If requested (the default), the model frame. |
converged |
Logical. Was the IWLS algorithm judged to have converged? |
boundary |
Logical. Is the fitted value on the boundary of the attainable values? |
call |
The (matched) function call. |
formula |
The formula supplied. |
terms |
The |
data |
The data argument. |
offset |
The offset vector used. |
control |
The value of the control argument used. |
method |
The name of the fitter function used, currently always ~code"glm.fit". |
contrasts |
(Where relevant) the contrasts used. |
xlevels |
(Where relevant) a record of the levels of the factors used in fitting. |
na.action |
(Where
relevant) information returned by
|
Additionally, dalr
returns
lev |
The
class labels (the levels of |
thr |
The threshold used. |
itr |
The number of iterations used. |
wf |
The window function used. Always a function, even if the input was a string. |
bw |
(Only if |
k |
(Only if |
nn.only |
(Logical. Only if |
adaptive |
(Logical.)
|
Hand, D. J., Vinciotti, V. (2003), Local versus global models for classification problems: Fitting models where it matters, The American Statistician, 57(2) 124–130.
predict.dalr
, glm
,
predict.glm
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | # generate toy data set of Hand und Vinciotti (2003):
x1 <- x2 <- seq(0.1,1,0.05)
train <- expand.grid(x1 = x1, x2 = x2)
posterior <- train$x2/(train$x1 + train$x2)
y <- as.factor(sapply(posterior, function(x) sample(0:1, size = 1,
prob = c(1-x,x))))
train <- data.frame(train, y = y)
par(mfrow = c(1,3))
# contours of true class posterior probabilities:
plot(train$x1, train$x2, col = y, pch = 19, main = "true posteriors")
contour(x1, x2, matrix(posterior, length(x1)), add = TRUE)
# 0.3-contour line fit of logistic regression:
glob.fit <- glm(y ~ ., data = train, family = "binomial")
plot(train$x1, train$x2, col = y, pch = 19, main = "global fit")
contour(x1, x2, matrix(glob.fit$fitted.values, length(x1)),
levels = 0.3, add = TRUE)
# 0.3-contour line fit of local logistic regression:
loc.fit <- dalr(y ~ ., data = train, thr = 0.3, wf = "gaussian", bw = 0.2)
plot(train$x1, train$x2, col = y, pch = 19, main = "local fit")
contour(x1, x2, matrix(loc.fit$fitted.values, length(x1)),
levels = 0.3, add = TRUE)
# specify wf as a character string:
dalr(y ~ ., data = train , thr = 0.3, wf = "rectangular", k = 50)
# use window function generating function:
rect <- rectangular(100)
dalr(y ~ ., data = train, thr = 0.3, wf = rect)
# specify own window function:
dalr(y ~ ., data = train, thr = 0.3, wf = function(x) exp(-10*x^2))
# generate test data set:
x1 <- runif(200, min = 0, max = 1)
x2 <- runif(200, min = 0, max = 1)
test <- data.frame(x1 = x1, x2 = x2)
pred <- predict(loc.fit, test)
prob <- test$x2/(test$x1 + test$x2)
y <- as.factor(sapply(prob, function(x) sample(0:1, size = 1,
prob = c(1-x,x))))
mean(y != pred$class)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.