-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from xoopR/transformers
Transformers
- Loading branch information
Showing
14 changed files
with
307 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# unify column names across pdfs in | ||
# matrices (merge_matpdf_cols) or arrays (merge_arrpdf_cols) | ||
.merge_cols <- function(arrs, fun = "pdf") { | ||
if (fun == "cdf") { | ||
arrs <- lapply(arrs, cdfpdf) | ||
} else if (fun == "surv") { | ||
arrs <- lapply(arrs, function(.x) cdfpdf(1 - .x)) | ||
} else if (fun != "pdf") { | ||
stop(sprintf( | ||
"Expected 'fun' to be 'pdf', 'cdf', or 'surv'. Got: '%s'.", | ||
fun | ||
)) | ||
} | ||
|
||
if (dim(arrs[[1L]]) == 2L) { | ||
out <- .merge_matpdf_cols(arrs) | ||
} else { | ||
out <- .merge_arrpdf_cols(arrs) | ||
} | ||
|
||
if (fun == "cdf") { | ||
lapply(out, pdfcdf) | ||
} else if (fun == "surv") { | ||
lapply(out, function(.x) 1 - pdfcdf(.x)) | ||
} else { | ||
out | ||
} | ||
} | ||
|
||
.merge_arrpdf_cols <- function(pdfs) { | ||
if (length(unique(viapply(pdfs, function(.x) dim(.x)[[3L]]))) > 1) { | ||
stop("Can only merge arrays with same length on third dimension.") | ||
} | ||
|
||
nc <- unique(viapply(pdfs, ncol)) | ||
|
||
if (length(nc) == 1) { | ||
if (all(vapply(pdfs, colnames, character(nc)) == colnames(pdfs[[1]]))) { | ||
return(pdfs) | ||
} | ||
} | ||
|
||
cnms <- sort(unique(as.numeric(unlist(lapply(pdfs, colnames))))) | ||
# new number of rows and columns | ||
nc <- length(cnms) | ||
nl <- dim(pdfs[[1]])[3L] | ||
|
||
lapply(pdfs, function(.x) { | ||
out <- array(0, c(nrow(.x), nc, nl), list(NULL, cnms, NULL)) | ||
out[, match(as.numeric(colnames(.x)), cnms), ] <- .x | ||
out | ||
}) | ||
} | ||
|
||
.merge_matpdf_cols <- function(pdfs) { | ||
|
||
nc <- unique(viapply(pdfs, ncol)) | ||
|
||
if (length(nc) == 1) { | ||
if (all(vapply(pdfs, colnames, character(nc)) == colnames(pdfs[[1]]))) { | ||
return(pdfs) | ||
} | ||
} | ||
|
||
cnms <- sort(unique(as.numeric(unlist(lapply(pdfs, colnames))))) | ||
# new number of rows and columns | ||
nc <- length(cnms) | ||
|
||
lapply(pdfs, function(.x) { | ||
out <- matrix(0, nrow(.x), nc, FALSE, list(NULL, cnms)) | ||
out[, match(as.numeric(colnames(.x)), cnms)] <- .x | ||
out | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
pdfcdf <- function(pdf) { | ||
d <- dim(pdf) | ||
dn <- dimnames(pdf) | ||
|
||
if (is.null(d)) { | ||
return(cumsum(pdf)) | ||
} else if (length(d) == 2) { | ||
out <- C_mat_PdfCdf(pdf) | ||
} else if (length(d) == 3) { | ||
# quicker than apply with C_mat_ | ||
out <- aperm(apply(unname(pdf), c(1, 3), C_vec_PdfCdf), c(2, 1, 3)) | ||
} else { | ||
stop(sprintf("Expected maximum of three dimensions but got '%s'.", length(d))) | ||
} | ||
|
||
dimnames(out) <- dn | ||
out | ||
} | ||
|
||
cdfpdf <- function(cdf) { | ||
d <- dim(cdf) | ||
dn <- dimnames(cdf) | ||
|
||
if (is.null(d)) { | ||
return(c(cdf[1], diff(cdf))) | ||
} else if (length(d) == 2) { | ||
out <- C_mat_CdfPdf(cdf) | ||
} else if (length(d) == 3) { | ||
# quicker than apply with C_mat_ | ||
out <- aperm(apply(unname(cdf), c(1, 3), C_vec_CdfPdf), c(2, 1, 3)) | ||
} else { | ||
stop(sprintf("Expected maximum of three dimensions but got '%s'.", length(d))) | ||
} | ||
|
||
dimnames(out) <- dn | ||
out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.