1 FLXMRglm <- function(formula=.~., family=gaussian, offset=NULL)
3 if (is.character(family))
4 family <- get(family, mode = "function", envir = parent.frame())
5 if (is.function(family))
7 if (is.null(family$family)) {
9 stop("'family' not recognized")
12 glmrefit <- function(x, y, w) {
13 fit <- c(glm.fit(x, y, weights=w, offset=offset, family=family),
14 list(call = sys.call(), offset = offset,
15 control = eval(formals(glm.fit)$control),
16 method = "weighted.glm.fit"))
17 fit$df.null <- sum(w) + fit$df.null - fit$df.residual - fit$rank
18 fit$df.residual <- sum(w) - fit$rank
23 z <- new("FLXMRglm", weighted=TRUE, formula=formula,
24 name=paste("FLXMRglm", family$family, sep=":"), offset = offset,
25 family=family$family, refit=glmrefit)
27 z@preproc.y <- function(x) {
29 stop(paste("for the", family$family, "family y must be univariate"))
33 if (family$family=="gaussian") {
34 z@defineComponent <- function(para) {
35 predict <- function(x, ...) {
37 if("offset" %in% names(dotarg)) offset <- dotarg$offset
39 if (!is.null(offset)) p <- p + offset
43 logLik <- function(x, y, ...)
44 dnorm(y, mean=predict(x, ...), sd=para$sigma, log=TRUE)
47 parameters=list(coef=para$coef, sigma=para$sigma),
48 logLik=logLik, predict=predict,
52 z@fit <- function(x, y, w, component){
53 fit <- glm.fit(x, y, w=w, offset=offset, family = family)
54 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)+1,
55 sigma = sqrt(sum(fit$weights * fit$residuals^2 /
56 mean(fit$weights))/ (nrow(x)-fit$rank))))
60 else if (family$family=="binomial") {
61 z@preproc.y <- function(x) {
64 stop("for the binomial family, y must be a 2 column matrix\n",
65 "where col 1 is no. successes and col 2 is no. failures")
68 stop("negative values are not allowed for the binomial family")
72 z@defineComponent <- function(para) {
73 predict <- function(x, ...) {
75 if("offset" %in% names(dotarg))
76 offset <- dotarg$offset
82 logLik <- function(x, y, ...)
83 dbinom(y[,1], size=rowSums(y), prob=predict(x, ...), log=TRUE)
86 parameters=list(coef=para$coef),
87 logLik=logLik, predict=predict,
91 z@fit <- function(x, y, w, component) {
92 fit <- glm.fit(x, y, weights=w, family=family, offset=offset, start=component$coef)
93 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)))
97 else if (family$family=="poisson") {
98 z@defineComponent <- function(para) {
99 predict <- function(x, ...) {
101 if("offset" %in% names(dotarg)) offset <- dotarg$offset
103 if (!is.null(offset)) p <- p + offset
106 logLik <- function(x, y, ...)
107 dpois(y, lambda=predict(x, ...), log=TRUE)
110 parameters=list(coef=para$coef),
111 logLik=logLik, predict=predict,
115 z@fit <- function(x, y, w, component) {
116 fit <- glm.fit(x, y, weights=w, family=family, offset=offset, start=component$coef)
117 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)))
121 else if (family$family=="Gamma") {
122 z@defineComponent <- function(para) {
123 predict <- function(x, ...) {
125 if("offset" %in% names(dotarg)) offset <- dotarg$offset
127 if (!is.null(offset)) p <- p + offset
130 logLik <- function(x, y, ...)
131 dgamma(y, shape = para$shape, scale=predict(x, ...)/para$shape, log=TRUE)
134 parameters = list(coef = para$coef, shape = para$shape),
135 predict = predict, logLik = logLik,
139 z@fit <- function(x, y, w, component) {
140 fit <- glm.fit(x, y, weights=w, family=family, offset=offset, start=component$coef)
141 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)+1,
142 shape = sum(fit$prior.weights)/fit$deviance))
146 else stop(paste("Unknown family", family))