Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
m
(Answer: truncateIndex improved)
Line 437: Line 437:
 
# Merge all but show_bins largest bins of indices cols to 'Other'.
 
# Merge all but show_bins largest bins of indices cols to 'Other'.
  
truncateIndex <- function( # Truncates an index to contain only the largest index bins.
+
truncateIndex <- function( # Truncates indices to contain only the largest index bins.
 
obj, # ovariable to use.
 
obj, # ovariable to use.
 
cols, # names of the columns to truncate.
 
cols, # names of the columns to truncate.
bins = 10, # Number of bins to show. Other locations will be lumped to bin "Other".
+
bins = rep(10, length(cols)), # Number of bins to show, including Others. Smallest locations will be lumped to bin "Other".
 
sum_others = TRUE # Should "Other" be summed to maintain marginal status
 
sum_others = TRUE # Should "Other" be summed to maintain marginal status
 
) {
 
) {
#obj@output <- obj@output[!is.na(result(obj)),]
+
if(nrow(obj@output) == 0) stop("Ovariable ", obj@name, " not evaluated.\n")
test <- oapply(abs(obj), cols, sum, na.rm = TRUE)
+
test <- oapply(abs(obj), INDEX = cols, sum, na.rm = TRUE)
 +
if(length(cols) > 1 & length(bins) == 1) bins <- rep(bins, length(cols))
 +
 
for(i in 1:length(cols))
 
for(i in 1:length(cols))
 
{
 
{
test2 <- oapply(test, cols[i], sum)
+
test2 <- oapply(test, INDEX = cols[i], sum)
 +
test2@output <- test2@output[result(test2) > 0 , ]
 
 
if (bins < nrow(test2@output)) {
+
temp <- as.factor(obj@output[[cols[i]]])
temp <- as.character(obj@output[[cols[i]]])
+
location_weight_order <- order(result(test2), decreasing = TRUE)
location_weight_order <- order(result(test2), decreasing = TRUE)
+
if (bins[i] <= nrow(test2@output)) {
keeps <- test2@output[[cols[i]]][location_weight_order[1:bins]]
+
keeps <- test2@output[[cols[i]]][location_weight_order[0:(bins[i] - 1)]]
temp[!temp %in% keeps] <- "Other"
+
levels(temp)[!levels(temp) %in% keeps] <- "Other"
 +
temp <- factor(temp, levels = c(levels(temp)[levels(temp) != "Other"], "Other"))
 
obj@output[[cols[i]]] <- temp
 
obj@output[[cols[i]]] <- temp
 
# After changing some locations to "Other", sum along indices to avoid problems
 
if(sum_others) {
 
ind <- colnames(obj@output)[obj@marginal | colnames(obj@output) %in% cols]
 
obj <- oapply(obj, ind, sum)
 
}
 
 
}
 
}
 +
}
 +
# After changing some locations to "Other", sum along indices to avoid problems
 +
if(sum_others) {
 +
# ind <- colnames(obj@output)[obj@marginal | colnames(obj@output) %in% cols]
 +
obj <- oapply(obj, cols = "", FUN = sum, na.rm = TRUE)
 
}
 
}
 
return(obj)
 
return(obj)

Revision as of 10:02, 27 July 2015



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer")

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>