Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer)
(Answer: correlvar added)
Line 18: Line 18:
  
 
library(OpasnetUtils)
 
library(OpasnetUtils)
 +
 +
############ Shuffles columns of a data.frame so that they match a pre-defined correlation matrix
 +
 +
#### THIS SHOULD BE UPDATED FOR OVARIABLES AS WELL: SHUFFLING ACROSS Iter WITH
 +
#### CORRELATION MATRIX ACROSS DEFINED INDICES AND THEIR LOCATIONS. OTHER INDICES ARE
 +
#### KEPT UNCHANGED, SO THE SHUFFLING HAS TO HAPPEN WITHIN EACH UNIQUE LOCATION COMBINATION.
 +
 +
correlvar <- function(
 +
  vars, # multivariable object to be correlated.
 +
  Sigma # covariance matrix wanted.
 +
) {
 +
 
 +
  # Method from http://www.r-bloggers.com/easily-generate-correlated-variables-from-any-distribution-without-copulas/
 +
  require(MASS)
 +
  mu <- rep(0,ncol(vars))
 +
  rawvars <- as.data.frame(mvrnorm(n = nrow(vars), mu = mu, Sigma = Sigma))
 +
  out <- as.data.frame(
 +
    lapply(
 +
      1:ncol(vars),
 +
      FUN = function(i, vars, rawvars) {
 +
        pvars <- rank(rawvars[[i]], ties.method = "random")
 +
        tmp <- sort(vars[[i]]) # Make sure you start with ordered data.
 +
        tmp <- tmp[pvars] # Order based on correlated ranks
 +
        return(tmp)
 +
      },
 +
      vars = vars,
 +
      rawvars = rawvars
 +
    )
 +
  )
 +
  colnames(out) <- colnames(vars)
 +
  return(out)
 +
}
  
 
##################### Forgets decisions so that decision indices will be recreated.
 
##################### Forgets decisions so that decision indices will be recreated.
Line 839: Line 871:
 
}
 
}
  
 +
rm(wiki_username)
 
objects.store(list = ls())
 
objects.store(list = ls())
cat(paste("All objects in the global namespace were stored.\n"))
+
cat("All objects in the global namespace were stored:", ls(), "\n")
  
 
</rcode>
 
</rcode>

Revision as of 07:29, 12 May 2016



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>