This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| download.file("https://fx.sauder.ubc.ca/etc/CADpages.pdf", dest = "CADpages.pdf") | |
| library(tabulapdf) | |
| xx <- extract_tables("CADpages.pdf") | |
| txt <- xx[[1]][[1]][-(1:4)] |> | |
| ## glitches | |
| gsub(pattern = "[[:alpha:]]", replacement = "") |> | |
| gsub(pattern = "-0.\\s+", replacement = " 0.") |> | |
| gsub(pattern = "-", replacement = "", fixed = TRUE) | |
| txt <- txt[-length(txt)] | |
| hdr <- strsplit(xx[[1]][[1]][4], " ")[[1]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## scraped from Edwards et al 2021 PNAS "Exhaled aerosol increases with COVID-19 infection, age, and obesity" doi: 10.1073/pnas.2021830118 | |
| library(ggplot2); theme_set(theme_bw()) | |
| dd <- read.table(header = TRUE, text = " | |
| BMI_years exhaled_particles super_spreader | |
| 200.035325376 50.0311243148 n | |
| 353.718561745 26.9914433584 n | |
| 362.042435728 75.3702213313 n | |
| 374.750834553 7.02162110504 n | |
| 386.246395482 30.3918564146 n | |
| 393.241199689 0.387986576661 n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## https://help.displayr.com/hc/en-us/articles/360003582875-How-to-Import-a-Wikipedia-Table-Using-R | |
| library(rvest) | |
| page = read_html("https://en.wikipedia.org/wiki/List_of_chemical_elements") | |
| my.table = html_node(page, ".wikitable") |> | |
| html_table(fill = TRUE) | |
| nm_vec <- my.table[[3]][-1] | |
| vowels <- c("a","e","i","o","u") | |
| consonants <- setdiff(letters, vowels) | |
| cc <- function(x) paste(x, collapse = "") | |
| str <- sprintf("^.*[%s]([%s]+[%s]*)$", cc(consonants), cc(vowels), cc(consonants)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(readxl) | |
| library(dplyr) | |
| library(ggplot2) | |
| library(ragg) | |
| ## https://www.sthda.com/english/wiki/ggplot2-themes-and-background-colors-the-3-elements#google_vignette | |
| theme_set(theme_classic(base_size = 16) + | |
| theme(panel.background = element_rect(fill = "lightgray"))) | |
| library(scales) # trans_new() is in the scales library |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## https://bsky.app/profile/benjiturnbull.bsky.social/post/3lpevi67z4c2w | |
| library(lme4) | |
| dd <- data.frame(f = factor(rep(1:3, each = 10))) | |
| set.seed(105) ## seed-hack to get a singular fit ... | |
| dd$y <- simulate(~1 + (1|f), newdata = dd, | |
| newparams = list(beta = 0, sigma = 1, theta = 0.01))[[1]] | |
| m <- lmer(y ~ 1 + (1|f), data = dd) | |
| performance::icc(m) ## NA | |
| multilevelTools::iccMixed("y", "f", data = dd) ## ICC(f) == 0 | |
| flexplot::icc(m)$icc ## 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(glmmTMB) | |
| set.seed(101) | |
| dd <- data.frame(x = rnorm(1000)) | |
| dd$y0 <- simulate_new( ~ 1, | |
| seed = 101, | |
| ziformula = ~ 1, | |
| family = ziGamma(link = "log"), | |
| newdata = dd, | |
| newparams = list( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| download.file("https://www.census.gov/foreign-trade/balance/country.xlsx", dest = "balance.xlsx") | |
| library(readxl) | |
| library(dplyr) | |
| library(ggplot2); theme_set(theme_bw()) | |
| library(ggrepel) | |
| bdat <- read_excel("balance.xlsx") |> | |
| filter(year == 2024) |> | |
| select(countrycode=CTY_CODE, country=CTYNAME,imports=IYR,exports=EYR) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(gstat) | |
| library(lattice) | |
| # Create Data Points (Random) | |
| n <- 50 | |
| data3D <- data.frame(x = runif(n), y = runif(n), z = runif(n), v = rnorm(n)) | |
| coordinates(data3D) = ~x+y+z | |
| # Create empty grid to krige | |
| range1D <- seq(from = 0, to = 1, length = 20) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## https://fediscience.org/@[email protected]/113439584737810431 | |
| library(MASS) # for eqscplot | |
| library(RTMB) | |
| set.seed(101) | |
| n <- 1000 | |
| X <- cbind( | |
| x1 = rnorm(n , 10, 2), | |
| x2 = rnorm(n, 7, 1), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## simulate example (NOT satisfying constraints, but large and close enough that we | |
| ## probably won't hit singular fits etc. etc.) | |
| library(glmmTMB) | |
| set.seed(1001) | |
| nx <- 3 | |
| N <- 1000 | |
| ng <- 20 | |
| dd <- replicate(nx, rnorm(N), simplify = FALSE) |> as.data.frame() |> setNames(paste0("x", 1:nx)) | |
| dd$g <- factor(sample(1:ng, replace = TRUE, size = N)) |
NewerOlder