R/mice.impute.grouped.R

Defines functions mice.impute.grouped

Documented in mice.impute.grouped

## File Name: mice.impute.grouped.R
## File Version: 0.17

##################################################
# imputation for grouped data
mice.impute.grouped <- function (y, ry, x, low=NULL, upp=NULL,  ...)
{
    TAM::require_namespace_msg("grouped")
    x <- cbind(1, as.matrix(x) )
    newstate <- ma_exists_get('newstate', pos=parent.frame(n=1))
    vname <- ma_exists_get('yname', pos=parent.frame(n=1))
    Y <- cbind( low[[vname]], upp[[vname]] )
    # draw bootstrap sample
    N <- nrow(Y)
    ind <- sample( 1:N, replace=TRUE )
    X1 <- x[ ind, -1]
    Y1 <- Y[ ind, ]
    # fit grouped model to bootstrap sample
    mod <- grouped::grouped( Y1 ~ X1 )
    beta <- coef(mod)
    sigma <- summary(mod)$sigma
    # calculate predicted values
    ypred <- x %*% beta
    # compute quantiles
    qlow <- stats::pnorm( Y[,1], mean=ypred, sd=sigma )
    qupp <- stats::pnorm( Y[,2], mean=ypred, sd=sigma )
    # draw randomly a quantile
    samp_quant <- qlow + ( qupp - qlow)* stats::runif(N)
    # draw imputed value
    yimp <- stats::qnorm( samp_quant, mean=ypred, sd=sigma )
    return(yimp)
}
##################################################

Try the miceadds package in your browser

Any scripts or data that you put into this service are public.

miceadds documentation built on July 7, 2018, 1 a.m.