install.packages(c("tsibble", "lubridate", "patchwork", "colorspace", "tsibbledata", "forcats", "chron", "sugrrants", "brolgar"))ETC5521 Tutorial 9
Exploring data having a space and time context Part I
🎯 Objectives
These exercise are to do some exploratory analysis with graphics and statistical models, focusing on temporal data analysis.
🔧 Preparation
- The reading for this week is Reintroducing tsibble: data tools that melt the clock and brolgar: An R package to BRowse Over Longitudinal Data Graphically and Analytically in R.
- Install the following R-packages if you do not have them already:
- Open your RStudio Project for this unit, (the one you created in week 1,
ETC5521). Create a.qmddocument for this weeks activities.
📥 Exercises
Exercise 1: Imputing missings for pedestrian sensor using a model
Sometimes imputing by a simple method such as mean or moving average doesn’t work well with multiple seasonality in a time series. Here we will use a linear model to capture the seasonality and produce better imputations for the pedestrian sensor data (from the tsibble package). This data has counts for four sensors, for two years 2015-2016.
- What are the multiple seasons of the pedestrian sensor data, for
QV Market-Elizabeth St (West)? (Hint: Make a plot to check. You might filter to a single month to make it easier to see seasonality. You might also want to check when Queen Victoria Market is open.)
- Check temporal gaps for all the pedestrian sensor data. Subset to just the QV market sensor for the two years. Where are the missing values? Fill these with NA. (Note that
fill_gapsdoesn’t fill in the additional variables,Date,Time, andyear,monthso these will need to be computed after filling.)
- Create a new variable to indicate if a day is a non-working day, called
hol. We need this to accurately model the differences between pedestrian patterns on working vs not working days. Make hour a factor - this helps to make a simple model for a non-standard daily pattern.
- Fit a linear model with Count as the response on predictors
Timeandholinteracted.
- Predict the count for all the data at the sensor.
- Make a line plot focusing on the last two weeks in 2015, where there was a day of missings, where the missing counts are substituted by the model predictions. Do you think that these imputed values match the rest of the series, nicely?
- Rather than zooming into a fortnight, use
sugrrants::facet_calendar()again (as in (a)) to lay the whole of 2015 out as a calendar, colouring each day’s line by whether it contains an original or imputed value. Does the New Year’s Eve block of missings (found in (b)) stand out against the rest of the calendar? What about the single daylight-saving hour in early April – is it visible at this scale?
Exercise 2: Men’s heights
The heights data provided in the brolgar package contains average male heights in 144 countries from 1500-1989.
- What’s the time index for this data? What is the key?
- Filter the data to keep only measurements since 1700, when there are records for many countries. Make a spaghetti plot for the values from Australia. Does it look like Australian males are getting taller?
- Check the number of observations for each country. How many countries have less than five years of measurements? Filter these countries out of the data, because we can’t study temporal trend without sufficient measurements.
- Make a spaghetti plot of all the data, with a smoother overlaid. Does it look like men are generally getting taller?
- Use
facet_stratato break the data into subsets using theyear, and plot is several facets. What sort of patterns are there in terms of the earliest year that a country appears in the data?
- Compute the three number summary (min, median, max) for each country. Make density plots of these statistics, overlaid in a single plot, and a parallel coordinate plot of these three statistics. What is the average minimum (median, maximum) height across countries? Are there some countries who have roughly the same minimum, median and maximum height?
- Which country has the tallest men? Which country has highest median male height? Which country has the shortest men? Would you say that the distribution of heights within a country is similar for all countries?
👌 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.