Skip to content

Commit

Permalink
Update chapter 3
Browse files Browse the repository at this point in the history
  • Loading branch information
stefvanbuuren committed May 8, 2018
1 parent 3bc0a50 commit 05da4f4
Show file tree
Hide file tree
Showing 8 changed files with 1,842 additions and 1,555 deletions.
827 changes: 749 additions & 78 deletions 02-multiple-imputation.Rmd

Large diffs are not rendered by default.

1,735 changes: 942 additions & 793 deletions 03-univariate-missing.Rmd

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions R/fimd.R
Expand Up @@ -464,11 +464,14 @@ library(knitr)
opts_chunk$set(fig.path = 'fig/ch3_', self.contained = FALSE)

## ----init3, echo = FALSE, results = 'hide'-------------------------------
rm(list = ls())
source("R/chapterinit.R")
source("R/mice.impute.x.R")
library(mice, warn.conflicts = FALSE, quietly = TRUE)
opts_chunk$set(fig.path = 'fig/ch03-', self.contained = FALSE)
suppressPackageStartupMessages(library(mice, warn.conflicts = FALSE, quietly = TRUE))
suppressPackageStartupMessages(library(lattice, warn.conflicts = FALSE, quietly = TRUE))
suppressPackageStartupMessages(library(gamlss, warn.conflicts = FALSE, quietly = TRUE))
suppressPackageStartupMessages(library(MASS, warn.conflicts = FALSE, quietly = TRUE))
suppressPackageStartupMessages(library(ImputeRobust, warn.conflicts = FALSE, quietly = TRUE))
suppressPackageStartupMessages(library(rpart, warn.conflicts = FALSE, quietly = TRUE))
source("R/mice.impute.x.R")

## ----gas1, six=TRUE, echo=FALSE, fig.width=4.5, fig.height=6.75---------
library("MASS")
Expand Down Expand Up @@ -708,8 +711,8 @@ box(lwd = 0.7)
}

## ----linhc2, cache=TRUE, warning = FALSE---------------------------------
library(ImputeRobust)
library(gamlss)
library("ImputeRobust")
library("gamlss")
data(db)
data <- subset(db, age > 1 & age < 2, c("age", "head"))
names(data) <- c("age", "hc")
Expand Down Expand Up @@ -756,7 +759,7 @@ axis(2, lwd = 0.7, las = 1, cex.axis = 0.7)
box(lwd = 0.7)
}

## ----figmisspecify, echo=FALSE, duo = TRUE, fig.height=2.25, fig.width=4.5----
## ----misspecify, echo=FALSE, duo = TRUE, fig.height=2.25, fig.width=4.5----
data <- boys[boys$age<=2,c("age","bmi")]
set.seed(87120)
data[sample(92:136,10),"bmi"] <- NA
Expand Down Expand Up @@ -843,7 +846,7 @@ legend(x="bottomleft", legend=c("before insulation","after insulation"), pch=c(3
## res <- res.pmm

## ----cart, echo=FALSE, duo=TRUE, fig.width=4.5, fig.height=2.25---------
library(rpart)
library("rpart")

fit <- rpart(Gas ~ Temp + Insul, data=whiteside)
plot(fit, branch=0, margin=0.15)
Expand Down
105 changes: 105 additions & 0 deletions R/mice.impute.x.R
@@ -0,0 +1,105 @@

###------------------------MICE.IMPUTE.NORM.BB----------------------
mice.impute.norm.bb <- function(y, ry, x, ridge=0.00001, ...)
{
# mice.impute.norm.bb
# Regression imputations of y given x, with a fixed regression
# line, and with random draws of the residuals around the line.
# Bayesian bootstrap
#

x <- cbind(1, as.matrix(x))
xobs <- x[ry,]
yobs <- y[ry]
n1 <- sum(ry)
n0 <- sum(!ry)

# do here the Bayesian bootstap Rubin p. 124
u <- runif(n1-1)
u <- diff(c(0,u[order(u)],1))
s <- sample(n1, n1, replace=TRUE, prob=u)

dotxobs <- xobs[s,]
dotyobs <- yobs[s]
xtx <- t(dotxobs) %*% dotxobs
pen <- ridge * diag(xtx)
if (length(pen)==1) pen <- matrix(pen)
v <- solve(xtx + diag(pen))
coef <- t(dotyobs %*% dotxobs %*% v)
residuals <- dotyobs - dotxobs %*% coef
sigma <- sqrt((sum(residuals^2))/(n1-ncol(x)-1))
parm <- list(coef, sigma)
names(parm) <- c("beta", "sigma")
return(x[!ry, ] %*% parm$beta + rnorm(n0) * parm$sigma)
}

mice.impute.normdump <- function (y, ry, x, ...)
{
x <- cbind(1, as.matrix(x))
parm <- .norm.draw(y, ry, x, ...)
betadump <<- c(betadump,parm$beta)
return(x[!ry, ] %*% parm$beta + rnorm(sum(!ry)) * parm$sigma)
}

mice.impute.pmmdump <- function (y, ry, x, ...)
{
x <- cbind(1, as.matrix(x))
parm <- .norm.draw(y, ry, x, ...)
yhatobs <- x[ry, ] %*% parm$coef
yhatmis <- x[!ry, ] %*% parm$beta
betadump <<- c(betadump,parm$beta)
return(apply(as.array(yhatmis), 1, .pmm.match, yhat = yhatobs,
y = y[ry], ...))
}




#-------------------------MICE.IMPUTE.2L.NORM.BOOT--------------------

mice.impute.2L.norm.boot <- function(y, ry, x, type, intercept=FALSE, ...)
{
#
# Bootstrap version of mice.impute.2L.norm
#
# Author: Stef van Buuren, 2011
#
n.class <- length(unique(x[, type==(-2)]))
s1 <- sample(n.class, n.class, replace=TRUE)
dotx <-
gf.full <- factor(x[,type==(-2)], labels=1:n.class)
gf <- gf.full[ry]
n.g <- tabulate(gf)


## draw a bootstrap sample for yobs and xobs
xobs <- x[ry,]
yobs <- y[ry]
n1 <- sum(ry)
n0 <- sum(!ry)
s <- sample(n1, n1, replace=TRUE)
doty <- y
doty[ry] <- yobs[s]
dotx <- x
dotx[ry,] <- xobs[s,]

x <- dotx
y <- doty

## append intercept
if (intercept) {
x <- cbind(1, as.matrix(x))
type <- c(2, type)
}

expr <- expression(glm.fit(x[ry, ], y[ry],
family = binomial(link = logit)))
fit <- suppressWarnings(eval(expr))
beta.star <- beta <- coef(fit)
p <- 1/(1 + exp(-(x[!ry,] %*% beta.star)))
vec <- (runif(nrow(p)) <= p)
vec[vec] <- 1
if (is.factor(y)) {
vec<-factor(vec,c(0,1),levels(y))}
return(vec)
}
4 changes: 3 additions & 1 deletion _bookdown.yml
@@ -1,9 +1,11 @@
book_filename: "FIMD-bookdown"
delete_merged_file: true
language:
label:
def: 'Algorithm '
ui:
chapter_name: "Chapter "
rmd_files: ["index.Rmd", "01-introduction.Rmd", "02-multiple-imputation.Rmd", "05-analysis.Rmd", "13-references.Rmd"]
rmd_files: ["index.Rmd", "01-introduction.Rmd", "02-multiple-imputation.Rmd", "03-univariate-missing.Rmd", "05-analysis.Rmd", "13-references.Rmd"]
before_chapter_script: "R/before_chapter_script.R"

# rmd_files: ["index.Rmd", "01-introduction.Rmd", "02-multiple-imputation.Rmd", "05-analysis.Rmd"]
16 changes: 8 additions & 8 deletions fimd2.bib
Expand Up @@ -4595,7 +4595,7 @@ @book{GELMAN2004B
Address = {Chichester, UK},
Date-Added = {2011-01-19 11:18:04 +0100},
Date-Modified = {2011-10-23 19:45:28 +0000},
Editor = {Gelman, A. and Meng, X-L.},
Editor = {Gelman, A. and Meng, X. L.},
Publisher = {John Wiley \& Sons},
Title = {Applied {B}ayesian Modeling and Causal Inference from Incomplete-Data Perspectives},
Year = {2004}}
Expand All @@ -4622,7 +4622,7 @@ @article{REITER2007
Year = {2007}}

@article{LI1991B,
Author = {Li, K-H. and Raghunathan, T. E. and Rubin, D. B.},
Author = {Li, K. H. and Raghunathan, T. E. and Rubin, D. B.},
Date-Added = {2011-01-16 09:40:32 +0100},
Date-Modified = {2011-10-23 19:44:27 +0000},
Journal = {Journal of the American Statistical Association},
Expand Down Expand Up @@ -6543,7 +6543,7 @@ @article{BARNARD1998
Year = {1998}}

@article{BARNARD1999,
Author = {Barnard, J. and Meng, X-L.},
Author = {Barnard, J. and Meng, X. L.},
Date-Modified = {2011-10-23 19:45:36 +0000},
Journal = {Statistical Methods in Medical Research},
Number = {1},
Expand Down Expand Up @@ -9612,7 +9612,7 @@ @incollection{HILL2004
Booktitle = {Applied {B}ayesian modeling and causal inference from incomplete-data perspectives},
Chapter = 5,
Date-Modified = {2011-10-23 19:45:16 +0000},
Editor = {Gelman, A. and Meng, X-L.},
Editor = {Gelman, A. and Meng, X. L.},
Pages = {49-60},
Publisher = {John Wiley \& Sons},
Title = {A comparison of experimental and observational data analyses},
Expand Down Expand Up @@ -10753,7 +10753,7 @@ @article{LEWSEY2006
Year = {2006}}

@article{LI1991,
Author = {Li, K-H. and Meng, X-L. and Raghunathan, T. E. and Rubin, D. B.},
Author = {Li, K. H. and Meng, X. L. and Raghunathan, T. E. and Rubin, D. B.},
Date-Modified = {2011-10-23 19:44:15 +0000},
Journal = {Statistica Sinica},
Number = {1},
Expand Down Expand Up @@ -11382,7 +11382,7 @@ @article{MENDOZA2004
Year = {2004}}

@article{MENG1992,
Author = {Meng, X-L. and Rubin, D. B.},
Author = {Meng, X. L. and Rubin, D. B.},
Date-Modified = {2011-10-23 19:44:59 +0000},
Journal = {Biometrika},
Number = {1},
Expand All @@ -11392,7 +11392,7 @@ @article{MENG1992
Year = {1992}}

@article{MENG1994,
Author = {Meng, X-L.},
Author = {Meng, X. L.},
Date-Modified = {2011-10-23 19:44:52 +0000},
Journal = {Statistical Science},
Keywords = {Multiple imputation},
Expand All @@ -11403,7 +11403,7 @@ @article{MENG1994
Year = {1994}}

@article{MENG2002,
Author = {Meng, X-L. and Zaslavsky, A. M.},
Author = {Meng, X. L. and Zaslavsky, A. M.},
Date-Modified = {2011-10-23 19:44:43 +0000},
Journal = {Annals of Statistics},
Number = {5},
Expand Down
38 changes: 24 additions & 14 deletions style.css
@@ -1,14 +1,24 @@
p.caption {
color: #777;
margin-top: 10px;
}
p code {
white-space: inherit;
}
pre {
word-break: normal;
word-wrap: normal;
}
pre code {
white-space: inherit;
}
h1 {
color: #006CC2B3;
}
h2 {
color: #006CC2B3;
}
h3 {
color: #006CC2B3;
}

.caption {
color: #006CC2B3;
margin-top: 10px;
}
p code {
white-space: inherit;
}
pre {
word-break: normal;
word-wrap: normal;
}
pre code {
white-space: inherit;
}

0 comments on commit 05da4f4

Please sign in to comment.