monteCarlo: Monte Carlo method [p-value]

View source: R/rMethods.R

monteCarloR Documentation

Monte Carlo method [p-value]

Description

Calculates an empirical p-value based on Monte Carlo simulations. Perfect for small sequences (both markov chains and identically and independently distributed) with length ~ 10^3.

Usage

monteCarlo(local_score, FUN, ..., plot = TRUE, numSim = 1000, keepSimu = FALSE)

monteCarlo_double(
  local_score,
  FUN,
  ...,
  plot = TRUE,
  numSim = 1000,
  keepSimu = FALSE
)

Arguments

local_score

local score observed in a segment.

FUN

function to simulate similar sequences with.

...

parameters for FUN

plot

boolean value if to display plots for cumulated function and density

numSim

number of sequences to generate during simulation

keepSimu

Boolean, default to FALSE. If TRUE, the simulated local scores are returned as the localScores element of the output list.

Details

Be careful that the parameters names of the function FUN should differ from those of monteCarlo function.
The density plot produced by plot == TRUE depends on the type of the simulated local scores: if they are integer, a barplot of relative frequency is used, else plot(density(...)) is used.
This function calls localScoreC which type of the output depends on the type of the input. To be efficient, be aware to use a simulating function FUN that return a vector of adequate type ("integer" or "numeric"). Warning: in R, typeof(c(1,3,4,10)) == "double". You can set a type of a vector with mode() or as.integer() functions for example.
monteCarlo_double() is deprecated. At this point, it is just a call to monteCarlo() function.

Value

If keepSimu is FALSE, returns a numeric value corresponding to the probability to obtain a local score with value greater or equal to the parameter local_score.
If keepSimu is TRUE, returns a list containing:

p_value Floating value corresponding to the probability to obtain a local score with a value greater or equal to the parameter local_score
localScores Vector of size numSim containing the simulated local scores

Functions

  • monteCarlo_double(): Monte-Carlo function for double [deprecated]

See Also

karlinMonteCarlo localScoreC

Examples


monteCarlo(120, FUN = rbinom, n = 100, size = 5, prob=0.2)


mySeq <- sample(-7:3, replace = TRUE, size = 1000)
monteCarlo(local_score = 18, FUN = function(x) {return(sample(x = x, 
           size = length(x), replace = TRUE))}, x = mySeq)


#Examples of non integer score function
mySeq2 <- sample(-7:6 - 0.5, replace = TRUE, size = 1000)
monteCarlo(local_score = 50.5, FUN = function(x) {return(sample(x = x, 
           size = length(x), replace = TRUE))}, x = mySeq2)


#Examinating simulated local scores
mySeq2 <- sample(-7:6, replace = TRUE, size = 1000)
simu <- monteCarlo(local_score = 50.5, FUN = function(x) {return(sample(x = x, 
           size = length(x), replace = TRUE))}, x = mySeq2, keepSimu = TRUE)
hist(simu$localScores)


# Markovian example
MyTransMat <-
    matrix(c(0.3,0.1,0.1,0.1,0.4, 0.2,0.2,0.1,0.2,0.3, 0.3,0.4,0.1,0.1,0.1, 0.3,0.3,0.1,0.0,0.3,
             0.1,0.1,0.2,0.3,0.3), ncol = 5, byrow=TRUE)
monteCarlo(local_score = 50,
          FUN = transmatrix2sequence, matrix = MyTransMat,
          length=150, score = c(-2,-1,0,2,3), plot=FALSE, numSim = 5000)


localScore documentation built on April 3, 2025, 5:26 p.m.