Skip to content

Instantly share code, notes, and snippets.

View njtierney's full-sized avatar
🐝
Back at work

Nicholas Tierney njtierney

🐝
Back at work
View GitHub Profile

This was code originally written by Nick Golding, in August 2022.

It was initially written to demonstrate using symmetrical terms and the benefit of them in conmat.

They handily demonstrate a nice plot of the different terms in a gam - effectively giving us a "contact matrix" of the effect of each term. The part where that happens is where predict is used inside mutate.

library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.9-1. For overview type 'help("mgcv-package")'.
library(greta)
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
#>     %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
# blog post?

my_mean <- function(x, ...) mean(x, na.rm = TRUE, ...)

vec <- c(1:5, NA, 5:1)

my_mean(vec)
#> [1] 3
my_mean(vec, na.rm = TRUE)
library(tibble)
library(tidyverse)
ihr_car_ratio <- greta::uniform(0,1)
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> ✔ Initialising python and checking dependencies ... done!
#> 

ihr_car_ratio
#> greta array (variable following a uniform distribution)
# something like this?
library(tidyverse)
library(lobstr)
existing_data_list <- list(
  data.frame(x = 1:5, y = 0),
  data.frame(z = 5:1),
  data.frame(y = 1:5, a = 2)
)
library(conmat)
library(socialmixr)
library(tidyverse)
countries_of_interest <- c("Italy", "Spain")
list_countries <- map(countries_of_interest,
\(x) wpp_age(x, years = 2015))
list_conmat_pop <- map(
.x = list_countries,
bee_trips <- 1:10

sample(bee_trips,
       size = 100,
       replace = TRUE)
#>   [1]  2  4  5  2  3  7  5  8  5  7  6  2  8  4 10  6  2  3 10  4  6  8  9  5  6
#>  [26]  6  8  7  6  8  5  4  7  5  6  3  5  3  7  1  9  4  8  7  8  3 10  3  5 10
#>  [51] 10  5  8  6  2  6  6  3  6  5  9  6  5  9  6  9  2  5  6  4  8  4  6  2  7
#&gt; [76] 8 9 10 7 1 3 4 5 5 4 6 8 6 2 4 5 9 10 5 9 4 5 2 5 3
library(socialmixr)
library(conmat)
italy_2005 <- wpp_age("Italy", "2005")
head(italy_2005)
italy_2005_pop <- as_conmat_population(
data = italy_2005,
age = lower.age.limit,
library(tidyverse)
library(palmerpenguins)

ggplot(penguins,
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           colour = island)) + 
  geom_point() + 
  scale_colour_brewer(palette = "Dark2")
# this didn't work in MCMC, we'll have to work out why.
# simulate species count data over 10 timesteps
n_times <- 10
times <- seq_len(n_times)
truth <- 100 * exp(sin(times))
y <- rpois(n_times, truth)
plot(y ~ times, type = "l")

library(greta.gam)