Skip to content

Instantly share code, notes, and snippets.

View sdaza's full-sized avatar

Sebastian Daza sdaza

View GitHub Profile
@sdaza
sdaza / lookvar.R
Last active December 11, 2015 21:18
lookvar
# dat has to be a data.frame or data.table
# varnames should bea vector with the variable names you are looking for, e.g., c("hc", "hv"), or regular expressions
# the result would be a vector with variables names
lookvar <- function(dat, varnames) {
n <- names(dat)
nn <- list()
for (i in 1:length(varnames)) {
nn[[i]] <- grep(varnames[i],n)
@sdaza
sdaza / APCex.R
Last active December 29, 2015 08:19
Average predictive comparisons
# AVERAGE PREDICTVE COMPARISON
# loading libraries
library(arm)
# data
dat <- read.csv("http://dl.getdropbox.com/u/18116710/example.csv")
# id: individual identifier
# wave: 1 to 7
@sdaza
sdaza / ape.R
Last active December 30, 2015 11:59
ape
mycoefplot <- function(coef) {
# reverse coefficients
coef <- apply(coef, 2, rev)
nvar <- length(rownames(coef))
# dotplot
dotplot(1:nvar~coef[,1],
xlim=c(min(coef[,1]-2*coef[,2])-.02, max(coef[,1]+2*coef[,2])+.02),
xlab='Average predictive comparison', ylab=" ",
@sdaza
sdaza / samplesize.R
Last active December 30, 2015 22:49
Function for sample size and error
# SAMPLING ERROR
serr <- function(n, deff=1, rr=1, N=NULL, cl=.95, p=0.5, relative=FALSE) {
# validation
if (sum(n==0)>=1) {
stop("n vector contains 0 values")
}
######################################################
# SIMULATION FUNCTION TERM PAPER SOC 952, SPRING 2014
######################################################
# RETURN STANDARDIZED BIAS AND COVERAGE RATES
# These libraries have to be installed
library(data.table)
library(lavvan)
@sdaza
sdaza / myFunctions.R
Last active August 29, 2015 14:02
MyFunctions.R
# MY R FUNCTIONS
# AUTHOR: SEBASTIAN DAZA
# Jun 02, 2014, 10:35 AM
# LOOKING FOR VARIABLES ####
lookvar <- function(dat, varnames) {
n <- names(dat)
nn <- list()
for (i in 1:length(varnames)) {
@sdaza
sdaza / example_ergm.R
Last active August 29, 2015 14:05
Example ERGM (dyadic cov)
###########
# Example
##########
library(igraph)
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
"Esmeralda"),
age=c(48,33,45,34,21),
gender=c("F","M","F","M","F"))
@sdaza
sdaza / acs_example.R
Created March 12, 2015 13:37
acs example
library(acs)
rm(list=ls(all=TRUE))
variables <- c("C17002_002", "C17002_003", "C17002_004", "C17002_005","C17002_006", "C17002_001")
dat <- acs.fetch(geo = geo.make(state = "WI", county = "*", tract = "*"), variable = variables)
p_output <- divide.acs(dat[,1] + dat[,2] + dat[,3] + dat[,4] + dat[,5], dat[,6], method = "proportion")
r_output <- divide.acs(dat[,1] + dat[,2] + dat[,3] + dat[,4] + dat[,5], dat[,6], method = "ratio")
@sdaza
sdaza / examples_acsr.R
Last active August 29, 2015 14:17
examples ACSR
# EXAMPLES USING PACKAGE ACSR
# AUTHOR: SEBASTIAN DAZA
# March 23, 2015, 8:19 AM
# INSTALL LAST VERSION OF DATA.TABLE AND ACS PACKAGE (YOU HAVE TO INSTALL DEVTOOLS BEFORE!)
# I ASSUME ACS IS ALREADY INSTALLED!
install.packages("devtools")
devtools::install_github("Rdatatable/data.table", build_vignettes = FALSE)
# INSTALL MY PACKAGE
@sdaza
sdaza / miceMultilevelExample.R
Last active May 22, 2017 13:55
Multilevel imputation using MICE
# load data
load("ex.Rdata")
###################################
# example only using mice package
###################################
library(mice)
imp <- mice(ex, maxit = 0)