Exercise 1: Data Manipulation
Part 1: The map functions from purrr
- Use
purrr
functions to find the column means for mtcars
dataset. Note that the mean performance cars in this dataset is very poor. That’s because these models are from the 70s!
map_dbl(mtcars, mean)
mpg cyl disp hp drat wt qsec vs
20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750 0.437500
am gear carb
0.406250 3.687500 2.812500
- Use
purrr
functions to generate 10 random normals for each of the mean parameter: \(\mu = -18, 5, 10, 30\).
map(c(-18, 5, 10, 30), rnorm, n = 10)
[[1]]
[1] -18.18672 -19.21094 -18.98525 -17.38768 -18.37241 -18.65973 -18.50938 -16.32759
[9] -17.83721 -16.94049
[[2]]
[1] 4.239685 4.028045 6.054269 6.761718 3.786109 5.606558 6.752187 4.734244 5.102416
[10] 5.852126
[[3]]
[1] 9.319304 9.128619 10.128532 11.069123 9.171212 7.801036 10.571461 11.529022
[9] 10.306849 10.788314
[[4]]
[1] 29.79419 32.22650 29.03999 30.65289 27.28731 30.26733 31.25100 27.90372 28.48056
[10] 29.02555
- What is the difference between running `
map(1:5, runif)
map(list(1:5), rnorm)
, map(list(1, 2, 3, 4, 5), rnorm)
and map(1:5, rnorm, n = 5)
?
Below, map
takes in each argument from a vector: 1, 2, 3, 4, 5 as the first argument n =
for rnorm()
. So the function will generate iteratively: one, two, three, four, five random normal numbers (with default mean 0, and standard deviaton 1).
map(1:5, rnorm)
[[1]]
[1] 0.3543547
[[2]]
[1] -1.0807805 -0.3617925
[[3]]
[1] -2.59539565 1.04837239 -0.08422438
[[4]]
[1] 0.01762317 -0.22745079 -1.11850532 -0.45947053
[[5]]
[1] 0.2661181 1.3736770 -1.8404807 1.4866985 -1.0442055
Below, note that list(1:5)
is a list of one argument. This first argument is equal to a vector c(1,2,3,4,5)
, and is supplied to rnorm()
as the first argument, so map only loops over once and is equivalent to calling: rnorm(c(1,2,3,4,5))
.
map(list(1:5), rnorm)
[[1]]
[1] 0.03501295 -1.13494274 -2.01695308 -1.06612147 0.54139114
The syntax below will be equivalent to the first example, since now list(1, 2, 3, 4, 5)
is a list of 5 elements.
map(list(1, 2, 3, 4, 5), rnorm)
[[1]]
[1] 0.07671454
[[2]]
[1] -0.4366112 -0.5760937
[[3]]
[1] 0.6754205 -0.7238659 -0.5108671
[[4]]
[1] -1.3759907 -0.6210061 0.4072337 0.4596481
[[5]]
[1] -0.8108628 2.4343855 1.1015686 0.7011843 1.5210154
The example below is slightly different. Here, since we already force the n
parameter to equal 7, the map
function will loop over the vector 1:5
and supply each of the five elements to rnorm()
as the second argument, mean =
. Thus, resulting in a list of random normals, each of length 7, and each with different mean parameter.
map(1:5, rnorm, n = 7)
[[1]]
[1] 0.72775020 0.82556315 0.03440454 0.33252510 2.08009368 1.59179423 -0.57368740
[[2]]
[1] 1.2699895 0.7281602 4.6343781 1.9799894 1.0692924 1.7431891 1.9119823
[[3]]
[1] 4.373457 2.403356 3.049195 3.350636 2.416042 2.364417 3.469767
[[4]]
[1] 2.453333 3.379174 5.364128 2.917396 2.550765 4.287269 5.384675
[[5]]
[1] 4.370332 3.204837 5.230435 5.440600 3.635311 4.868689 6.862728
- For looping over multiple arguments you can use
map2()
or pmap()
function. Use map2()
to generate 5 random numbers with for each pair of mean, and standard devtaion equal to: c(2, 1), c(-3, 10), c(20, 0.1), c(-17, 2)
.
map2(c(2, -3, 20, -17), c(1, 10, 0.1, 2), rnorm, n = 5)
[[1]]
[1] 1.693059 2.779361 4.028268 2.752682 3.002951
[[2]]
[1] 1.5636282 12.1845171 7.1027527 15.1386819 -0.8149126
[[3]]
[1] 19.95008 19.92959 20.02988 20.06964 19.96810
[[4]]
[1] -18.12892 -16.15568 -17.49418 -14.22181 -15.94779
Part 2: Merging
- Identify the keys in the following datasets (you might need to install some packages and read some documentation):
Lahman::Batting
nasaweather::atmos
ggplot2::diamonds
Lahman::Batting %>%
count(playerID, yearID, teamID, lgID, stint) %>%
filter(n > 1)
nasaweather::atmos %>%
count(lat, long, year, month) %>%
filter(n > 1)
None of the combination of the variables uniquely identifies observations. Thus, the row number
is the key for the diamonds
dataset.
ggplot2::diamonds %>%
count(carat, cut, color, clarity, depth, table, price, x, y, z) %>%
filter(n > 1)
- Add the location (i.e. the lat and lon) of the destination airport
to flights
dataset from nycflights13
package. Then, construct a new data table mean_delay
containing information on the average arrival delay for each destination airport and its location (i.e. the lat and lon). Remember that some of the recoreds are missing, so use na.rm = TRUE
when computing means.
library(nycflights13)
flights2 <- flights %>%
left_join(airports, by = c("dest" = "faa"))
mean_delay <- flights2 %>%
group_by(lat, lon, origin) %>%
summarise(mean_dep_delay = mean(arr_delay, na.rm = TRUE))
You can plot your data on the map as follows:
(plt <- ggplot(mean_delay, aes(lon, lat)) +
borders("state") +
geom_point(aes(color = mean_dep_delay), size = 3) +
coord_quickmap() + theme_bw() +
scale_color_viridis_c())
Part 3: Export datasets
- Export the
mean_delay
data as a ‘.tsv’ file. Save it on your “Desktop” then check the file was properly saved, then delete it.
write_tsv(mean_delay, path = "~/Desktop/mean_delay.tsv")
- Check what objects are currently stored in your environment using
ls()
. Save mean_delay
as a R object using saveRDS()
function to save it as am ‘.rds’ file. Then, select 2 different objects from the environment and use save()
to save them to an ‘.rda’ file. Then, remove these objects and the mean_delay
tibble. Check that you can reload these objects from files you saved.
ls()
[1] "flights2" "mean_delay" "plt"
Saving as R objects:
saveRDS(mean_delay, file = "~/Desktop/mean_delay.rds")
save(list = c("plt", "flights2"), file = "~/Desktop/two_objects.rda")
Now, you can delete the objects:
rm(mean_delay, flights2, plt)
ls()
character(0)
And then load them in using the following:
mean_delay <- readRDS("~/Desktop/mean_delay.rds")
mean_delay
load("~/Desktop/two_objects.rda")
ls()
[1] "flights2" "mean_delay" "plt"
Exercise 2: Exploratory Data Analysis
Zillow provides data on home prices, including median rental price per square feet and the median estimated home value. There are many more statistics provided by zillow in this website that you can explore.
zillow_url1 <- paste0("http://files.zillowstatic.com/research/public/City/",
"City_MedianRentalPricePerSqft_AllHomes.csv")
zillow_url2 <- paste0("http://files.zillowstatic.com/research/public/City/",
"City_ZriPerSqft_AllHomes.csv")
price_per_sqft <- read_csv(zillow_url1)
Parsed with column specification:
cols(
.default = col_double(),
RegionName = col_character(),
State = col_character(),
Metro = col_character(),
CountyName = col_character(),
SizeRank = col_integer()
)
See spec(...) for full column specifications.
value_per_sqft <- read_csv(zillow_url2)
Parsed with column specification:
cols(
.default = col_double(),
RegionID = col_integer(),
RegionName = col_character(),
State = col_character(),
Metro = col_character(),
CountyName = col_character(),
SizeRank = col_integer()
)
See spec(...) for full column specifications.
# First, we tidy the datasets:
price_per_sqft <- price_per_sqft %>%
select(-Metro) %>%
gather(`2010-01`:`2018-08`, key = "date", value = "price")
value_per_sqft <- value_per_sqft %>%
select(RegionID:CountyName, `2011-01`:`2018-08`) %>%
gather(`2011-01`:`2018-08`, key = "date", value = "value")
# Do the inner join:
home_per_sqft <- price_per_sqft %>%
inner_join(value_per_sqft)
Joining, by = c("RegionName", "State", "CountyName", "date")
# Format dates:
home_per_sqft <- home_per_sqft %>%
mutate(date = paste0(date, "-01"),
date = as.Date(date))
# We filter to only top 10 priciest states this year
# excluding DC and HI:
top10_States <- home_per_sqft %>%
filter(date >= as.Date("2018-01-01")) %>%
filter(State != "HI", State != "DC") %>%
group_by(State) %>%
summarise(price = mean(price, na.rm = TRUE)) %>%
top_n(10)
Selecting by price
home_per_sqft <- home_per_sqft %>%
filter(State %in% top10_States$State)
#rm(price_per_sqft, value_per_sqft)
The most expensive states by median home price per sq. ft. are:
top10_States
- For the current year (2018), using the filtered
home_per_sqft
dataset show the relationship between the value and the price per square foot of homes in 10 chosen states. The value is a dollar-denominated and estimated by Zillow.
- Is there a correlation between the price and the value per square foot?
- Are there any atypical trends you observe?
- Are there any clusters of regions that are pricier than, expected based on their estimated value? Find, the
RegionName
for an example these outliers, and see if it makes sense.
- Are there any clusters of regions that have higher value than, expected based on how cheap they are? Find, the
RegionName
for an example of these outliers, and see if it makes sense.
home_per_sqft %>%
filter(date >= as.Date("2018-01-01")) %>%
ggplot(aes(x = value, y = price)) +
geom_point(aes(color = State), alpha = 0.3) +
theme(legend.position = "bottom") +
geom_smooth()
home_per_sqft %>%
filter(date >= as.Date("2018-01-01")) %>%
filter(price > 5, value > 4)
home_per_sqft %>%
filter(date >= as.Date("2018-01-01")) %>%
filter(price > 3, value < 2)
home_per_sqft %>%
filter(date >= as.Date("2018-01-01")) %>%
filter(price < 2.2, value > 2.8)
- Collapse the data by computing the average of the median home price per square foot for each (state, date) pair. Then show the price trend over time for each of the top 10 states.
df <- home_per_sqft %>%
group_by(State, date) %>%
summarise(
price = mean(price, na.rm = TRUE),
value = mean(value, na.rm = TRUE))
ggplot(data = df, aes(x = date, y = price, color = State)) +
geom_line(lwd = 0.7)
- Now, subset your
home_per_sqft
dataset to homes located in the Bay Area:
bayarea_counties <- c("Alameda", "Napa", "Santa Clara", "Contra Costa",
"San Francisco", "Solano", "Marin", "San Mateo",
"Sonoma")
bay_area_home <- home_per_sqft %>%
filter(CountyName %in% bayarea_counties)
Filter your observations to the once after “2013-09-01”. Then generate a boxplot of home prices per square foot for the following regions:
cities <- c("Oakland", "San Francisco", "Berkeley", "San Jose",
"San Mateo", "Redwood City", "Mountain View", "Napa",
"South San Francisco", "Menlo Park", "Cupertino")
For which region were the median home prices per square foot highest over the most recent 5 years period?
df <- bay_area_home %>%
filter(date >= as.Date("2013-09-01")) %>%
filter(RegionName %in% cities)
ggplot(df, aes(x = RegionName, y = price)) +
geom_boxplot(aes(color = CountyName)) +
coord_flip()
- For the same regions plot the prices over time.
ggplot(df) +
geom_line(aes(date, price, color = RegionName))
If you are curious, you can repeat this type of analysis for the listing prices of home sales using the following files from zillow: ‘City_MedianListingPricePerSqft_AllHomes.csv’, ‘City_Zhvi_AllHomes.csv’.
Exercise 3: Interactive graphics with plotly
First generate the following (x,y,z) coordinates for your scatter 3D plot:
library(plotly)
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
theta <- 2 * pi * runif(1000)
phi <- pi * runif(1000)
x <- sin(phi) * cos(theta)
y <- sin(phi) * sin(theta)
z <- cos(phi)
dat <- data.frame(x, y, z)
- Generate a scatter plot with (x, y, z) coordinates, just computed. What do you observe?
plot_ly(dat, x = ~x, y = ~y, z = ~z,
type = "scatter3d", mode = "markers", marker = list(size = 3))
- Now generate new points as follows.
N <- 10
theta <- rep(2*pi * seq(0, 1, length.out = 100), N)
phi <- rep(pi * seq(0, 1, length.out = N*100))
x <- sin(phi) * cos(theta)
y <- sin(phi) * sin(theta)
z <- cos(phi)
dat <- data.frame(x, y, z)
Then, use plotly
to create a 3D scatter point plot and a separate line plot. Color the points by their ordering in the data-frame.
plot_ly(dat, x = ~x, y = ~y, z = ~z, color = 1:1000) %>%
add_markers(marker = list(size = 2))
plot_ly(dat, x = ~x, y = ~y, z = ~z, color = 1:1000, type = "scatter3d",
mode = "lines", line = list(width = 3))
LS0tCnRpdGxlOiAiTGVjdHVyZSA1OiBFeGVyY2lzZXMgd2l0aCBhbnN3ZXJzIgpkYXRlOiBPY3RvYmVyIDExdGgsIDIwMTgKb3V0cHV0OiAKICBodG1sX25vdGVib29rOgogICAgdG9jOiB0cnVlCiAgICB0b2NfZmxvYXQ6IHRydWUKLS0tCgpgYGB7ciwgbWVzc2FnZT1GQUxTRSwgd2FybmluZz1GQUxTRX0KbGlicmFyeSh0aWR5dmVyc2UpCmBgYAoKCiMgRXhlcmNpc2UgMTogRGF0YSBNYW5pcHVsYXRpb24KCiMjIFBhcnQgMTogVGhlIG1hcCBmdW5jdGlvbnMgZnJvbSBgcHVycnJgIAoKYS4gVXNlIGBwdXJycmAgZnVuY3Rpb25zIHRvIGZpbmQgdGhlIGNvbHVtbiBtZWFucyBmb3IgYG10Y2Fyc2AgZGF0YXNldC4KTm90ZSB0aGF0IHRoZSBtZWFuIHBlcmZvcm1hbmNlIGNhcnMgaW4gdGhpcyBkYXRhc2V0IGlzIHZlcnkgCnBvb3IuIFRoYXQncyBiZWNhdXN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