## Normal model ------------------------------
## prepare outcome variable
library(dplyr, warn.conflicts = FALSE)
spend <- nld_expenditure |>
mutate(log_spend = log(value + 1))
## specify model
mod <- mod_norm(log_spend ~ age * diag + year,
data = spend,
weights = 1) |>
set_datamod_noise(sd = 0.1)
## fit model
mod <- mod |>
fit()
mod
## create new aggregated diagnositic
## group variable
library(dplyr, warn.conflicts = FALSE)
spend <- spend |>
mutate(diag_ag = case_when(
diag == "Neoplasms" ~ diag,
diag == "Not allocated" ~ diag,
TRUE ~ "Other"
))
## assume size of measurement errors
## varies across these aggregated groups
sd_diag <- data.frame(diag_ag = c("Neoplasms",
"Not allocated",
"Other"),
sd = c(0.05, 0.2, 0.1))
## fit model that uses diagnostic-specific
## standard deviations
mod <- mod_norm(log_spend ~ age * diag + year,
data = spend,
weights = 1) |>
set_datamod_noise(sd = sd_diag)
## Poisson model -----------------------------
mod <- mod_pois(deaths ~ month,
data = usa_deaths,
exposure = 1) |>
set_datamod_noise(sd = 200)
Run the code above in your browser using DataLab