aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Kellner <ken@kenkellner.com>2023-12-05 15:31:19 -0500
committerKen Kellner <ken@kenkellner.com>2023-12-05 15:31:19 -0500
commit24e1bfef2148e043dc4727a95bbf7eb5fd416f18 (patch)
tree15af8f34e9e9b7d4e975d2f41b43f19443607ae2
parent07a1c53902871fefd08b19bfcbc824b95117e6a0 (diff)
Remove old code
-rw-r--r--R/datacheck.R36
-rw-r--r--R/geninits.R86
-rw-r--r--R/processinput.R110
3 files changed, 0 insertions, 232 deletions
diff --git a/R/datacheck.R b/R/datacheck.R
deleted file mode 100644
index 7c40473..0000000
--- a/R/datacheck.R
+++ /dev/null
@@ -1,36 +0,0 @@
-
-data.check <- function(x,name,verbose=TRUE){
-
- test = FALSE
-
- if(is.data.frame(x)){
- if(!is.null(dim(x))){
- if(verbose){cat('\nConverting data frame \'',name,'\' to matrix.\n',sep="")}
- x = as.matrix(x)
- } else {
- if(verbose){cat('\nConverting data frame',name,'to vector.\n')}
- x = as.vector(x)}
- }
-
-
- if (is.numeric(x)&&is.matrix(x)){
- #if(1%in%dim(x)){
- # if(verbose){cat('\nConverting 1-column matrix \'',name,'\' to vector\n',sep="")}
- # x = as.vector(x)
- #}
- test = TRUE
- }
-
- if(is.numeric(x)&&is.array(x)&&!test){
- test = TRUE
- }
-
- if (is.numeric(x)&&is.vector(x)&&!test){
- test = TRUE
- }
-
- if(test){
- return(x)
- } else{return('error')}
-
-}
diff --git a/R/geninits.R b/R/geninits.R
deleted file mode 100644
index e62ba0f..0000000
--- a/R/geninits.R
+++ /dev/null
@@ -1,86 +0,0 @@
-
-gen.inits <- function(inits,n.chains,seed,parallel){
-
- if(!is.null(seed)){
- warning("The 'seed' argument will be deprecated in the next version. You can set it yourself with set.seed() instead.")
- #Save old seed if it exists
- if(exists('.Random.seed')){
- old.seed <- .Random.seed
- }
- #Generate seed for each chain
- set.seed(seed)
-
- }
-
- #Error check and run init function if necessary
- if(is.list(inits)){
- if(length(inits)!=n.chains){stop('Length of initial values list != number of chains')}
- init.values <- inits
- } else if(is.function(inits)){
- init.values <- list()
- for (i in 1:n.chains){
- init.values[[i]] <- inits()
- }
- } else if(is.null(inits)){
- init.values <- NULL
-
- } else {stop('Invalid initial values. Must be a function or a list with length=n.chains')}
-
- #Add random seed info if specified
- if(!is.null(seed)){
-
- init.rand <- floor(runif(n.chains,1,100000))
-
- #Restore old seed if it exists
- if(exists('old.seed')){
- assign(".Random.seed", old.seed, pos=1)
- }
-
- #Add random seeds to inits
- if(is.null(inits)){
- init.values <- vector("list",length=n.chains)
- for(i in 1:n.chains){
- init.values[[i]]$.RNG.name="base::Mersenne-Twister"
- init.values[[i]]$.RNG.seed=init.rand[i]
- }
-
- } else if(is.list(init.values)){
- for(i in 1:n.chains){
- init.values[[i]]$.RNG.name="base::Mersenne-Twister"
- init.values[[i]]$.RNG.seed=init.rand[i]
- }
-
- } else if (is.function(inits)){
- for (i in 1:n.chains){
- init.values[[i]]$.RNG.name="base::Mersenne-Twister"
- init.values[[i]]$.RNG.seed=init.rand[i]
- }
-
- }
-
-
- #If seed is not set
- } else {
-
- other.RNG <- all(c(".RNG.name",".RNG.seed")%in%names(init.values[[1]]))
-
- needs.RNG <- is.null(init.values)|!other.RNG
-
- #If parallel and no custom RNG has been set, add one. Otherwise all chains will start with same seed.
- # if(needs.RNG&parallel){
- if(needs.RNG){
-
- init.rand <- floor(runif(n.chains,1,100000))
-
- if(is.null(init.values)){init.values <- vector("list",length=n.chains)}
-
- for(i in 1:n.chains){
- init.values[[i]]$.RNG.name="base::Mersenne-Twister"
- init.values[[i]]$.RNG.seed=init.rand[i]
- }
-
- }
- }
-
- return(init.values)
-}
diff --git a/R/processinput.R b/R/processinput.R
deleted file mode 100644
index c208b07..0000000
--- a/R/processinput.R
+++ /dev/null
@@ -1,110 +0,0 @@
-
-process.input = function(x,y,inits,n.chains,n.iter,n.burnin,n.thin,n.cores,DIC=FALSE,autojags=FALSE,max.iter=NULL,
- verbose=TRUE,parallel=FALSE,seed=NULL){
- if(verbose){cat('\nProcessing function input.......','\n')}
-
- #Quality control
- if(n.iter<=n.burnin){
- stop('Number of iterations must be larger than burn-in.\n')
- }
-
- if(parallel){
- #Set number of clusters
- p <- detectCores()
- if(is.null(n.cores)){
- if(is.na(p)){
- p <- n.chains
- if(verbose){
- options(warn=1)
- warning('Could not detect number of cores on the machine. Defaulting to cores used = number of chains.')
- options(warn=0,error=NULL)
- }
- }
- n.cores <- min(p,n.chains)
- } else {
- if(n.cores>p){
- if(verbose){
- options(warn=1)
- warning(paste('You have specified more cores (',n.cores,') than the available number of cores on this machine (',p,').\nReducing n.cores to max of ',p,'.',sep=""))
- options(warn=0,error=NULL)
- }
- n.cores <- p
- }
- }
- }
-
- if(autojags){
- if(n.chains<2){stop('Number of chains must be >1 to calculate Rhat.')}
- if(max.iter<n.burnin&verbose){
- options(warn=1)
- warning('Maximum iterations includes burn-in and should be larger than burn-in.')
- options(warn=0,error=NULL)
- }
- }
-
- if(n.thin>1&&(n.iter-n.burnin)<10&&verbose){
- options(warn=1)
- warning('The number of iterations is very low; jagsUI may crash. Recommend reducing n.thin to 1 and/or increasing n.iter.')
- options(warn=0,error=NULL)
- }
-
- final.chain.length <- (n.iter - n.burnin) / n.thin
- even.length <- floor(final.chain.length) == final.chain.length
- if(!even.length&verbose){
- options(warn=1)
- warning('Number of iterations saved after thinning is not an integer; JAGS will round it up.')
- options(warn=0,error=NULL)
- }
-
- #Check if supplied parameter vector is the right format
- if((is.character(y)&is.vector(y))){
- } else{stop('The parameters to save must be a vector containing only character strings.\n')}
-
- #If DIC requested, add deviance to parameters (if not already there)
- if(DIC&&(!'deviance'%in%y)){
- params <- c(y,"deviance")
- } else {params <- y}
-
- #Check if supplied data object is the proper format
- if(is.list(x)||(is.character(x)&is.vector(x))){
- } else{stop('Input data must be a list of data objects OR a vector of data object names (as strings)\n')}
-
- if(is.list(x)&&all(sapply(x,is.character))){
- warning("Suppling a list of character strings to the data argument will be deprecated in the future")
- x = unlist(x)
- }
-
- if((is.list(x)&&is.null(names(x)))||(is.list(x)&&any(names(x)==""))){
- stop('At least one of the elements in your data list does not have a name\n')
- }
-
- #Convert a supplied vector of characters to a list of data objects
- if((is.character(x)&is.vector(x))){
- warning("Suppling a character vector to the data argument will be deprecated in the future")
- temp = lapply(x,get,envir = parent.frame(2))
- names(temp) = x
- x = temp
- }
-
- #Check each component of data object for issues and fix if possible
- for (i in 1:length(x)){
-
- if(is.factor(x[[i]])){
-
- stop('\nElement \'',names(x[i]) ,'\' in the data list is a factor.','\n','Convert it to a series of dummy/indicator variables or a numeric vector as appropriate.\n')
-
- }
-
- process <- data.check(x[[i]],name = names(x[i]),verbose=verbose)
- if(!is.na(process[1])&&process[1]=="error"){stop('\nElement \'',names(x[i]) ,'\' in the data list cannot be coerced to one of the','\n','allowed formats (numeric scalar, vector, matrix, or array)\n')
- } else{x[[i]] <- process}
-
- }
-
- #Get initial values
- init.vals <- gen.inits(inits,n.chains,seed,parallel)
-
- if(verbose){cat('\nDone.','\n','\n')}
- return(list(data=x,params=params,inits=init.vals,n.cores=n.cores))
-
-}