263
huber1 <- function(llocation = "identity",
270
A1 <- (2 * dnorm(k) / k - 2 * pnorm(-k))
273
if (!is.Numeric(imethod, allow = 1, integ = TRUE, posit = TRUE) ||
275
stop("argument 'imethod' must be 1 or 2 or 3 or 4")
277
if (!is.Numeric(k, allow = 1, posit = TRUE))
278
stop("bad input for argument 'k'")
280
if (mode(llocation) != "character" && mode(llocation) != "name")
281
llocation = as.character(substitute(llocation))
282
if (!is.list(elocation)) elocation = list()
285
blurb = c("Huber least favorable distribution\n\n",
287
namesof("location", llocation, earg = elocation), "\n\n",
289
initialize = eval(substitute(expression({
291
c(namesof("location", .llocat, earg = .elocat, tag = FALSE))
293
if (ncol(y <- cbind(y)) != 1)
294
stop("response must be a vector or a one-column matrix")
296
if (!length(etastart)) {
297
junk = lm.wfit(x = x, y = y, w = w)
298
location.init <- if ( .llocat == "loge") pmax(1/1024, y) else {
299
if ( .imethod == 3) {
300
rep(weighted.mean(y, w), len = n)
301
} else if ( .imethod == 2) {
302
rep(median(rep(y, w)), len = n)
303
} else if ( .imethod == 1) {
310
theta2eta(location.init, .llocat, earg = .elocat))
312
}), list( .llocat = llocation,
314
.imethod = imethod ))),
315
linkinv = eval(substitute(function(eta, extra = NULL) {
316
eta2theta(eta, .llocat, earg = .elocat)
317
}, list( .llocat = llocation,
318
.elocat = elocation ))),
319
last = eval(substitute(expression({
320
misc$link <- c("location" = .llocat )
321
misc$earg <- list("location" = .elocat )
322
misc$expected <- TRUE
324
misc$imethod <- .imethod
325
}), list( .llocat = llocation,
327
.k = k, .imethod = imethod ))),
328
loglikelihood = eval(substitute(
329
function(mu, y, w, residuals = FALSE, eta, extra = NULL) {
330
location <- eta2theta(eta, .llocat, earg = .elocat)
332
if (residuals) stop("loglikelihood residuals not ",
333
"implemented yet") else {
334
sum(w * dhuber(y, k = kay, mu = location, sigma = 1,
337
}, list( .llocat = llocation,
340
vfamily = c("huber1"),
341
deriv = eval(substitute(expression({
342
mylocat <- eta2theta(eta, .llocat, earg = .elocat)
345
zedd <- (y - mylocat) # / myscale
346
cond2 <- (abs(zedd) <= myk)
347
cond3 <- (zedd > myk)
349
dl.dlocat <- -myk + 0 * zedd # cond1
350
dl.dlocat[cond2] <- zedd[cond2]
351
dl.dlocat[cond3] <- myk # myk is a scalar
352
dl.dlocat <- dl.dlocat # / myscale
356
dl.dscale <- (-myk * zedd)
357
dl.dscale[cond2] <- (zedd^2)[cond2]
358
dl.dscale[cond3] <- ( myk * zedd)[cond3]
359
dl.dscale <- (-1 + dl.dscale) / myscale
362
dlocat.deta <- dtheta.deta(mylocat, .llocat, earg = .elocat)
364
c(w) * cbind(dl.dlocat * dlocat.deta)
366
}), list( .llocat = llocation,
368
.eps = eps, .k = k ))),
369
weight = eval(substitute(expression({
370
wz <- matrix(as.numeric(NA), n, 1) # diag matrix; y is one-col too
375
temp4 <- erf(myk / sqrt(2))
376
ed2l.dlocat2 <- temp4 * (1 - .eps) # / myscale^2
379
wz[, iam(1,1,M)] <- ed2l.dlocat2 * dlocat.deta^2
382
}), list( .eps = eps ))))