# R/Estimators.R In pimdevel: Probabilistic Index Models

#### Documented in estimator.BBestimator.glmestimator.nleqslv

#' Estimator functions for probabilistic index models
#'
#' of a probabilistic index model or \code{\link{pim}}. All functions mentioned on this page, are essentially wrappers around different solver functions.
#'
#' All functions share the same three arguments, being the design matrix \code{x},
#' the response vector \code{y} and the start values for the estimating function.
#' If you follow the same principles, you can write your own wrapper function
#' for any solver function of your choice.
#'
#' The solvers \code{estimator.nleqslv} and \code{estimator.BBsolve}
#' allow for specification of your own score function as well. For
#' this, you have to construct a function that takes three arguments
#' \describe{
#' \item{x}{The model matrix}
#' \item{y}{the vector with pseudo-observations}
#' }
#' The function should return a function that can be used in
#' \code{\link[BB]{BBsolve}}. If you don't specify, the package
#' contains the function \code{\link{CreateScoreFun}} to do
#' this for you.
#'
#' @section WARNING: If you specify your own score function
#' without changing the estimators for the variance-covariance
#' matrix, this vcov matrix will be blatantly wrong!!!!!
#'
#'
#' algorithms.
#'
#' @param x a model matrix for the respective pim model. See also
#'
#' @param y a vector with the response for the respective pim model.
#' @param start a vector as long as there are columns in \code{x}, containing
#' the starting values for the algorithm
#' @param method A vector of integers specifying which
#' Barzilai-Borwein steplengths should be used in a consecutive
#' manner. The methods will be used in the order specified.
#' @param construct a function that creates the score function
#' used by either \code{\link{nleqslv}} or
#' \code{\link{BBsolve}} for numerical optimization. See Details.
#' The estimator \code{estimator.glm} doesn't allow f
#' or specification of your own score function.
#' @param ... extra arguments passed down to the actual solver function. See details.
#' @param control a list with extra controlling parameters for
#' \code{BBsolve}. See the help page of \code{\link{BBsolve}} for
#'
#' @return a list with following elements:
#'  \item{coef}{the estimated coefficients}
#'
#' @import nleqslv
#' @name estimators
#' @aliases estimator.nleqslv estimator.glm
#' @rdname estimators
#' @export
estimator.nleqslv <-
construct = NULL, ...){
construct <- if(is.null(construct)) CreateScoreFun else
match.fun(construct)

res <- nleqslv(start,fn, ...)

if(res$termcd != 1){ warning(paste("nleqslv says:", res$message,"\n",
call. = FALSE)
}
list(coef = res$x) } #' @rdname estimators #' @export estimator.glm <- function(x, y, start= rep(0,ncol(x)), link="logit", ...){ if(is.character(link)){ family <- .glmfamilies[[link]] if(is.null(family)) stop("Link function",link,"not recognized by estimator.glm") } res <- withCallingHandlers( glm.fit(x,y, start = start, family=family, ...), warning = catch.noninteger.handler ) list(coef = res$coef)
}

#' @rdname estimators
#' @import BB
#' @export
estimator.BB <-
construct = NULL,
method = c(1,2,3),
control=list(NM = c(FALSE,TRUE)), ...){

construct <- if(is.null(construct)) CreateScoreFun else
match.fun(construct)
res <- BBsolve(start,fn, control = control, method = method,...)

if(res$convergence != 0 ){ warning(paste("BBsolve says:", res$message, "\n",