Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
@jaehyeon-kim
jaehyeon-kim / downloadStockData
Created November 19, 2014 01:54
download NASDAQ stock data
# assumes codes are known beforehand
codes <- c("MSFT", "1234")
urls <- paste0("http://www.google.com/finance/historical?q=NASDAQ:",
codes,"&output=csv")
paths <- paste0(codes,"csv")
missing <- !(paths %in% dir(".", full.name = TRUE))
missing
# simple error handling in case file doesn't exists
downloadFile <- function(url, path, ...) {
@jaehyeon-kim
jaehyeon-kim / downloadMergeStockData
Created November 19, 2014 09:54
download and merge NASDAQ stock data
library(lubridate)
library(data.table)
library(plyr)
library(dplyr)
# create data folder
dataDir <- paste0("data",format(Sys.Date(),"_%Y_%m_%d"))
if(file.exists(dataDir)) {
unlink(dataDir, recursive = TRUE)
dir.create(dataDir)
library(lubridate)
library(stringr)
library(reshape2)
library(plyr)
library(dplyr)
# create data folder
dataDir <- paste0("data","_",format(Sys.Date(),"%Y-%m-%d"))
if(file.exists(dataDir)) {
unlink(dataDir, recursive = TRUE)
library(knitr)
library(lubridate)
library(stringr)
library(reshape2)
library(plyr)
# create data folder
dataDir <- paste0("data","_",format(Sys.Date(),"%Y-%m-%d"))
if(file.exists(dataDir)) {
unlink(dataDir, recursive = TRUE)
## set up variables
size <- 36000
numUsers <- 4900
# roughly each user has 7 sessions
numSessions <- (numUsers / 7) - ((numUsers / 7) %% 1)
## create data frame
set.seed(123457)
userIds <- sample.int(numUsers, size=size, replace=TRUE)
ssIds <- sample.int(numSessions, size=size, replace=TRUE)
MyData <- data.frame(Year=seq(2000,2009))
# year variables
dim <- length(MyData$Year)
yMat <- matrix(NA, nrow=dim, ncol=dim)
for(i in 1:dim) {
yMat[i,] <- rbind(t(MyData)) # transpose
}
yMat
@jaehyeon-kim
jaehyeon-kim / getUpdatedCM.R
Last active August 29, 2015 14:15
getUpdatedCM aims to extend a confusion matrix
#
# getUpdatedCM aims to extend a confusion matrix
# by adding model errors to the last column and use errors to the last row.
#
# Usage
# getUpdatedCM(cm)
#
# last modified on Feb 8, 2015
#
#
# updateCM aims to extend a confusion matrix
# by adding model errors to the last column and use errors to the last row.
# Both square and non-square matrices are supported
#
# Usage
# set.seed(1)
# actual = ifelse(rnorm(100,0,1)<0,"low","high")
# fitted = ifelse(rnorm(100,1,5)<0,"low","high")
# updateCM(table(actual,fitted))
#
# 4 constructor functions are included and they create S3 objects of the CART model
# by the rpart, caret and mlr packages - the last two extends the first. They are
#
# - cartRPART()
# - cartCARET()
# - cartMLR()
# - cartDT()
#
# Both classification and regression are supported. Together with the relevant packages,