Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Created July 19, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlislerainey/d651887552184794a467cf806f481803 to your computer and use it in GitHub Desktop.
Save carlislerainey/d651887552184794a467cf806f481803 to your computer and use it in GitHub Desktop.
code for help teaching chs. 11 and 12 of FPP
# 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. But trust me that it grabs the data you submitted
# in the forms and loads it as a data frame.
library(googlesheets) # used to load the google sheet data
sheet <- gs_key("1mJVzIpolvJ5XPROMCXg1A5iU_mlWXwHBlXQwG5t_QVk") # register google sheet
bodies <- gs_read(sheet) # load sheet
saveRDS(bodies, "data/bodies.rds") # save the former google sheet as a .rds
rm(sheet, bodies) # remove the sheet to have a clean workspace
# done with tidying
###################
# load newly saved data frame
bodies <- readRDS("data/bodies.rds")
# quick look at data
tibble::glimpse(bodies)
# calculate aveages and SDs for each variable
apply(bodies, 2, mean)
apply(bodies, 2, sd)
# calculate correlations
num_bodies <- dplyr::select(bodies, -time, -sex)
cor(dplyr::select(bodies, -time, -sex))
# plot of forearm length and height
gg1 <- ggplot(bodies, aes(x = forearm, y = height)) +
geom_point() +
labs(title = "Scatterplot of Forearm Length and Height")
# plot of hand span and height
gg2 <- ggplot(bodies, aes(x = hand, y = height, color = sex)) +
geom_point() +
labs(title = "Scatterplot of Hand Span and Height")
# plot of head circumference and height
gg3 <- ggplot(bodies, aes(x = head, y = height, color = sex)) +
geom_point() +
labs(title = "Scatterplot of Head Circumference and Height")
# combine the three ggplots
gridExtra::grid.arrange(gg1, gg2, gg3)
# plot forearms and height with SD line
slope <- sd(bodies$height)/sd(bodies$forearm)
intercept <- mean(bodies$height) - mean(bodies$forearm)*slope
gg1 + geom_abline(slope = slope, intercept = intercept) +
geom_smooth(method = "lm", linetype = "dashed", color= "red", se = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment