Skip to content

Instantly share code, notes, and snippets.

@slwu89
slwu89 / testCompiler.R
Created February 27, 2017 16:44
testing out speed of R "compiler" package with Vectorize
library(compiler)
inFun <- Vectorize(function(x,y,z){
return((x^2)+y*z)
},SIMPLIFY = FALSE)
inFun1 <- cmpfun(Vectorize(function(x,y,z){
return((x^2)+y*z)
},SIMPLIFY = FALSE),options = list(optimize=1))
inFun2 <- cmpfun(Vectorize(function(x,y,z){
return((x^2)+y*z)
@slwu89
slwu89 / rcppGSLpackage.txt
Last active March 3, 2017 21:28
How to make a package with RcppGSL
Things to do (MacOS Sierra):
1. make a file: src/Makevars that contains:
PKG_CFLAGS = `gsl-config --cflags`
PKG_LIBS = `gsl-config --libs`
PKG_CPPFLAGS = -I../inst/include
2. the DESCRIPTION should include:
@slwu89
slwu89 / ABM_sketch_R6.R
Created May 10, 2017 00:52
sketch of interacting R6 classes for simple ABM
#################################################################
#
# MASH
# R6-ified
# How to nest interacting classes
# Hector Sanchez & Sean Wu
# May 9, 2017
#
#################################################################
@slwu89
slwu89 / constrainOptim.R
Created May 25, 2017 00:37
Optimization with constraints via parameter transform
#############################################################
# Optimization with constraints via parameter transform
# Sean Wu
#############################################################
# same as qlogis
logit <- function(x){
log(x/(1-x))
}
@slwu89
slwu89 / trickCpp.cpp
Last active July 21, 2017 16:22
C++ tricks
#include <algorithm>
#include <iostream>
#include <vector>
// quickly find the position of the minimum and maximum elements of an STL vector
int main()
{
std::vector<int> v = { 3, 9, 1, 4, 2, 5, 9 };
auto result = std::minmax_element(v.begin(), v.end());
@slwu89
slwu89 / trickyR.R
Last active July 29, 2017 00:47
R tricks
# passArgsReplicate: how to pass additional named args as ... to function in replicate
passArgsReplicate <- function(n, func, ...){
args = as.list(substitute(list(...)))[-1L]
replicate(n = n,expr = do.call(func,args),simplify = FALSE)
}
# test calling and using global variables on different parallel processes
library(parallel)
parTest <- function(arg){
@slwu89
slwu89 / stat243-bash.txt
Last active August 27, 2017 18:54
stat243-bash
1. mypython=$(which python)
2. myvar=$(hostname)@$(whoami)
3. mkdir -p temp/proj{1..3}/{code,data}
4. wc -l FILE
5.
#first 3 lines in file
@slwu89
slwu89 / stat243-week2lecture1.txt
Last active August 28, 2017 23:41
stat243-week2lecture1.txt
################################################################################
# Lecture
################################################################################
# BASH stuff:
ls -tr *.R # gives you the list of the R files modified most recently (closest in time at bottom, farthest in time at top)
ls -tr *.R | tail - n 5 # last five things modified
ls -tr *.R | tail - n 5 | grep optim # look for files called 'optim' in the name
grep 'example.pdf' $(ls -tr *.R) # look for example.pdf in the stuff from inside $(.)
@slwu89
slwu89 / stat243-ps1.txt
Last active August 29, 2017 01:17
stat243-ps1.txt
# download the data
wget -O apricots.zip "http://data.un.org/Handlers/DownloadHandler.ashx?DataFilter=itemCode:526&DataMartId=FAO&Format=csv&c=2,3,4,5,6,7&s=countryName:asc,elementCode:asc,year:desc"
unzip -p apricots.zip > apricots.csv
# split into region and country
grep -v "+" apricots.csv > countries.csv
grep "+" apricots.csv > regions.csv
# put all the 2005 data to a new countries05.csv file
@slwu89
slwu89 / c2rcpp.cpp
Created September 1, 2017 05:04
C in Rcpp
#include <Rcpp.h>
using namespace Rcpp;
// C code with Rcpp; basic example
// [[Rcpp::export]]
extern "C" SEXP rngScopeCppScalar() {
NumericVector x(4);