Skip to content

Instantly share code, notes, and snippets.

View mikedecr's full-sized avatar

Mike DeCrescenzo mikedecr

View GitHub Profile
# Creates a 3D plot to demonstrate how a combination of OLS parameters minimizes residual sum of squares
# set values for X
x <- seq(-50, 50, 1)
# Y as a function of X with random error
y <- NA
for (i in 1:length(x)) {
y[i] <- 2*x[i] + rnorm(1, 0, 5)
}
library(tidyr)
library(dplyr)
library(ggplot2)
library(cowplot) # for aesthetic purposes
set.seed(555)
df <-
data.frame(
xrand = runif(100, min = 1, max = 10),
tidy.fix <- function(mod) {
tidy(mod) %>%
mutate(
term = ifelse(term=="(Intercept)", "intercept", term))
}
@mikedecr
mikedecr / pred_fx.r
Last active July 17, 2017 02:36
Function for generating predicted values from lm and glm model objects
fx <- function(model, design, predictor = design[,2], alpha = 0.05, reps = 10000, vc = vcov(model)){
require(mvtnorm)
require(sandwich)
# sample from MV normal
matrix.of.draws1 <- t(rmvnorm(n = reps, mean = coef(model), sigma = vc))
design <- as.matrix(design)
@mikedecr
mikedecr / McCrary_Test.R
Last active December 5, 2016 23:53
Modifies the DCdensity() function from R's `rdd` package so it returns enough data for custom plotting
mccrary <-
function (runvar, cutpoint, bin = NULL, bw = NULL, verbose = FALSE,
plot = TRUE, ext.out = FALSE, htest = FALSE)
{
library(rdd)
runvar <- runvar[complete.cases(runvar)]
rn <- length(runvar)
rsd <- sd(runvar)
rmin <- min(runvar)
rmax <- max(runvar)
@mikedecr
mikedecr / tweet_if_prime_date.R
Last active January 8, 2017 17:16
Prime Number Date Tweeter
########################################
# Script to tweet if the date is a prime number (American date)
# (((European and International date formats coming soon)))
########################################
library(lubridate) # date functions
library(matlab) # contains isprime()
@mikedecr
mikedecr / heart-gif.R
Last active February 14, 2018 15:33
make a gif of a heart using R, ggplot, and gganimate
# ----------------------------------------------------
# happy valentine's day
# trig code from @dmi3k (https://twitter.com/dmi3k/status/963758478375444480)
# ----------------------------------------------------
library("tidyverse")
library("gganimate")
theme_set(theme_classic())
# ---- Heiss code -----------------------
# From: https://gist.github.com/andrewheiss/5e162c836575721d1dd53ec2af38753c
# by Andrew Heiss
library("magrittr")
library("tidyverse")
library("lubridate")
library("broom")
library("scales")
@mikedecr
mikedecr / smartly-reinstall-R-pkgs.R
Last active November 30, 2021 20:42
When you're installing packages after a major R upgrade and you DON'T just want to get everything from CRAN.
# ----------------------------------------------------
# Installing packages for new R version.
# Michael DeCrescenzo
#
# Hi, I'm a political science PhD student looking for
# a data science/quantitative research/statistics job.
# Learn more at <mikedecr.github.io> and <github.com/mikedecr>
#
# This script deals with the following problem:
# I upgraded to new major version of R, & I want to reinstall packages.
---
title: '`xaringanthemer` font problem MWE'
author: Michael G. DeCrescenzo
date: |
`r format(Sys.time(), '%B %d, %Y')`
---
Specifying a different font with `base_family` works fine.
```{r}