set.seed(1)
data(occupationalStatus)
## Definition of old MultHomog plug-in function
MultHomogPlugIn <- function(...){
designList <- lapply(list(...), nnet:::class.ind)
## get labels for all levels
allLevels <- lapply(designList, colnames)
labels <- unique(unlist(allLevels))
nLevels <- length(labels)
## expand design matrices if necessary
if (!all(mapply(identical, allLevels, list(labels)))) {
labels <- sort(labels)
M <- matrix(0, nrow = nrow(designList[[1]]), ncol = nLevels,
dimnames = list(NULL, labels))
designList <- lapply(designList, function(design, M) {
M[,colnames(design)] <- design
M}, M)
}
pprod <- function(...) {
factorList <- list(...)
nFactors <- length(factorList)
if (nFactors == 0) return(1)
else if (nFactors == 1) return(factorList[[1]])
else {
tryProduct <- try(factorList[[1]] * do.call("Recall", factorList[-1]),
silent = TRUE)
if (inherits(tryProduct, "try-error"))
stop("multiplication not implemented for types of argument supplied")
else tryProduct
}
}
predictor <- function(coef) {
do.call("pprod", lapply(designList, "%*%", coef))
}
localDesignFunction <- function(coef, ind = NULL, ...) {
X <- 0
vList <- lapply(designList, "%*%", coef)
for (i in seq(designList)) {
if (is.null(ind))
X <- X + designList[[i]] * drop(do.call("pprod", vList[-i]))
else
X <- X + designList[[i]][, ind] *
drop(do.call("pprod", vList[-i]))
}
X
}
list(labels = labels, predictor = predictor,
localDesignFunction = localDesignFunction)
}
## Fit an association model with homogeneous row-column effects
RChomog <- gnm(Freq ~ origin + destination + Diag(origin, destination) +
Nonlin(MultHomogPlugIn(origin, destination)), family = poisson,
data = occupationalStatus)
Run the code above in your browser using DataLab