R/safeRunAndEvaluate.R

Defines functions safeRunAndEvaluate

safeRunAndEvaluate <- function(model, return.model=F)
{
	# this is to trick the strict CRAN check which hates
  # OpenMx style, actually this can be removed in future
	objective <- NULL
	#
	
  if(inherits(model,"MxModel") || inherits(model,"MxRAMModel")){
    modelrun <- try(mxRun(model,silent=TRUE, suppressWarnings=TRUE),silent=T)
    if (is(modelrun,"try-error")) {
      return(NA)
    }  else {
      if (return.model) {
        result <- c()
        result$model <- modelrun
        result$LL <- getLikelihood(modelrun)
        return(result)
      } else {
        return(getLikelihood(modelrun))
      }
    }  
  }
  else if(inherits(model,"lavaan")){
    data <- data.frame(model@Data@X[[1]])
    colnames(data) <- model@Data@ov$name
    modelrun <- try(suppressWarnings(eval(parse(text=paste("lavaan::",model@Options$model.type,'(lavaan::parTable(model),data=data,missing=\'',model@Options$missing,'\')',sep="")))),silent=T)
    if (is(modelrun,"try-error")) {
      #if(control$verbose){message("try error found...")}
      return(NA)
    }  
    else if(modelrun@Fit@converged) {
      if (return.model) {
        result <- c()
        result$model <- modelrun
        result$LL <- -2*lavaan::logLik(modelrun)
        return(result)
      } else {
        return(-2*lavaan::logLik(modelrun))
      }
    }
    else { return(NA)}
  }
}
brandmaier/semtree documentation built on Sept. 7, 2024, 10:38 p.m.