X-Git-Url: https://git.auder.net/images/assets/rpsls.css?a=blobdiff_plain;f=pkg%2FR%2FoptimParams.R;h=2eada8f1f9a4fb98526d82d116bd479ca011f3ba;hb=d294ece1cf943b74d96b26cc28b08c00cb191264;hp=9185efba0f8403cc412eb52f8500fcbf9740b726;hpb=cbd88fe5729bf206a784238a2637aa60e697fcdc;p=morpheus.git diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R index 9185efb..2eada8f 100644 --- a/pkg/R/optimParams.R +++ b/pkg/R/optimParams.R @@ -56,11 +56,6 @@ optimParams = function(K, link=c("logit","probit"), optargs=list()) M <- computeMoments(optargs$X,optargs$Y) } - # TODO: field?! - exactComp <<- optargs$exact - if (is.null(exactComp)) - exactComp <<- FALSE - # Build and return optimization algorithm object methods::new("OptimParams", "li"=link, "M1"=as.double(M[[1]]), "M2"=as.double(M[[2]]), "M3"=as.double(M[[3]]), "K"=as.integer(K)) @@ -218,13 +213,23 @@ setRefClass( { "Run optimization from x0 with solver..." - if (!is.numeric(x0) || any(is.na(x0)) || length(x0) != (d+2)*K-1 - || any(x0[1:(K-1)] < 0) || sum(x0[1:(K-1)]) > 1) - { - stop("x0: numeric vector, no NA, length (d+2)*K-1, sum(x0[1:(K-1) >= 0]) <= 1") - } - - op_res = constrOptim( x0, .self$f, .self$grad_f, + if (!is.list(x0)) + stop("x0: list") + if (is.null(x0$β)) + stop("At least x0$β must be provided") + if (!is.matrix(x0$β) || any(is.na(x0$β)) || ncol(x0$β) != K) + stop("x0$β: matrix, no NA, ncol == K") + if (is.null(x0$p)) + x0$p = rep(1/K, K-1) + else if (length(x0$p) != K-1 || sum(x0$p) > 1) + stop("x0$p should contain positive integers and sum to < 1") + # Next test = heuristic to detect missing b (when matrix is called "beta") + if (is.null(x0$b) || all(x0$b == x0$β)) + x0$b = rep(0, K) + else if (any(is.na(x0$b))) + stop("x0$b cannot have missing values") + + op_res = constrOptim( linArgs(x0), .self$f, .self$grad_f, ui=cbind( rbind( rep(-1,K-1), diag(K-1) ), matrix(0, nrow=K, ncol=(d+1)*K) ), @@ -249,6 +254,8 @@ setRefClass( # link="probit"; order=2; λ=c(531.8099,586.8893,523.5816); b=c(-118.512674,-3.488020,2.109969) # Switch to pracma package for that (but it seems slow...) + exactComp <- FALSE #TODO: global, or argument... + if (exactComp && link == "probit") { # Use exact computations