1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
## Authors
## Martin Schlather, schlather@math.uni-mannheim.de
##
### Diese Datei wandelt RMmodel in eine Liste um
##
## Copyright (C) 2015 -- 2016 Alexander Malinowski, Martin Schlather
## 2017 -- 2017 Martin Schlather
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 3
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
reps <- function(n, sign=",") paste(rep(sign, n), collapse="")
## FUNCTION-STARP***********************************************************************************
## NAME parseModel, isGenuineCovModel, extractSummands
## PARAM $model - list, formula
## AUTHOR Sebastian Gross
### DATE 29.08.2011
## changed by Martin Schlather 2015 -- 2017
## FUNCTION-END*************************************************************************************
parseModel <- function(model, ..., x=NULL) {
## check whether $model is already in list syntax
if (is.list(model)) return(model)
## check whether $model has RMmodel syntax
if (isRMmodel(model)) return(buildCovList(model, x=x))
## check whether $model has correct formula syntax
if (!is(model, "formula")) stop(syntaxError)
## extract tokens/summands
tmpList <- list()
## ignore rest of the formula
rightSide <- tail(as.character(model), 1)
chars <- strsplit(rightSide, "")[[1]]
## toggles parenthesis, eg. whether we confront a toplevel plus or not
parToggle <- 0
token <- ""
for (char in chars) {
if (char == SYMBOL_PLUS && parToggle == 0) {
tmpList <- c(tmpList, token)
token <- ""
} else {
if (char != " ") token <- paste(token, char, sep="")
if (char == SYMBOL_L_PAR) parToggle <- parToggle+ 1
if (char == SYMBOL_R_PAR) parToggle <- parToggle- 1
}
}
tmpList <- c(tmpList, token)
summands <- vector("list", length(tmpList))
for (i in 1:length(tmpList)) summands[[i]] <- removeParenthesis(tmpList[[i]])
## if (length(summands)==1) return(summands) ## sonst steht unten paste(NULL), was "" gibt
paramEnv <- new.env(parent=.GlobalEnv)
dots <- list(...)
assign("dots", dots, envir=paramEnv)
if (length(dots)>0)
eval(parse(text = paste(names(dots), "<- dots[[", 1:length(dots), "]]",
collapse=";")),
envir=paramEnv)
trendfct <- paste(RM_TREND[1], "(", sep="")
isGenuineCovModel <- lapply(summands, FUN=function(name)
## Martin: habe hier RFtrend hinzugefuegt!
substr(name, 1, length(trendfct)) != trendfct &&
(is(try(eval(parse(text=name),envir=paramEnv), silent=TRUE), CLASS_CLIST) ||
(regexpr("^[[:alnum:]_]+\\([[:print:]]*\\)$", name) == 1 &&
(exists(fun <- strsplit(name, "\\(")[[1]][1]) && is(get(fun), CLASS_RM))
)))
isGenuineCovModel <- unlist(isGenuineCovModel)
summands <- c(summands[!isGenuineCovModel],
list(paste(unlist(summands[isGenuineCovModel]),
collapse=SYMBOL_PLUS)))
if (length(summands) == 1) {
listModel <- buildFactorList(summands[[1]], Env=paramEnv, ..x..=x)
} else {
listModel <- list(SYMBOL_PLUS)
##last <- getLastCovIndex(summands)
for (i in 1:length(summands))
listModel[[i+1]] <- buildFactorList(summands[[i]], Env=paramEnv, ..x..=x)
firstgenuine <- sum(!isGenuineCovModel) + 2
if (firstgenuine <= length(listModel) && listModel[[firstgenuine]][[1]] == SYMBOL_PLUS) {
listModel <- c(listModel[1:(firstgenuine - 1)], listModel[[firstgenuine]][-1])
names(listModel) <- NULL
}
}
return(listModel)
}
## FUNCTION-STARP***********************************************************************************
## NAME buildCovList
## PARAM $model - RMmodel
## RETURN list
## REQUIRE none
## ENSURE isListModel(output) == TRUE
## SEE RMmodel, devel-doc
## AUTHOR Sebastian Gross
# modified 2015--2017 by Martin Schlather
### DATE 26.08.2011
## FUNCTION-END*************************************************************************************
buildCovList <- function(model, x=NULL) {
if (is.atomic(model) || is.list(model) ||
is.language(model) || is.environment(model))
return(model) ## for recursive calling
if (!isRMmodel(model)) stop('model must be of class ', CLASS_CLIST)
li <- c(list(model@name),
lapply(model@par.model[model@par.model != RM_DEFAULT],
FUN=buildCovList, x=x),
lapply(model@submodels, FUN=buildCovList, x=x) )
if (li[[1]] == RM_PLUS[1]) li[[1]] <- SYMBOL_PLUS
if (li[[1]] == RM_MULT[1]) li[[1]] <- SYMBOL_MULT
## par.general.is.default <-
## unlist(lapply(model@par.general, FUN=function(x) x==RM_DEFAULT))
if (length(model@par.general)>0 &
!all(model@par.general==RM_DEFAULT)) {
li <- c(DOLLAR[1],
lapply(model@par.general[model@par.general != RM_DEFAULT],
FUN=buildCovList, x=x),
list(li))
# if (length(pos <- which(names(li)=="Aniso")) > 0)
# ## in c-level, parameter is called 'A'
# names(li)[pos] <- "A"
}
return(li)
}
## FUNCTION-STARP***********************************************************************************
## NAME removeParenthesis
## PARAM $string - string
## RETURN string
## REQUIRE none
## ENSURE The returned string is one not enclosed by parenthesis
## AUTHOR Sebastian Gross
## DATE 26.08.2011
## FUNCTION-END*************************************************************************************
removeParenthesis <- function(string)
{
# split the string
chars <- strsplit(string, "")[[1]]
while (head(chars, 1) == SYMBOL_L_PAR &&
tail(chars, 1) == SYMBOL_R_PAR)
{
chars <- chars[-1]
chars <- chars[-length(chars)]
}
# rejoin the string
string <- ""
for (char in chars)
{
string <- paste(string, char, sep="")
}
return (string)
}
## FUNCTION-STARP***********************************************************************************
## NAME buildFactorList, isFormalCovModel, catch
## PARAM $summand - string
## AUTHOR Sebastian Gross
### DATE 29.08.2011
## changed by Martin Schlather 2015 -- 2017
## FUNCTION-END*************************************************************************************
buildFactorList <- function(summand, Env, ..x..) { #, last
## remove parenthesis
factorA <- removeParenthesis(summand)
## formerly partially 'catch'
X <- eval(parse(text=factorA), envir=Env)
## Print(X, factorA, !isFormalCovModel(factorA))
isFormalCovModel <- is(X, CLASS_CLIST) ||
(regexpr("^[[:alnum:]_]+\\([[:print:]]*\\)$", factorA) == 1 &&
exists(fun <- strsplit(factorA, "\\(")[[1]][1]) && is(get(fun), CLASS_RM))
if (!isFormalCovModel) { ## (( && last))
if (is.factor(X)) {
lev <- levels(X)
if (length(lev) > MAXSUB^2 + 1)
stop("max number of factors limited to ", MAXSUB^2)
L <- list(RM_PLUS[1])
i <- 2
while (i <= length(lev)) {
last <- min(length(lev), i + MAXSUB -1)
plusList <- list(RM_PLUS[1])
while (i <= last) {
tmpList <- list(RM_COVARIATE)
tmpList[[COVARIATE_C_NAME]] <- as.numeric(X == lev[i])
tmpList[[COVARIATE_X_NAME]] <- ..x..
tmpList[[COVARIATE_ADDNA_NAME]] <- TRUE
plusList[[length(plusList) + 1]] <- tmpList
i <- i + 1;
}
L[[length(L) + 1]] <- plusList
}
return(if (length(lev) == 2) tmpList
else if (length(lev) <= MAXSUB + 1) plusList
else L)
# else list("RMtrend", mean=if (length(lev) <= MAXSUB + 1) plusList
# else list("RMtrend", L)))
} else if (is.vector(X)) {
if (length(X) > 1) {
tmpList <- list(RM_COVARIATE)
tmpList[[COVARIATE_C_NAME]] <- X
tmpList[[COVARIATE_X_NAME]] <- ..x..
tmpList[[COVARIATE_ADDNA_NAME]] <- TRUE
} else {
tmpList <- list(R_CONST)
tmpList[[CONST_A_NAME]] <-
if (is.finite(X) && X == 1) NA else as.numeric(X)
}
return(tmpList)
} else stop(ERRMIXED)
} else {
tmpList <- buildCovList(X, x=..x..)
return(tmpList)
}
}
|