Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
thoughtfulbloke / OECDmortRate.R
Created September 17, 2021 04:07
calculating weekly mortality rates from the OECD data source
# OECD weekly all cause deaths to mortality rates
# Keep in mind, some countries (Sweden) exclude 3% of deaths from the weekly
# dataset as the day of death is not known
library(OECD)
library(dplyr)
country1 = "NZL"
country2 = "USA"
@thoughtfulbloke
thoughtfulbloke / lockdownvsCases.R
Created September 14, 2021 05:07
R code for graph showing NZ lockdown vs. Cases
# MoH is the ministry of Health all cases demographics csv file
# https://www.health.govt.nz/our-work/diseases-and-conditions/covid-19-novel-coronavirus/covid-19-data-and-statistics/covid-19-case-demographics#case-details
library(readr)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
# you need to provide csv name
file_name <- "covid_cases_2021-09-14.csv"
@thoughtfulbloke
thoughtfulbloke / davidise_graph.R
Created September 5, 2021 05:55
takes an unthemed ggplot graph and saves it in my preferred theme style, timestamped, on the desktop
davidise_graph <- function(x, savepath="~/Desktop/output", show_graph = TRUE){
# x is an unthemed ggplot graph but all the scales & labs are applied
# since this function wants a ggplot graph, I am assuming ggplot is loaded)
# assume Open Sans installed as a font
library(showtext)
# sort out your own font path declarations
font_add(family = "OpenSans", regular = "~/Library/Fonts/OpenSans-Light.ttf",
bolditalic = "~/Library/Fonts/OpenSans-BoldItalic.ttf",
italic = "~/Library/Fonts/OpenSans-LightItalic.ttf",
# Data
# https://www.health.govt.nz/our-work/diseases-and-conditions/covid-19-novel-coronavirus/covid-19-data-and-statistics/covid-19-case-demographics#case-details
library(readr)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
library(patchwork)
# you need to provide csv name
@thoughtfulbloke
thoughtfulbloke / analysing_nzpol_themes.R
Created July 14, 2021 03:42
Applying topic analysis of nzpol and relating it to user accounts
library(rtweet)
library(dplyr)
library(lubridate)
library(ggplot2)
library(tidytext)
library(topicmodels)
search_term <- "#nzpol"
corpus <- search_tweets(search_term, n=18000)
nzpol_utm <- corpus %>% select(screen_name, text) %>%
unnest_tokens(word,text, token="tweets") %>%
library(readr)
library(suncalc)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
library(tidyr)
quake10yr <- c("https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2020-02-01&enddate=2021-03-01T0:00:00",
"https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2019-02-01&enddate=2020-02-01",
library(rvest)
target_url <- "https://www.publicservice.govt.nz/resources/proactive-releases/"
webpage <- target_url %>% read_html()
links <- webpage %>% html_nodes("a") %>% html_attr("href")
link_text <- webpage %>% html_nodes("a") %>% html_text()
link_set <- data.frame(links, link_text, stringsAsFactors = FALSE)
if(file.exists("linkcurrent.csv")){
linkcurrent <- read.csv("linkcurrent.csv", stringsAsFactors = FALSE)
newlinks <- link_set[!link_set$links %in% linkcurrent$links,]
} else {
# works through
# https://www.health.govt.nz/news-media/media-releases?page=1 and subsequent pages
library(rvest)
library(dplyr)
library(stringr)
table_fetch <- function(x){
url_loc <- paste0("https://www.health.govt.nz",x)
page_obj <- url_loc %>% read_html()
library(readr)
library(suncalc)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
library(tidyr)
quake10yr <- c("https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2020-02-01&enddate=2021-03-01T0:00:00",
"https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2019-02-01&enddate=2020-02-01",
############ collection
## assumes the working directory is the folder the script is in, and is set to the same for future runs of the file.
# This also assumes you have twitter developer credientals, and have run the create_token() function in the rtweet package
# to authorise r to access Twitter. This stores the credentials in an environmental variable loaded at startup so they are
# not exposed in the script
# as an alternative, if using a Mac or PC, and the httpuv package is installed, you can interactively authorise the script
# at run time
# these packages need to be already installed in order to be loaded and used