Skip to content

Instantly share code, notes, and snippets.

View robwschlegel's full-sized avatar

Robert William Schlegel robwschlegel

View GitHub Profile
data_gender <- years_of_education[,-5] %>%
left_join(., gender_equality_education[,-5], by = c("ccode", "country.name", "year")) %>%
rename(., education_years = value.x, gender_equality_education = value.y)
data_gender <- data_gender[complete.cases(data_gender$gender_equality_education),]
lm_data_gender1 <- glance(lm(gender_equality_education ~ education_years, data = data_gender))
lm_data_gender2 <- glance(lm(gender_equality_education ~ education_years,
data = filter(data_gender, education_years >= 5)))
# And now for a scatterplot
# Load libraries
library(tidyverse)
library(lubridate)
library(reshape2)
library(MBA)
library(mgcv)
# Load and screen data
# For ease I am only using monthly means
# and depth values rounded to 0.1 metres
# The date column must then be converted to numeric values
ctd$date <- decimal_date(ctd$date)
# Now we may interpolate the data
ctd_mba <- mba.surf(ctd, no.X = 300, no.Y = 300, extend = T)
dimnames(ctd_mba$xyz.est$z) <- list(ctd_mba$xyz.est$x, ctd_mba$xyz.est$y)
ctd_mba <- melt(ctd_mba$xyz.est$z, varnames = c('date', 'depth'), value.name = 'temp') %>%
filter(depth < 0) %>%
mutate(temp = round(temp, 1))
# Create a bounding box
# We want to slightly extend the edges so as to use all of our data
left <- ctd[ctd$date == min(ctd$date),] %>%
select(-temp) %>%
ungroup() %>%
mutate(date = date-0.01)
bottom <- ctd %>%
group_by(date) %>%
summarise(depth = min(depth)) %>%
mutate(depth = depth-0.01)
# Libraries
library(tidyverse)
library(lubridate)
library(broom)
library(gridExtra)
# President data
data(presidential)
presidential$start <- year(presidential$start)
presidential$end <- year(presidential$end)-1
ggplot(data = green_card, aes(x = Year, y = Number)) +
geom_col(aes(fill = Party), colour = "black") +
geom_smooth(method = "lm", colour = "black") +
labs(x = NULL, y = "Green Cards Granted") +
scale_fill_manual(values = c("slateblue1", "firebrick1"))
# Calculate residuals
green_card_resids <- augment(lm(Number~Year, data = green_card))
green_card_resids <- merge(green_card_resids, party_year, by = "Year")
# Plot them
ggplot(data = green_card_resids, aes(x = Year, y = .resid)) +
geom_col(aes(fill = Party), colour = "black") +
geom_smooth(method = "lm", colour = "black") +
labs(x = NULL, y = "Green Cards Granted") +
scale_fill_manual(values = c("slateblue1", "firebrick1"))
# Returns
return_bar <- ggplot(data = removals, aes(x = Year, y = Returns)) +
geom_col(aes(fill = Party), colour = "black") +
# geom_smooth(method = "lm", colour = "black") +
labs(x = NULL, y = "Immigrants Returned") +
scale_fill_manual(values = c("slateblue1", "firebrick1")) +
ggtitle("Returns")
# Removals
removal_bar <- ggplot(data = removals, aes(x = Year, y = Removals)) +
# Load libraries
library(tidyverse)
library(gridExtra)
# Load data
goats <- read_csv("../data/GoatsperCapita_Compact.csv") %>%
filter(year >= 1900)
goats %>%
group_by(year) %>%
select(-ccode, -country.name) %>%
summarise(value = mean(value)) %>%
ggplot(aes(x = year, y = value)) +
geom_boxplot(data = goats, aes(group = year)) +
geom_smooth(method = "lm")