Skip to content

Instantly share code, notes, and snippets.

View josherrickson's full-sized avatar

Josh Errickson josherrickson

View GitHub Profile
@josherrickson
josherrickson / summary.lm2.R
Created July 21, 2023 13:18
summary.lm with vcov. argument
summary.lm <- function(object, correlation = FALSE, symbolic.cor = FALSE, ..., vcov. = NULL) {
ss <- stats::summary.lm(object, correlation, symbolic.cor, ...)
if (!is.null(vcov.)) {
if (is.function(vcov.)) {
vcov. <- vcov.(object, ...)
}
ss$cov.unscaled <- vcov./ss$sigma^2
ss$coefficients[,2] <- sqrt(diag(vcov.))
library(forestplot)
data(mtcars)
mtcars$disp <- mtcars$disp/100
coefs <- summary(mod <- lm(mpg ~ disp + cyl + qsec + wt, data = mtcars))$coeff
cis <- confint(mod)
dat <- data.frame(cbind(coefs[, c(1, 4)], cis))
names(dat) <- c("mean", "pv", "lower", "upper")
@josherrickson
josherrickson / sane_yesno.R
Last active May 18, 2022 14:31
a more sane `devtools:::yesno`
suppressMessages(require(devtools))
yesno <- function(...) {
cat(paste0(..., collapse = ""))
# For whatever reason, devtools:::yesno returns `TRUE` if you select a No
# option, and `FALSE` if you select a Yes option
utils::menu(c("Yes", "No")) != 1
}
utils::assignInNamespace("yesno", yesno, "devtools")
# remove stand-alone `yesno`
rm(yesno)
@josherrickson
josherrickson / how_many_imputations.R
Last active May 2, 2022 15:50
how_many_imputations
# This gist has been superceded by the howManyImputations package (https://cran.r-project.org/web/packages/howManyImputations/index.html and https://errickson.net/howManyImputations/).
@josherrickson
josherrickson / mi_plus_IC.do
Last active April 8, 2021 10:35
Stata code to include additional statistics to multiple imputation
webuse mheart1s20, clear
* Run the input model, producing the results as well as returning the AIC/BIC
capture program drop modelPlusIC
program modelPlusIC, eclass properties(mi)
args model
quiet `model'
quiet estat ic
matrix S = r(S)
* Append the new AIC/BIC to IC
#!/bin/bash
echo "i <- rownames(installed.packages())" > __tmp__.R
grep -oh --include=\*.{Rmd,R} -r * -e "\(library\|require\)([A-Za-z0-9.]\+)" | \
sed 's_library(_"_' | \
sed 's_require(_"_' | \
sed 's_)_",_' | sort | uniq | \
perl -p -e 's/\n/ /' | \
sed 's_^_new <- c(_' | \
@josherrickson
josherrickson / colmean
Last active February 20, 2018 19:11
Awk script to compute the mean of a csv file's column.
#!/bin/bash
SEP=","
HEADER=true
STDERR=false
while [ "$#" -gt 0 ]; do
case "$1" in
-s) SEP="$2"; shift 2;;
-c) COL="$2"; shift 2;;
@josherrickson
josherrickson / ggheatmap.R
Last active March 8, 2017 19:29
ggheatmap - Create a heatmap in ggplot2
library(dplyr)
library(reshape2)
library(ggplot2)
#' Create a heatmap in ggplot2
#'
#' @param data Data set
#' @param print.cors Default TRUE. Should the correlations be printed in each block of the heatmap?
#' @param reoder Default TRUE. Should the entries be re-ordered via clustering, similar to `heatmap`?
#' @param colors Default c("red", "white", "blue"). Vector of length three defining colors for correaltions (1, 0, -1).
#'