Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
#rstats-ing all the things

Andrew Heiss andrewheiss

👨‍💻
#rstats-ing all the things
View GitHub Profile
View tikz_fun.Rmd
---
title: "Fancy math"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
# Conditional tikz output types; use PDF if this is LaTeX, otherwise use a
# shrunken down high resolution PNG
View brms_ame.md
library(tidyverse)
library(brms)
library(tidybayes)
library(magrittr)

options(mc.cores = 4,
        brms.backend = "cmdstanr")

set.seed(7305)  # From random.org
View rotated_lollipops.md
library(tidyverse)

# Make some data
data_to_plot <- mpg %>% 
  group_by(drv) %>% 
  summarize(avg_mpg = mean(hwy))
data_to_plot
#> # A tibble: 3 x 2
#>   drv   avg_mpg
View bayes_fractional_logit.r
library(tidyverse)
library(broom)
library(brms)
data_401k <- haven::read_dta("https://www.stata-press.com/data/r17/401k.dta")
head(data_401k)
#> # A tibble: 6 x 10
#> partic totemp employ mrate prate age sole ltotemp agesq ltotempsq
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl+lbl> <dbl> <dbl> <dbl>
#> 1 188 353 296 0.581 0.635 2 0 [Not only pl… 5.87 4 34.4
View bad_inits_brms.R
library(tidyverse)
library(brms)
panel_data <- read_csv("https://gist.githubusercontent.com/andrewheiss/6dfbebf552125a534fa95ca9d0ae1ddf/raw/5d3bbaf908217c4e751bc66eea98414053db84be/example_panel_data.csv", col_types = cols())
model <- brm(
bf(prop_contentious_lead1 | weights(iptw) ~
barriers_total + barriers_total_lag1_cumsum +
(1 | gwcode),
zi ~ 1),
View example_panel_data.csv
country gwcode year prop_contentious_lead1 iptw barriers_total barriers_total_lag1_cumsum
Cuba 40 1990 0 1 1 0
Cuba 40 1991 0 1.1803553735484706 1 1
Cuba 40 1992 0 1.4477007082008233 1 2
Cuba 40 1993 0.0015546961272616035 1.796894823940133 1 3
Cuba 40 1994 0.01739994148386913 2.1415209120607304 1 4
Cuba 40 1995 0.18145681949582365 2.6187144352126963 1 5
Cuba 40 1996 0.10006359889381158 0.01982968868454197 9.5 6
Cuba 40 1997 0.0052085529255595005 0.019985305995256622 9.5 15.5
Cuba 40 1998 0.03698599151631047 0.020292716257838513 9.5 25
View tidygeocode.md
library(tidyverse)
library(sf)
library(albersusa)
library(tidygeocoder)

usa_map <- usa_sf("longlat") %>% 
  filter(!(name %in% c("Alaska", "Hawaii")))

some_cities <- tribble(
View interaction_stuff.md
library(tidyverse)
library(broom)

# Load data
world_happiness <- read_csv("https://raw.githubusercontent.com/andrewheiss/evalsp21.classes.andrewheiss.com/master/static/data/world_happiness.csv") %>% 
  mutate(latin_america = region == "Latin America & Caribbean") %>% 
  mutate(latin_america = factor(latin_america, labels = c("Not Latin America", "Latin America")))

# Basic model, no interaction
View tidy_magic.md
library(tidyverse)
library(lme4)  # For mixed models
library(broom)
library(broom.mixed)  # Only necessary for models made with lmer()
library(knitr)  # For kable()

# Build some models
model1 <- lm(hwy ~ displ, data = mpg)
model2 <- lm(hwy ~ displ + drv, data = mpg)
View lognormal.md

Haha so this should have been obvious to me all along, but the results for log_gdpPercap with gaussian() are identical to original gdpPercap with lognormal()

library(tidyverse)
library(gapminder)
library(brms)
library(broom)
library(broom.mixed)
library(modelsummary)