This file contains hidden or 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
# 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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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) |
This file contains hidden or 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
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) | |
This file contains hidden or 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
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) |
This file contains hidden or 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
# 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) |
This file contains hidden or 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
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" |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
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) |