Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer: a bug in findrest fixed)
(Answer: safer implementation of truncateIndex)
Line 368: Line 368:
  
 
truncateIndex <- function( # Truncates an index to contain only the largest index bins.
 
truncateIndex <- function( # Truncates an index 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 bins will be merged to "Other".
+
bins = 10, # Number of bins to show. Other bins will be merged to "Other".
 +
sum_others = TRUE # Should "Other" be summed to maintain marginal status
 
) {
 
) {
 +
#obj@output <- obj@output[!is.na(result(obj)),]
 +
test <- oapply(obj, cols, sum, na.rm = TRUE)
 
for(i in 1:length(cols))
 
for(i in 1:length(cols))
 
{
 
{
sums <- as.data.frame(as.table(tapply(result(obj), obj@output[cols[i]], sum)))
+
test2 <- oapply(test, cols[i], sum)
bins <- min(bins, length(levels(obj@output[[cols[i]]])))
+
limit <- sort(sums$Freq, decreasing = TRUE)[bins]
+
if (bins < nrow(test2@output)) {
keeps <- sums[sums$Freq > limit , cols[i]]
+
temp <- as.character(obj@output[[cols[i]]])
keeps <- levels(keeps)[keeps]
+
location_weight_order <- order(result(test2), decreasing = TRUE)
 +
keeps <- test2@output[[cols[i]]][location_weight_order[1:bins]]
 +
temp[!temp %in% keeps] <- "Other"
 +
obj@output[[cols[i]]] <- temp
  
levels(obj@output[[cols[i]]]) <- ifelse(
+
# After changing some locations to "Other", sum along indices to avoid problems
levels(obj@output[[cols[i]]]) %in% keeps,
+
if(sum_others) {
levels(obj@output[[cols[i]]]),
+
ind <- colnames(obj@output)[obj@marginal | colnames(obj@output) %in% cols]
"Other"
+
obj <- oapply(obj, ind, sum)
)
+
}
 +
}
 
}
 
}
 
return(obj)
 
return(obj)

Revision as of 08:31, 10 June 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>