Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer: collapsemarg added)
(Answer: better version of collapsemarg, also contains pick)
Line 69: Line 69:
  
 
## collapsemarg is a placeholder for a better functionality within CollapseMarginals.
 
## collapsemarg is a placeholder for a better functionality within CollapseMarginals.
## It takes an ovariable, and summarises all but given marginals using tapply and a user-defined function.
+
## It takes an ovariable, and summarises all indices in cols using tapply and a user-defined function.
 +
## However, you can also use function "pick" to select locations defined in a list picks found in indices cols.
 
## The output is an ovariable with the same name as the input.
 
## The output is an ovariable with the same name as the input.
 
## This was first created for [[:op_fi:Radonin terveysvaikutukset]]
 
## This was first created for [[:op_fi:Radonin terveysvaikutukset]]
  
collapsemarg <- function(object, marginals, fun = sum) {
+
collapsemarg <- function(variable, cols, fun = "sum", picks = list(), ...) { # cols is a character vector, while probs is a list
 +
out <- dropall(variable@output)
 +
marginals <- colnames(out)[variable@marginal]
 +
margtemp <- colnames(out)[colnames(out) %in% marginals & !colnames(out) %in% cols] # You must leave at least one index.
  
object@output <- dropall(object@output)
+
if(tolower(fun) == "pick") { # The function must be a string, otherwise this row will fail.
 +
for(i in cols) {
 +
out <- out[out[[i]] %in% picks[[match(i, cols)]] , ]
 +
}
 +
} else {
 +
out <- as.data.frame(as.table(tapply(result(variable), out[margtemp], fun)))
 +
out <- out[!is.na(out$Freq) , ]
  
temp <- as.data.frame(as.table(tapply(result(object), object@output[marginals], fun)))
+
colnames(out)[colnames(out) == "Freq"] <- ifelse(
 +
length(variable@name) == 0,
 +
"Result",  
 +
paste(variable@name, "Result", sep = "")
 +
)
 +
}
  
colnames(temp)[colnames(temp) == "Freq"] <- "Result"
+
variable@output <- out
 
+
variable@marginal <- colnames(out) %in% marginals & ! colnames(out) %in% cols
out <- EvalOutput(Ovariable(object@name, data = temp))
+
 
+
return(variable)
return(out)
 
 
}
 
}
 
  
 
objects.store(ograph, fillna, collapsemarg)
 
objects.store(ograph, fillna, collapsemarg)

Revision as of 09:28, 19 November 2013



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

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>