Skip to content

Instantly share code, notes, and snippets.

@infotroph
infotroph / soft-censor.R
Created November 17, 2015 20:07
Is "soft" censoring a thing?
# Testing a soft-left-censored Stan model:
# Values above some minimum are detected normally,
# values less than minimum go detected with some probability > 0.
set.seed(2345767)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = 7)
sim_mu = 3
library(ggplot2)
library(dplyr)
library(zoo) # for rollmean()
d_mean = (diamonds
%>% mutate(price=round(price, -2))
%>% group_by(price)
%>% mutate(mean_x = mean(x)))
d_roll = (diamonds
@infotroph
infotroph / sim_logis.R
Last active November 30, 2015 20:22
This is a visual approach to evaluating whether my logistic regression estimates are close to the simulated values. Can I instead compute scale and location directly from glm estimates?
set.seed(254469)
n=100
xlo=0
xhi=20
loc_logis=5
scale_logis=3
x = runif(n, xlo, xhi)
p_detect = plogis(x, location=loc_logis, scale=scale_logis)
@infotroph
infotroph / sim_mixdist.R
Created December 14, 2015 22:04
Simulating data from a mixed distribution of zeroes (nondetection) or lognormal positive values (root volume when we detect any)
sim_mixdist = function(
n_tubes=1,
depths=1:3,
intercept=1, # E[y] at depth=0 (log scale)
b_depth=-1, # slope for depth (log scale)
sig_tube=1, # sd for N(0) tube offsets (log scale)
detect_loc=1, # intercept for detection logistic. (scale... same as mu?)
detect_scale=1, # slope for detection logistic. (scale?)
sigma=1){ # residual (log scale)
@infotroph
infotroph / gist:ec031ff026b064879dc0
Created January 20, 2016 21:46
Unwanted ggplot2 update from running install_github(...)
R version 3.2.2 (2015-08-14) -- "Fire Safety"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)
> packageVersion("ggplot2")
[1] ‘1.0.1’
> library(ggplotTicks)
Error in library(ggplotTicks) : there is no package called ‘ggplotTicks’
> library(devtools)
@infotroph
infotroph / gist:7675ccacdfff28cd64ac
Created January 26, 2016 21:43
Controlling new column names from mutate_each?
# Sample data: 2x2 manipulation, replicated 4x, multiple responses measured repeatedly over time.
# The response variables aren't related: y_also doesn't come "after" y_1 in any meaningful sense.
df = expand.grid(
day=1:3,
replicate=1:4,
t1=c("ctrl", "more"),
t2=c("ambient", "manipulated"))
df$y_1 = rnorm(48)
df$y_other = runif(48)
df$y_also = rlnorm(48)
strs = c("nomatch", "Odum 1969", "2001FACE_3b", "1800", "sdfghj1928qwer")
strs_yrs = gsub(".*((19|20)[0-9]{2}).*", "\\1", strs)
strs_yrs
# [1] "nomatch" "1969" "2001" "1800" "1928"
# Oops, strings with no year (or year from wrong century) are returned unchanged! Let's remove them separately:
strs_haveyr = grepl("(19|20)[0-9]{2}", strs)
strs_yrs[!strs_haveyr] = ""
strs_yrs
# [1] "" "1969" "2001" "" "1928"
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.3 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets base
@infotroph
infotroph / do_not_attempt.R
Last active March 12, 2016 21:39
Just getting some bad aesthetic decisions out of my system while working out how to do a different thing. Might be "useful" for teaching ggplot themes?
anycolors = function(n)rgb(runif(n), runif(n), runif(n))
color_grid = function(x, y, nx=10, ny=nx){
g = expand.grid(
x=seq(min(x), max(x), by=diff(range(x))/nx),
y=seq(min(y), max(y), by=diff(range(y))/ny))
g$color = anycolors(nrow(g))
g
}
@infotroph
infotroph / gist:c288c39e260548c9d34c
Created March 24, 2016 20:07
A possibly terrible approach to plotting across year boundaries
library(ggplot2)
library(dplyr)
library(lubridate)
dates = c(
seq(from=as.Date("2014-10-01"), to=as.Date("2015-05-30"), by="days"),
seq(from=as.Date("2015-10-01"), to=as.Date("2016-05-30"), by="days"))
Tmin = rnorm(length(dates), mean=10*sin((yday(dates) - 90)/365*2*pi))
Tmax = Tmin + rnorm(length(dates), mean=5)