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)) |
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://ivelasq.rbind.io/blog/politely-scraping/index.html | |
## To clean data | |
library(tidyverse) | |
library(lubridate) | |
library(janitor) | |
# To scrape data | |
library(rvest) | |
library(httr) | |
library(polite) |
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://stats.stackexchange.com/questions/649052/selecting-the-correct-r-lme4-syntax-for-nested-models | |
library(lme4) | |
df <- data.frame(Subject = rep(factor(1:120), each = 200), | |
Group = rep(LETTERS[1:3], each = 40*200)) | |
with(df, table(Group)) | |
with(df, table(table(Subject))) | |
df$Accuracy <- simulate(~ Group + (1|Subject), | |
newdata = df, |
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(brms) | |
library(metafor) | |
library(broom) | |
library(broom.mixed) | |
set.seed(101) | |
y <- rnorm(10) | |
sei <- rgamma(10, shape = 1) | |
## metafor: fixed-effect meta-analysis |
NewerOlder