.packageName <- "rake"
"colvar" <-
function (rake) 
attr(rake, "colCol")
"dataname" <-
function (rake) 
attr(rake, "dataname")
"format.rake" <-
function (x, ...) 
{
    rake <- x
    rake <- rakeTot(rake)
    rake <- format(rake, ...)
    rownames(rake)[nrow(rake)] <- "Column Total"
    colnames(rake)[ncol(rake)] <- "Row Total"
    rake
}
"predict.rake" <-
function (object, col = stop("Prediction column (argument 'col') must be specified."), 
    forcefactor = FALSE, data = eval(parse(text = dataname(rake)), 
        parent.frame(1)), ...) 
{
    rake <- object
    W <- rep(0, nrow(data))
    rowCol <- rowvar(rake)
    colCol <- colvar(rake)
    for (rnam in rownames(rake)) {
        for (cnam in colnames(rake)) {
            clas <- (data[, rowCol] == rnam & data[, colCol] == 
                cnam)
            W[clas] <- rake[rownames(rake) == rnam, colnames(rake) == 
                cnam]/sum(clas)
        }
    }
    v <- data[, col]
    if (class(v) == "character" || class(v) == "factor" || forcefactor) {
        datasum <- summary(as.factor(v))
        for (nam in names(datasum)) {
            datasum[nam] <- sum((v == nam) * W)
        }
        v <- summary(as.factor(v))
        est <- datasum
    }
    else {
        est <- v * W
    }
    list(data = v, weight = W, data.est = est)
}
"print.rake" <-
function (x, ...) 
{
    rake <- x
    cat("  data: ", dataname(rake), "\n", "rowvar: ", rowvar(rake), 
        "\n", "colvar: ", colvar(rake), "\n", sep = "")
    rake <- rakeTot(rake)
    print(rake)
}
"rake" <-
function (data, colCol = 1, rowCol = 2, weight = if ("weight" %in% 
    names(data)) "weight" else 1) 
{
    if (class(weight) == "character") 
        weight <- data[, names(data) == weight]
    weight <- rep(weight, length = nrow(data))
    cols <- summary(as.factor(data[, colCol]))
    rows <- summary(as.factor(data[, rowCol]))
    cols <- cols[cols != 0]
    rows <- rows[rows != 0]
    r <- matrix(0, ncol = length(cols), nrow = length(rows))
    if (sum(names(rows) %in% names(cols)) > 0) {
        warning("Rake rows and columns have some shared names,\n", 
            "but must not in order for rake adjustment to succeed.\n")
    }
    rownames(r) <- names(rows)
    colnames(r) <- names(cols)
    for (rname in names(rows)) {
        for (cname in names(cols)) {
            r[rownames(r) == rname, colnames(r) == cname] <- sum(weight[data[, 
                colCol] == cname & data[, rowCol] == rname])
        }
    }
    attr(r, "dataname") <- deparse(substitute(data))
    attr(r, "class") <- "rake"
    if (class(rowCol) != "character" && !is.null(colnames(data))) 
        rowCol <- colnames(data)[rowCol]
    if (class(colCol) != "character" && !is.null(colnames(data))) 
        colCol <- colnames(data)[colCol]
    attr(r, "rowCol") <- rowCol
    attr(r, "colCol") <- colCol
    r
}
"rakeTot" <-
function (x) 
{
    rake <- x
    rake <- cbind(rake, rowSums(rake))
    rake <- rbind(rake, colSums(rake))
    rake
}
"rakeadj" <-
function (rake, marg = stop("\nMarginal total population weights", 
    "(argument 'marg') must be specified."), verbose = FALSE) 
{
    if (!class(rake) == "rake") 
        stop("\nArgument 'rake' must be an object of class \"rake\".")
    if (sum(rownames(rake) %in% colnames(rake)) > 0) {
        print(rake)
        stop("\nRake rows and columns have shared names,\n", 
            "but must not in order for rake adjustment to succeed.")
    }
    if (class(marg) == "character") 
        marg <- read.table(marg, header = T)
    if (class(marg) == "data.frame") {
        margnames <- marg$name
        marg <- marg$weight
        names(marg) <- margnames
    }
    if (!is.vector(marg) || is.null(names(marg))) 
        stop("\nMarginal total weights (argument 'marg') ", "in unrecognized format.\n", 
            "Please refer to 'help(rakeadj)'.")
    rowtot <- marg[rownames(rake)]
    coltot <- marg[colnames(rake)]
    if (sum(rowtot) != sum(coltot)) {
        print(c(sum(rowtot), rowtot))
        print(c(sum(coltot), coltot))
        stop("\nSum of population marginal total weights ", "(argument 'marg')\n", 
            "of rows and columns are not the same, ", "but need to be for Raking to converge.")
    }
    rakesum <- sum(rake)
    adjust <- function(v, key) v * key/sum(v)
    oldrake <- 0
    count <- 0
    while (sum(oldrake == rake) != length(rake)) {
        oldrake <- rake
        for (nam in rownames(rake)) rake[nam, ] <- adjust(rake[nam, 
            ], marg[nam])
        for (nam in colnames(rake)) rake[, nam] <- adjust(rake[, 
            nam], marg[nam])
        count <- count + 1
    }
    if (verbose) 
        cat("The rake adjustment converged in ", count, " steps.\n", 
            sep = "")
    rake/sum(rake) * rakesum
}
"rowvar" <-
function (rake) 
attr(rake, "rowCol")
"simpleRake" <-
function (data, marg, colCol = 1, rowCol = 2, col = 3, forcefactor = FALSE, 
    weight = if ("weight" %in% names(data)) "weight" else 1, 
    verbose = FALSE) 
{
    if (missing(marg)) 
        stop("\nMarginal total population weights ", "(argument 'marg') must be specified.")
    r <- rake(data, colCol, rowCol, weight)
    r <- rakeadj(r, marg, verbose)
    predict(r, col, forcefactor)
}
