binomial_to_bernoulli_data.Rd
Convert binomial data to bernoulli data by expanding dataset.
binomial_to_bernoulli_data(
data.,
y,
size,
type = c("rest", "total"),
response_name = "response",
rest_name = NULL
)
A dataframe
Column with 'successes' in binomial distribution y~bin(size, p)
Column with 'failures', i.e. size-y or 'total', i.e. size.
Whether size
is rest (i.e. 'failures') or 'total'
Name of response variable in output dataset.
Name of 'failures' in column response_name
.
dat <- budworm
dat <- dat[dat$dose %in% c(1,2), ]
dat$ntotal <- 5
dat
#> sex dose ndead ntotal
#> 1 male 1 1 5
#> 2 male 2 4 5
#> 7 female 1 0 5
#> 8 female 2 2 5
dat.a <- dat |>
binomial_to_bernoulli_data(ndead, ntotal, type="total")
dat.b <- dat |>
dplyr::mutate(nalive=ntotal-ndead) |> dplyr::select(-ntotal) |>
binomial_to_bernoulli_data(ndead, nalive, type="rest")
m0 <- glm(cbind(ndead, ntotal-ndead) ~ dose + sex, data=dat, family=binomial())
m1 <- glm(ndead / ntotal ~ dose + sex, data=dat, weight=ntotal, family=binomial())
ma <- glm(response ~ dose + sex, data=dat.a, family=binomial())
mb <- glm(response ~ dose + sex, data=dat.b, family=binomial())
dat.a$response
#> [1] ndead not_ndead not_ndead not_ndead not_ndead ndead ndead
#> [8] ndead ndead not_ndead not_ndead not_ndead not_ndead not_ndead
#> [15] not_ndead ndead ndead not_ndead not_ndead not_ndead
#> Levels: not_ndead ndead
dat.b$response ## Not same and therefore the following do not match
#> [1] ndead nalive nalive nalive nalive ndead ndead ndead ndead nalive
#> [11] nalive nalive nalive nalive nalive ndead ndead nalive nalive nalive
#> Levels: nalive ndead
all.equal(coef(m0), coef(ma))
#> [1] TRUE
all.equal(coef(m0), coef(mb))
#> [1] TRUE
all.equal(coef(m1), coef(ma))
#> [1] TRUE
all.equal(coef(m1), coef(mb))
#> [1] TRUE