Skip to content

Instantly share code, notes, and snippets.

to_clipboard <- function(x, row.names = F){
write.table(x, "clipboard", row.names=row.names, sep='\t', na = "")
}
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" />
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",
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
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
#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)
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) %>%
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(tidyverse)
library(janitor)
library(pxweb)
library(scales)
get_pxweb_data(url = "http://api.scb.se/OV0104/v1/doris/sv/ssd/ME/ME0104/ME0104C/ME0104T3",
dims = list(Region = c('VR00'),
Partimm = c('*'),
ContentsCode = c('*'),
Tid = c('*')),
library(tidyverse)
library(mgcv)
splines_and_derivative <- function(gam_object, n_eval = 200, n_sim = 100, eps = 0.0000001) {
number_of_smooths <- gam_object$smooth %>% length()
data <- model.frame(gam_object)
Vc <- vcov(gam_object,
unconditional = TRUE
)