Skip to content

Instantly share code, notes, and snippets.

View mathzero's full-sized avatar

Matt Whitaker mathzero

  • Imperial College London
View GitHub Profile
@mathzero
mathzero / README.md
Created April 28, 2020 23:26 — forked from sebkopf/README.md
wolfram alpha from R

Wolfram Alpha API from R

The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by the wolframalpha module available for Python.

source("wa_lib.R")

Initialize client

@mathzero
mathzero / geo_plotting.R
Created April 28, 2020 23:36
Plot uk map with data
# load packages
library(ggmap)
library(ggplot2)
library(ggthemes)
### set up a google API for google static maps at https://developers.google.com/maps/documentation ###
# register key
register_google(key = "XXXXXXXXXXXXXXX")
@mathzero
mathzero / table_one.R
Last active April 29, 2020 08:15
Use tableone package to create table one
# Create table 1 ------------------------------------
library(tableone)
myVars <- c("var1","var2") ### create list of variables to include intable
# Make sure any categorical variables are saved as factors
MyData$var2 <- as.factor(MyData$var2)
# package says to pass a list of categorical variables separately but this doesn't seem to work
# passing categorical variables in the main variable list works, so long as they are saved as factors
@mathzero
mathzero / elo.R
Created April 29, 2020 08:30
Elo score calculator functions
### Functions for calculating Elo probabilities and scores ###
# Uses the weighted Elo formula created by 538, with a K of 250, a shape of 0.4 and an offset of 5
# https://fivethirtyeight.com/features/how-were-forecasting-the-2016-us-open/
### Define a function that calculates probabilities on the basis of Elo score inputs
#p1elo is p1's current Elo score
@mathzero
mathzero / scientific_notation.R
Last active May 4, 2020 19:05
A short function to convert numeric values to scientific notation, eg 0.013 –> 1.3 x 10^-2
### define function for conversion to scientific notation of p-values or other numeric values
scientific_notation <- function (values, digits = 1)
{
if (!is.numeric(values)) {
stop("values must be numbers")
}
if (grepl("^\\d{2}$", digits)) {
stop("digits must a one or two digit whole number")
}
x <- sprintf(paste0("%.", digits, "e"), values)
@mathzero
mathzero / install_package_list.R
Last active May 10, 2020 18:04
Snippet of code to include at start of a script to install and load any necessary packages
### Install packages
list.of.packages <- c("citr", "colourpicker", "rcrossref", "jpeg","RCurl") ### add all the packages that are used in your script here
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] ### check if they are installed
if(length(new.packages)) install.packages(new.packages) ### install any that need installing
### Load packages
lapply(list.of.packages, require, character.only = TRUE)
@mathzero
mathzero / pheatmap_save.R
Created June 14, 2020 09:15
Save pheatmap function (png or pdf)
### slightly adapted from here to save png or pdf and specify resolution etc: https://stackoverflow.com/questions/43051525/how-to-draw-pheatmap-plot-to-screen-and-also-save-to-file
# function detects '.png' or '.pdf' in the declared filename and assigns that file type
### pheatmap save function
save_pheatmap <- function(x, filename, width=12, height=12){
stopifnot(!missing(x))
stopifnot(!missing(filename))
if(grepl(".png",filename)){
png(filename, width=width, height=height, units = "in", res=300)
@mathzero
mathzero / pheatmap_save.R
Created June 14, 2020 09:15
Save pheatmap function (png or pdf)
### slightly adapted from here to save png or pdf and specify resolution etc: https://stackoverflow.com/questions/43051525/how-to-draw-pheatmap-plot-to-screen-and-also-save-to-file
# function detects '.png' or '.pdf' in the declared filename and assigns that file type
### pheatmap save function
save_pheatmap <- function(x, filename, width=12, height=12){
stopifnot(!missing(x))
stopifnot(!missing(filename))
if(grepl(".png",filename)){
png(filename, width=width, height=height, units = "in", res=300)
@mathzero
mathzero / stability_analysis.R
Last active July 9, 2020 09:52
Penalised regression stability analysis
### prep data
Xdata <- data %>% select(-outcome) %>% as.matrix()
Ydata <- as.matrix(data$outcome)
### definte stability function
LassoSub=function(k=1, Xdata, Ydata){
set.seed(k)
s=sample(nrow(data), size=0.8*nrow(data))
Xsub=Xdata[s, ]
Ysub=Ydata[s]