Skip to content

Instantly share code, notes, and snippets.

#' Calculates rolling weeks backward from the end_date.
#'
#' The purpose is to group a vector of dates into 7 day weeks
#' backward in time. For example, if the end date was 2020-06-09,
#' the days 2020-06-03 through 2020-06-09 would be one week,
#' 2020-05-27 through 2020-06-02 would be another week, 2020-05-20
#' through 2020-05-26 another and so on. The function will work even
#' if the vector of dates is missing one or more dates in the series.
#'
#' @param date_vector the vector of dates to group into weeks. Must be
@carlbfrederick
carlbfrederick / sampleLatoReport.Rmd
Created November 12, 2018 21:57
Windows Fonts in Rmd
---
title: "Sample Lato .Rmd"
author: "Carl Frederick"
date: "11/12/2018"
mainfont: Lato
output:
pdf_document:
latex_engine: xelatex
---
@carlbfrederick
carlbfrederick / DescribeDataSharing.R
Created July 20, 2018 14:51
Using babynames package to illustrate complex data sharing
library(babynames)
library(tidyverse)
set.seed(8746)
snames <- sample(babynames$name, 14)
unames <- snames[11:14]
snames <- snames[1:10]
Agency1 <- data_frame(Person = c(sample(snames, 5), unames[1]),
@carlbfrederick
carlbfrederick / dataMaid_cleanPlot.R
Created July 10, 2018 14:57
4th attempt at clean plots for data documentation
#'dataMaid_cleanPlot.R
#'
#'These functions makes the default graphics prettier/easier to read:
#'
#' 1. Minimal Theme
#' 2. Angled Axis Text
#'
#' @example
cleanPlotHelper <- function(data, vnam, sideways) {
@carlbfrederick
carlbfrederick / dataMaid_isIDvar.R
Last active January 11, 2019 19:48
dataMaid check function: deal with "key" variables that do not uniquely identify rows
#'dataMaid_isIDvar.R
#'
#'This function identifies "Key" and similar variables and stops dataMaid::makeDataReport from creating
#'visualizations and/or other inappropriate summaries. Instead it outputs a table with minimal information
#'(see https://cran.rstudio.com/web/packages/dataMaid/vignettes/extending_dataMaid.html for inspiration).
#'
#' @example
#' makeDataReport(toyData, output = "html",
#' preChecks = c("isKey", "isSingular", "isSupported", "isIDvar"))
@carlbfrederick
carlbfrederick / packageFunctionMap.R
Last active September 8, 2020 14:37
Visualize internal package functions
library(tidyverse)
library(DiagrammeR)
#Get package functions ----
ls_fcns <- function(pkg) {
fcns <- unclass(lsf.str(envir = asNamespace(pkg), all = TRUE))
return(as.character(fcns))
}
@carlbfrederick
carlbfrederick / survProbs.coxme.R
Last active September 2, 2015 14:58
I wrote these functions to calculate survival probabilities at various levels of the random effect estimate from a coxme.object. The code is heavily adapted from survfit.coxph(). It should work, but all errors and inelegant hacks I have introduced are certainly my own doing. Comments/improvements welcome, enjoy!
#Internal Functions
MYagsurv <- function(y, x, wt, risk, survtype=3, vartype=3) {
nvar <- ncol(as.matrix(x))
status <- y[, ncol(y)]
dtime <- y[, ncol(y) - 1]
death <- (status == 1)
time <- sort(unique(dtime))
nevent <- as.vector(rowsum(wt * death, dtime))
ncens <- as.vector(rowsum(wt * (!death), dtime))
wrisk <- c(wt * risk) #Had to add c() to remove the dimnames so that the multiplication later would work