Associated Material

Module: Module 04 - Summarising

Readings

Topics

About descriptive statistics

Visualising a data set (revision)

  • Frequency distributions
  • Illustrating group effects
  • Dealing with categorical data

New ggplot features

  • Hex codes for colour
  • Changing colour palette
  • Positioning in bar graphs

Measures of central tendency

  • Mean, median and mode
  • Using function summary
  • Summarising categorical variables

Measures of variability

  • Ranges and quartiles
  • Standard deviation

Summarising by group

  • Using aggregate
  • Using group_by and summarise

Third party libraries for descriptives

Working with two DVs

  • Scatterplots with linear trend
  • Correlation coefficients

Practice Exercises

Exercise 1

Many scientific data sets have histograms that are “bell-shaped”. That is, most of the values cluster in the middle, and the frequency drops off symmetrically toward smaller and larger scores. The distribution of penguin body mass is a good example of a data set that is approximately bell-shaped.

Many inferential statistical techniques require data sets to be close to a very specific bell-shape called the normal curve. If your data deviate too far from normal, the inferential tests will give incorrect results. The most serious such deviation is skew, which is when the bell-shape tilts to one side, with a long tail in either the smaller or larger direction.

The penguin body mass graph is not perfectly symmetrical. It shows a bit of skewness with a longer tail toward the heavier penguins. But many data sets from nature are extremely skewed. We can see this in the lakes data.

Observe the distribution of ChlA for Lake Ellesmere (shown below with a smoothed density line for illustration). Although most of the readings are clustered below 100, there are some much larger values – one reading is more than 500 – creating a long “tail” to the right. The ChlA measure in Lake Ellesmere is skewed.

We can compute an exact numerical value for the skewness of a distribution using function skewness from package e1071. As in the examples on the Module page, we can compute the skewness for each lake using aggregate, using group_by and summarise from library dplyr, or using describeBy from library psych, which includes skewness among its summary statistics.

With the method of your choice, compute the skewness value for each of the three lakes. What does the pattern of results tell you about the health of each lake?

Exercise 2

A perfectly symmetrical distribution will have a skewness of 0. As a distribution tilts further and further from normal, the absolute value of the skewness measure goes up. What consititutes “too much skew” varies between disciplines, and for an assignment you will want to check with your lecturer. However, a common rule of thumb is that any value greater than 1 (or less than -1) has enough skew that you need to think about dealing with it. A value greater than 3 (as found with the Lake Ellesmere data) is definitely skewed.

As mentioned earlier, many inferential tests give inaccurate results with skewed data, so in cases like the lakes data, we must “unskew” our values. A common approach is to compute the natural logarithm of each data value (using R function log), and analyse those logs (ask your lecturer about alternative approaches). The logarithm computation pulls extreme scores in, reducing the skew, without changing the overall relationships between data values.

In Module 03 you saw how to add a new computed column to a data frame using either function mutate or the $ operator. Using the technique of your choice, add a column to data frame lakes that holds the natural log of each ChlA reading.

Exercise 3

Using ggplot, make a histogram of all the log ChlA values in data frame lakes, with each lake in a different colour. This code is extremely similar to the example in the Module. How would you describe these distributions? Are Lake Ellesmere’s log ChlA values skewed? What can you provide as evidence for your answer?

LS0tCnRpdGxlOiAiWm9vbSBOb3RlcyAwNCAtIFN1bW1hcmlzaW5nIERhdGEiCmRhdGU6ICJTZW1lc3RlciAyLCAyMDIzIgpvdXRwdXQ6CiAgaHRtbF9kb2N1bWVudDoKICAgIHRvYzogdHJ1ZQogICAgdG9jX2Zsb2F0OiB0cnVlCiAgICB0b2NfZGVwdGg6IDMKICAgIGNvZGVfZG93bmxvYWQ6IHRydWUKICAgIGNvZGVfZm9sZGluZzogc2hvdwotLS0KCmBgYHtyIHNldHVwLCBpbmNsdWRlPUZBTFNFfQpsaWJyYXJ5KGtuaXRyKQoKa25pdHI6Om9wdHNfY2h1bmskc2V0KAogIGNvbW1lbnQgPSAiIz4iLAogIGZpZy5wYXRoID0gImZpZ3VyZXMvMDQvIiwgIyB1c2Ugb25seSBmb3Igc2luZ2xlIFJtZCBmaWxlcwogIGNvbGxhcHNlID0gVFJVRSwKICBlY2hvID0gVFJVRQopCgoKYGBgCgoKPiAjIyMjIEFzc29jaWF0ZWQgTWF0ZXJpYWwKPgo+IE1vZHVsZTogW01vZHVsZSAwNCAtIFN1bW1hcmlzaW5nXSgwNC1zdW1tYXJpc2UuaHRtbCkKPgo+IFJlYWRpbmdzCj4KPiAtIFtSIGZvciBEYXRhIFNjaWVuY2UgLSBDaGFwdGVyIDddKGh0dHBzOi8vcjRkcy5oYWQuY28ubnovZXhwbG9yYXRvcnktZGF0YS1hbmFseXNpcy5odG1sKQoKCiMgV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