Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / parity.R
Created January 15, 2016 20:38
Calculating parity (even or odd number of 1 bits) with a neural network.
## Including the required R packages.
packages <- c('caret', 'RSNNS')
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
#
library(caret)
library(RSNNS)
@primaryobjects
primaryobjects / classifytext.R
Last active August 9, 2020 19:54
Simple example of classifying text in R with machine learning (text-mining library, caret, and bayesian generalized linear model). Classify. tfidf tdm term document matrix
library(caret)
library(tm)
# Training data.
data <- c('Cats like to chase mice.', 'Dogs like to eat big bones.')
corpus <- VCorpus(VectorSource(data))
# Create a document term matrix.
tdm <- DocumentTermMatrix(corpus, list(removePunctuation = TRUE, stopwords = TRUE, stemming = TRUE, removeNumbers = TRUE))
@primaryobjects
primaryobjects / index.html
Created January 27, 2016 22:04
RepoTagger feature request: automatic parsing of keywords from repo description.
<script src="js/stopwords.js"></script>
@primaryobjects
primaryobjects / server.R
Created February 2, 2016 22:26
Diabetes risk calculator. First shiny app made in R.
## Including the required R packages.
packages <- c('shiny')
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
library(shiny)
shinyServer(function(input, output) {
submit <- FALSE
@primaryobjects
primaryobjects / manipulate.R
Last active February 3, 2016 02:21
Using manipulate on R chart to create an interactive chart.
## Including the required R packages.
packages <- c('manipulate', 'UsingR')
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
library(manipulate)
library(UsingR)
myHist <- function(mu) {
@primaryobjects
primaryobjects / data-products-q1.R
Last active November 25, 2016 05:39
Coursera Developing Data Products - Quiz 1
# Q1
# Consider the following code for the cars data set
# This function plots distance versus speed, each de-meaned and an associated line of slope s.
library(manipulate)
myPlot <- function(s) {
plot(cars$dist - mean(cars$dist), cars$speed - mean(cars$speed))
abline(0, s)
}
# Which of the following code will make a manipulate plot that creates a slider for the slope?
# A
@primaryobjects
primaryobjects / read-xlsx.R
Created February 4, 2016 16:40
Reading exported monthly xlsx files. List of .csv.xlsx files would exist in /data folder, named in the form January.csv.xlsx, February.csv.xlsx, etc.
## Including the required R packages.
packages <- c('openxlsx')
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
library(openxlsx)
data <- data.frame()
x <- sapply(month.name, function(month) {
@primaryobjects
primaryobjects / data-products-q2.R
Last active October 20, 2020 18:50
Coursera Developing Data Products - Quiz 2
---
title: "Developing Data Products - Quiz 2"
output: html_document
---
Q1
===
In the slidify YAML text. Changing the framework from io2012 to shower does what?
@primaryobjects
primaryobjects / data-products-q3.R
Created February 5, 2016 19:25
Coursera Developing Data Products - Quiz 3
# Coursera - Developing Data Products - Quiz 3
# Q1
# Which of the following items is required for an R package to pass R CMD check without any warnings or errors?
# A
# DESCRIPTION file
# Q2
# Which of the following is a generic function in a fresh installation of R, with only the default packages loaded?
@primaryobjects
primaryobjects / energy-jnk-hyg.R
Created February 12, 2016 20:33
Rough analysis JNK vs HYG energy holdings (really quick and dirty analysis).
# Analysis of JNK holdings.
data <- read.csv('jnk-holdings.csv', skip=3)
data <- data[!is.na(data$Coupon),]
# Clean names.
data$Name2 <- sub(' \\d.+', '', data$Name)
# Find energy holdings.
e <- grepl('ENERGY|WIND|OCEAN| OIL |DRILL |HALCON|OCEANICS| OFFSHORE |DRILLING|PETROLEUM| PETRO | COAL ', data$Name, ignore.case = TRUE)
energy <- data[which(e),]