[Returnanalytics-commits] r3048 - in pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Sep 10 22:23:51 CEST 2013
Author: shubhanm
Date: 2013-09-10 22:23:51 +0200 (Tue, 10 Sep 2013)
New Revision: 3048
Added:
pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/glmi.R
pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/lmi.R
pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/glmi.Rd
pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/lmi.Rd
Modified:
pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION
Log:
Addition of support HAC/HC functions for glm and lm regression model
Modified: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION 2013-09-10 20:01:31 UTC (rev 3047)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION 2013-09-10 20:23:51 UTC (rev 3048)
@@ -36,3 +36,5 @@
'LoSharpe.R'
'se.LoSharpe.R'
'table.Sharpe.R'
+ 'glmi.R'
+ 'lmi.R'
Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/glmi.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/glmi.R (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/glmi.R 2013-09-10 20:23:51 UTC (rev 3048)
@@ -0,0 +1,92 @@
+glmi <- function (formula, family = gaussian, data,vcov = NULL, weights, subset,
+ na.action, start = NULL, etastart, mustart, offset, control = list(...),
+ model = TRUE, method = "glm.fit", x = FALSE, y = TRUE, contrasts = NULL,
+ ...)
+{
+ call <- match.call()
+ if (is.character(family))
+ family <- get(family, mode = "function", envir = parent.frame())
+ if (is.function(family))
+ family <- family()
+ if (is.null(family$family)) {
+ print(family)
+ stop("'family' not recognized")
+ }
+ if (missing(data))
+ data <- environment(formula)
+ mf <- match.call(expand.dots = FALSE)
+ m <- match(c("formula", "data", "subset", "weights", "na.action",
+ "etastart", "mustart", "offset"), names(mf), 0L)
+ mf <- mf[c(1L, m)]
+ mf$drop.unused.levels <- TRUE
+ mf[[1L]] <- as.name("model.frame")
+ mf <- eval(mf, parent.frame())
+ if (identical(method, "model.frame"))
+ return(mf)
+ if (!is.character(method) && !is.function(method))
+ stop("invalid 'method' argument")
+ if (identical(method, "glm.fit"))
+ control <- do.call("glm.control", control)
+ mt <- attr(mf, "terms")
+ Y <- model.response(mf, "any")
+ if (length(dim(Y)) == 1L) {
+ nm <- rownames(Y)
+ dim(Y) <- NULL
+ if (!is.null(nm))
+ names(Y) <- nm
+ }
+ X <- if (!is.empty.model(mt))
+ model.matrix(mt, mf, contrasts)
+ else matrix(, NROW(Y), 0L)
+ weights <- as.vector(model.weights(mf))
+ if (!is.null(weights) && !is.numeric(weights))
+ stop("'weights' must be a numeric vector")
+ if (!is.null(weights) && any(weights < 0))
+ stop("negative weights not allowed")
+ offset <- as.vector(model.offset(mf))
+ if (!is.null(offset)) {
+ if (length(offset) != NROW(Y))
+ stop(gettextf("number of offsets is %d should equal %d (number of observations)",
+ length(offset), NROW(Y)), domain = NA)
+ }
+ mustart <- model.extract(mf, "mustart")
+ etastart <- model.extract(mf, "etastart")
+ fit <- eval(call(if (is.function(method)) "method" else method,
+ x = X, y = Y, weights = weights, start = start, etastart = etastart,
+ mustart = mustart, offset = offset, family = family,
+ control = control, intercept = attr(mt, "intercept") >
+ 0L))
+ if (length(offset) && attr(mt, "intercept") > 0L) {
+ fit2 <- eval(call(if (is.function(method)) "method" else method,
+ x = X[, "(Intercept)", drop = FALSE], y = Y, weights = weights,
+ offset = offset, family = family, control = control,
+ intercept = TRUE))
+ if (!fit2$converged)
+ warning("fitting to calculate the null deviance did not converge -- increase 'maxit'?")
+ fit$null.deviance <- fit2$deviance
+ }
+ if (model)
+ fit$model <- mf
+ fit$na.action <- attr(mf, "na.action")
+ if (x)
+ fit$x <- X
+ if (!y)
+ fit$y <- NULL
+ fit <- c(fit, list(call = call, formula = formula, terms = mt,
+ data = data, offset = offset, control = control, method = method,
+ contrasts = attr(X, "contrasts"), xlevels = .getXlevels(mt,
+ mf)))
+ class(fit) <- c(fit$class, c("glm", "lm"))
+ fit
+ if(is.null(vcov)) {
+ se <- vcov(fit)
+ } else {
+ if (is.function(vcov))
+ se <- vcov(fit)
+ else
+ se <- vcov
+ }
+ fit = list(fit,vHaC = se)
+ fit
+
+}
\ No newline at end of file
Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/lmi.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/lmi.R (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/lmi.R 2013-09-10 20:23:51 UTC (rev 3048)
@@ -0,0 +1,77 @@
+
+lmi <- function (formula, data,vcov = NULL, subset, weights, na.action, method = "qr",
+ model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
+ contrasts = NULL, offset, ...)
+{
+ ret.x <- x
+ ret.y <- y
+ cl <- match.call()
+ mf <- match.call(expand.dots = FALSE)
+ m <- match(c("formula", "data", "subset", "weights", "na.action",
+ "offset"), names(mf), 0L)
+ mf <- mf[c(1L, m)]
+ mf$drop.unused.levels <- TRUE
+ mf[[1L]] <- as.name("model.frame")
+ mf <- eval(mf, parent.frame())
+ if (method == "model.frame")
+ return(mf)
+ else if (method != "qr")
+ warning(gettextf("method = '%s' is not supported. Using 'qr'",
+ method), domain = NA)
+ mt <- attr(mf, "terms")
+ y <- model.response(mf, "numeric")
+ w <- as.vector(model.weights(mf))
+ if (!is.null(w) && !is.numeric(w))
+ stop("'weights' must be a numeric vector")
+ offset <- as.vector(model.offset(mf))
+ if (!is.null(offset)) {
+ if (length(offset) != NROW(y))
+ stop(gettextf("number of offsets is %d, should equal %d (number of observations)",
+ length(offset), NROW(y)), domain = NA)
+ }
+ if (is.empty.model(mt)) {
+ x <- NULL
+ z <- list(coefficients = if (is.matrix(y)) matrix(, 0,
+ 3) else numeric(), residuals = y, fitted.values = 0 *
+ y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w !=
+ 0) else if (is.matrix(y)) nrow(y) else length(y))
+ if (!is.null(offset)) {
+ z$fitted.values <- offset
+ z$residuals <- y - offset
+ }
+ }
+ else {
+ x <- model.matrix(mt, mf, contrasts)
+ z <- if (is.null(w))
+ lm.fit(x, y, offset = offset, singular.ok = singular.ok,
+ ...)
+ else lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok,
+ ...)
+ }
+ class(z) <- c(if (is.matrix(y)) "mlm", "lm")
+ z$na.action <- attr(mf, "na.action")
+ z$offset <- offset
+ z$contrasts <- attr(x, "contrasts")
+ z$xlevels <- .getXlevels(mt, mf)
+ z$call <- cl
+ z$terms <- mt
+ if (model)
+ z$model <- mf
+ if (ret.x)
+ z$x <- x
+ if (ret.y)
+ z$y <- y
+ if (!qr)
+ z$qr <- NULL
+ #z
+ if(is.null(vcov)) {
+ se <- vcov(z)
+ } else {
+ if (is.function(vcov))
+ se <- vcov(z)
+ else
+ se <- vcov
+ }
+ z = list(z,vHaC = se)
+ z
+}
Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/glmi.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/glmi.Rd (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/glmi.Rd 2013-09-10 20:23:51 UTC (rev 3048)
@@ -0,0 +1,18 @@
+\name{glmi}
+\alias{glmi}
+\title{Support of HAC methods within lm regression model}
+\usage{
+ glmi(formula, family = gaussian, data, vcov = NULL,
+ weights, subset, na.action, start = NULL, etastart,
+ mustart, offset, control = list(...), model = TRUE,
+ method = "glm.fit", x = FALSE, y = TRUE,
+ contrasts = NULL, ...)
+}
+\description{
+ Support of HAC methods within lm regression model
+}
+\seealso{
+ \code{\link{glm}
+}
+}
+
Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/lmi.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/lmi.Rd (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/man/lmi.Rd 2013-09-10 20:23:51 UTC (rev 3048)
@@ -0,0 +1,17 @@
+\name{lmi}
+\alias{lmi}
+\title{Support of HAC methods within glm regression model}
+\usage{
+ lmi(formula, data, vcov = NULL, subset, weights,
+ na.action, method = "qr", model = TRUE, x = FALSE,
+ y = FALSE, qr = TRUE, singular.ok = TRUE,
+ contrasts = NULL, offset, ...)
+}
+\description{
+ Support of HAC methods within glm regression model
+}
+\seealso{
+ \code{\link{lm}
+}
+}
+
More information about the Returnanalytics-commits
mailing list