ETC5521 Tutorial 9

Going beyond two variables, exploring high dimensions

Author

Prof. Di Cook

Published

September 21, 2023

🎯 Objectives

These are exercises in plots to make to explore relationships between multiple variables. You will use interactive scatterplot matrices, interactive parallel coordinate plots and tours to explore the world beyond 2D.

🔧 Preparation

install.packages(c("tidyverse", "cassowaryr", "tourr", "GGally", "plotly", "colorspace", "SMPracticals", "vcdExtra", "patchwork", "vcd"))
  • Open your RStudio Project for this unit, (the one you created in week 1, eda or ETC5521).

Exercise 1: Melbourne housing

  1. Read in a copy of the Melbourne housing data from Nick Tierney’s github repo which is a collation from the version at kaggle. Its fairly large, so let’s start simply, and choose two suburbs to focus on. I recommend “South Yarra” and “Brighton”. (Note: there are a number of missing values. I recommend removing these before making plots.)
mel_houses <- read_csv("https://raw.githubusercontent.com/njtierney/melb-housing-data/master/data/housing.csv") %>%
  dplyr::filter(suburb %in% c("South Yarra", "Brighton")) %>%
  dplyr::filter(!is.na(bedroom2)) %>%
  dplyr::filter(!is.na(bathroom)) %>%
  dplyr::filter(!is.na(price))
  1. Make a scatterplot matrix of price, rooms, bedroom2, bathroom, suburb, type. The order of variables can affect the readability. I advise that the plot will be easier to read if you order them with the numerical variables first, and then the categorical variables. Think about what associations can be seen?
  1. Subset the data to South Yarra only. Make an interactive scatterplot matrix of rooms, bedroom2, bathroom and price, coloured by type of property. There is a really high price property. Select this case, and determine what’s special about it – why did it sell for so much? Select the outlier in bedrooms and bathrooms, and examine the other characteristics of this property.

Exercise 2: Olive oils

Following on from the olive oils example from lecture, we will explore the oils from the south here.

  1. Grab a copy of the data, and subset to contain just the samples from region = south (1), and also drop eicosenoic acid, because there is nothing useful about this variable for the southern oils.
# Read data and filter to just south
olive <- read_csv("http://ggobi.org/book/data/olive.csv") %>%
  rename(id = `...1`) %>% 
  dplyr::filter(region == 1) %>%
  dplyr::select(palmitic:arachidic, id, area) %>%
  mutate(area = factor(area))
  #mutate(area = as.integer(area))
  1. Only looking at areas (1-3), that is not Sicily:
    • Make an interactive parallel coordinate plot of the fatty acids (except eicosenoic), where the lines are coloured by area. (Code is provided, code is a bit tricky, but worth it!)
    • Look at the data in a tour.
    • Describe what you learn about differences between the three areas, whether these are separated. Are some variables more useful for distinguishing the three areas? Are there any outliers?
# Look at first three areas, first
not_sicily <- olive %>%
  filter(area != 4)

# Set our colours for the par coords, and also tour
clrs <- divergingx_hcl(palette="Zissou 1", n=4)

# Make the interactive parcoords with plotly
# SOME NOTES ABOUT THE CODE:
#    - Notice that some variables have been flipped by
#      putting a minus sign in front, this is to make
#      the correlation between variables positive, making
#      the par coords easier to read.
#    - Note the strange colour mapping: colour value has 
#      to range between 0-1 weirdly enough, so 
#      area 1 becomes 0, area 3 becomes 1
#    - The range of each variable needs to be stated
#      so that each is scaled from min to max for the display
# ABOUT THE INTERACTIVITY
#    - Click and drag along an axis to select observations
#    - Click and drag the variable label to re-order
#      I find the best order to see groups is palmitoleic,
#      oleic, palmitic, linoleic, 
#      linolenic, arachidic, stearic
not_sicily_pcp <- not_sicily %>%
  plot_ly(type = 'parcoords', 
            line = list(color = ~area,
                colorscale = list(c(0,clrs[1]), c(0.5,clrs[2]),
                                  c(1,clrs[3]))),
            dimensions = list(
            list(range = c(35,280),
                 label = 'palmitoleic', values = ~palmitoleic),
            list(range = c(-8113,-6300),
                 label = 'oleic', values = ~(-oleic)),
            list(range = c(875,1753),
                 label = 'palmitic', values = ~palmitic),
            list(range = c(448,1462),
                 label = 'linoleic', values = ~linoleic),
            list(range = c(-74,-20),
                 label = 'linolenic', values = ~(-linolenic)),
            list(range = c(-102,-32),
                 label = 'arachidic', values = ~(-arachidic)),
            list(range = c(-375,-152),
                 label = 'stearic', values = ~(-stearic))

            )
          )
not_sicily_pcp

To generate a tour use:

animate_xy(not_sicily[,1:7], col=not_sicily$area, rescale=TRUE)
  1. Re-do b. with Sicily. Explain what you learn about Sicily relative to the other areas.
  1. Use your AI helper or do some googling. What can you find out about Sicilian olive oils? Are they higher in value? Does Sicily even grow olives, or does it use olives from neighbouring areas?

Exercise 3: Baker field soils

The data measures essential elements in the soils for agricultural uses in the Baker field in Iowa. Some of these variables are skewed, which makes examining associations between them and other variables less useful. It can help to transform skewed variables to be more symmetric and then check for associations. This is what we will do here.

corn <- read_csv("https://eda.numbat.space/data/baker.csv") 
  1. Make density plots of the soil variables in the Baker field corn yield data. Choose an appropriate transformation to symmetrise the distribution.
  1. Examine the pairwise associations using an interactive scatterplot matrix. Describe what you learn.
  1. Using a grand tour, how would you answer the following questions? Is there clustering? Is there linear dependence? Non-linear dependence? outliers. For any structure that you see determine which variables contribute to it, and make plots of these variables (or check the scatterplot matrix) to check whether the pattern is visible there too.

Exercise 4: Exam marks

There is a dataset mathmarks in the SMPracticals package, which has marks out of 100 for 88 students. It is interesting to note that all students had marks for all tests, which makes one wonder whether marks for students who missed a test were dropped. Mechanics and vectors were closed book exams, and the others were open book.

data(mathmarks, package="SMPracticals")
  1. Make a side-by-side boxplot of the test scores. What do you learn about the test scores on the different subjects?
  1. Make a scatterplot matrix, even better if it is interactive. Describe the relationships between the tests. Is there something different about the open book vs closed book scores?
  1. Make an interactive parallel coordinate plot. Are there some students who have done consistently well on all tests? Consistently badly on all tests? Badly on some but better on others?

Exercise 5: Knowledge and resources

The vcdExtra package contains a dataset Dyke about how 1729 survey respondents’ knowledge of cancer depended on whether they listened to the radio, read newspapers, did solid reading, or attended lectures. This data is all categorical, so we need to make plots to show the relationships between five categorical variables.

data(Dyke, package="vcdExtra")
  1. Make separate bar charts for each of the explanatory variables, with bars filled by the response variable Knowledge. What do you learn?
  1. Make a 100% bar chart of Newspaper, with Knowledge mapped to fill, and faceted by Reading. What do you learn about the relative proportions in the groups?
  1. Make a doubledecker plot of the data. What combination of factors leads to the highest level of knowledge about cancer? What combination leads to the lowest?
# I had a difficult time getting the Good category being the first
# and to be coloured red, but this appears to work
Dyke_tbl <- Dyke_tsb %>%
  mutate(Knowledge = factor(Knowledge, levels=c("Poor", "Good"))) %>%
  uncount(n) %>%
  select(Reading, Radio, Lectures, Newspaper, Knowledge) %>%
  arrange(Knowledge, Reading, Radio, Lectures, Newspaper)
doubledecker(Knowledge~., Dyke_tbl,
             gp = gpar(fill=c("grey90", "orangered")))

Exercise 6: Parkinsons

This dataset is composed of a range of biomedical voice measurements from 31 people, 23 with Parkinson’s disease (PD). Each column in the table is a particular voice measure, and each row corresponds one of 195 voice recording from these individuals (“name” column). The main aim of the data is to discriminate healthy people from those with PD, according to “status” column which is set to 0 for healthy and 1 for PD.

The data is available at The UCI Machine Learning Repository in ASCII CSV format. The rows of the CSV file contain an instance corresponding to one voice recording. There are around six recordings per patient, the name of the patient is identified in the first column. There are 24 variables in the file, including the persons name in column 1.

The data are originally analysed in: Max A. Little, Patrick E. McSharry, Eric J. Hunter, Lorraine O. Ramig (2008), ‘Suitability of dysphonia measurements for telemonitoring of Parkinson’s disease’, IEEE Transactions on Biomedical Engineering (to appear).

library(cassowaryr)
# Load the data
data(pk)
  1. How many pairwise plots would you need to look at, to look at all of them?
  1. Compute several of the scagnostics (monotonic, outlying, clumpy2) for the first five variables of variables, except for name. (Note: We are using just five for computing speed, but the scagnostics could be calculated on all variables.)
# Compute the scagnostics on the relevant variables
s <- calc_scags_wide(pk[,2:5],
                scags=c("outlying","monotonic",
                        "clumpy2"))
s
  1. Sort the scagnostics, separately by the values on (i) monotonic (ii) outlying (iii) clumpy2, and plot the pair of variables with the highest values on each.
  1. Make an interactive scatterplot matrix. Browse over it to choose other interesting pairs of variables and make the plots.
  1. The scagnostics help us to find interesting associations between pairs of variables. However, the problem here is to detect differences between Parkinsons patients and normal patients. How would you go about that? Think about some ideas long the line of scagnostics but look for differences between the two groups.

👋 Finishing up

Make sure you say thanks and good-bye to your tutor. This is a time to also report what you enjoyed and what you found difficult.