{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "library(repr) ; options(repr.plot.res = 100, repr.plot.width=4, repr.plot.height= 4) # Change plot sizes (in cm) - this bit of code is only relevant if you are using a juyter notebook - ignore otherwise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic hypothesis testing: $t$ and $F$ tests" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "\n", "Aims of this chapter:\n", "\n", "* Using $t$ tests to look at differences between means\n", "\n", "* Using $F$ tests to compare the variance of two samples\n", "\n", "* Using non-parametric tests for differences\n", "\n", "## $t$ tests\n", "\n", "The $t$ test is used to compare the mean of a sample to another value, which can be: \n", "* Some reference point (Is the mean different from 5?) \n", "* Another mean (Does the mean of A differ from the mean of B?). \n", "\n", "If you have a factor with two levels then a $t$ test is a good way to compare the means of those samples. If you have more than two levels, then you have a problem: as the number of levels ($n$) increases, the number of possible comparisons between pairs of levels ($p$) increases very rapidly. The number of possible pairs is the binomial coefficient $C(n,2)$ — inevitably, R has a function for this (try it in R):" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
1. 1
2. 3
3. 6
4. 10
5. 15
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 3\n", "\\item 6\n", "\\item 10\n", "\\item 15\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 3\n", "3. 6\n", "4. 10\n", "5. 15\n", "\n", "\n" ], "text/plain": [ " 1 3 6 10 15" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "choose(2:6,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Making these many comparisons using something like a t-test is a problem because we neglect the covariance among measures and inflate the chance of falsely rejecting at least one null hypothesis (false positive, or [Type I error](ExpDesign:Some-statistical-parlance)). \n", "\n", "---\n", "\n", ":::{figure-md} multiple-comparisons\n", " \n", "\n", "**Your Type I error rate increases with the number of pairwise comparisons ($p$) you make using a single dataset.** Say, you make multiple comparisons using an $\\alpha = 0.05$ significance level. Then you have that much chance of making a Type I error on each comparison. Thus, assuming each pairwise comparison is made independently, the type I error rate just adds up with every new comparison you make. Thus, the chance of committing at least one Type I error for a set of $n$ comparisons is $n \\times 0.05$; so for 6 pairwise comparisons, your chance of making at least one Type I (false positive) error would be $6 \\times 0.05 = 0.3$, or $30\\%$!\n", ":::\n", "\n", "--- \n", "\n", "A more-than-two-factors example would be if you were, say, comparing the means of more than two types of insect groups – below, we compared means of two groups (dragonflies and damselflies); if we added stoneflies to this comparison, the t-test would not be the way to go as we would be making three pairwise comparisons (n = 3, p = 3).\n", "\n", "*OK, assuming you are interested in making just one paiwise comparison, let's learn about $t$ tests.*\n", "\n", "The basic idea behind the $t$ test is to divide the difference between two values by a measure of how much uncertainty there is about the size of that difference. The measure of uncertainty used is the *standard error*.\n", "\n", "$$t=\\frac{\\textrm{difference between values}}{\\textrm{standard error}}$$\n", "\n", "When there is no difference between the values $t$ will be zero, but with big differences and/or small errors, will be larger. The $t$ distribution below shows how commonly different values of $t$ are found under the null hypothesis:\n", "\n", "---\n", "\n", ":::{figure-md} t-distribution\n", "\n", " \n", "\n", "**The $t$ distribution.**\n", "\n", ":::\n", "\n", "---\n", "\n", "Some points about the $t$-distribution illustrated in {numref}t-distribution:\n", "\n", "* The null hypothesis is that there is no difference between the values but because we only estimate the values from samples, differences will creep in by chance.\n", "\n", "* Mostly these differences will be small — hence the peak in the middle — but sometimes the differences will be large and the errors will be small.\n", "\n", "* 95% of the area under the curves are between these two sets of vertical lines. Values of $t$ more extreme than this will only occur 1 in 20 times or with a probability ($p$) of 0.05.\n", "\n", "* The means of small samples are more easily influenced by extreme values and so produce extreme $t$ values more frequently. This is why the red curve above for smaller samples is more flattened out and why the 95% limits are more spread out.\n", "\n", "### One-sample $t$ tests\n", "\n", "In the simplest example, a $t$ test can be used to test whether the mean of a sample is different from a specific value. For example:\n", "\n", "* Is the ozone in a set of air samples above the legal limit?\n", "\n", "* Is the change in a set of patients' weights different from zero?\n", "\n", "* Is the mean genome size for Odonata smaller than 1.25 pg, which is the average for insects [see here](http://www.genomesize.com/statistics.php?stats=insects)?\n", "\n", "Oh look! We can test that last one...\n", "\n", "[Previously](12-ExpDesign.ipynb#Data-exploration), we looked at the genome size and morphology of species of dragonflies and damselflies (Odonates: Anisoptera and Zygoptera). Box and whisker plots and density plots both show that the two groups have rather different genome size and morphology. We can use $t$ tests to test whether the means of the variables of the two groups are different and we can use the $F$ test to check whether they have the same variance.\n", "\n", "In this chapter, we will continue to practise building scripts and creating your own R code, so we will start from an empty script file (you can refer to t_F_tests.R from TheMulQuaBio). Use this script to store the code you run in this practical session and add notes to keep a record of what each bit is doing:\n", "\n", "$\\star$ Open R and change (setwd) to the code directory. If you have misplaced the data, download it again from TheMulQuaBio.\n", "\n", "$\\star$ Create a new blank script called ttests.R and save it to the working directory.\n", "\n", "$\\star$ Put a comment at the top (using #) to describe the script.\n", "\n", "For the rest of this session, type your code into this script, adding comments and then run them in R using Ctrl+R. If you make mistakes, correct the script and run the code again. This way you will end up with a complete neat version of the commands for your analysis.\n", "\n", "Add code to your ttests.R script to load the genome size data into R, assigning the object name genome and use str(genome) to check it has loaded correctly:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "genome <- read.csv('../data/GenomeSize.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To calculate a $t$ value in a one-sample scenario, we need that observed difference and then the standard error of the difference between the mean of our sample and the known value. This is calculated using the *variance* and the *sample size* ($n$) of the sample ($s$).\n", "\n", "$$se_s = \\sqrt{\\frac{\\textrm{var}(s)}{n}}$$\n", "\n", "This simple equation trades off variance — high variance in the data gives higher uncertainty about the location of the mean — and sample size – more data gives more certainty. So, *low variance* and *large datasets* have *small* standard errors; *high variance* and *small datasets* have *large* standard errors. Variance is calculated using sums of squares and so the square root is needed to give a standard error in the same units as the mean.\n", "\n", "So, all we need are three values calculated from the data: mean, variance and the number of data points and we can calculate $t$. R can do this for us. \n", "\n", "First calculate the three values from the data:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1.0143\n" ] } ], "source": [ "mean.gs <- mean(genome$GenomeSize)\n", "print(mean.gs)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0.1396975\n" ] } ], "source": [ "var.gs <- var(genome$GenomeSize)\n", " print(var.gs)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 100\n" ] } ], "source": [ "n.gs <- length(genome$GenomeSize)\n", "print(n.gs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now get the difference in means:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " -0.2357\n" ] } ], "source": [ "diff <- mean.gs - 1.25\n", "print(diff)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the standard error:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0.03737613\n" ] } ], "source": [ "se.gs <- sqrt(var.gs/n.gs)\n", "print(se.gs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, get the t value" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " -6.306164\n" ] } ], "source": [ "t.gs <- diff/se.gs\n", "print(t.gs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a big$t$value — values this extreme don't even appear on the graph above — so we would conclude that the mean genome size for Odonata is different from the average for insects.\n", "\n", "$\\star$Copy and paste the code above into your script in R and run it. Read through the code and make sure you understand the steps.\n", "\n", "We can do the above more easily and get some more information using R's t.test function. The null hypothesis can be set using the option (sometimes called a function *argument*) mu — the Greek letter$\\mu$is typically used to refer to a (true, not sample) mean:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "\n", "\tOne Sample t-test\n", "\n", "data: genome$GenomeSize\n", "t = -6.3062, df = 99, p-value = 8.034e-09\n", "alternative hypothesis: true mean is not equal to 1.25\n", "95 percent confidence interval:\n", " 0.9401377 1.0884623\n", "sample estimates:\n", "mean of x \n", " 1.0143 \n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t.test(genome$GenomeSize, mu = 1.25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This confirms the values we calculated by hand and adds a$p$value. The output also gives the degrees of freedom. This is something we will come back to later, but the degrees of freedom are basically the number of data points minus the number of estimated parameters, which in this case is one mean.\n", "\n", "(13-t_F_tests:CI)=\n", "#### Confidence Intervals\n", "The output also gives a confidence interval for the observed mean. The mean is the best estimate of the population mean given our sample of species of Odonata, but the actual mean for the order could be bigger or smaller. The confidence interval tells us the region in which we are 95% confident that this actual mean lies.\n", "\n", "It is calculated using the$t$distribution. Remember that$t$is a difference divided by a standard error; if we multiply$t$by a standard error, we get back to a difference. If we pick a pair of$t$values that contain the middle 95% of the$t$distribution, as in the plot on page 2, then we can multiply that by the standard error from the data to get a range above and below the mean. If we sampled lots of sets of 100 species of Odonata, we expect 95% of the observed means to lie inside this range. The code below shows the calculation of the confidence interval for the test above.\n", "\n", "First, find the edges of the middle 95% of a$t$distribution with 99 df:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " -1.984217 1.984217\n" ] } ], "source": [ "tlim <- qt(c(0.025,0.975), df = 99)\n", "print(tlim)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(quantiles of the t distribution, so qt)\n", "\n", "Now use the mean and standard error from above to get a confidence intervals:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", "\t 1. 0.940137655780312 2. \n", "\t 3. 1.08846234421969 4. \n", " \n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 0.940137655780312\n", "\\item 1.08846234421969\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 0.940137655780312\n", "2. 1.08846234421969\n", "\n", "\n" ], "text/plain": [ " 0.9401377 1.0884623" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mean.gs + tlim * se.gs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Using the t.test code above as a template, test whether the body weight (in grams) of Odonata is different from the average for arthropods of 0.045 grams. Note that this slightly dodgy estimate comes from an estimated average volume for arthropods of 45.21 mm$^3$, and assuming a density of 1 gm per cm$^3$; see: \n", "\n", "Orme, C. D. L., Quicke, D. L. J., Cook, J. M. and Purvis, A. (2002), Body size does not predict species richness among the metazoan phyla. Journal of Evolutionary Biology, 15: 235–247.\n", "\n", "\n", "### Two-sample$t$tests\n", "\n", "It is more common to use a$t$test to compare the means of two samples. This includes questions like:\n", "\n", "* Do two rivers have the same concentration of a pollutant?\n", "\n", "* Do chemicals A and chemical B cause different rates of mutation?\n", "\n", "* Do damselflies and dragonflies have different genome sizes?\n", "\n", "The main difference here is that with a one sample$t$test, we assume that one of the means is known exactly: the only error is in the single sample. With a two sample test, we are comparing two means estimated from samples and both contain error. The graph below illustrates this:\n", "\n", "---\n", ":::{figure-md} 2-sample-t-test\n", " \n", "\n", "**Illustration of one-sample (left) vs. two-sample (right)$t$tests**. The vertical lines show the mean (solid lines) and one standard error to each side (dashed lines). The red mean is the same in both cases, but the second graph shows that this is also estimated from a sample with error: the difference in the means looks less convincing and we'd expect a smaller$t$value.\n", "\n", ":::\n", "\n", "---\n", "\n", "A few points to note about {numref}2-sample-t-test: \n", "\n", "* The$t$tests in for these two graphs confirm this:\n", "\n", "* The mean for blue is significantly different from 16.74 (mean=14.98, se=0.38, df=59,$t$=-4.65,$p$=0.00002).\n", "\n", "* The means of blue and red are significantly different (blue: mean=14.98, se=0.38; red: mean=16.74, se=0.42; df=118,$t$=-3.13,$p$=0.002)\n", "\n", "$\\star$Have a close look at the previous two statements. This shows the kind of detail needed when reporting the results of$t$tests. The following is *not* acceptable: The means of blue and red are significantly different ($p=0.002$).\n", "\n", "So, with two samples, we shouldn't be so confident about the difference between the values — it should have a higher standard error. We can do this simply by combining the variance and sample size for the two samples ($a$and$b$) into the calculation:\n", "\n", "$$se_{a-b}= \\sqrt{\\frac{\\textrm{var}(a)}{n_a} + \\frac{\\textrm{var}(b)}{n_b}}$$\n", "\n", "\n", "Note that the two-sample t-test makes two assumptions; that the data are: \n", "\n", "(i) *normally distributed*, so that the two means are a good measure of central tendencies of the two samples and be estimated sensibly; and \n", "\n", "(ii) have *similar variances*. \n", "\n", "The first assumption also applies to the one-sample t-test.\n", "\n", "Let's use a$t$test to address the question of whether the genome sizes of Anisoptera and Zygoptera are different. First, we'll do this by hand. \n", "\n", "We'll use a really handy function tapply(X, INDEX, FUN) to quickly find the values for the two groups: it takes some values (X), splits those values into groups based on a factor (INDEX) and runs each group through another function (FUN).\n", "\n", "So let's first calculate the three values from the data:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Anisoptera Zygoptera \n", " 1.011774 1.018421 \n" ] } ], "source": [ "mean.gs <- tapply(X = genome$GenomeSize, INDEX = genome$Suborder, FUN = mean)\n", "print(mean.gs)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Anisoptera Zygoptera \n", " 0.1845755 0.0694569 \n" ] } ], "source": [ "var.gs <- tapply(X = genome$GenomeSize, INDEX = genome$Suborder, FUN = var)\n", "print(var.gs)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Anisoptera Zygoptera \n", " 62 38 \n" ] } ], "source": [ "n.gs <- tapply(X = genome$GenomeSize, INDEX = genome$Suborder, FUN = length)\n", "print(n.gs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now get the difference in means:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Anisoptera \n", "-0.006646859 \n" ] } ], "source": [ "diff <- mean.gs - mean.gs\n", "print(diff)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now get the standard error of the difference:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Anisoptera \n", "0.06931693 \n" ] } ], "source": [ "se.gs <- sqrt((var.gs/n.gs) + (var.gs/n.gs))\n", "print(se.gs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And finally, the t-value. " ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Anisoptera \n", "-0.09589084 \n" ] } ], "source": [ "t.gs <- diff/se.gs\n", "print(t.gs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\star$Type the above code into your growing R script and run it in R. Again, read through the code and make sure you understand the steps.\n", "\n", "And as before, the t.test function can be used to automate this all for us. We can use a formula as we have seen [before](12-ExpDesign.ipynb#Take-a-quick-look-at-effects-of-certain-factors) to get a test between the two suborders:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "\tWelch Two Sample t-test\n", "\n", "data: GenomeSize by Suborder\n", "t = -0.095891, df = 97.997, p-value = 0.9238\n", "alternative hypothesis: true difference in means is not equal to 0\n", "95 percent confidence interval:\n", " -0.1442041 0.1309104\n", "sample estimates:\n", "mean in group Anisoptera mean in group Zygoptera \n", " 1.011774 1.018421 \n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t.test(GenomeSize ~ Suborder, data = genome)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output looks very similar to the one-sample t-test, except that the output now gives two estimated means, rather than one and it reports the$p$value for the calculated$t$value.\n", "\n", "(13-t_F_tests:CI2)=\n", "#### Confidence Intervals again\n", "\n", "Also, in the output above unlike the 95% interval of a 1 sample t-test, which provides the confidence intervals around the estimated mean of the single population, this output of the 2-sample test provides the confidence intervals around the observed difference in the means of the two samples. That is, the 95% confidence interval (-0.1442041, 0.1309104) above is around the observed difference in the sample means:$1.011774 - 1.018421 = - 0.006647$. This is essentially not distinguishable from zero as the intervals include the value 0 (and therefore, the p-value is also insignificant).\n", "\n", "$\\star$Add this last step to your script and run it.\n", "\n", "### Exercise \n", "$\\star$Expand your t-test code to test whether the body weight of the two suborders are different and add it to in the script file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The$F$test\n", "\n", "The$F$test is used to compare the variances of two samples or populations. You will also see them featured in analysis of variance (ANOVA), to test the hypothesis that the means of a given set of normally distributed populations all have the same variance.\n", "\n", "The distribution of the test statistic$F$(the$F$distribution) is simply the ratio of the variances for sample$a$and$b$:$\\frac{\\textrm{var}(a)}{\\textrm{var}(b)}$:\n", "* If the two variances are the same then$F=1$; \n", "* if$\\textrm{var}(a) > \\textrm{var}(b)$then$F > 1$; \n", "* and if$\\textrm{var}(a) < \\textrm{var}(b)$then$F < 1$\n", "\n", "---\n", ":::{figure-md} f-distribution\n", "\n", " \n", " \n", "**The F distribution.** The two vertical blue lines show the edges of the central 95% of the area of the curve.\n", ":::\n", "\n", "---\n", "\n", "In the above figure, note that:\n", "\n", "* If the two samples are drawn at random from a population with the same variance then values of$F < 0.482$or$> 2.074$are observed fewer than 1 time in 20 ($p \\le 0.05$). And because$1/0.482 \\approx 2.074$and$1/2.074 \\approx 0.482$, in this case, it doesn't matter which way round you compare the two variances. This is not always true, and the order in which you compare two variances will very often matter.\n", "\n", "* The shape of the$F$distribution changes depending on the amount of data in each of the two samples but will always be centered near 1 and with a tail to the right (right-skewed).\n", "\n", "* This F-distribution arises as the ratio of two appropriately scaled *chi-square distributed variates*, because, as we saw above, variances should be chi-square distributed.\n", "\n", "Let's use our same genome size dataset to learn how to do F-tests as well. We will use the F test to judge whether a key assumption of the two-sample$t$tests we did above holds: that the two samples have *equal variances*. \n", "\n", "First, let's visualize the data. As I hope you've already noticed, this session has been neglecting one very important part of analysis — *plotting the data*. \n", "\n", "We are going to compare two plots, so it helps to have them side by side in the same window. We can use the function par to change a set of options called graphics parameters to get R to do this. The option to change is mfrow. This sets a graphics window to include *m*ultiple *f*igures and we need to tell R the number of rows and columns to divide the window into: par(mfrow=c(1,2)).\n", "\n", "$\\star$Type par(mfrow=c(1,2)) into your script, add a comment, and run it.\n", "\n", "Using your (rapidly improving!) R skills, create a boxplot comparing the genome sizes of the two suborders.\n", "\n", "Add another boxplot beside it comparing the body weight of the two suborders.\n", "\n", "It should look like {numref}odonata-wt-boxplots.\n", "\n", "---\n", "\n", ":::{figure-md} odonata-wt-boxplots\n", "\n", " \n", " \n", "**Reproduce this figure.**\n", "\n", ":::\n", "\n", "---\n", "\n", "Now, we can use R to calculate$F$for the variance in genome size in each of the two suborders. We calculated the variance for the$t$test above, so we can just do this:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Anisoptera: 2.65741044885742" ], "text/latex": [ "\\textbf{Anisoptera:} 2.65741044885742" ], "text/markdown": [ "**Anisoptera:** 2.65741044885742" ], "text/plain": [ "Anisoptera \n", " 2.65741 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "var.gs/var.gs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's quite a big$F$value and we can use the function var.test to do all the calculations for us and give us the actual$p$value:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "\tF test to compare two variances\n", "\n", "data: GenomeSize by Suborder\n", "F = 2.6574, num df = 61, denom df = 37, p-value = 0.001946\n", "alternative hypothesis: true ratio of variances is not equal to 1\n", "95 percent confidence interval:\n", " 1.449330 4.670705\n", "sample estimates:\n", "ratio of variances \n", " 2.65741 \n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "var.test(GenomeSize ~ Suborder, data = genome) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It produces the same value that we calculated by hand and shows that, if\n", "the two samples are drawn from populations with the same variance, an\n", "$F$value this extreme will only be observed roughly 1 time in 500\n", "($1/0.00195 \\approx 500$) .\n", "\n", "$\\star$Open a new empty script called FTests.R.\n", "\n", "In this write your script to test whether the variances in the body weight of the two suborders from the GenomSize dataset are different.\n", "\n", "There are clearly problems with the variance in both examples. The next two sections present ways to address these kinds of problems.\n", "\n", "##$t$tests revisited\n", "\n", "The first thing to say is that R is aware of the problem with the variance. If you look back at the output from the previous$t$tests, you will see that the degrees of freedom vary a lot. We have 100 observations and – after subtracting one for each mean we calculate — our degrees of freedom should be either 99 (one sample test) or 98 (two\n", "sample test). What we actually see are smaller numbers, with the smallest being df = 60.503 for the two sample test of body weight.\n", "\n", "The explanation is that R is applying a *penalty* to the degrees of freedom to account for differences in variance. With fewer degrees for freedom, more extreme$t$values are more likely and so it is harder to find significant results. This doesn't mean we can forget about checking the variances or plotting the data!\n", "\n", "In this case, we can also apply a transformation to the data in order to make the variances more equal. Forgetting the wings and assuming Odonata are shaped like a box, the model in the graph below shows how volume changes with length: equal changes in length do not lead to equal changes in volume and longer species will have a disproportionately large volume. This is a classic feature of morphological data known as allometric scaling and we'll look at it again when we learn about [ANOVA](15-anova.ipynb).\n", "\n", "In the meantime, a log transformation will turn body weight from a skewed distribution to a more normal distribution, as shown in {numref}log-transformation. \n", "\n", "---\n", ":::{figure-md} log-transformation\n", "\n", " \n", "\n", "**Log transformation of body weights.**\n", ":::\n", "\n", "\n", "--- \n", "\n", "We can do a$\\log_e$(natural log) transform of our body weight data as follows:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "genome$logBodyWeight <- log(genome$BodyWeight)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That is, you took a natural log of the whole Body weight column and saved it as a new column called logBodyWeight\n", "\n", "$\\star$Copy the line into your FTests.R script and run it.\n", "\n", "### Exercise \n", "Now write three lines of code to get a boxplot of$\\log_e$body weight and then run a variance test and$t$test on the differences in$\\log_e$body weight between suborders. This gives a much clearer result — the variances are almost identical and the differences between the suborders are much more cleanly tested.\n", "\n", "## Non-parametric tests\n", "\n", "What happens if there isn't a convenient transformation for the variable that gives roughly constant variation and equal variance? In a parametric test, like the$t$and$F$test above, we use parameters (mean and variance) to describe the data, assume these describe the data well and then just use these parameters to run the test. If these assumptions don't seem very sound, the non-parametric tests provide a way of using the ranks of the data to test for differences. They aren't as powerful — they are less likely to reveal significant differences — but they are more robust. The most commonly used alternative is the Wilcoxon test, which uses the function wilcox.test in R.\n", "\n", "$\\star$Using wilcox.test as a replacement for t.test, repeat the one and two sample$t$test for genome size and body weight.\n", "\n", "For example to carry out a wilcox test equivalent to the one-sample t test you performed above: " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "\tWilcoxon signed rank test with continuity correction\n", "\n", "data: genome$GenomeSize\n", "V = 893.5, p-value = 2.043e-08\n", "alternative hypothesis: true location is not equal to 1.25\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wilcox.test(genome$GenomeSize, mu=1.25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare the two (t vs wilcox test) results.\n", "\n", "### Exercise\n", "\n", "Repeat the above analyses with the predator and prey body mass data that you were introduced to [previously](08-Data_R.ipynb). Again check how different the results are when using$t\$ vs. Wilcoxon tests." ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.1.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": false, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "460.6px", "left": "10px", "top": "150px", "width": "254.637px" }, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 4 }