Skip to content

Commit

Permalink
update blocksdesign algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
emitanaka committed Nov 20, 2023
1 parent b705547 commit 6544fb3
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions R/permute.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#' @export
order_trts.blocksdesign <- function(x, trts_df, units_df, unit, vparents, prov, model = NULL, ...) {
order_trts.blocksdesign <- function(x, trts_table, units_table, unit, constrain, prov, model = NULL, ...) {
if(!requireNamespace("blocksdesign")) abort("You need to install `blocksdesign` package.")
udf <- units_df[, setdiff(names(units_df), as.character(unit)), drop = FALSE]
names(trts_df) <- paste0("T", 1:ncol(trts_df))
tlist <- lapply(trts_df, as.factor)
udf <- data.frame(lapply(udf, as.factor))
names(trts_table) <- paste0("T", 1:ncol(trts_table))
# drop any treatment factors that appear only once
tcount <- sapply(trts_table, function(x) length(unique(x)))
tlist <- lapply(trts_table[tcount > 1], as.factor)
if(length(tlist)==0) return(rep(1, nrow(units_table)))
# drop any unit factors that have completely distinct labels
ucount <- sapply(units_table, function(x) length(unique(x)))
udf <- data.frame(lapply(units_table[ucount < nrow(units_table)], as.factor))
res <- blocksdesign::design(treatments = tlist, blocks = udf, treatments_model = model,
weighting = 0.5)
trts_df$..trtid.. <- 1:nrow(trts_df)
trts_table$..trtid.. <- 1:nrow(trts_table)
out <- res$Design
out$..id.. <- 1:nrow(out)
out <- merge(out, trts_df)
out <- merge(out, trts_table)
out[order(out$..id..), "..trtid..", drop = TRUE]
}

Expand Down

0 comments on commit 6544fb3

Please sign in to comment.