tidymodels / tidymodels.org

Source of tidymodels.org
https://www.tidymodels.org/
19 stars 15 forks source link

new Learn article: using offsets #59

Open topepo opened 8 months ago

topepo commented 8 months ago

With the new tidymodels offsets package (offsetreg), I realize that we have not documented that this can already be done:


library(tidymodels)
library(offsetreg)
library(poissonreg)

us_deaths$offset <- log(us_deaths$population)

fit <- 
  poisson_reg() %>% 
  fit(deaths ~ gender + age_group + year + offset(offset), data = us_deaths)

tidy(fit)
#> # A tibble: 9 × 5
#>   term            estimate std.error statistic p.value
#>   <chr>              <dbl>     <dbl>     <dbl>   <dbl>
#> 1 (Intercept)    -18.3     0.136        -135.        0
#> 2 genderMale       0.328   0.000388      845.        0
#> 3 age_group35-44   0.443   0.00177       250.        0
#> 4 age_group45-54   1.21    0.00155       780.        0
#> 5 age_group55-64   1.99    0.00146      1367.        0
#> 6 age_group65-74   2.71    0.00143      1899.        0
#> 7 age_group75-84   3.65    0.00141      2579.        0
#> 8 age_group85+     4.77    0.00140      3402.        0
#> 9 year             0.00568 0.0000672      84.5       0```

<sup>Created on 2024-01-23 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>