R, Julia, SQL, Octave and others: Personal notes on data analysis, computation, data access most especially for querying voter history, Census, PDC, and other election data. Reader is advised to just paste the code text into Notepad++.
Wednesday, December 16, 2015
Defense Indexes vs. Mass Shooting
Political post here.
# Code to plot Defense indexes vs. Mass Shooting Tracker data from this post which
# you will need to get the 'GV' ('Gun Violence') data.
# You will also need a Quandl account (free) with API number.
# You will probably need to install these packages first
# e.g. install.packages()
library(lubridate)
library(zoo)
library(tseries)
library(stringi)
# install.packages("devtools")
library(devtools)
# install_github('quandl/R-package')
library(Quandl)
# Quandl.auth("[your auth code here")
Quandl.auth("tqu9yZSzGX2BJFS5aReL")
NAS2717 = Quandl("NASDAQOMX/NQUSB2717")
BEA3114Q = Quandl("BEA/NIPA_3_11_4_Q")
GOOGNYSEITA = Quandl("GOOG/NYSE_ITA")
colnames(NAS2717) <- c("Date","Index","High","Low","TMV","DMV")
s1 <- subset(NAS2717, ymd(Date) >= ymd('2013-01-01') & ymd(Date) <= ymd('2015-12-14'))
with(arrange(s1,Date),plot(Index ~ ymd(Date),ylim=c(400,3250),col="purple"))
with(subset(GV,(Dead + Injured) >= 14),lines(lowess(((Dead + Injured) * 100) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
with(GV,lines((Dead + Injured) * 100 ~ ymd(Date)))
with(GV,lines(lowess(((Dead + Injured) * 100) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
abline(h=1400,col="red")
mtext("'NASDAQOMX/NQUSB2717' Defense Index (purple) vs. (100x scaled) Mass Shooting Tracker Database",line=-1)
mtext("Lowess >= 14 'Dead + Injured' (top) and lowess 'over all time' (bottom) in Blue", line=-2)
mtext("ABLINE >= 14 'Dead + Injured' (red)",line=-3)
s1 <- subset(GOOGNYSEITA, ymd(Date) >= ymd('2013-01-01') & ymd(Date) <= ymd('2015-12-14'))
with(arrange(s1,Date),plot((Close * 2) ~ ymd(Date),ylim=c(40,325),col="purple"))
with(subset(GV,(Dead + Injured) >= 14),lines(lowess(((Dead + Injured) * 10) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
with(GV,lines((Dead + Injured) * 10 ~ ymd(Date)))
with(GV,lines(lowess(((Dead + Injured) * 10) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
abline(h=140,col="red")
mtext("'GOOG/NYSE_ITA' Defense ETF (purple) vs. (10x scaled) Mass Shooting Tracker Database",line=-1)
mtext("Lowess >= 14 'Dead + Injured' (top) and lowess 'over all time' (bottom) in Blue", line=-2)
mtext("ABLINE >= 14 'Dead + Injured' (red)",line=-3)
s1 <- subset(BEA3114Q, ymd(Date) >= ymd('2012-12-01') & ymd(Date) <= ymd('2015-12-14'))
s1 <- s1[,1:2]
colnames(s1) <- c("Date","Expend")
with(arrange(s1,Date),plot((Expend * 2) ~ ymd(Date),ylim=c(0,325)))
with(subset(GV,(Dead + Injured) >= 14),lines(lowess(((Dead + Injured) * 10) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
with(GV,lines((Dead + Injured) * 10 ~ ymd(Date)))
with(GV,lines(lowess(((Dead + Injured) * 10) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
abline(h=140,col="red")
# Charts
# Chart creation Macro
jpeg_create <- function() {
systime <- as.numeric(Sys.time())
# dev.new()
jpeg(filename = systime,
width = 1224, height = 968, units = "px", pointsize = 16,
quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
type = c("windows"))
Sys.sleep(2)
}
jpeg_create()
colnames(NAS2717) <- c("Date","Index","High","Low","TMV","DMV")
s1 <- subset(NAS2717, ymd(Date) >= ymd('2013-01-01') & ymd(Date) <= ymd('2015-12-14'))
with(arrange(s1,Date),plot(Index ~ ymd(Date),ylim=c(400,3250),col="purple"))
with(subset(GV,(Dead + Injured) >= 14),lines(lowess(((Dead + Injured) * 100) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
with(GV,lines((Dead + Injured) * 100 ~ ymd(Date)))
with(GV,lines(lowess(((Dead + Injured) * 100) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
abline(h=1400,col="red")
mtext("'NASDAQOMX/NQUSB2717' Defense Index (purple) vs. (100x scaled) Mass Shooting Tracker Database",line=-1)
mtext("Lowess >= 14 'Dead + Injured' (top) and lowess 'over all time' (bottom) in Blue", line=-2)
mtext("ABLINE >= 14 'Dead + Injured' (red)",line=-3)
jpeg_create()
s1 <- subset(GOOGNYSEITA, ymd(Date) >= ymd('2013-01-01') & ymd(Date) <= ymd('2015-12-14'))
with(arrange(s1,Date),plot((Close * 2) ~ ymd(Date),ylim=c(40,325),col="purple"))
with(subset(GV,(Dead + Injured) >= 14),lines(lowess(((Dead + Injured) * 10) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
with(GV,lines((Dead + Injured) * 10 ~ ymd(Date)))
with(GV,lines(lowess(((Dead + Injured) * 10) ~ ymd(Date)),type="l",col="blue",lwd=2.0))
abline(h=140,col="red")
mtext("'GOOG/NYSE_ITA' Defense ETF (purple) vs. (10x scaled) Mass Shooting Tracker Database",line=-1)
mtext("Lowess >= 14 'Dead + Injured' (top) and lowess 'over all time' (bottom) in Blue", line=-2)
mtext("ABLINE >= 14 'Dead + Injured' (red)",line=-3)
No comments:
Post a Comment