# \donttest{
summary(farmsubmission)
# construct vglm model matrix
X <- matrix(data = 0, nrow = 2 * NROW(farmsubmission), ncol = 7)
X[1:NROW(farmsubmission), 1:4] <- model.matrix(
~ 1 + log_size + log_distance + C_TYPE,
farmsubmission
)
X[-(1:NROW(farmsubmission)), 5:7] <- X[1:NROW(farmsubmission), c(1, 3, 4)]
# this attribute tells the function which elements of the design matrix
# correspond to which linear predictor
attr(X, "hwm") <- c(4, 3)
# get starting points
start <- glm.fit(
y = farmsubmission$TOTAL_SUB,
x = X[1:NROW(farmsubmission), 1:4],
family = poisson()
)$coefficients
res <- estimatePopsizeFit(
y = farmsubmission$TOTAL_SUB,
X = X,
method = "IRLS",
priorWeights = 1,
family = ztoigeom(),
control = controlMethod(verbose = 5),
coefStart = c(start, 0, 0, 0),
etaStart = matrix(X %*% c(start, 0, 0, 0), ncol = 2),
offset = cbind(rep(0, NROW(farmsubmission)), rep(0, NROW(farmsubmission)))
)
# extract results
# regression coefficient vector
res$beta
# check likelihood
ll <- ztoigeom()$makeMinusLogLike(y = farmsubmission$TOTAL_SUB, X = X)
-ll(res$beta)
# number of iterations
res$iter
# working weights
head(res$weights)
# Compare with optim call
res2 <- estimatePopsizeFit(
y = farmsubmission$TOTAL_SUB,
X = X,
method = "optim",
priorWeights = 1,
family = ztoigeom(),
coefStart = c(start, 0, 0, 0),
control = controlMethod(verbose = 1, silent = TRUE),
offset = cbind(rep(0, NROW(farmsubmission)), rep(0, NROW(farmsubmission)))
)
# extract results
# regression coefficient vector
res2$beta
# check likelihood
-ll(res2$beta)
# number of calls to log lik function
# since optim does not return the number of
# iterations
res2$iter
# optim does not calculated working weights
head(res2$weights)
# }
Run the code above in your browser using DataLab