R/check.fun.arguments.R

# this function checks if all the necessary arguments in the functions are correctly specified


check.fun.arguments <- function(model, surveydata, censusdata, location_survey,
                                mResponse, n_boot, welfare.function){
  ##### check whether model is specified correctly and try to correct misspecification
  if(missing(model)) stop("A model has to be specified")
  if(class(model) != "formula"){
    model <- try(as.formula(model), silent = T)
    if (class(model) == "try-error"){
      stop("model must either be provided as a formula or as a string.
           See ?formula for help")
    }
  }
  ##### check whether surveydata is specified correctly and try to correct
  if(missing(surveydata)) stop("Data frame with the surveydata is missing")
  if(class(surveydata) != "data.frame"){
    surveydata <- try(as.data.frame(surveydata), silent = T)
    if (class(surveydata) == "try-error"){
      stop("survey data should be provided as data.frame or something similar.
           ELLsae was not able to convert your input into a data.frame")
    }
    }
  ##### check whether censusdata is specified correctly and try to correct
  if(missing(censusdata)) stop("Data frame with the censusdata is missing")
  if(class(censusdata) != "data.frame"){ # alternativ if(!is.data.frame(censusdata))?
    censusdata <- try(as.data.frame(censusdata))
    if (class(censusdata) == "try-error"){
      stop("census data should be provided as data.frame or something similar.
           ELLsae was not able to convert your input into a data.frame")
    }
    }
  ##### check whether the locations are specified correctly and try to correct
  if(missing(location_survey)) stop("you have to provide either 1) a vector of locations
                                    of length corresponding to the number of observations
                                    in the survey data or 2) a string with the name of a
                                    variable in the surveydata that provides the locations
                                    of individual observations")
  # if locations are specified as string with variable name, convert into vector
  if (length(location) == 1 & is.character(location)) {
    location_survey <- try(as.factor(eval(parse(text = paste("surv_data$", location, sep = "")))),
                    silent = T)
    if(class(location_survey) == "try-error"){
      "String that was specified as variable name for the location is not the name
      of one of the variables in the survey data set."
    }
  }
  if(length(location_survey != nrow(surveydata))){
    stop("Length of vector of locations that was provided does not correspond to the
          number of observations in the survey data set")
  }


  # # check whether there are different locations in census und survey
  # { survey_l <- levels(as.factor(surveydata[,which( colnames(surveydata)==eval(location_survey))]))
  #   census_l <- levels(as.factor(censusdata[,which( colnames(censusdata)==eval(location_survey))]))
  #   for (i in length(surveyW_l)) {
  #     if(!any(survey_l[i] == census_l)) warning("There are different locations in the surveydata
  #                                               compared to the censusdata")
  #   }}
}
nikosbosse/SAE documentation built on May 12, 2019, 4:37 a.m.