| f_binomial | R Documentation |
This function can be used in the family argument of create_sampler
or generate_data to specify a binomial sampling distribution.
f_binomial(
link = c("logit", "probit"),
n.trial = NULL,
control = binomial_control()
)
link |
the name of a link function. Currently the only allowed link functions
for the binomial distribution are |
n.trial |
the number of binomial trials. This can be specified either as a formula for a variable number of trials, or as a scalar value for a common number of trials for all units. |
control |
a list with computational options. These options can
be specified using function |
For the binomial family, the left hand side of the formula argument of
create_sampler can be specified in several ways (similar to the
options allowed by glm for binomial family):
as a factor, character, or boolean variable, say y.
In this case the first level of as.factor(y) is interpreted as 'failures' and all
other values as 'successes'. This option can only be used for binary data.
as a numeric or integer vector with no values strictly between 0 and 1.
The values are then interpreted as numbers of successes. This requires specifying
the number of trials through argument n.trial, unless the data is
binary (values 0 and 1 only). For non-integer values a warning is issued.
as a numeric vector with values between 0 and 1. The values are then interpreted
as the proportion of the number of successes. This requires specifying the number of trials
through argument n.trial.
as a two-column integer matrix. The first column is interpreted as the number of successes, the second column as the number of failures.
A family object.
y <- c(TRUE, FALSE, TRUE)
sampler <- create_sampler(y ~ 1, family=f_binomial())
# logistic binary regression is the default, and may also be
# specified as family="binomial"
sim <- MCMCsim(sampler, n.chain=2, n.iter=100, burnin=50, verbose=FALSE)
summary(predict(sim, newdata=data.frame(id=1)))
y <- c(0, 0, 1, 1, 0, 0, 0, 0, 1)
sampler <- create_sampler(y ~ 1, family=f_binomial(link="probit"))
sim <- MCMCsim(sampler, n.chain=2, n.iter=100, burnin=50, verbose=FALSE)
summary(predict(sim, newdata=data.frame(id=1)))
sampler <- create_sampler(c(0.2, 0.3, 0.5, 0.0) ~ 1, family=f_binomial(n.trial=10))
# is interpreted as
sampler <- create_sampler(c(2, 3, 5, 0) ~ 1, family=f_binomial(n.trial=10))
sim <- MCMCsim(sampler, n.chain=2, n.iter=100, burnin=50, verbose=FALSE)
summary(predict(sim, newdata=data.frame(id=1)))
n <- 1000
dat <- data.frame(
x = runif(n),
g = factor(sample(1:10, n, replace=TRUE)),
trials = sample(1:5, n, replace=TRUE)
)
v <- rnorm(10)
dat$y <- rbinom(n, size=dat$trials, prob=1 / (1 + exp(-(1 - dat$x + v[dat$g]))))
sampler <- create_sampler(y ~ x + (1|g), data=dat, family=f_binomial(n.trial = ~ trials))
# alternatively:
sampler <- create_sampler(cbind(y, trials - y) ~ x + (1|g), data=dat, family="binomial")
sim <- MCMCsim(sampler, store.all=TRUE, burnin=200, n.iter=300, n.chain=2, verbose=FALSE)
summ <- summary(sim)
plot(v, summ$gen2[, "Mean"]); abline(0, 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.