# Statistics

## Descriptive statistics

The usual suspects: mean, median.

### Variance

The “spread” of the samples or population. A low variance means the values are grouped very closely together; they will be very close to the mean.

Variance is defined in two ways: $$\sigma^2 = \frac{\sum_i (x_i-\mu)^2}{n} \qquad \textrm{(population)},$$ $$s^2 = \frac{\sum_i (x_i-\bar{x})^2}{n-1} \qquad \textrm{(sample)},$$ where $\mu$ is the population mean, $\bar{x}$ is the sample mean (calculated the same way as population mean), $x_i$ represents a data point, and $n$ is the number of $x_i$'s.

In other words, $\sigma^2$ is the average of the squared distance between each datum and the mean. We use the square to ensure the difference is positive (so differences above/below don't cancel out), and we don't use absolute value, generally, because it's not differentiable. (We like differentiable functions, generally, so we can find their min/max using calculus...)

The sample variance, $s^2$, differs from $\sigma^2$ only by dividing by $n-1$ rather than $n$. This makes $s^2 > \sigma^2$ assuming $\bar{x}=\mu$. This is done to handle bias in the measure.

#### Example

Recall the InsectSprays plot:

These are the variances of the counts of insects killed for the different sprays:

Spray Variance
A 22.27
B 18.24
C 3.90
D 6.27
E 3.00
F 38.61

These numbers should match your intuitions after looking at the plot. They tell us that sprays C and E are most “reliable”, though they may not always perform best (kill most insects).

### Standard deviation

This is simply the square-root of the variance, i.e., $\sigma$ (population) or $s$ (sample).

### Correlation

When we have two variables changing (we might be controlling one and measuring the other), we often want to measure how correlated they are. Positive correlation occurs when both variables increase/decrease together, and inverse correlation occurs when they move in opposite directions of each other. A correlation is always between -1 and 1, with 0 meaning absolutely no correlation.

Correlation is defined as, $$r_{XY} = \frac{\sum_i (x_i-\bar{x})(y_i-\bar{y})}{(n-1)s_X s_Y},$$ where $s_X, s_Y$ are standard deviations of the $X$ or $Y$ dataset (respectively), and all other variables have the same definitions as above (in the definition of variance). Note $n$ must be the same for both sets of values; we're finding the correlation by pairing the values, after all.

When makes a correlation “high?” There are some rules of thumb but no exact answers. Maybe don’t bet your life on a <0.5 correlation.

From Wikipedia, here are some correlation examples. All the examples on the third row are symmetric, or nearly so, on the x-axis so the correlation is 0 (x does not seem to consistently impact y). Bear in mind that strong correlation does not always imply linearity, as the Wikipedia page notes.

#### Example

Consider ozone versus temperature in New York City:

This is from the airquality dataset:

We can measure the correlation between ozone and temperature as follows:

The use="complete.obs" parameter means to drop rows where NA is found in one or both of the columns (see row 5 of ozone, above).

The correlation was about 0.70, which is positive, indicating positive correlation (both values move together, rather than inversely). It is also a moderately high correlation, suggesting that ozone does have some effect on temperature.

#### Correlation vs. causation?

It must be said, correlation does not equal causation. Even with very high correlation between, say, number of kids eating popsicles and number of kids swimming, neither causes the other (rather, temperature probably causes both). There is no way to make claims about causation without having some theory or model of how the system works. Numbers like correlation can’t tell you about causation.

### Covariation

Covariance is like a measure of variance between variables. It is very similar to correlation but not scaled by the standard deviations. Thus, its formula is, $$cov(X,Y) = \frac{\sum_i (x_i-\bar{x})(y_i-\bar{y})}{n-1}.$$

The covariance is not between -1 and 1 (usually), because it’s not scaled like correlation is. The units are the units of $X$ times the units of $Y$.

Note, $cov(X,X)$ is just the variance of $X$.

Sometimes covariance is more useful than correlation, particularly when the original magnitude is desired. (Recall that correlation is just a normalized covariance.)

#### Example

Again, with airquality:

### Correlation/covariance matrices

Sometimes we want to check the correlation/covariance of multiple variables against each other, all pairs, all at once. We can give multiple columns to cor() or cov() to do this.

Naturally, a variable is always in perfect correlation with itself, so we’ll always have 1.0 correlation on the diagonals. We see, at a glance, that ozone and wind are inversely correlated, moderately so. Maybe the wind blows away the ozone?

Using the GGally library and ggpairs function, you can create a (somewhat crazy) plot of multiple variables vs. each other, all pairs. Note that ggpairs does not produce a ggplot, so you have to use an older R technique for saving plots (if you care to):

The scatter plots show each variable vs. the other. In the diagonal we see density plots for that variable (there is no reason to show a scatter plot vs. itself). The top-right shows correlation values.

## Applying descriptive statistics to groups

Use by() to apply a function to a data frame column according to values from another column or columns.

Consider the InsectSprays data,

We can ask for the mean to be calculated per spray:

Sometimes, it is convenient to use with() as well so we can more easily refer to column names:

Here is an easy way to get simple summary statistics for each spray:

Note running summary(InsectSprays) is nearly useless because it will find the stats for all counts of all sprays. That’s not interesting because we know (from the plot, and above summaries) that the sprays perform quite differently.

## Statistical inference

Usually, we only have samples from a population, not the population itself. So our descriptive statistics (mean, correlation, etc.) are calculated with respect to the sample. However, the purpose of sampling from the population is to be able to make claims about the population, without measuring every member of the population. Statistical inference allows us to try to make such claims. Of course, we only have a sample, so we can’t be certain our claims are true or false, but there are a variety of tools that help us estimate the probability our claims are true, given the sample and some assumptions about the population.

### Hypothesis testing

A simple kind of claim is something along the lines of, “what this sample shows is not due to chance, but due to a real phenomenon in the actual population.” We can ask this about the sample mean (“this mean is close to the true population mean”), correlation (“these variables are actually positively correlated”), or differences in populations (“these two sets of samples came from very different populations,” e.g., a population receiving a placebo and a population receiving a drug).

#### The “null” hypothesis

The tools for hypothesis testing only support two hypotheses. One we call the “null” hypothesis, often indicated by $h_0$, which always means “there is no difference in the populations” or “these variables are not correlated” or whatever the case. You want the null hypothesis to be proven wrong beyond a reasonable doubt.

You never have evidence to prove the null hypothesis is true. Rather, you can only fail to reject the null hypothesis. If, on the other hand, there is enough evidence the null hypothesis is false, you may succeed in rejecting the null hypothesis, and conclude that the two populations are in fact different, or the variables are positively correlated, or whatever.

#### p-values

Often, the output of a statistical test is a “significance” value known as the p-value. From Wikipedia,

Before performing the test a threshold value is chosen, called the significance level of the test, traditionally 5% or 1% and denoted as $\alpha$. If the p-value is equal to or smaller than the significance level $\alpha$, it suggests that the observed data are inconsistent with the assumption that the null hypothesis is true, and thus that hypothesis must be rejected and the alternative hypothesis is accepted as true. When the p-value is calculated correctly, such a test is guaranteed to control the Type I error rate to be no greater than $\alpha$.

A Type I error is “incorrect rejection of a true null hypothesis (a false positive)” and a Type II error is “the failure to reject a false null hypothesis (a false negative)” (quoted from Wikipedia).

When a test tells us the p-value is <0.05 or, better, <0.01, we can confidently reject the null hypothesis. (Many moons ago, 0.05 or 0.01 were deemed good enough thresholds to be confident.)

### Distributions

Before we can perform inference, we have to make assumptions about the distributions of our populations. There are many statistical distributions. The normal distribution is the most common. It looks like a bell curve. The mean and standard deviation determine how the curve looks. For example, the R dataset nhtemp (average yearly temperatures in New Haven, Connecticut, 1912-1971) appears to be a normal distribution:

A bell curve is defined by the mean and standard deviation. The mean of nhtemp is 51.16, and the standard deviation is 1.27. Using these values, we can overlay a plot of the normal distribution:

#### Tests for different population distributions

We can test how “normal” a sample is using the Shapiro test. The Shapiro test uses a null hypothesis stating “the distribution is normal”, so if the p-value is low and we should reject the null hypothesis, then we can believe the distribution that the values were sampled from is not normal. In that case, we would use one of a large number of other tests depending on the kind of distribution we think we have.

It seems that kill counts for insect spray A are normally distributed (since we can’t reject this null hypothesis).

Here is a density plot for each spray:

Sprays A, B, and F look “normal”, but D does not. The Shapiro test confirms: the null hypothesis (the distribution is normal) can be rejected for spray D with p-value 0.0027. Perhaps more trials with spray D would bring it into a normal distribution. I expect that the way the spray actually works (after an infinite number of trials) would be a normal distribution.

Tipping at a restaurant, apparently, is not normally distributed:

Here is the tips density plot:

I suspect that the sole reason tips are not normally distributed is because they are cut off at \$0 (can’t give a negative tip).

### t-tests

A t-test compares two sample means and supports inferences about the population means.

A t-test asks whether a difference between two groups’ [means] is unlikely to have occurred because of random chance in sample selection. A difference is more likely to be meaningful and “real” if (1) the difference between the averages is large, (2) the sample size is large, and (3) responses are consistently close to the average values and not widely spread out (the standard deviation is low). – Statwing

#### Claims about a population’s mean

A sample’s mean may not be the true population mean. We can test whether this is the case for a certain sample using a “t-test.” We don’t actually need a null hypothesis in this case.

Given a sample in a vector, such as the tip column in the tips dataset, we can get an interval estimating the population’s mean:

The interval is 2.82-3.17 (the sample’s mean is about 3.00). This is an interval of 95% confidence, meaning the true mean is in this interval with 95% certainty. I.e., there is an estimated 5% chance that the true mean is not in this interval, meaning there is a 5% chance this sample was not truly random but instead has some kind of bias.

The confidence interval would likely shrink if any of the following happen: we get more samples from the population, the standard deviation of the samples decreases (i.e., there is less variance, i.e., the samples are closer to the sample mean), or we decrease the desired confidence level from 95% (default) to something like 80% or lower (with a lower confidence, we can define a more narrow interval).

#### Claims about two samples coming from different populations

Often, we want to perform an experiment (or analyze the results from an experiment) that gives two groups different treatments. For example, you may give one group a placebo and another group a drug. You then want to find out if the drug is having a “significant” effect on the people it would be given to in the future. In other words, you want to estimate if any/all people given the placebo have different outcomes than any/all people given the drug. The people you actually studied are just samples, but you want to make inferences about the populations they represent.

Given the InsectSprays dataset, let’s ask whether spray A is better than spray B. That is, we want to test our claim that the sprays perform differently, i.e., their means differ. The null hypothesis is that they do not perform differently. The alternative hypothesis is that they do. If they do perform significantly differently, it would be easy to see which performs better.

Comparing sprays A and B, we see that the p-value is 0.65, which is not strong enough to reject the null hypothesis at the 0.05 level of significance. So, we cannot say that either A is a significantly better spray than B or vice versa.

Comparing sprays A and C gives a different story:

Now, p«0.01 (“very less” than 0.01) so we can confidently say A and C sprays perform differently. Looking at their means (last line of output), or the plot from before, we can say that spray A is significantly better at killing insects than spray C.

We can test if a certain event is happening more often than chance, or more often than some expected probability. For example, suppose somebody is guessing heads/tails of a coin flip. We expect that person to be right 50% of the time. But suppose the person is right 97 times out of a 150 flips. We can use prop.test to check if we should reject the null hypothesis (which states that the person is not any better than chance):

With 95% confidence, the person is actually achieving 56%-72% accuracy in their guesses, and with p<0.01 confidence, we can reject the null hypothesis.

#### Claims about proportions from two populations

We can also test if the proportion of some event in one group is significantly different than the proportion of some event in another group. For example, suppose Jane and Frank are guessing weights at a circus. Jane guesses people’s weights correctly (within 10 pounds, say) 120 out of 200 tries. Frank gets 30 out of 52 tries correctly. Is Jane better than Frank, or vice versa?

Use prop.test again, but give a two-element vector to x and n (for Jane and Frank, respectively). We don’t need p because we have no assumptions about how often somebody should be able to correctly guess a weight, and it doesn’t matter anyway since we only want to compare Jane vs. Frank’s performance.

Since p>0.05, we can’t reject the null hypothesis, so we have to believe Jane is just as good as Frank at guessing weights.

Suppose Frank begins studying human anatomy and gets the next 50 guesses correct. Now he has 80 out of 102 guesses correct. Is he better than Jane now?

It seems so. prop 1 (Jane’s performance) is 60%, prop 2 (Frank’s performance) is 78.4%, so Frank is better, and significantly so since p<0.01.

You can use any number of values in x and n (as long as they have the same length) to compare multiple proportions simultaneously.

## Worries about multiple hypothesis testing

If you do enough variations of similar tests, you’re bound to hit that 5% or 1% (p<0.05, p<0.01) where “significance” happens by chance. That’s what the p-value means.