Skip to content

Instantly share code, notes, and snippets.

@rubenarslan
rubenarslan / amigoingmad.R
Last active April 13, 2016 13:39
am I going mad or has some package overwritten dplyr's functions again?
#' Function is also in this package: https://github.com/rubenarslan/formr
#' It's easy to attach packages that overwrite functions from other packages. Especially dplyr has a lot of conflicts
#' with base packages, MASS and plyr. Because some of these conflicts do not always lead to error messages, sometimes
#' just incorrect behaviour, this function exists. Don't trust your faulty memory, just check whether dplyr's (or any other
#' package's) functions are 'on top' if you so desire.
#'
#' @param fix defaults to true. Detaches the desired package (without unloading) and loads it again. Won't work for base packages and can't overwrite functions that you defined yourself.
#' @param package the package you want to be on top (loaded last), defaults to dplyr
#' @param iteration for internal use only, if set to 0 the function will call itself to check that it worked, if set to 1, it won't.
#' @export
#' easier marginal effect plots from brms objects
#' ## ideas?
#' visualise uncertainty with violin plots instead of pointranges
#' (would mean getting rid of early-on summary)
#' ### shorthand for finding mode
#' from https://stackoverflow.com/questions/2547402/standard-library-function-in-r-for-finding-the-mode
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
@rubenarslan
rubenarslan / gist:b02d6fca3ec3f64b0e20
Created June 12, 2015 14:03
reproducible error with ifelse typing in dplyr
library(dplyr)
diary = structure(list(short_session = c("006935", "006935", "006935",
"006935", "006935", "006935", "006935", "006935", "006935", "006935",
"006935", "006935", "006935", "006935", "006935", "006935", "006935",
"006935", "006935", "006935", "006935", "006935", "006935", "006935",
"006935", "006935", "006935", "006935", "006935", "006935", "006935",
"006935", "006935", "006935", "006935", "006935", "006935", "006935",
"006935", "008e1a", "008e1a", "008e1a", "008e1a", "008e1a", "008e1a",
"008e1a", "008e1a", "008e1a", "008e1a", "008e1a", "008e1a", "008e1a",
"008e1a", "008e1a", "008e1a", "008e1a", "008e1a", "008e1a", "008e1a",
@rubenarslan
rubenarslan / repro_mcmcglmm_zapoisson
Last active August 29, 2015 14:17
reproducible example MCMCglmm prediction for zero-altered poisson outcome
fact_levels = c("[0,25]","(25,30]","(30,35]")
cases = 5e4
exampledata = data.frame(idParents = sample(1:(cases/10), size = cases, replace = T),
paternalage.factor = factor(sample(fact_levels, size = cases, replace=T),levels = fact_levels))
exampledata$outcome = rpois(cases,lambda = log(30 + (as.numeric(exampledata$paternalage.factor))*-0.2 + 0.8 *rnorm(cases)))
exampledata$outcome = ifelse(plogis(log(as.numeric(exampledata$paternalage.factor))-2 + rnorm(cases)) > 0.5, 0, exampledata$outcome)
samples = 1000; thin = 10; burnin = 3000; nitt = samples * thin + burnin
@rubenarslan
rubenarslan / index.html
Created June 3, 2014 07:56
Scatterplot
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<script src='http://ramnathv.github.io/rCharts/libraries/widgets/polycharts/js/polychart2.standalone.js' type='text/javascript'></script>
<style>
.rChart {
display: block;
@rubenarslan
rubenarslan / ggprplots.R
Last active December 17, 2015 10:58 — forked from masverba/ggprplots.R
new ggplot 2 syntax, return data object, print plot, fix wrong name for first argument
ggprplots <- function(fit, alpha=0.1) {
design <- model.matrix(fit)
# Skip the intercept term, if any.
m0 <- if (colnames(design)[1] == '(Intercept)') { 2 } else { 1 }
M <- ncol(design)
data <- do.call(rbind, lapply(m0:M, function(m) {
var <- colnames(design)[m]
@rubenarslan
rubenarslan / gist:3006283
Created June 27, 2012 19:34
poly(,3) raw=T vs raw=F
> raw_c3_a3
Linear mixed model fit by REML ['lmerMod']
Formula: churchattendance ~ poly(cohort_c, 3, raw = T) * poly(age_c, 3, raw = T) + ctd_c + dropoutalive + obs_c + (1 + poly(age_c, 3, raw = T) | PERSNR)
Data: long.kg
REML criterion at convergence: 316048.9
Random effects:
Groups Name Variance Std.Dev. Corr
PERSNR (Intercept) 0.6409284 0.80058
@rubenarslan
rubenarslan / hierarchical.nonnormal.r
Created October 11, 2011 00:59
reproducible example for stats post
set.seed(272773)
individuals = data.frame(
children = c(6, 6, 1, 4, 1, 1, 1, 2, 11, 3, 7, 2, 6, 4, 4, 3, 1, 4, 2, 10,
2, 1, 2, 3, 1, 6, 14, 4, 4, 1, 7, 8, 2, 3, 2, 1, 1, 8, 8, 5,
7, 4, 1, 3, 4, 1, 2, 8, 1, 1, 1, 1, 2, 7, 4, 3, 4, 1, 1, 3, 1,
1, 6, 3, 1, 3, 4, 3, 3, 5, 5, 5, 3, 6, 2, 6, 4, 7, 4, 2, 7, 7,
5, 1, 4, 1, 9, 1, 6, 5, 3, 1, 4, 1, 9, 3, 5, 3, 3, 12, 1, 4,
4, 8, 14, 2, 1, 4, 5, 4, 4, 10, 3, 5, 1, 1, 2, 5, 4, 1, 4, 1,
5, 1, 3, 5, 2, 1, 8, 3, 7, 2, 6, 5, 3, 2, 4, 1, 3, 7, 3, 1, 1,
14, 3, 1, 3, 3, 2, 1, 5, 3, 4, 13, 7, 9, 3, 3, 7, 2, 2, 4, 7,