Skip to content

Instantly share code, notes, and snippets.

View perlatex's full-sized avatar

wang minjie perlatex

  • sichuan normal universtiy
View GitHub Profile
@ivelasq
ivelasq / rotation.R
Last active January 7, 2023 03:39
Rotated ggplot2 graph
# Credits to this Stack Overflow post
# https://stackoverflow.com/questions/13445753/force-ggplot2-scatter-plot-to-be-square-shaped
library(ggplot2)
library(grid)
rotation <- 45
p <-
ggplot() +
@ivelasq
ivelasq / ssbm.R
Last active January 3, 2021 15:24
Age Distribution of the Top 100 Super Smash Bros. Melee Players (2019)
##########################################################
# Get Ages of Top 100 Super Smash Brothers Melee Players #
##########################################################
# Tutorial ----------------------------------------------------------------
# https://www.r-bloggers.com/2020/05/intro-to-polite-web-scraping-of-soccer-data-with-r/
# Library -----------------------------------------------------------------
library(tidyverse)
library(broom)
library(latex2exp)
library(patchwork)
set.seed(1234)

logit_df <- tibble(x = seq(-5, 5, length.out = 100)) %>% 
    mutate(p = 1/(1 + exp(-x))) %>% 
    mutate(y = rbinom(n(), size = 1, prob = p))
# devtools::install_github("sharlagelfand/ggkeyboard", ref = "main")
library(ggkeyboard)
library(dplyr)
library(ggplot2)
# mac keys
keys <- tkl
keys$r1$key[1:7] <- c("Fn", "Ctrl", "\u2325", "\u2318", "Spacebar", "\u2318", "\u2325")
keys$r1$width[1:7] <- c(rep(1.05, 4), 8.25, rep(1.25, 2))
library(tidyverse)
library(brms)
library(Amelia)
library(broom)

set.seed(1234)
data("africa")

# Impute missing data with Amelia (since it handles country/year panel stuff really well)
@dgrtwo
dgrtwo / cubic-model-animation.R
Created May 8, 2020 19:56
Showing how a 3rd-degree polynomial model would predict future deaths if a plateau continues
library(tidyverse)
library(broom)
library(scales)
theme_set(theme_light())
US <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv") %>%
mutate(new_deaths = deaths - lag(deaths)) %>%
filter(date >= "2020-02-26")
today <- max(US$date)
@dgrtwo
dgrtwo / comparing-polynomial-models-covid.R
Created May 5, 2020 19:33
Comparing the CEA's "cubic model" to quadratic and quartic models
library(tidyverse)
library(broom)
US <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv") %>%
mutate(new_deaths = deaths - lag(deaths)) %>%
filter(date >= "2020-02-26")
models <- tibble(degrees = 2:4) %>%
mutate(model = map(degrees, ~ lm(log(new_deaths + 1) ~ poly(date, .), data = US)))
# Install and load required packages
install.packages("needs")
library(needs)
needs(tidyverse, magrittr, animation, pdftools, png, scales)
# Function that extracts data from Google Mobility PDFs
process_google_mobility <- function(country_code, start_date, end_date){
# Convert first page of PDF into high-res PNG
pdf_convert(paste0("https://www.gstatic.com/covid19/mobility/",end_date,"_",country_code,"_Mobility_Report_en.pdf"), format = "png", pages = 1, dpi = 300, filenames = "IMG1.png")
library(dplyr)
library(slider)
library(lubridate)
library(tsibbledata)
# Google, Apple, Facebook, Amazon stock
gafa_stock <- as_tibble(gafa_stock)
gafa_stock <- select(gafa_stock, Symbol, Date, Close, Volume)
head(gafa_stock, 2)
read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv") %>%
gather(date, cases, 5:ncol(.)) %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
group_by(country = `Country/Region`, date) %>%
summarise(cases = sum(cases)) %>%
filter(country != "Others" & country != "Mainland China") %>%
bind_rows(
tibble(country = "Republic of Korea", date = as.Date("2020-03-11"), cases = 7755)
) %>%
group_by(country) %>%