Last data update: 2014.03.03

R: Predict function for Bayesian Model Averaging for generalized...
predict.bic.glmR Documentation

Predict function for Bayesian Model Averaging for generalized linear models.

Description

Bayesian Model Averaging (BMA) accounts for the model uncertainty inherent in the variable selection problem by averaging over the best models in the model class according to approximate posterior model probability. This function predicts the response resulting from a BMA generalized linear model from given data.

Usage

## S3 method for class 'bic.glm'
predict( object, newdata, ...)

Arguments

object

a fitted object inheriting from class bic.glm.

newdata

a data frame containing observations on variables from which the predictor variables are to be selected or constructed from a formula.

...

ignored (for compatibility with generic function).

Value

The predicted values from the BMA model for each observation in newdata.

See Also

bic.glm

Examples


## Not run: 
# Example 1 (Gaussian)

     library(MASS)
     data(UScrime)

     f <- formula(log(y) ~  log(M)+So+log(Ed)+log(Po1)+log(Po2)+
            log(LF)+log(M.F)+log(Pop)+log(NW)+log(U1)+log(U2)+
            log(GDP)+log(Ineq)+log(Prob)+log(Time))

     bic.glm.crimeT <- bic.glm(f, data = UScrime, 
                               glm.family = gaussian())
     predict(bic.glm.crimeT, newdata = UScrime)

     bic.glm.crimeF <- bic.glm(f, data = UScrime, 
                               glm.family = gaussian(),
                               factor.type = FALSE)
     predict(bic.glm.crimeF, newdata = UScrime)

## End(Not run)

## Not run: 
# Example 2 (binomial)

     library(MASS)
     data(birthwt)

     y <- birthwt$lo
     x <- data.frame(birthwt[,-1])
     x$race <- as.factor(x$race)
     x$ht <- (x$ht>=1)+0
     x <- x[,-9]
     x$smoke <- as.factor(x$smoke)
     x$ptl <- as.factor(x$ptl)
     x$ht  <- as.factor(x$ht)

     x$ui <- as.factor(x$ui)

     bic.glm.bwT <- bic.glm(x, y, strict = FALSE, OR = 20,
                            glm.family="binomial",  
                            factor.type=TRUE)
     predict( bic.glm.bwT, newdata = x)

     bic.glm.bwF <- bic.glm(x, y, strict = FALSE, OR = 20,
                            glm.family="binomial",  
                            factor.type=FALSE)
     predict( bic.glm.bwF, newdata = x)

## End(Not run)

## Not run: 
# Example 3 (Gaussian)

     library(MASS)
     data(anorexia)

     anorexia.formula <- formula(Postwt ~ Prewt+Treat+offset(Prewt))

     bic.glm.anorexiaF <- bic.glm( anorexia.formula, data=anorexia,
                       glm.family="gaussian", factor.type=FALSE)
     predict( bic.glm.anorexiaF, newdata=anorexia)

     bic.glm.anorexiaT <- bic.glm( anorexia.formula, data=anorexia,
                           glm.family="gaussian", factor.type=TRUE)
     predict( bic.glm.anorexiaT, newdata=anorexia)

## End(Not run)

## Not run: 
# Example 4 (Gamma)

     library(survival)
     data(veteran)

     surv.t <- veteran$time
     x <- veteran[,-c(3,4)]
     x$celltype <- factor(as.character(x$celltype))
     sel<- veteran$status == 0
     x <- x[!sel,]
     surv.t <- surv.t[!sel]

     bic.glm.vaT <- bic.glm(x, y=surv.t, 
                            glm.family=Gamma(link="inverse"),
                            factor.type=TRUE)
     predict( bic.glm.vaT, x)

     bic.glm.vaF <- bic.glm(x, y=surv.t, 
                            glm.family=Gamma(link="inverse"),
                            factor.type=FALSE)
     predict( bic.glm.vaF, x)

## End(Not run)

# Example 5 (poisson - Yates teeth data)

     x <- rbind.data.frame(c(0, 0, 0),
                           c(0, 1, 0),
                           c(1, 0, 0),
                           c(1, 1, 1))

     y <- c(4, 16, 1, 21)
     n <- c(1,1,1,1)

     bic.glm.yatesF <- bic.glm( x, y, glm.family=poisson(),
                               weights=n, factor.type=FALSE)

     predict( bic.glm.yatesF, x)

## Not run: 
# Example 6 (binomial - Venables and Ripley)

    ldose <- rep(0:5, 2)
    numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
    sex <- factor(rep(c("M", "F"), c(6, 6)))
    SF <- cbind(numdead, numalive=20-numdead) 

    budworm <- cbind.data.frame(ldose = ldose, numdead = numdead,
                                sex = sex, SF = SF)
    budworm.formula <- formula(SF ~ sex*ldose)

    bic.glm.budwormF <- bic.glm( budworm.formula, data=budworm,
                   glm.family="binomial", factor.type=FALSE)
    predict(bic.glm.budwormF, newdata=budworm)

    bic.glm.budwormT <- bic.glm( budworm.formula, data=budworm,
                      glm.family="binomial", factor.type=TRUE)
    predict(bic.glm.budwormT, newdata=budworm)

## End(Not run)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(BMA)
Loading required package: survival
Loading required package: leaps
Loading required package: robustbase

Attaching package: 'robustbase'

The following object is masked from 'package:survival':

    heart

Loading required package: inline
Loading required package: rrcov
Scalable Robust Estimators with High Breakdown Point (version 1.3-11)

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/BMA/predict.bic.glm.Rd_%03d_medium.png", width=480, height=480)
> ### Name: predict.bic.glm
> ### Title: Predict function for Bayesian Model Averaging for generalized
> ###   linear models.
> ### Aliases: predict.bic.glm
> ### Keywords: regression models
> 
> ### ** Examples
> 
> 
> ## Not run: 
> ##D # Example 1 (Gaussian)
> ##D 
> ##D      library(MASS)
> ##D      data(UScrime)
> ##D 
> ##D      f <- formula(log(y) ~  log(M)+So+log(Ed)+log(Po1)+log(Po2)+
> ##D             log(LF)+log(M.F)+log(Pop)+log(NW)+log(U1)+log(U2)+
> ##D             log(GDP)+log(Ineq)+log(Prob)+log(Time))
> ##D 
> ##D      bic.glm.crimeT <- bic.glm(f, data = UScrime, 
> ##D                                glm.family = gaussian())
> ##D      predict(bic.glm.crimeT, newdata = UScrime)
> ##D 
> ##D      bic.glm.crimeF <- bic.glm(f, data = UScrime, 
> ##D                                glm.family = gaussian(),
> ##D                                factor.type = FALSE)
> ##D      predict(bic.glm.crimeF, newdata = UScrime)
> ## End(Not run)
> 
> ## Not run: 
> ##D # Example 2 (binomial)
> ##D 
> ##D      library(MASS)
> ##D      data(birthwt)
> ##D 
> ##D      y <- birthwt$lo
> ##D      x <- data.frame(birthwt[,-1])
> ##D      x$race <- as.factor(x$race)
> ##D      x$ht <- (x$ht>=1)+0
> ##D      x <- x[,-9]
> ##D      x$smoke <- as.factor(x$smoke)
> ##D      x$ptl <- as.factor(x$ptl)
> ##D      x$ht  <- as.factor(x$ht)
> ##D 
> ##D      x$ui <- as.factor(x$ui)
> ##D 
> ##D      bic.glm.bwT <- bic.glm(x, y, strict = FALSE, OR = 20,
> ##D                             glm.family="binomial",  
> ##D                             factor.type=TRUE)
> ##D      predict( bic.glm.bwT, newdata = x)
> ##D 
> ##D      bic.glm.bwF <- bic.glm(x, y, strict = FALSE, OR = 20,
> ##D                             glm.family="binomial",  
> ##D                             factor.type=FALSE)
> ##D      predict( bic.glm.bwF, newdata = x)
> ## End(Not run)
> 
> ## Not run: 
> ##D # Example 3 (Gaussian)
> ##D 
> ##D      library(MASS)
> ##D      data(anorexia)
> ##D 
> ##D      anorexia.formula <- formula(Postwt ~ Prewt+Treat+offset(Prewt))
> ##D 
> ##D      bic.glm.anorexiaF <- bic.glm( anorexia.formula, data=anorexia,
> ##D                        glm.family="gaussian", factor.type=FALSE)
> ##D      predict( bic.glm.anorexiaF, newdata=anorexia)
> ##D 
> ##D      bic.glm.anorexiaT <- bic.glm( anorexia.formula, data=anorexia,
> ##D                            glm.family="gaussian", factor.type=TRUE)
> ##D      predict( bic.glm.anorexiaT, newdata=anorexia)
> ## End(Not run)
> 
> ## Not run: 
> ##D # Example 4 (Gamma)
> ##D 
> ##D      library(survival)
> ##D      data(veteran)
> ##D 
> ##D      surv.t <- veteran$time
> ##D      x <- veteran[,-c(3,4)]
> ##D      x$celltype <- factor(as.character(x$celltype))
> ##D      sel<- veteran$status == 0
> ##D      x <- x[!sel,]
> ##D      surv.t <- surv.t[!sel]
> ##D 
> ##D      bic.glm.vaT <- bic.glm(x, y=surv.t, 
> ##D                             glm.family=Gamma(link="inverse"),
> ##D                             factor.type=TRUE)
> ##D      predict( bic.glm.vaT, x)
> ##D 
> ##D      bic.glm.vaF <- bic.glm(x, y=surv.t, 
> ##D                             glm.family=Gamma(link="inverse"),
> ##D                             factor.type=FALSE)
> ##D      predict( bic.glm.vaF, x)
> ## End(Not run)
> 
> # Example 5 (poisson - Yates teeth data)
> 
>      x <- rbind.data.frame(c(0, 0, 0),
+                            c(0, 1, 0),
+                            c(1, 0, 0),
+                            c(1, 1, 1))
> 
>      y <- c(4, 16, 1, 21)
>      n <- c(1,1,1,1)
> 
>      bic.glm.yatesF <- bic.glm( x, y, glm.family=poisson(),
+                                weights=n, factor.type=FALSE)
> 
>      predict( bic.glm.yatesF, x)
        1         2         3         4 
 2.918699 17.064605  2.081301 19.935395 
> 
> ## Not run: 
> ##D # Example 6 (binomial - Venables and Ripley)
> ##D 
> ##D     ldose <- rep(0:5, 2)
> ##D     numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
> ##D     sex <- factor(rep(c("M", "F"), c(6, 6)))
> ##D     SF <- cbind(numdead, numalive=20-numdead) 
> ##D 
> ##D     budworm <- cbind.data.frame(ldose = ldose, numdead = numdead,
> ##D                                 sex = sex, SF = SF)
> ##D     budworm.formula <- formula(SF ~ sex*ldose)
> ##D 
> ##D     bic.glm.budwormF <- bic.glm( budworm.formula, data=budworm,
> ##D                    glm.family="binomial", factor.type=FALSE)
> ##D     predict(bic.glm.budwormF, newdata=budworm)
> ##D 
> ##D     bic.glm.budwormT <- bic.glm( budworm.formula, data=budworm,
> ##D                       glm.family="binomial", factor.type=TRUE)
> ##D     predict(bic.glm.budwormT, newdata=budworm)
> ## End(Not run)
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>