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")
11 glmrefit <- function(x, y, w) {
12 fit <- c(glm.fit(x, y, weights=w, offset=offset,
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)
26 z@preproc.y <- function(x){
28 stop(paste("for the", family$family, "family y must be univariate"))
32 if(family$family=="gaussian"){
33 z@defineComponent <- function(para) {
34 predict <- function(x, ...) {
36 if("offset" %in% names(dotarg)) offset <- dotarg$offset
38 if (!is.null(offset)) p <- p + offset
42 logLik <- function(x, y, ...)
43 dnorm(y, mean=predict(x, ...), sd=para$sigma, log=TRUE)
46 parameters=list(coef=para$coef, sigma=para$sigma),
47 logLik=logLik, predict=predict,
51 z@fit <- function(x, y, w, component){
52 fit <- glm.fit(x, y, w=w, offset=offset, family = family)
53 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)+1,
54 sigma = sqrt(sum(fit$weights * fit$residuals^2 /
55 mean(fit$weights))/ (nrow(x)-fit$rank))))
58 else if(family$family=="binomial"){
59 z@preproc.y <- function(x){
61 stop("for the binomial family, y must be a 2 column matrix\n",
62 "where col 1 is no. successes and col 2 is no. failures")
64 stop("negative values are not allowed for the binomial family")
67 z@defineComponent <- function(para) {
68 predict <- function(x, ...) {
70 if("offset" %in% names(dotarg)) offset <- dotarg$offset
72 if (!is.null(offset)) p <- p + offset
75 logLik <- function(x, y, ...)
76 dbinom(y[,1], size=rowSums(y), prob=predict(x, ...), log=TRUE)
79 parameters=list(coef=para$coef),
80 logLik=logLik, predict=predict,
84 z@fit <- function(x, y, w, component){
85 fit <- glm.fit(x, y, weights=w, family=family, offset=offset, start=component$coef)
86 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)))
89 else if(family$family=="poisson"){
90 z@defineComponent <- function(para) {
91 predict <- function(x, ...) {
93 if("offset" %in% names(dotarg)) offset <- dotarg$offset
95 if (!is.null(offset)) p <- p + offset
98 logLik <- function(x, y, ...)
99 dpois(y, lambda=predict(x, ...), log=TRUE)
102 parameters=list(coef=para$coef),
103 logLik=logLik, predict=predict,
107 z@fit <- function(x, y, w, component){
108 fit <- glm.fit(x, y, weights=w, family=family, offset=offset, start=component$coef)
109 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)))
112 else if(family$family=="Gamma"){
113 z@defineComponent <- function(para) {
114 predict <- function(x, ...) {
116 if("offset" %in% names(dotarg)) offset <- dotarg$offset
118 if (!is.null(offset)) p <- p + offset
121 logLik <- function(x, y, ...)
122 dgamma(y, shape = para$shape, scale=predict(x, ...)/para$shape, log=TRUE)
125 parameters = list(coef = para$coef, shape = para$shape),
126 predict = predict, logLik = logLik,
130 z@fit <- function(x, y, w, component){
131 fit <- glm.fit(x, y, weights=w, family=family, offset=offset, start=component$coef)
132 z@defineComponent(para = list(coef = coef(fit), df = ncol(x)+1,
133 shape = sum(fit$prior.weights)/fit$deviance))
137 else stop(paste("Unknown family", family))