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
### FORKED FROM: https://gist.github.com/cavedave/8ff22e94c882e9f6be933f99f2ae0b50 | |
## Charles changed: | |
## Absolute URLs to library/data (rather than local files) | |
## Raster rather than tiles (didn't like the grid lines) | |
## Gradient from red (hot) to blue (cold) with black at zero (no change from average) | |
## Removed year filter | |
## Defined title and breaks via automatic extraction of dates from data | |
## Scaled down plot to be more friendly for Gist | |
## Took out unused libraries |
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
## It's a real pain to deal with deep-linking to Gists (eg sourcing raw code), so I'm working on this | |
## in GitHub proper: | |
## https://github.com/maptracker/RandomR/blob/master/RefClassHeadaches.R | |
## Quickly source this code: | |
## source("https://gist.github.com/maptracker/00f2c18383e844c3f4a0886b7897ceb7/raw/532410cf19b4df6f9949bdc9ec1dd0e9d13ef898/RefClassHeadaches.R") |
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
# Fork of Scott's coin flip simulation | |
# https://gist.github.com/maptracker/e34a8596a9c2a7f261d6b892e5df42c5 | |
flipCoin <- function( n = 50, pvals = 0.5, nosim = 100, file = NULL ) { | |
coverage <- lapply(pvals, function(p) { | |
phats <- (rbinom(nosim, prob = p, size = n) + 2)/(n + 4) | |
ll <- phats - qnorm(0.975) * sqrt(phats * (1 - phats)/n) | |
ul <- phats + qnorm(0.975) * sqrt(phats * (1 - phats)/n) | |
cbind(lower = ll, upper = ul) | |
})[[1]] # De-listify |
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
## Generate a markdown table of permuted peer assignments | |
## Designed for putting into output into GitHub Wiki | |
## Permutation code | |
source("https://gist.github.com/maptracker/f0ec01bed4d1c1583bf6/raw/a9cbd9983703a293584e826d903f21a8556e41a3/StudentPeerReview.R") | |
makeMarkdownTable <- function | |
(file, peers = 2, out = paste(c(file,"md"), collapse = '.'), | |
subtitle = NULL, |
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
## Subset of arrest data for murder and assault in the 50 US states | |
ma <- USArrests[, c("Murder", "Assault")] | |
## (FWIW, I don't think this represents a meaningful clustering of states) | |
## Cluster | |
mahc <- hclust(dist(ma), "ave") | |
## Cut into 6 groups | |
ct <- cutree(mahc, k = 6) | |
## Get the order of the states along the x axis | |
xord = mahc$labels[ mahc$order ]; | |
## Plot the dendrogram |
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
## Code was initially taken From Coursera lecture: | |
## https://www.coursera.org/learn/exploratory-data-analysis/lecture/6hOqi/k-means-clustering-part-2 | |
set.seed(1234) | |
## Generate 30 points randomly assorted around 3 centroids: (1,1), (2,2), (3,1) | |
numPoints <- 30 | |
x <- rnorm(numPoints, mean = rep(1:3, each = numPoints/3), sd = 0.2) | |
y <- rnorm(numPoints, mean = rep(c(1, 2, 1), each = numPoints/3), sd = 0.2) | |
## Organize as DF | |
dataFrame <- data.frame(x = x, y = y, row.names = 1:numPoints) |
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
### Explore the colors used by R, as hex codes | |
## The default palette, a vector of strings (like "green2") | |
myPal <- palette() | |
## Find the RGB codes for the palette, a red/blue/green matrix of 0-255: | |
myRGB <- col2rgb( myPal ) | |
## Convert to hex codes: | |
myHex <- apply( myRGB, 2, function (x) { | |
rgb(x[1], x[2], x[3], maxColorValue = 255) | |
} ) |
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
# From @Greg Snow https://stackoverflow.com/a/3740473 | |
# Plots a grid with default tokens on bottom two rows, characters in others | |
png(filename="GregSnow.png", width = 600, height = 600) | |
plot( 0:15, 0:15, type='n', main = "pch values by Greg Snow" ) | |
points( (0:255)%% 16, (0:255) %/% 16, pch=0:255, font=5, cex = 2 ) | |
dev.off() | |
# From the R help for ?points | |
png(filename="PointsHelp.png", width = 600, height = 600) | |
pchShow <- |
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 UAH satellite data to demonstrate faceting and smoothing | |
library(ggplot2) | |
## There are a bunch of comments at the end of the file, take first | |
## 443 rows (up to Oct 2015) | |
numRow <- 443 | |
url <- "http://vortex.nsstc.uah.edu/data/msu/t2lt/uahncdc_lt_5.6.txt" | |
uah <- read.delim(url, sep = "", nrows = numRow) | |
## Assign month names as a factor |
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
# Code to make random matrices: | |
source("https://gist.github.com/maptracker/07390983253758614ecc/raw/5995a7c1112420b64ff33dcb1e7dac679d05dbf9/randomMatrix.R") | |
library("microbenchmark") | |
library("ggplot2") | |
# Iterate 1000 times over solving three sizes of matrices | |
bench <- microbenchmark( "10x10" = solve( randomMatrix( 10 ) ), | |
"20x20" = solve( randomMatrix( 20 ) ), | |
"50x50" = solve( randomMatrix( 50 ) ), | |
times = 1000 ) | |
# Plot results: |
NewerOlder