Skip to content

Instantly share code, notes, and snippets.

library(pacman)
p_load(tidyverse, janitor, brms)
tibble::tribble(~Year, ~OR, ~z,
1765, 1.80, 1.69,
1775, 2.41, 2.86,
1790, 2.36, 2.67,
1805, 2.58, 3.07,
1820, 1.83, 2.63,
1835, 1.55, 1.48,
1850, 1.06, 0.14,
library(pacman)
p_load(tidyverse, gsheet, ggforce)
timeline <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1S6dWcSNKVQK3tZkt-PvA55LkM8ZuOJykDntECYjExhk/edit?usp=sharing")
timeline %>% filter(Type=="Regentperiod") %>% select(Person, Tidsperiod) %>% distinct() -> tidsperioder
tidsperioder %>% select(Tidsperiod) %>% distinct %>% pull(Tidsperiod) -> tidsperioder_vector
timeline %>% filter(Type!="Regentperiod") %>% select(-Tidsperiod) %>% left_join(tidsperioder) %>%
#pacman::p_load(pacman, tidyverse, rio, brms, DHARMa)
# inspired by https://frodriguezsanchez.net/post/using-dharma-to-check-bayesian-models-fitted-with-brms/
# example code commented out, assumes that you have DHARMa and brms installed.
# posterior_epred is more likely a better choice than posterior_linpred,
# but the former does not work with ordinal models
# due to being a multivariate model in disguise (which shares linear predictor)
model.check.ocat <- function(model) {
# Some R 4.1 syntax here!
simulatedResponse = model %>%
predict(type="response") %>%
apply(1, \(x) sample(1:length(x), prob = x, size=1000, replace = TRUE)) %>%
t()
createDHARMa(
simulatedResponse = simulatedResponse,
observedResponse = model$y,
fittedPredictedResponse = model %>% predict(), # the linear predictor
We can't make this file beautiful and searchable because it's too large.
# Data source: Statistics Sweden
# No data on county and municipality level if count < 3
# No data on country level if count < 2
"sex","county_code","county","municipality","municipality_code","name","count"
"male",NA,NA,NA,NA,"Aadam",2
"male",NA,NA,NA,NA,"Aadhiran",2
"female",NA,NA,NA,NA,"Aadya",3
"male",NA,NA,NA,NA,"Aahil",2
"female",NA,NA,NA,NA,"Aaliya",2
"female","12","Skåne län","Malmö","1280","Aaliyah",3
library(tidyverse)
library(magrittr)
library(brms)
#library(betareg) # for example data
#### Code declaring ordered beta family for brms ####
# code from Robert Kubinec
# custom family
ord_beta_reg <- custom_family("ord_beta_reg",
This file has been truncated, but you can view the full file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
to_clipboard <- function(x, row.names = F){
write.table(x, "clipboard", row.names=row.names, sep='\t', na = "")
}
We can't make this file beautiful and searchable because it's too large.
county_code,county_name,county_name_long,municipality_code,municipality_name,municipality_name_long,regso_code,regso_name,deso,valdeltagande
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R001,Bollstanäs,0114C1010,0.88
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R001,Bollstanäs,0114C1030,0.88
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R001,Bollstanäs,0114C1050,0.91
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R002,Hammarby-Vaxmyra,0114C1180,0.84
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R002,Hammarby-Vaxmyra,0114C1220,0.72
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R003,Odenslunda norra-Frestaby-Ekeby,0114C1100,0.83
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R003,Odenslunda norra-Frestaby-Ekeby,0114C1140,0.9
01,Stockholm,Stockholms län,0114,Upplands Väsby,Upplands Väsby kommun,0114R004,Odenslunda södra
library(qs)
`%q>%` <- function(lhs, rhs){as.character(substitute(rhs)) -> name
qcache(substitute(lhs), name = name) -> value
assign(x = name, value = value, envir = .GlobalEnv)}