This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load ggplot2 | |
library(ggplot2) | |
# Load example data | |
data(mtcars) | |
# Create a character vector of car names | |
mtcars$name <- row.names(mtcars) | |
# Plot car names by mpg | |
ggplot(mtcars, aes(x = name, y = mpg)) + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################################### | |
## Title: Regression Module Assignment 1 | |
## Exploring Student Composition Effects on Test Score Growth | |
## Author: Jared E. Knowles, Civilytics Consulting | |
## Date: 6/12/2019 | |
## Last Updated: 6/17/2019 | |
################################################################################################### | |
# ---------------------------------------------------------------------------- | |
# Load the data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# Functions to find the data | |
################################################################################ | |
# Finders | |
# Simple functions that take the ID code from CPE (e.g. 49-00039) and look up | |
# the respective data for it in the structure provided in teh competition | |
find_police_shape <- function(dept_id, kaggle_kernel = FALSE) { | |
if(kaggle_kernel == TRUE) { | |
prefix = "../input/cpe-data/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################### | |
## SDP Fall Workshop Predictive Analytics | |
## Advanced / Additional Code Snippets for Working with PA Data and Models | |
## Author: Jared E. Knowles | |
## Date: 09/14/2018 | |
## You do not need to use all or even any of this code. The code does not need to | |
## be run together. This is just a survey of some additional techniques/tricks you | |
## can do in R to make explaining predictive models and complex data easier. | |
## As always - your needs and approaches may different. | |
################################################################################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Additional tools for machine learning and predictive analytics in stata | |
/* | |
Author: Jared Knowles | |
Date: 09/12/2018 | |
Purpose: Survey of some additional code helpful in conducting and explaining | |
or demonstrating predictive analytics to stakeholders. | |
You do not need to run all of this code - this is a survey of commands that | |
tackle different techniques. Pick and choose what might be most useful to you. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using the dataUSA.io API for Census Data in R | |
This gist contains some notes on constructing a query for census and economic data from the [DataUSA.io](http://datausa.io/) site. This is a quick-start guide to their API; for in-depth documentation check out their [API documentation](https://github.com/DataUSA/datausa-api/wiki/Overview). | |
A great way to learn how to structure a query is to visit a specific datausa.io page and click on the "Options" button on top of any graph, then select "API" to see the query syntax that created the graph. | |
 | |
## Example Use |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
predict.robust <- function(model, data, robust_vcov = NULL, level = 0.95, | |
interval = "prediction"){ | |
# adapted from | |
# https://stackoverflow.com/questions/38109501/how-does-predict-lm-compute-confidence-interval-and-prediction-interval | |
# model is an lm object from r | |
# data is the dataset to predict from | |
# robust_vcov must be a robust vcov matrix created by V <- sandwich::vcovHC(model, ...) | |
# level = the % of the confidence interval, default is 95% | |
# interval = either "prediction" or "confidence" - prediction includes uncertainty about the model itself | |
if(is.null(robust_vcov)){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate the AUC of a GLM model easily | |
# Jared Knowles | |
# model = a fitted glm in R | |
# newdata = an optional data.frame of new fitted values | |
auc.glm <- function(model, newdata = NULL){ | |
if(missing(newdata)){ | |
resp <- model$y | |
# if(class(resp) == "numeric"){ | |
# resp <- factor(resp) | |
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helper functions that allow string arguments for dplyr's data modification functions like arrange, select etc. | |
# Author: Sebastian Kranz | |
# Examples are below | |
#' Modified version of dplyr's filter that uses string arguments | |
#' @export | |
s_filter = function(.data, ...) { | |
eval.string.dplyr(.data,"filter", ...) | |
} |
NewerOlder