Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / garage-door-opener.R
Last active April 3, 2016 18:45
[2016-03-28] Challenge #260 [Easy] Garage Door Opener
#
# [2016-03-28] Challenge #260 [Easy] Garage Door Opener
# https://www.reddit.com/r/dailyprogrammer/comments/4cb7eh/20160328_challenge_260_easy_garage_door_opener/
# Demo at http://www.r-fiddle.org/#/fiddle?id=83RtwcPP&version=1
#
# Kory Becker 4/3/2016
# http://primaryobjects.com
#
# Valid states and commands.
@primaryobjects
primaryobjects / ctypist.R
Created April 4, 2016 02:01
[2016-03-21] Challenge #259 [Easy] Clarence the Slow Typist
#
# [2016-03-21] Challenge #259 [Easy] Clarence the Slow Typist
# https://www.reddit.com/r/dailyprogrammer/comments/4bc3el/20160321_challenge_259_easy_clarence_the_slow/
# Demo at http://www.r-fiddle.org/#/fiddle?id=JwY7VC2k&version=1
#
# Kory Becker 4/3/2016
# http://primaryobjects.com
#
keypad <- data.frame(c(1, 4, 7, 10),
@primaryobjects
primaryobjects / magic-square.R
Created April 5, 2016 02:37
[2016-04-04] Challenge #261 [Easy] verifying 3x3 magic squares
#
# [2016-04-04] Challenge #261 [Easy] verifying 3x3 magic squares
# https://www.reddit.com/r/dailyprogrammer/comments/4dccix/20160404_challenge_261_easy_verifying_3x3_magic/
# Demo at http://www.r-fiddle.org/#/fiddle?id=AgVxiDgU&version=1
#
# Kory Becker 4/4/2016
# http://primaryobjects.com
#
magicSquare <- function(input) {
@primaryobjects
primaryobjects / wordToNumber.js
Created April 11, 2016 18:21
Method for converting a natural language ordinal to a numerical value. Given an array, first = 1, second = 2, last = arr.length
CommonManager = {
wordToNumber: function(word, arr) {
var dict = {
'first': 1,
'second': 2,
'third': 3,
'fourth': 4,
'fifth': 5,
'sixth': 6,
'seventh': 7,
@primaryobjects
primaryobjects / linear-regression.Rmd
Last active November 13, 2018 02:51
Single variable linear regression, calculating baseline prediction, SSE, SST, R^2 of the model.
---
title: "Single Variable Linear Regression R^2"
output: html_document
---
The following figure shows three data points and the best fit line:
y = 3x + 2.
The x-coordinate, or "x", is our independent variable and the y-coordinate, or "y", is our dependent variable.
@primaryobjects
primaryobjects / nba.R
Created April 25, 2016 17:20
NBA dataset analysis and linear regression prediction.
NBA <- read.csv('nba_train.csv')
# Seems around 42 wins, the team has a good chance of making it to the playoffs.
table(NBA$W, NBA$Playoffs)
NBA$PTSdiff <- NBA$PTS - NBA$oppPTS
WinsReg <- lm(W ~ PTSdiff, data = NBA)
PointsReg <- lm(PTS ~ X2PA + X3PA + FTA + AST + ORB + DRB + TOV + STL + BLK, data=NBA)
SSE <- sum(PointsReg$residuals^2)
@primaryobjects
primaryobjects / climate_change.R
Created April 25, 2016 17:21
Climate change dataset analysis, using linear regression.
data <- read.csv('climate_change.csv')
train <- subset(data, Year <= 2006)
test <- subset(data, Year > 2006)
fit <- lm(Temp ~ MEI + CO2 + CH4 + N2O + CFC.11 + CFC.12 + TSI + Aerosols, data=train)
fit2 <- lm(Temp ~ MEI + TSI + Aerosols + N2O, data=train)
# Auto-create model (note, step does not address the collinearity of the variables, so some features removed in fit2 are present in this model).
fit3 <- step(fit)
@primaryobjects
primaryobjects / pisa.R
Last active April 25, 2016 18:02
PISA dataset analysis with linear regression.
train <- read.csv('pisa2009train.csv')
test <- read.csv('pisa2009test.csv')
tapply(train$readingScore, train$male, mean)
# Which columns have an NA value?
which(unlist(lapply(train, function(x) any(is.na(x)))))
# Remove missing values.
train <- na.omit(train)
@primaryobjects
primaryobjects / fluTrend.R
Created April 25, 2016 19:28
Flu trend analysis with linear regression and time-series features.
train <- read.csv('flutrain.csv')
test <- read.csv('flutest.csv')
train[order(train$ILI),]
train[order(train$Queries),]
hist(train$ILI)
plot(log(train$ILI), train$Queries)
@primaryobjects
primaryobjects / logisticregressionintro.Rmd
Created April 27, 2016 14:26
Logistic regression intro formulas.
---
title: "Logistic Regression Intro"
output: html_document
---
Suppose the coefficients of a logistic regression model with two independent variables are as follows:
B0 = -1.5
B1 = 3
B2 = -0.5