Skip to content

Instantly share code, notes, and snippets.

View dsjohnson's full-sized avatar

Devin Johnson dsjohnson

  • NOAA Fisheries
  • Loveland, CO
  • 20:59 (UTC -06:00)
View GitHub Profile
library(glmmTMB)
library(mgcv)
#########################################################################
## Function to create prediction matrices from mgcv for glmmTMB
## from ?mgcv::smooth2random
########################################################################
s2rPred <- function(sm,re,data) {
## Function to aid prediction from smooths represented as type==2
## random effects. re must be the result of smooth2random(sm,...,type=2).
#' @title Find the sections of a path that pass thorugh a restricted area
#' @description This function is used to identify sections of a path that pass through
#' restricted areas. the CTCRW model in crawl cannot actively steer paths away
#' from restricted areas as it knows nothing of spatial information. So, this function
#' will identify areas that for which the unrestrained path passes through these areas.
#' If the path/points end within the restricted area, those records will be removed.
#' The user can then use this information to adjust the path as desired.
#' @param xy A \code{SpatialPoints} object from the \code{sp} package or a
#' 2-column matrix of x and y locations
#' @param res_raster A \code{raster} object from the raster package that indicates
### Load packages ###
# Must have 'marked' v >= 1.1.8
library(marked)
# The splines package is only necessary for fitting b-spline curves used in the paper
# It is not required for the multivate models in the marked package
library(splines)
### Import and process data ###
@dsjohnson
dsjohnson / logistic_analysis_stan_summary.R
Created November 13, 2015 19:18
Summary of logistic growth MCMC output from stan
library(cowplot)
library(coda)
load("Sig_test_DJ.RData")
data = all_model
data$Year = factor(data$Year)
data = droplevels(data)
### Add beach variables
data$Beach4 = ifelse(data$Beach=="4", 1, 0)
data$Beach7 = ifelse(data$Beach=="7", 1, 0)
library(rstan)
devtools::source_gist("8f675b9aa74ab9900fb4")
load("Sig_test_DJ.RData")
data = all_model
data$Year = factor(data$Year)
data = droplevels(data)
### Add beach variables
data$Beach4 = ifelse(data$Beach=="4", 1, 0)
data$Beach7 = ifelse(data$Beach=="7", 1, 0)
@dsjohnson
dsjohnson / logistic_funcs.R
Last active November 9, 2015 19:46
Fit logistic growth model like "nlsList" but allow user to specify a design matrix for each parameter
### New growth function
logistic_growth = function(x, phi1, phi2, phi3){
phi1/(1 + exp(-(x-phi2)/phi3))
}
ll_logistic = function(par, y, xvar, phi1_dm, phi2_dm, phi3_dm, sigma_dm, sigma=1, predict=FALSE, scale=-2){
n1 = ncol(phi1_dm)
n2 = ncol(phi2_dm)
n3 = ncol(phi3_dm)
if(!missing(sigma_dm)) n4 = ncol(sigma_dm)
@dsjohnson
dsjohnson / R symlinks
Last active August 29, 2015 14:00
Symliks to add for R 3.1.0 (Mavericks version) to run and compile packages
@dsjohnson
dsjohnson / NFS_CR_simulation.R
Last active January 2, 2016 20:39
Simulation of a Northern fur seal CR data set
library("marked")
#### Some preliminaries ###
ilogit = function(x){log(x/(1-x))}
min.adult.age=5
additional.resight=3
adult.age="stable"
set.seed(111)
n.rel.pup=c(138,57,38,250,rep(100,4))
@dsjohnson
dsjohnson / run_nfs_data_assimilation.R
Created December 11, 2013 21:30
Building data for fur seal analysis in: Devin S. Johnson, Mevin B. Hooten, and Carey E. Kuhn (2013) Estimating animal resource selection from telemetry data using point process models. Journal of Animal Ecology 82:1155--1164
library(sp)
library(rgdal)
library(raster)
library(gstat)
library(rgeos)
library(automap)
library(adehabitatHR)
source("stpp_rsf_helper.R")
# Read in telemtry
@dsjohnson
dsjohnson / run_nfs_analysis_indiv.R
Last active December 31, 2015 02:09
Individual space-time analysis of fur seal data from : Devin S. Johnson, Mevin B. Hooten, and Carey E. Kuhn (2013) Estimating animal resource selection from telemetry data using point process models. Journal of Animal Ecology 82:1155--1164
library(sp)
library(rgdal)
library(raster)
library(gstat)
library(rgeos)
library(automap)
library(mgcv)
source("stpp_rsf_helper.R")
load("nfs_analysis/JAE_analysis_data_st.RData")
# load("stQuadList_countVersion.RData")