R/myprobit.R

#' Probit estimation
#'
#' This function estimates a tobit model for a lieft censoring at 0. Its arguments are a vector for the dependent variable (y) and a matrix for the explanatory variables (X).
#'
#' It uses the low memory BFGS algorithm from the optim function. The starting values are the coefficients of a linear model and 1 for the sd.
#' @examples
#' set.seed(1)
#' X=matrix(rnorm(20),ncol=2)
#' y=pmax(X %*% c(2,1) +rnorm(10),0)
#' mytobit(X = X,y = y)

myprobit<- function(X,y){
    X=cbind(rep(1,nrow(X)),X)
    res <- optim(
        par=c(coef( lm(y~.,data=as.data.frame(cbind(y,X[,-1]))) ) ,1),
        fn=loglik_probit,
        X=X,
        y=y,
        method  = "BFGS",
        control = list(fnscale = -1), #maxmization
        # lower=c(rep(-Inf,ncol(X)-1),1e-10)
    )
    res$fitted.val <- pnorm(X %*% res$par[-length(res$par)]) %>% 
        round(digits=2)
    return(res)
}
VictorAmse/myprobit documentation built on May 9, 2019, 9:56 p.m.