<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/announcing-mcht-an-r-package-for-bootstrap-and-monte-carlo-hypothesis-testing/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ntguardian.wordpress.com/2018/10/08/announcing-mcht-package-bootstrap-monte-carlo-hypothesis-testing/"> R – Curtis Miller's Personal Website</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am very excited to announce my first (public) package (and the second package I’ve ever written, the first being unannounced until the accompanying paper is accepted). That package is <strong>MCHT</strong>, a package for bootstrap and Monte Carlo hypothesis testing, currently available <a rel="nofollow" href="https://github.com/ntguardian/MCHT" target="_blank">on GitHub</a>.</p>
<p><span></span></p>
<p>This will be the first of a series of blog posts introducing the package. Most of the examples in the blog posts are already present in <a rel="nofollow" href="https://github.com/ntguardian/MCHT/blob/master/MANUAL.pdf" target="_blank">the manual</a>, but I plan to go into more depth here, including some background and more detailed explanations.</p>
<p><strong>MCHT</strong> is a package implementing an interface for creating and using Monte Carlo tests. The primary function of the package is <code>MCHTest()</code>, which creates functions with S3 class <code>MCHTest</code> that perform a Monte Carlo test.</p>
<h2>Installation</h2>
<p><strong>MCHT</strong> is not presently available on CRAN. You can download and install <strong>MCHT</strong> from GitHub using <a rel="nofollow" href="https://github.com/r-lib/devtools" target="_blank"><strong>devtools</strong></a> via the R command <code>devtools::install_github(&quot;ntguardian/MCHT&quot;)</code>.</p>
<h2>Monte Carlo Hypothesis Testing</h2>
<p>Monte Carlo testing is a form of hypothesis testing where the <img alt="p" title="p" src="https://s0.wp.com/latex.php?latex=p&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-values are computed using the empirical distribution of the test statistic computed from data simulated under the null hypothesis. These tests are used when the distribution of the test statistic under the null hypothesis is intractable or difficult to compute, or as an exact test (that is, a test where the distribution used to compute $p$-values is appropriate for any sample size, not just large sample sizes).</p>
<p>Suppose that <img alt="s_n" title="s_n" src="https://s0.wp.com/latex.php?latex=s_n&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> is the observed value of the test statistic and large values of <img alt="s_n" title="s_n" src="https://s0.wp.com/latex.php?latex=s_n&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> are evidence against the null hypothesis; normally, <img alt="p" title="p" src="https://s0.wp.com/latex.php?latex=p&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-values would be computed as <img alt="1 - F(s_n)" title="1 - F(s_n)" src="https://s0.wp.com/latex.php?latex=1+-+F%28s_n%29&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">, where <img alt="F(s_n) = F(S_n leq s_n)" title="F(s_n) = F(S_n leq s_n)" src="https://s0.wp.com/latex.php?latex=F%28s_n%29+%3D+F%28S_n+%5Cleq+s_n%29&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> is the cumulative distribution functions and <img alt="S_n" title="S_n" src="https://s0.wp.com/latex.php?latex=S_n&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> is the random variable version of <img alt="s_n" title="s_n" src="https://s0.wp.com/latex.php?latex=s_n&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">. We cannot use <img alt="F" title="F" src="https://s0.wp.com/latex.php?latex=F&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> for some reason; it’s intractable, or the <img alt="F" title="F" src="https://s0.wp.com/latex.php?latex=F&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> provided is only appropriate for large sample sizes.</p>
<p>Instead of using <img alt="F" title="F" src="https://s0.wp.com/latex.php?latex=F&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> we will use <img alt="hat{F}_N" title="hat{F}_N" src="https://s0.wp.com/latex.php?latex=%5Chat%7BF%7D_N&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">, which is the empirical CDF of the same test statistic computed from simulated data following the distribution prescribed by the null hypothesis of the test. For the sake of simplicity in this presentation, assume that <img alt="S" title="S" src="https://s0.wp.com/latex.php?latex=S&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> is a continuous random variable. Now our <img alt="p" title="p" src="https://s0.wp.com/latex.php?latex=p&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-value is <img alt="1 - hat{F}_N(s_n)" title="1 - hat{F}_N(s_n)" src="https://s0.wp.com/latex.php?latex=1+-+%5Chat%7BF%7D_N%28s_n%29&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">, where <img alt="hat{F}_N(s_n) = frac{1}{N}sum_{j = 1}^{N}I_{{tilde{S}_{n,j} leq s_n}}" title="hat{F}_N(s_n) = frac{1}{N}sum_{j = 1}^{N}I_{{tilde{S}_{n,j} leq s_n}}" src="https://s0.wp.com/latex.php?latex=%5Chat%7BF%7D_N%28s_n%29+%3D+%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bj+%3D+1%7D%5E%7BN%7DI_%7B%5C%7B%5Ctilde%7BS%7D_%7Bn%2Cj%7D+%5Cleq+s_n%5C%7D%7D&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> where <img alt="I" title="I" src="https://s0.wp.com/latex.php?latex=I&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> is the indicator function and <img alt="tilde{S}_{n,j}" title="tilde{S}_{n,j}" src="https://s0.wp.com/latex.php?latex=%5Ctilde%7BS%7D_%7Bn%2Cj%7D&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> is an independent random copy of <img alt="S_n" title="S_n" src="https://s0.wp.com/latex.php?latex=S_n&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> computed from simulated data with a sample size of <img alt="n" title="n" src="https://s0.wp.com/latex.php?latex=n&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">.</p>
<p>The power of these tests increase with <img alt="N" title="N" src="https://s0.wp.com/latex.php?latex=N&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> (see [1]) but modern computers are able to simulate large <img alt="N" title="N" src="https://s0.wp.com/latex.php?latex=N&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> quickly, so this is rarely an issue. The procedure above also assumes that there are no nuisance parameters and the distribution of <img alt="S_n" title="S_n" src="https://s0.wp.com/latex.php?latex=S_n&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> can effectively be known precisely when the null hypothesis is true (and all other conditions of the test are met, such as distributional assumptions). A different procedure needs to be applied when nuisance parameters are not explicitly stated under the null hypothesis. [2] suggests a procedure using optimization techniques (recommending simulated annealing specifically) to adversarially select values for nuisance parameters valid under the null hypothesis that maximize the <img alt="p" title="p" src="https://s0.wp.com/latex.php?latex=p&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-value computed from the simulated data. This procedure is often called <em>maximized Monte Carlo</em> (MMC) testing. That is the procedure employed here. (In fact, the tests created by <code>MCHTest()</code> are the tests described in [2].) Unfortunately, MMC, while conservative and exact, has much less power than if the unknown parameters were known, perhaps due to the behavior of samples under distributions with parameter values distant from the true parameter values (see [3]).</p>
<h2>Bootstrap Hypothesis Testing</h2>
<p>Bootstrap statistical testing is very similar to Monte Carlo testing; the key difference is that bootstrap testing uses information from the sample. For example a parametric bootstrap test would estimate the parameters of the distribution the data is assumed to follow and generate datasets from that distribution using those estimates as the actual parameter values. A permutation test (like Fisher’s permutation test; see [4]) would use the original dataset values but randomly shuffle the labeles (stating which sample an observation belongs to) to generate new data sets and thus new simulated test statistics. <img alt="p" title="p" src="https://s0.wp.com/latex.php?latex=p&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-values are essentially computed the same way.</p>
<p>Unlike Monte Carlo tests and MMC, these tests are not exact tests. That said, they often have good finite sample properties. (See [3].) See the documentation mentioned above for more details and references.</p>
<h2>Motivation</h2>
<p>Why write a package for these types of tests? This is not the only package that facilitates bootstrapping or Monte Carlo testing. The website <a rel="nofollow" href="https://www.rdocumentation.org/?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">RDocumentation</a> includes documentation for the package <a rel="nofollow" href="https://www.rdocumentation.org/packages/MChtest/?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank"><strong>MChtest</strong></a>, by Michael Fay which exists for Monte Carlo testing, too. The package <a rel="nofollow" href="https://github.com/julienneves/MaxMC" target="_blank"><strong>MaxMC</strong></a> by <a rel="nofollow" href="http://julienneves.com/" target="_blank">Julien Neves</a> is devoted to MMC specifically, as described by [2]. Then there’s the package <a rel="nofollow" href="https://cran.r-project.org/web/packages/boot/index.html" target="_blank"><strong>boot</strong></a>, which is intended to facilitate bootstrapping. (If I’m missing anything, please let me know in the comments.)</p>
<p><strong>MChtest</strong> is no longer on CRAN and implements a particular form of Monte Carlo testing and thus does not work for MMC. <strong>MaxMC</strong> appears to be in a very raw state. <strong>boot</strong> seems general enough that it could be used for bootstrap testing but still seems more geared towards constructing bootstrap confidence intervals and standard errors rather than hypothesis testing. All of these have a very different architecture from <strong>MCHT</strong>, which is primarily for creating a function like <code>t.test()</code> that performs a hypothesis test that was described when the function was created.</p>
<p>Additionally, this was good practice in practicing package development and more advanced R programming. This is the first time I made serious use of closures, S3 classes and R’s flavor of object-oriented programming, and environments. So far the result seems to be an flexible and robust tool for performing tests based on randomization.</p>
<h2>Getting Started</h2>
<p>Let’s start with a “Hello, world!”-esque example for a Monte Carlo test: a Monte Carlo version of the <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test.</p>
<p>The one-sample <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test, one of the oldest statistical tests used today, is used to test for the location of the population mean <img alt="mu" title="mu" src="https://s0.wp.com/latex.php?latex=%5Cmu&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">. It decides between the set of hypotheses:</p>
<p><img alt="H_0: mu = mu_0" title="H_0: mu = mu_0" src="https://s0.wp.com/latex.php?latex=H_0%3A+%5Cmu+%3D+%5Cmu_0&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"></p>
<p><img alt="H_A: mu neq mu_0" title="H_A: mu neq mu_0" src="https://s0.wp.com/latex.php?latex=H_A%3A+%5Cmu+%5Cneq+%5Cmu_0&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"></p>
<p>(The alternative could also be one-sided, perhaps instead stating <img alt="mu  mu_0" title="mu  mu_0" src="https://s0.wp.com/latex.php?latex=%5Cmu++%5Cmu_0&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">.) The <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test is an exact, most-powerful test for any sample size if the data generating process (DGP) that was used to produce the sample is a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Normal_distribution" target="_blank">Gaussian distribution</a>. If we believe this assumption then the Monte Carlo version of the test is a contrived example as we could not do better than to use <code>t.test()</code>, but the moment we drop this assumption there is an opening for Monte Carlo testing to be useful.</p>
<p>Let’s load up the package.</p>
<pre>
library(MCHT)
</pre>
<pre>
## .------..------..------..------.
## |M.--. ||C.--. ||H.--. ||T.--. |
## | (\/) || :/\: || :/\: || :/\: |
## | :\/: || :\/: || (__) || (__) |
## | '--'M|| '--'C|| '--'H|| '--'T|
## `------'`------'`------'`------' v. 0.1.0
## Type citation(&quot;MCHT&quot;) for citing this R package in publications
</pre>
<p>(Yes, I’ve got a cute little <code>.onAttach()</code> package start-up message. I first saw a message like this implemented by <a rel="nofollow" href="https://cran.r-project.org/web/packages/mclust/index.html" target="_blank"><strong>mclust</strong></a> and of course <a rel="nofollow" href="https://www.stata.com/" target="_blank">Stata’s</a> start-up message and thought they’re so adorable that I will likely add such messages to all my packages. You can use <code>suppressPackageStartupMessages()</code> to make this quiet if you want. Thanks to the Python package <a rel="nofollow" href="https://pypi.org/project/art/" target="_blank"><strong>art</strong></a> for the cool ASCII art.)</p>
<p>The star function of the pacakge is the <code>MCHTest()</code> function.</p>
<pre>
args(MCHTest)
</pre>
<pre>
## function (test_stat, stat_gen, rand_gen = function(n) {
##     stats::runif(n)
## }, N = 10000, seed = NULL, memoise_sample = TRUE, pval_func = MCHT::pval, 
##     method = &quot;Monte Carlo Test&quot;, test_params = NULL, fixed_params = NULL, 
##     nuisance_params = NULL, optim_control = NULL, tiebreaking = FALSE, 
##     lock_alternative = TRUE, threshold_pval = 1, suppress_threshold_warning = FALSE, 
##     localize_functions = FALSE, imported_objects = NULL) 
## NULL
</pre>
<p>The documentation for this function is the majority of the manual and I’ve written multiple examples demonstrating its use. In short, a single call to <code>MCHTest()</code> will create an <code>MCHTest</code>-S3-class object (which is just a function) that can be use for hypothesis testing. Three arguments (all of which are functions) passed to the call will characterize the resulting test:</p>
<ul>
<li><code>test_stat</code>: A function with an argument <code>x</code> that computes the test statistic, with <code>x</code> being the argument that accepts the dataset from which to compute the test statistic.</li>
<li><code>rand_gen</code>: A function generating random datasets, and must have either an argument <code>x</code> that would accept the original dataset or an argument <code>n</code> that represents the size of the dataset.</li>
<li><code>stat_gen</code>: A function with an argument <code>x</code> that will take the random numbers generated by <code>rand_gen</code> and turn them into a simulated test statistic. Sometimes <code>stat_gen</code> is the same as <code>test_stat</code>, but it is better to write separate functions, as will be seen later.</li>
</ul>
<p>The functions passed to these arguments can accept other parameters, particularly parameters describing test parameters (that is, the parameter values we are testing, such as the population mean <img alt="mu" title="mu" src="https://s0.wp.com/latex.php?latex=%5Cmu&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">), fixed parameters (parameter values the test assumes, like <img alt="sigma" title="sigma" src="https://s0.wp.com/latex.php?latex=%5Csigma&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">, the population standard deviation, whose value is assumed by the <img alt="z" title="z" src="https://s0.wp.com/latex.php?latex=z&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test often taught in introductory statistics courses; see <a rel="nofollow" href="https://onlinecourses.science.psu.edu/stat414/node/269/" target="_blank">this link</a>), and nuisance parameters (parameter values we don’t know, are not directly investigating, and may be needed to know the distribution of the test statistic). For the cases mentioned above, there are <code>MCHTest()</code> parameters that can be used for recognizing them: <code>test_params</code>, <code>fixed_params</code>, and <code>nuisance_params</code>, respectively. While one could in principle ignore these parameters and pass functions to <code>test_stat</code>, <code>stat_gen</code>, and <code>rand_gen</code> that use them anyway, I would recommend not doing so. First, there’s no guarantee that <code>MCHTest</code>-class objects would handle the extra parameters correctly. Second, when <code>MCHTest()</code> is made aware of these special cases, it can check that the functions passed to <code>test_stat</code>, <code>stat_gen</code>, and <code>rand_gen</code> handle these types of parameters correctly and will throw an error when it appears they do not. This safety measure helps you use <strong>MCHT</strong> correctly.</p>
<p>Carrying on, let’s create our first <code>MCHTest</code> object for a <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test.</p>
<pre>
ts &lt;- function(x) {
  sqrt(length(x)) * mean(x)/sd(x)
}

mc.t.test.1 &lt;- MCHTest(ts, ts, rnorm, N = 10000, seed = 123)
</pre>
<p>Above, both <code>test_stat</code> and <code>stat_gen</code> are <code>ts()</code> (they're the first and second arguments, respectively) and the random number generator <code>rand_gen</code> is <code>rnorm()</code>. Two other parameters are:</p>
<ul>
<li><code>N</code>: the number of simulated test statistics to generate.</li>
<li><code>seed</code>: the seed of the random number generator, which makes test results consistent and reproducible.</li>
</ul>
<p><code>MCHTest</code>-class objects have a <code>print()</code> method that summarize how the object was defined. We see it in action here:</p>
<pre>
mc.t.test.1
</pre>
<pre>
## 
## 	Details for Monte Carlo Test
## 
## Seed:  123 
## Replications:  10000 
## 
## Memoisation enabled
## Argument &quot;alternative&quot; is locked
</pre>
<p>This will tell us the seed being used and the number of replicates used for hypothesis testing, along with other messages. I want to draw attention to the message <code>Argument &quot;alternative&quot; is locked</code>. This means that the test we just created will ignore anything passed to the parameter <code>alternative</code> (similar to the parameter of the same name <code>t.test()</code> has). We can enable that parameter by setting the <code>MCHTest()</code> parameter <code>lock_alternative</code> to <code>FALSE</code>.</p>
<pre>
(mc.t.test.1 &lt;- MCHTest(ts, ts, rnorm, N = 10000, seed = 123,
                        lock_alternative = FALSE))
</pre>
<pre>
## 
## 	Details for Monte Carlo Test
## 
## Seed:  123 
## Replications:  10000 
## 
## Memoisation enabled
</pre>
<p>Let's now try this function out on data.</p>
<pre>
dat &lt;- c(0.27, 0.04, 1.37, 0.23, 0.34, 1.44, 0.34, 4.05, 1.59, 1.54)

mc.t.test.1(dat)
</pre>
<pre>
## 
## 	Monte Carlo Test
## 
## data:  dat
## S = 2.9445, p-value = 0.0072
</pre>
<p>If you run the above code you may see a complaint about <code>%dopar%</code> being run sequentially. This complaint appears when we don't register CPU cores for parallelization. <strong>MCHT</strong> uses <a rel="nofollow" href="https://cran.r-project.org/package=foreach" target="_blank"><strong>foreach</strong></a>, <a rel="nofollow" href="https://cran.r-project.org/package=doParallel" target="_blank"><strong>doParallel</strong></a>, and <a rel="nofollow" href="https://cran.r-project.org/package=doRNG" target="_blank"><strong>doRNG</strong></a> to parallelize simulations and thus hopefully speed them up. Simulations can take a long time and parallelization can help make the process faster. If we were to continue we would not see the complaint again; R accepts that there's only one core visible and thus doesn't parallelize. But we can register the other cores on our system with the following:</p>
<pre>
library(doParallel)

registerDoParallel(detectCores())
</pre>
<p>Not only do we have parallelization enabled, <code>MCHTest()</code> automatically enables <a rel="nofollow" href="https://en.wikipedia.org/wiki/Memoization" target="_blank">memoization</a> so that it doesn't redo simulations if the data (or at least the data's sample size) hasn't changed. (This can be turned off by setting the <code>MCHTest()</code> parameter <code>memoise-sample</code> to <code>FALSE</code>.) Again, this is so that we save time and don't have to fear repeat usage of our <code>MCHTest</code>-class function.</p>
<p>The above test effectively checked whether the population mean was zero against the alternative that the population mean is greater than zero (due to the default behaviour when <code>alternative</code> is not specified). By changing the <code>alternative</code> parameter we can test against other alternative hypotheses.</p>
<pre>
mc.t.test.1(dat, alternative = &quot;less&quot;)
</pre>
<pre>
## 
## 	Monte Carlo Test
## 
## data:  dat
## S = 2.9445, p-value = 0.9928
## alternative hypothesis: less
</pre>
<pre>
mc.t.test.1(dat, alternative = &quot;two.sided&quot;)
</pre>
<pre>
## 
## 	Monte Carlo Test
## 
## data:  dat
## S = 2.9445, p-value = 0.0144
## alternative hypothesis: two.sided
</pre>
<p>Compare this to <code>t.test()</code>.</p>
<pre>
t.test(dat, alternative = &quot;two.sided&quot;)
</pre>
<pre>
## 
## 	One Sample t-test
## 
## data:  dat
## t = 2.9445, df = 9, p-value = 0.01637
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.2597649 1.9822351
## sample estimates:
## mean of x 
##     1.121
</pre>
<p>The two tests reach similar conclusions.</p>
<p>However, <code>t.test()</code> is an exact and most-powerful test at any sample size under the assumptions we made. But all we need to do is not assume the data was drawn from a Gaussian distribution to throw the <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test for a loop. <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test will often do well even when the Gaussian assumption is violated but those statements hold for large sample sizes; at no sample size will the test be an exact test. Monte Carlo tests, though, can be exact tests for any sample size under different (often strong) distributional assumptions, without having to compute the distribution of the test statistic under the null hypothesis.</p>
<p>I know for a fact that <code>dat</code> was generated using an exponential distribution, so let's write a new version of the <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test that uses this information. While we're at it, let's add a parameter so that we know we're teseting for the mean of the data and that mean can be specified by the user.</p>
<pre>
ts &lt;- function(x, mu = 1) {
  # Throw an error if mu is not positive; exponential random variables have only
  # positive mu
  if (mu &lt;= 0) stop(&quot;mu must be positive&quot;)

  sqrt(length(x)) * (mean(x) - mu)/sd(x)
}

sg &lt;- function(x, mu = 1) {
  x &lt;- mu * x
  sqrt(length(x)) * (mean(x) - mu)/sd(x)
}

(mc.t.test.2 &lt;- MCHTest(ts, sg, rexp, seed = 123,
                        method = &quot;One-Sample Monte Carlo Exponential t-Test&quot;,
                        test_params = &quot;mu&quot;, lock_alternative = FALSE))
</pre>
<pre>
## 
## 	Details for One-Sample Monte Carlo Exponential t-Test
## 
## Seed:  123 
## Replications:  10000 
## Tested Parameters:  mu 
## Default mu:  1 
## 
## Memoisation enabled
</pre>
<p>Using this new function works the same, only now we can specify the <img alt="mu" title="mu" src="https://s0.wp.com/latex.php?latex=%5Cmu&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex"> we want to test.</p>
<pre>
mc.t.test.2(dat, mu = 2, alternative = &quot;two.sided&quot;)
</pre>
<pre>
## 
## 	One-Sample Monte Carlo Exponential t-Test
## 
## data:  dat
## S = -2.3088, p-value = 0.181
## alternative hypothesis: true mu is not equal to 2
</pre>
<pre>
mc.t.test.2(dat, mu = 1, alternative = &quot;two.sided&quot;)
</pre>
<pre>
## 
## 	One-Sample Monte Carlo Exponential t-Test
## 
## data:  dat
## S = 0.31782, p-value = 0.6888
## alternative hypothesis: true mu is not equal to 1
</pre>
<pre>
t.test(dat, mu = 1, alternative = &quot;two.sided&quot;)
</pre>
<pre>
## 
## 	One Sample t-test
## 
## data:  dat
## t = 0.31782, df = 9, p-value = 0.7579
## alternative hypothesis: true mean is not equal to 1
## 95 percent confidence interval:
##  0.2597649 1.9822351
## sample estimates:
## mean of x 
##     1.121
</pre>
<p>Now the <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test and the Monte Carlo test produce <img alt="p" title="p" src="https://s0.wp.com/latex.php?latex=p&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-values that are not similar, and the Monte Carlo <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test will in general be more accurate. (It appears that the regular <img alt="t" title="t" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0" class="latex">-test is more conservative than the Monte Carlo test and thus is less powerful.)</p>
<h2>Conclusion</h2>
<p>I would consider the current release of <strong>MCHT</strong> to be early beta; it is usable but it's not yet able to be considered &quot;stable&quot;. Keep that in mind if you plan to use it.</p>
<p>I'm very excited about this package and look forward to writing more about it. Stay tuned for future blog posts explaining its functionality. It's highly likely it has strange and mysterious behavior so I hope that if anyone encounters strange behavior, they report it and help push <strong>MCHT</strong> closer to a &quot;stable&quot; state.</p>
<p>I'm early in my academic career (in that I'm a Ph.D. student without any of my own publications yet), and I'm unsure if this package is worth a paper in, say, <a rel="nofollow" href="https://www.jstatsoft.org/" target="_blank"><em>J. Stat. Soft.</em></a> or the <a rel="nofollow" href="https://journal.r-project.org/" target="_blank"><em>R Journal</em></a> (heck, I'd even write a book about the package if it deserved it). I'd love to hear comments on any future publications that others would want to see.</p>
<p>Thanks for reading and stay tuned!</p>
<p>Next post: <a rel="nofollow" href="https://wp.me/p27liZ-TG" target="_blank">making <code>MCHTest</code> objects self-contained</a>.</p>
<h2>References</h2>
<ol>
<li>A. C. A. Hope, <em>A simplified Monte Carlo test procedure</em>, JRSSB, vol. 30 (1968) pp. 582-598</li>
<li>J-M Dufour, <em>Monte Carlo tests with nuisance parameters: A general approach to finite-sample inference and nonstandard asymptotics</em>, Journal of Econometrics, vol. 133 no. 2 (2006) pp. 443-477</li>
<li>J. G. MacKinnon, <em>Bootstrap hypothesis testing</em> in <em>Handbook of computational econometrics</em> (2009) pp. 183-213</li>
<li>R. A. Fisher, <em>The design of experiments</em> (1935)</li>
<li>R. Davidson and J. G. MacKinnon, <em>The size distortion of bootstrap test</em>, Econometric Theory, vol. 15 (1999) pp. 361-376</li>
</ol>
<hr>
<p><a rel="nofollow" href="https://www.packtpub.com/" target="_blank">Packt Publishing</a> published a book for me entitled <a rel="nofollow" href="https://ntguardian.wordpress.com/books/hands-on-data-analysis-with-numpy-and-pandas/" target="_blank"><em>Hands-On Data Analysis with NumPy and Pandas</em></a>, a book based on my video course <a rel="nofollow" href="https://ntguardian.wordpress.com/video-courses/unpacking-numpy-and-pandas/" target="_blank"><em>Unpacking NumPy and Pandas</em></a>. This book covers the basics of setting up a Python environment for data analysis with Anaconda, using Jupyter notebooks, and using NumPy and pandas. If you are starting out using Python for data analysis or know someone who is, please consider <a rel="nofollow" href="https://www.packtpub.com/big-data-and-business-intelligence/hands-data-analysis-numpy-and-pandas" target="_blank">buying my book</a> or at least spreading the word about it. You can buy the book directly or purchase a <a rel="nofollow" href="https://mapt.packtpub.com/?gclid=Cj0KCQjwuuHdBRCvARIsAELQRQGrXDoR4ox216bUlSD6mTQYibN24CA5bYF7QikiQQ6j3cGm63HRXsEaAjJjEALw_wcB" target="_blank">subscription to Mapt</a> and read it there.</p>
<p>If you like my blog and would like to support it, spread the word (if not get a copy yourself)!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ntguardian.wordpress.com/2018/10/08/announcing-mcht-package-bootstrap-monte-carlo-hypothesis-testing/"> R – Curtis Miller's Personal Website</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hjpJ-KdTAHA" height="1"></a></div>