Skip to content

Instantly share code, notes, and snippets.

View noamross's full-sized avatar

Noam Ross noamross

View GitHub Profile
@noamross
noamross / smooth_re2.R
Created June 12, 2020 22:28
Smooth construct, predict, and plot for random effects with numeric inputs
#' Create a random effect basis with integers rather than factors
#' @import mgcv
#' @export
smooth.construct.re2.smooth.spec <- function (object, data, knots) {
if (!is.null(object$id))
stop("random effects don't work with ids.")
if(any(sapply(data, is.numeric))) data <- lapply(data, as.factor) ## <-- All I did was this (and below)
form <- as.formula(paste("~", paste(object$term, collapse = ":"),
"-1"))
object$X <- model.matrix(form, data)
@noamross
noamross / partition.R
Last active October 27, 2020 13:13
A quick greedy algorithm to partition unequal-sized groups into near-equal shards
#' Function to partition unequal sized groups into shards of similar size.
#'
#' Based on the greedy algorithm described in [The Wikipedia article on the
#' the partitioning problem](https://en.wikipedia.org/wiki/Partition_problem#The_greedy_algorithm)
#'
#' @param groups_vector An integer vector of group ids, such as a group ID
#' column in a data frame
#' @param n_shards The number of shards to split groups up into
#' @examples
#' n_groups <- 200

Keybase proof

I hereby claim:

  • I am noamross on github.
  • I am noamross (https://keybase.io/noamross) on keybase.
  • I have a public key ASB-BxfY2ry-bqcNtfdHGtsrS2Xwb0kBX2i_fgE1znBsSQo

To claim this, I am signing this object:

@noamross
noamross / test.Rmd
Last active April 3, 2019 21:04
flexdashboard/shiny tests
---
title: "test"
output:
flexdashboard::flex_dashboard:
orientation: rows
runtime: shiny
---
```{r setup, include = FALSE}
library(shiny)
@noamross
noamross / criticmarkup.lua
Last active May 28, 2024 19:25
A pandoc filter for MS Word track changes to criticmarkup
-- a lua filter for panodoc
-- run pandoc your_word_doc.docx --track-change=all -t markdown --lua-filter=criticmarkup.lua
-- TODO: Detect substitutions in adjacent insertion/deletions
-- TODO: capture whole comment hightlight rather than just start point of comment
function Span(elem)
if elem.classes[1] and elem.classes[1] == "insertion" then
local opener = { pandoc.RawInline(FORMAT, "{++ ") }
local closer = { pandoc.RawInline(FORMAT, " ++}") }
return opener .. elem.content .. closer
elseif
@noamross
noamross / test.Rmd
Last active October 27, 2018 19:52
Test RVG PPTx
---
title: "Test RVG pptx"
output:
powerpoint_presentation:
keep_md: TRUE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
@noamross
noamross / mgcv-posterior-animate.R
Created September 2, 2018 00:29
Animating smoothing uncertainty in a GAM
library(tidyverse)
library(gganimate)
library(mgcv)
library(mvtnorm)
# Fit a GAM to the data
mod <- gam(hp ~ s(mpg), data=mtcars, method="REML")
# Get the linear prediction matrix
newdat = data.frame(
@noamross
noamross / rs_term_editor.sh
Last active August 30, 2018 10:41
An experiment in trying to make RStudio GIT_EDITOR for terminal sessions
#!/bin/bash
# Put this file somewhere and run
# export EDITOR=/path/to/rs_term_editor.sh
# export GIT_EDITOR=/path/to/rs_term_editor.sh
# There's probably some way to set thes e in my profile to only run
# when RSTUDIO=1
### Set initial time of file
LTIME=`stat -f "%m" $1`
@noamross
noamross / put64.R
Last active November 17, 2023 22:22
Simple R object to base64 conversion. Free to a good home in your R package! If you put it in your package, let me know 🙂
# Copyright 2018 Noam Ross
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
@noamross
noamross / hrbrfear.R
Created April 24, 2018 13:58
hrbrfear index
library(rtweet) #rtweet API creds should already be set up
library(dplyr)
library(ggplot2)
library(hrbrthemes)
library(tidytext)
library(lubridate)
hbt <- get_timeline("hrbrmstr", n=3200)
hbt2 <- hbt %>%