Skip to content

Instantly share code, notes, and snippets.

View actuaryactually's full-sized avatar

actuaryactually

View GitHub Profile
@actuaryactually
actuaryactually / Splines.R
Created May 31, 2017 06:05
Fitting Cubic Splines
#FITTING SPLINES IN R
# D Menezes 20170531
#0. PRELIMINARIES
#install.packages("pacman") #only needed if you don't have it installed
pacman::p_load("splines")
#1. INPUT VALUES
input.x<-c(seq(from=0,to=120,by=12)) #assuming we have annual spline points
@actuaryactually
actuaryactually / QV_04_Plot_Locations.R
Created February 22, 2017 15:19
Quake Visualisations (QV)
pacman::p_load(ggmap,fitdistrplus,maps,mapproj,ggrepel)
wellington = c(lon = 174.7762, lat = -41.28646)
wellington.map=get_map(location=wellington,zoom=8,color="bw")
r <- ggmap(wellington.map,extent="panel",maprange=FALSE)%+% mydat + aes(x = long, y = lat)
r <- r + geom_density2d()
r <- r + stat_density2d(aes(fill = ..level.., alpha = ..level..), size = 0.1, bins = 100, geom = 'polygon')
r <- r + scale_fill_gradient(low = "green", high = "red") + scale_alpha(range = c(0.00, 0.25), guide = FALSE)
@actuaryactually
actuaryactually / QV_03_Download_Quake_Data.R
Last active February 22, 2017 16:18
Quake Visualisations
#PRELIMINARIES:
pacman::p_load(data.table)
mydat1 <- fread('http://quakesearch.geonet.org.nz/csv?region=wellington&minmag=1&startdate=2014-05-01&enddate=2017-1-30T17:00:00')
head(mydat)
#the following site contains historic earthquake data for New Zealand: http://quakesearch.geonet.org.nz/
#Using the build query option on this page, we can look-up quakes in specific regions and time frames.
#Let's look up Wellington's seismic profile since 1 Jan 2011
@actuaryactually
actuaryactually / QV_01_Scrape_Locations.R
Last active February 22, 2017 15:21
Quake Visualisations (QV)
#PRELIMINARIES:
install.packages("pacman")
pacman::p_load(XML,rvest,magrittr,RCurl,stringi)
#Step 1: Establish location names and addresses for Harvey Norman Electrical stores
#draw in address locations from website:
url<-"http://www.harveynorman.co.nz/store-finder.html"
rvest_target<-read_html(url)
rvest_table_nodes <- html_nodes(rvest_target,"p.address ")
@actuaryactually
actuaryactually / QV_02_Assign_LatLong.R
Last active February 22, 2017 15:22
Quake Visualisations (QV)
#PRELIMINARIES:
pacman::p_load(XML)
#recode as data frame :
output.addresses.df<-data.frame(c(output.addresses))
output.addresses.df<-cbind(output.addresses.df,long=1,lat=2)
#assign long/lat
for (i in 1:length(output.addresses)){
@actuaryactually
actuaryactually / 01_Scrape_Locations.R
Last active February 20, 2017 04:56
Quake Visualisations
#PRELIMINARIES:
install.packages("pacman")
pacman::p_load(XML,rvest,magrittr,RCurl,stringi)
#Step 1: Establish location names and addresses for Harvey Norman Electrical stores
#draw in address locations from website:
url<-"http://www.harveynorman.co.nz/store-finder.html"
#preliminaries:
wkg.dir = "C:/OldWorld/Blog/DataWrangling" # define where we'll work, save files, etc. for this post
setwd(wkg.dir)
#Install a helper piece of code (pacman) and then load required code libraries in one-go:
install.packages("pacman")
pacman::p_load(ChainLadder,ggplot2,data.table,tidyr)
#Step 1: load some data and view
RAA
plot(RAA/1e3,lattice=TRUE, main="RAA Data by Accident Year (USD000s)",xlab="Dev Yrs")
setwd("C:/OldWorld/Blog/DataWrangling")
#Output console results to file:
# anova test
anova_test = aov(yield ~ block + N * P + K, npk)
anova_test
# t test
t_test = t.test(1:10, y = c(7:20))
@actuaryactually
actuaryactually / fanplot_example.R
Last active November 18, 2016 14:06
Investment
#--------------------------------------------------------------------------------------#
#
# FANPLOT EXAMPLE
#
# Made famous by the BoE inflation report, fanplots are good for showing distributions where
# there is a time series. In this script I have created a toy dataset that looks like a dispersing time series
# to illustrate the fan syntax and .
# In future posts I'll show how to build a time-series using arbitrary data for an equity share
#
# D Menezes 20161111
@actuaryactually
actuaryactually / Monty_Hall.R
Last active September 21, 2015 05:20
Game Theory Switching Strategy
#===============================================================================================
#Monty Hall Problem - Game Theory around Choice
#Three options available. Two options have no prize, third option delivers a prize. You select one, and
#then the host of the game removes one of the losing options. You are then offered the chance to reselect
#For this code, I assume that you always switch from your first choice. It then examines the number of times
#you win based on this "always-switching" strategy
#===============================================================================================