Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Last active February 23, 2017 17:10
Show Gist options
  • Select an option

  • Save carlislerainey/871c076aae5dde1eb4046c84c5ac0460 to your computer and use it in GitHub Desktop.

Select an option

Save carlislerainey/871c076aae5dde1eb4046c84c5ac0460 to your computer and use it in GitHub Desktop.
Code to load data from survey on predicting height using other body measurements
# data loading and tidying
##########################
# Note: we haven't talked about how to do this, and I don't expect you to
# understand the code below.
library(dplyr) # useful function for cleaning up data
library(lubridate) # useful for working with dates and durations
library(magrittr) # useful for data manipulation
library(tidyr) # to gather the data
library(googlesheets) # used to load the google sheet data
sheet <- gs_key("11pXvnrDfygcaMp6iI-30BasveKYLZ4Ce9Z0-cuuKLww") # register google sheet
df_raw <- gs_read(sheet) # load sheet
df <- df_raw %>%
gather(measurement, prediction_value, age:calf_circumference) %>%
select(-other, -comments) %>%
mutate(measurement = reorder(measurement, prediction_value),
pols_209 = ifelse(pols_209 == "No", "Not in POLS 209", "In POLS 209"))
# done with tidying
###################
# quick look at data
tibble::glimpse(df)
# compute the average for each measurement
smry <- summarize(group_by(df, measurement),
average_score = mean(prediction_value),
percentile_25 = quantile(prediction_value, .25),
percentile_75 = quantile(prediction_value, .75))
# scatterplot
library(ggplot2)
ggplot(df, aes(x = measurement, y = prediction_value, label = name)) +
geom_text(alpha = 0.5,
position = position_jitter(width = 0.1, height = 0.1),
size = 3) +
facet_wrap(~ pols_209) +
coord_flip() +
theme_bw()
# plot of averages
ggplot(smry, aes(x = measurement,
y = average_score,
ymin = percentile_25,
ymax = percentile_75)) +
geom_point() +
geom_linerange() +
labs(x = "Measurement",
y = "Subjective Predictive Value",
title = "Average Subjective Predictive Value and Interquartile Range")
# density plots
ggplot(df, aes(x = prediction_value, fill = pols_209)) +
geom_density(alpha = 0.5) +
facet_wrap(~ measurement, scales = "free_y")
# bar plot
ggplot(df, aes(x = prediction_value)) +
geom_bar() +
facet_wrap(~ measurement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment