This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--------------------------------------------------------------------------------------# | |
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=============================================================================================== | |
#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 | |
#=============================================================================================== |