tools: esci for R

Menu

Loading wiki pages...

View
Wiki Version:
<h1>esci for R</h1> <p>esci is now available as a package for R. </p> <p>esci is Effect Sizes and Confidence Intervals, a package for the estimation approach to inferential statistics. esci emphasizes effect sizes, interval estimates, and meta-analysis. It was designed to make it simple to obtain estimates for common research designs and to produce beautiful, modern visualizations that focus on effect sizes and uncertainty. </p> <p>Currently, esci can only do frequentist confidence intervals for estimates (no bootstrap intervals or Bayesian credible intervals, at least not yet).</p> <p>This page explains how to install the current alpha version of the esci package for R and also gives some uninspired examples using the mtcars dataset built into base R.</p> <p>If you use esci in R, your feedback and bug reports are more than welcome. Please post these to the GitHub page for the project: <a href="https://github.com/rcalinjageman/esci" rel="nofollow">https://github.com/rcalinjageman/esci</a></p> <p><strong>Be warned that the esci package is still in alpha</strong>, meaning that some functions and results objects will still change, potentially in ways that break code you develop for the current version. Documentation is also minimal to non-existent. Although the state of the package is rough, basic analyses are working, and there should be steady improvement towards a stable and well-documented package throughout 2020. </p> <p>The plots generates in esci are ggplot2 objects, so you once obtaining them you can apply any ggplot function/libraries to them.</p> <h2>Installing esci for R</h2> <p>esci for R is not on CRAN (yet). </p> <p>You can install the package directly from GitHub, but to do so you will need to have the devtools package installed first:</p> <pre class="highlight"><code>install.packages(&quot;devtools&quot;) devtools::install_github(&quot;rcalinjageman/esci&quot;)</code></pre> <h2>Estimate a mean</h2> <p>esci can estimate a single mean and CI. This is the estimation replacement for a one-sample t or z-test. In this example we estimate the average miles per gallon (mpg) for the cars in the mtcars population.</p> <pre class="highlight"><code>library(esci) estimate &lt;- estimateMean(mtcars, mpg, conf.level = .95) estimate plotEstimatedMean(estimate)</code></pre> <p>Like other plot functions in esci, plotEstimatedMean will return a ggplot2 object. So you can apply any ggplot2 function or packages. For example (assuming you have ggplot2 installed):</p> <pre class="highlight"><code>myplot &lt;- plotEstimatedMean(estimate) myplot &lt;- myplot + ggplot2::labs(title = &quot;my title&quot;)</code></pre> <p>![enter image description here](<a href="https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebe5f659828013ecf2fd4?mode=render" rel="nofollow">https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebe5f659828013ecf2fd4?mode=render</a> =30%x)</p> <h2>Estimate an independent mean difference</h2> <p>esci can estimate the difference between two independent means. This is the estimation replacement for an independent-samples t-test. In this example we estimate the difference in mpg for automatic vs. manual cars in the mtcars population.</p> <pre class="highlight"><code>library(esci) data &lt;- mtcars data$am &lt;- as.factor(data$am) levels(data$am) &lt;- c(&quot;automatic&quot;, &quot;manual&quot;) estimate &lt;- estimateMeanDifference(data, am, mpg, paired = FALSE, var.equal = TRUE, conf.level = .95, reference.group = 1 ) estimate plotEstimatedDifference(estimate)</code></pre> <p>The estimateMeanDifference function expects grouping variable that is a factor. For this example, we use the am variable in mtcars, which encodes if the car has an automatic or manual transmission. In the mtcars data, this is iniitally coded as a numeric column with 0s and 1s. To change this to a factor, we first make a copy of mtcars (data &lt;- mtcars). Then we redefine am as a factor. It is not required, but it also helps to recode the levels of am to real words/labels (automatic and manual) rather than 0s and 1s.</p> <p>The reference.group parameter defines how the contrast between the means is set: <em> reference.group = 1 sets the first level to be the reference group, so the contrast is: Mdiff = level2 - level1 </em> reference.group = 2 sets the second level to be the reference group, so the contrast is: Mdiff = level1 - level2 * the default is reference.group = 2</p> <p>![esci - estimate independent mean difference](<a href="https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebf20659828013bcf2daa?mode=render" rel="nofollow">https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebf20659828013bcf2daa?mode=render</a> =30%x)</p> <h2>Estimate a paired mean difference</h2> <p>esci can estimate the difference between two paired means. This is the estimation replacement for a paired t-test. In this example we estimate the difference between the number of gears and carburetors in the cars in the mtcars population (this makes no sense, but trying to stick with a single built-in data set, so really reaching here).</p> <pre class="highlight"><code>library(esci) estimate &lt;- estimateMeanDifference(data, gear, carb, paired = TRUE, conf.level = .95, reference.group = 2) estimate plotEstimatedDifference(estimate)</code></pre> <p>As above, the reference.group parameter defines the way the contrast between means is constructed. You can omit this and a default of 2 will be used (the second level in the factor will be used as the reference group, so Mdiff = level1 - level2).</p> <p>![esci - estimate paired mean difference](<a href="https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebf76145b1a01575338bc?mode=render" rel="nofollow">https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebf76145b1a01575338bc?mode=render</a> =30%x)</p> <h2>Estimate a proportion</h2> <p>esci can estimate a proportion and its confidence interval. Here we will estimate the proportion of cars in the mtcars population with an automatic transmission:</p> <pre class="highlight"><code>library(esci) data &lt;- mtcars data$am &lt;- as.factor(data$am) levels(data$am) &lt;- c(&quot;automatic&quot;, &quot;manual&quot;) estimate &lt;- estimateProportion(data, am, case.level = &quot;automatic&quot; ) estimate plotEstimatedProportion(estimate)</code></pre> <p>esci expects proportions to be stored as factors. In mtcars, the tyep of transmission is stored as an integer, so we convert it to a factor. We also label it to make things easier.</p> <p>The case.level parameter is used to determine which level of the factor is plotted. The default is 1. You can specify the case level by ordinal number of the level or by name (enclosed in quotes). So, in the example above we specify that we want to know the proportion automatic. But we could obtain the proportion manual by passing case.level = 2 (because manual is the second level of the factor) or by passing case.level = "manual" (because that's the name of the level)</p> <p>![esci - estimate proportion](<a href="https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebfb7659828013ecf3151?mode=render" rel="nofollow">https://files.osf.io/v1/resources/d89xg/providers/osfstorage/5eeebfb7659828013ecf3151?mode=render</a> =30%x)</p> <h2>Estimate a proportion difference</h2> <p>esci can estimate the difference between two independent dichotomies (nominal variables with only 2 mutall-exclusive levels). This is the estimation replacement for a 2x2 Chi Square test. In this example, we compare the proportion of automatic cars that are VS 1 to the proportion of manual cars that are VS1. I actually don't know what the vs variable means in the mtcars dataset.. but oh well:</p> <pre class="highlight"><code>library(esci) data &lt;- mtcars data$am &lt;- as.factor(data$am) levels(data$am) &lt;- c(&quot;automatic&quot;, &quot;manual&quot;) data$vs &lt;- as.factor(data$vs) levels(data$vs) &lt;- c(&quot;zero&quot;, &quot;one&quot;) estimate &lt;- estimateProportionDifference(data, am, vs, case.level = &quot;one&quot;, group.level = &quot;manual&quot; ) estimate plotEstimatedProportionDifference(estimate)</code></pre> <p>esci estimates the difference in proportions, which is often called the "Risk Difference". It does not yet output odds ratio or log odds ratio, but that's in the works.</p> <p>The case.level and group.level params are optional. The case.level specifies which level of the outcome variable is to be used to calualte the proportion. In the example, we specific "one" as the level, so we're calculating proportion that have "one" as their vs status. We can specify the case level by ordinal number of the level or by name. So, we could specifiy proportion zero by with case.level = 1 or case.level = "zero" (because "zero" is the first level of the vs variable... damn this example is a bit confusing). If not passed, case.level defaults to level 1. </p> <p>The group.level variable works the same way--it specifies the group level of interest by name or ordinal value of the level. If not passed, group.level defaults to level 1. </p> <p>What happens if your outcome variable has more than 2 levels? esci will calculate proportion of case.level against all other levels (it will collapse to a factor with just 2 levels, the case.level vs. all others).</p> <p>What happens if your grouping variable has more than 2 levels? esci will group by group.level vs. all others (it will collapse to just 2 levels: the group.level and not.group.level).</p> <p>![esci - estimate proportion difference](<a href="https://osf.io/txfdp/download" rel="nofollow">https://osf.io/txfdp/download</a> =30%x)</p> <h2>Estimate a correlation</h2> <p>esci can estimate a linear correlation (Pearson's r) and its CI. This is the same correlation test, but the emphasis is not on ruling out a null hypothesis, but on thinking about the CI and what r values remain compatible with the data. In this example we'll correlate horsepower (hp) with miles per gallon (mpg).</p> <pre class="highlight"><code>library(esci) estimate &lt;- estimateCorrelation(mtcars, hp, mpg) estimate plotEstimatedCorrelation(estimate) plotScatterPlot(estimate, show.line = TRUE, show.meanCI = TRUE, show.PI = TRUE, predictx = 200 )</code></pre> <p>plotEstimatedCorrelation shows the r value and its CI on a r scale (from -1 to 1). </p> <p>plotScatterPlot makes a scatter plot, with the option to show the regression line, the CI on the regression line, prediction intervals (really usefuL), and the option to pass an x value to obtain and graph a predicted y value (pass predictx for the graph to show how the prediction is made)</p> <p>![esci - estimate correlation](<a href="https://osf.io/by8z9/download" rel="nofollow">https://osf.io/by8z9/download</a> =30%x) ![esci - scatterplot](<a href="https://osf.io/mcyns/download" rel="nofollow">https://osf.io/mcyns/download</a> =30%x)</p> <h2>Estimate a 2x2 interaction</h2> <p>esci can produce estimates for a 2x2 between-subjects factorial design. I can't make this work with the mtcars data. So here is: <em> A sample data file: <a href="https://osf.io/6hv5s/" rel="nofollow">https://osf.io/6hv5s/</a> </em> R project: <a href="https://osf.io/4wjr2/" rel="nofollow">https://osf.io/4wjr2/</a> * R script: <a href="https://osf.io/3zxks/" rel="nofollow">https://osf.io/3zxks/</a></p> <p>Download these into the same directory, open the R project file in R Studio and you'll be able to examine and run the example.</p> <h2>Meta-analysis</h2> <p>esci makes it easy to conduct a meta-analysis. It can currently handle meta-analyses for: <em> Two group designs using raw data (means, standard deviations, sample sizes) </em> Two group designs using the standardized mean difference (cohens' d, sample sizes) <em> Correlations (r and sample size) </em> Proportion differences (2-group contingency table)</p> <p><a href="https://osf.io/d89xg/files/" rel="nofollow">The files section</a> has examples for the two-groups raw and the two-proportions meta-analyses. </p>
OSF does not support the use of Internet Explorer. For optimal performance, please switch to another browser.
Accept
This website relies on cookies to help provide a better user experience. By clicking Accept or continuing to use the site, you agree. For more information, see our Privacy Policy and information on cookie use.
Accept
×

Start managing your projects on the OSF today.

Free and easy to use, the Open Science Framework supports the entire research lifecycle: planning, execution, reporting, archiving, and discovery.