Skip to content

Instantly share code, notes, and snippets.

@ZeccaLehn
ZeccaLehn / anonymousR
Last active May 3, 2017 01:32
Setting Variables in an R Anonymous Function
> function(){x <- 1}
function(){x <- 1}
> x
Error: object 'x' not found
> y = 3
> (function(x = 5 + y) x)()
[1] 8
> x
Error: object 'x' not found
@ZeccaLehn
ZeccaLehn / exponentialRegression.r
Last active May 2, 2017 02:02
Exponential Regression Test in R
# Data From https://stats.stackexchange.com/questions/64927/how-do-i-plot-an-exponential-curve-in-r
x = c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,2,1,2,2,1,2,1,2,2,2,2,2,1,2,2,
1,3,1,3,1,2,1,2,2,3,3,2,3,3,3,2,3,3,2,1,5,5,1,4,3,
4,3,3,5,7,2,4,6,4,4,3,6,5,5,3,4,3,4,7,5,7,11,3,6,
6,6,9,1,1,3,9,3,4,6,6,12,6,2,12,8,2,4,12,6,8,9,6,
5,4,4,11,4,18,8,10,8,17,5,16,16,6,5,18,3,1,10,2,
11,21,8,11,15,9,13,14,3,11,17,18,9,16,17,12,18,2,
9,17,14,14,5,21,21,13,17,15,9,1,18,17,7,2,17,23,22,
@ZeccaLehn
ZeccaLehn / setExamples.r
Last active May 3, 2017 00:36
Set logic in R
# Starting Order
> c(1, 2) %in% c(1,3,5) # Indexes left side
[1] TRUE FALSE
> c(1, 2) == c(1,3,5) # Same as above, but indexes right side
[1] TRUE FALSE FALSE
> ! c(1, 2) %in% c(1,3,5)
[1] FALSE TRUE
> union(c(1, 2), c(1,3,5))
[1] 1 2 3 5
> intersect(c(1, 2), c(1,3,5))
@ZeccaLehn
ZeccaLehn / ApplyMultiple.r
Last active January 11, 2017 19:32
Return Multiple Objects with Apply Function in R -- Using Empirical Bayesian Baseball Example
# Data Sampled from Bayesian AB Testing Article: http://varianceexplained.org/r/credible_intervals_baseball/
hit_table <- data.frame(hits = c(0, 0, 4, 9, 28, 30), at_bat = c(3, 6, 10, 41, 130, 139))
propTable <- function(inputData = inputData, correction = TRUE){
prop_return <- apply(inputData, 1, function(x){
prop_row <- prop.test(x[1], x[2], conf.level = 0.95, correct = correction)
@ZeccaLehn
ZeccaLehn / MultipleSecurityD3.r
Last active February 18, 2017 16:50
Multiple Security Downloader with D3.js Plot
###### Multiple Security Downloader with D3.js Plot ######
library(quantmod)
# require(devtools)
# install_github('rCharts', 'ramnathv')
library(rCharts)
from = "2015-01-01"
to = "2016-01-01"
@ZeccaLehn
ZeccaLehn / dynamicStringSetting.R
Created December 19, 2016 01:33
[R] Setting variables dynamically via strings within a loop
# Static variable setting
eval(parse(text="adder1 <- 1 + 3; adder2 <- 1 + 4"))
adder1 # [1] 4
adder2 # [1] 5
# Dynamic variable setting
for(i in 1:2){
print(paste0("adder", i, " <- ", i + 1))
eval(parse(text = paste0("adder", i, " <-", i + 1)))
}
@ZeccaLehn
ZeccaLehn / posixRoundTrip.r
Created November 23, 2016 21:25
POSIXct Roundtrip (Date Conversions)
Sys.time()
# [1] "2016-11-23 21:22:46 UTC"
as.POSIXct( as.numeric(Sys.time()), origin = "1970-01-01" )
# [1] "2016-11-23 21:22:46 UTC"
@ZeccaLehn
ZeccaLehn / laggedTicker.ipynb
Created September 9, 2016 18:13
[R] Adjusted Returns with Checked Lag: Yahoo Finance Downloader (w/VIX)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ZeccaLehn
ZeccaLehn / balticDry_SP500.ipynb
Created September 9, 2016 03:42
[R] Baltic Dry Index (BDI) versus SP500 Lagged Changes -- Lag Theory Analysis
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ZeccaLehn
ZeccaLehn / tickerDownloads.ipynb
Last active August 17, 2016 01:03
[R] Download All Tickers from both Google and Yahoo Finance in Quandl API
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.