Skip to content

Instantly share code, notes, and snippets.

View kenkellner's full-sized avatar

Ken Kellner kenkellner

View GitHub Profile
@kenkellner
kenkellner / server.R
Created May 17, 2021 17:04
Draft Shiny app
library(unmarked)
library(shiny)
library(shinymanager)
credentials <- data.frame(
user = ********,
password = *********,
stringsAsFactors = FALSE
)
cat("Applying occuMulti maxOrder patch\n")
fl_getY <- unmarked:::fl_getY
setMethod("fl_getY", "unmarkedFitOccuMulti", function(fit, ...){
maxOrder <- fit@call$maxOrder
if(is.null(maxOrder)) maxOrder <- length(fit@data@ylist)
getDesign(getData(fit), fit@detformulas, fit@stateformulas, maxOrder)$y
})
name_to_ind <- unmarked:::name_to_ind
@kenkellner
kenkellner / get_shapefile.R
Last active September 20, 2022 17:46
Function to download and/or uncompress shapefile data and read it into R
get_shapefile <- function(path){
temp_dir <- tempdir()
dest_dir <- tempfile()
dir.create(dest_dir)
if(grepl('http',path)){
dest <- paste0(dest_dir,"/",basename(path))
utils::download.file(path, dest, quiet=T)
path <- dest
}
@kenkellner
kenkellner / read_bufdata.R
Created September 6, 2018 16:30
Function to streamline process of reading in CSV and shapefile data from Buffalo Open Data Portal
#Streamline process of reading in CSV and Shapefile data
#from Buffalo Open Data Portal (via Socrata API)
#
#Requires RSocrata, tidyverse, and sf
#
#read_bufdata() with no arguments for a list of possible dataset titles
read_bufdata <- function(title=NULL, format='CSV'){
if ( ! format %in% c('CSV', 'csv', 'Shapefile', 'shapefile') ){
@kenkellner
kenkellner / parse.SEARCH.R
Last active May 13, 2016 20:40
Script to parse and summarize raw output from the SEARCH IBM program.
#Get a list of all individual text files
fnames <- list.files(pattern='\\.txt')
#Get number of individuals
ninds <- length(fnames)
#Make blank output data frame
output <- as.data.frame(matrix(NA,nrow=ninds,ncol=6))
names(output) <- c('ID','PctSafe','PctRisky','Switches','FinalState','Fate')
#There isn't really an easy way to do this, unfortunately.
#In your stated example, the populations are ordered, so you should be able to calculate the mean in JAGS like this:
Pop1.mean <- mean(data[1:3,1:10])
Pop2.mean <- mean(data[4:6,1:10])
#but maybe your actual dataset isn't organized that way, and even if it was you'd have to
#manually go through the dataset to figure out the cutoffs.
@kenkellner
kenkellner / startstop.R
Last active December 9, 2016 13:12
Algorithm to select time series data start and stop times to maximize R2 of a linear model
###########################################################
###### #######
###### GET ACTUAL START/STOP TIMES #######
###### #######
###########################################################
#Read in input file
#Should be called startstop.csv and should be in same directory as the .dat files
#File should have three columns labeled start, stop, and plot (for plot names)
#Dates in start stop should be formatted e.g. 7/16/2014 13:12
@kenkellner
kenkellner / permuteinteraction.R
Last active December 12, 2022 12:22
Test for interaction effect in 2-way ANOVA using permutation
#############################################################
## Permutation test for interaction between 2 main effects ##
#############################################################
#Based on http://r.789695.n4.nabble.com/Randomization-of-a-two-way-ANOVA-td874581.html
#Read in data
data <- read.csv('final.project.data.csv',h=T)
#Full model with interaction
@kenkellner
kenkellner / oddsratios.R
Created July 25, 2014 19:03
Odds Ratio calculation
#Manual calc of odds ratio:
#Pyrantel: 10 taken / 29 left
#Control 12 taken / 9 left
(10/29)/(12/9) #0.2586
1/((10/29)/(12/9)) #3.87 (more intuitive)
log(0.2586) #On log scale -1.3524
@kenkellner
kenkellner / contest.R
Last active August 29, 2015 14:04
Probability of winning an office drawing
#How many simulations?
niter=100000
#Create matrix to hold final drawings (final)
#and vector to hold whether or not person "A" (Jeremy) won in a particular simulation (test)
final <- matrix(NA,nrow=niter,ncol=3)
test <- vector(length=niter)
#Iterate through $niter simulations one at a time in a {for} loop