This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ape) | |
| cat("(((Strix_aluco:4.2,Asio_otus:4.2):3.1,", | |
| "Athene_noctua:7.3):6.3,Tyto_alba:13.5);", | |
| file = "ex.tre", sep = "\n") | |
| tree.owls <- read.tree("ex.tre") | |
| set.seed(101) | |
| dd <- data.frame(y = rnorm(4), | |
| obs = factor(1:4), | |
| g = factor(1)) | |
| library(lme4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## example of updated refit, now incorporated in code | |
| ## * NO convergence testing/warnings/etc | |
| ## * won't work on weird binomial input (i.e. anything other than 0/1) | |
| ## * results seem only to be identical up to floating-point accuracy | |
| ## * doesn't yet do other clever things like resetting starting values | |
| remotes::install_github("glmmTMB/glmmTMB/glmmTMB@smart_refit") | |
| library(glmmTMB) | |
| library(rbenchmark) | |
| data("sleepstudy", package = "lme4") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dd <- data.frame(A = factor(c(1,2,2,2)), | |
| B = factor(1:4), | |
| y = 1:4) | |
| dd$i <- with(dd, factor(sprintf("A%d.B%d",A,B))) | |
| ## set up *inverse* contrast matrix | |
| invc <- matrix(c( | |
| 1/2, 1/6, 1/6, 1/6, ## A-weighted mean | |
| -1, 1/3, 1/3, 1/3, ## A1 vs A2 (== B1 vs mean{B2, B3, B4}) | |
| 0, 2/3, -1/3, -1/3, ## B2 vs mean(B2, B3, B4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## setup from https://www.tjmahr.com/morgan-stanley-cursed-covid-plot/ | |
| library(tidyverse) | |
| theme_set(theme_bw()) | |
| library(lme4) | |
| library(ggeffects) | |
| library(colorspace) | |
| library(directlabels) | |
| # a helper function to download the data from github |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidyverse) | |
| this_year <- 2022 | |
| (df <- read.table(text=" | |
| 2021 April 14 | |
| 2020 April 23 | |
| 2019 April 11 | |
| 2018 April 13 | |
| 2017 April 12 | |
| 2016 April 7 | |
| 2015 April 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(emdbook) | |
| set.seed(101) | |
| dd <- rgamma(5, shape = 1, scale = 1) | |
| ff <- function(x,y) -sum(dgamma(dd,shape=x, scale=y, log = TRUE)) | |
| ifun <- function(...) { | |
| image(cc$x, cc$y, cc$z, ...) | |
| contour(cc$x, cc$y, cc$z, level = min(cc$z) + 1.92, add = TRUE, label = "") | |
| } | |
| png("gamma_lik.png", width = 800, height = 400) | |
| par(mfrow=c(1,2), las = 1, bty = "l") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data("Contraception", package = "mlmRev") | |
| library(lme4) | |
| library(tidyverse) | |
| library(magrittr) | |
| ## make sure we have a variable that's actually continuous | |
| Contraception %<>% mutate(ja = jitter(age), | |
| n_use = as.numeric(use) -1) | |
| m1 <- glmer(use ~ urban * splines::ns(ja, 3) + (1|district), data = Contraception, | |
| family = binomial) | |
| aa <- broom.mixed::augment(m1, data = Contraception, type.residuals = "pearson") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2); theme_set(theme_bw()) | |
| library(dplyr) | |
| library(broom) | |
| #' Summarize binary data à la arm::binnedplot | |
| #' @param x continuous predictor variable | |
| #' @param y 0/1 response variable | |
| #' @param nclass number of categories for binning | |
| #' @param cifun function for extracting confidence intervals | |
| #' @return a data frame with columns "xbar" (centerpoint of bin), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(MuMIn) | |
| library(lme4) | |
| library(glmmTMB) | |
| library(emmeans) | |
| set.seed(2) ## simulate some data... | |
| dd <- expand.grid(f1 = factor(1:3), f2 = factor(1:3), | |
| g = factor(1:10), rep = 1:2) | |
| Z <- model.matrix(~g-1, dd) | |
| b <- rnorm(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(microbenchmark) | |
| library(Rcpp) | |
| library(ggplot2); theme_set(theme_bw()) | |
| library(colorspace) | |
| library(tidyverse) | |
| ## the most naive: a for loop, in pure R | |
| f_forloop <- function(n) { | |
| val = 0 | |
| for (i in 2:n) { |