Skip to content

Commit 3d6f2a9

Browse files
Merge pull request #50 from m-muecke/fixed
perf: use fixed strings where possible
2 parents 4f668f1 + 67a9fb4 commit 3d6f2a9

File tree

5 files changed

+93
-95
lines changed

5 files changed

+93
-95
lines changed

R/FDboost.R

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ FDboost <- function(formula, ### response ~ xvars
465465
## check if number of opening brackets is equal to number of closing brackets
466466
equalBrackets <- sapply(seq_along(trmstrings2), function(i)
467467
{
468-
lengths(regmatches(trmstrings2[i], gregexpr("\\(", trmstrings2[i]))) ==
469-
lengths(regmatches(trmstrings2[i], gregexpr("\\)", trmstrings2[i])))
468+
lengths(regmatches(trmstrings2[i], gregexpr("(", trmstrings2[i], fixed = TRUE))) ==
469+
lengths(regmatches(trmstrings2[i], gregexpr(")", trmstrings2[i], fixed = TRUE)))
470470
})
471471
}
472472

@@ -486,8 +486,8 @@ FDboost <- function(formula, ### response ~ xvars
486486
if(length(trmstrings) > 0){
487487
## insert index into the other base-learners of the tensor-product as well
488488
for(i in seq_along(trmstrings)){
489-
if(grepl( "%X", trmstrings2[i])){
490-
temp <- unlist(strsplit(trmstrings2[i], "%X"))
489+
if(grepl( "%X", trmstrings2[i], fixed = TRUE)){
490+
temp <- unlist(strsplit(trmstrings2[i], "%X", fixed = TRUE))
491491
temp1 <- temp[-length(temp)]
492492
## http://stackoverflow.com/questions/2261079
493493
## delete all trailing whitespace
@@ -497,13 +497,13 @@ FDboost <- function(formula, ### response ~ xvars
497497
trmstrings2[i] <- paste0(paste0(temp1, collapse = " %X"), " %X", temp[length(temp)])
498498
}
499499
## do not add index to base-learners bhistx()
500-
if( grepl("bhistx", trmstrings[i]) ) trmstrings2[i] <- trmstrings[i]
500+
if( grepl("bhistx", trmstrings[i], fixed = TRUE) ) trmstrings2[i] <- trmstrings[i]
501501
## do not add an index if an index is already part of the formula
502502
if( grepl("index[[:blank:]]*=", trmstrings[i]) ) trmstrings2[i] <- trmstrings[i]
503503
## do not add an index if an index for %A%, %A0%, %O%
504-
if( grepl("%A%", trmstrings[i]) ) trmstrings2[i] <- trmstrings[i]
505-
if( grepl("%A0%", trmstrings[i]) ) trmstrings2[i] <- trmstrings[i]
506-
if( grepl("%O%", trmstrings[i]) ) trmstrings2[i] <- trmstrings[i]
504+
if( grepl("%A%", trmstrings[i], fixed = TRUE) ) trmstrings2[i] <- trmstrings[i]
505+
if( grepl("%A0%", trmstrings[i], fixed = TRUE) ) trmstrings2[i] <- trmstrings[i]
506+
if( grepl("%O%", trmstrings[i], fixed = TRUE) ) trmstrings2[i] <- trmstrings[i]
507507
## do not add an index for base-learner that do not have brackets
508508
if( i %in% which(!equalBrackets) ) trmstrings2[i] <- trmstrings[i]
509509
}
@@ -538,7 +538,7 @@ FDboost <- function(formula, ### response ~ xvars
538538
scalarResponse <- TRUE
539539
if(is.null(timeformula)) scalarNoFLAM <- TRUE
540540

541-
if(grepl("df", formula[3]) || !grepl("lambda", formula[3]) ){
541+
if(grepl("df", formula[3], fixed = TRUE) || !grepl("lambda", formula[3], fixed = TRUE) ){
542542
timeformula <- ~bols(ONEtime, intercept = FALSE, df = 1)
543543
}else{
544544
timeformula <- ~bols(ONEtime, intercept = FALSE)
@@ -671,23 +671,23 @@ FDboost <- function(formula, ### response ~ xvars
671671

672672
## get formula over time
673673
tfm <- paste(deparse(timeformula), collapse = "")
674-
tfm <- strsplit(tfm, "~")[[1]]
675-
tfm <- strsplit(tfm[2], "\\+")[[1]]
674+
tfm <- strsplit(tfm, "~", fixed = TRUE)[[1]]
675+
tfm <- strsplit(tfm[2], "+", fixed = TRUE)[[1]]
676676

677677
## get formula in covariates
678678
cfm <- paste(deparse(formula), collapse = "")
679-
cfm <- strsplit(cfm, "~")[[1]]
679+
cfm <- strsplit(cfm, "~", fixed = TRUE)[[1]]
680680
cfm0 <- cfm
681681
#xfm <- strsplit(cfm[2], "\\+")[[1]]
682682
xfm <- trmstrings
683683

684684
## check that the timevariable in timeformula and in the bhistx-base-learners have the same name
685-
if(any(grepl("bhistx", trmstrings))){
685+
if(any(grepl("bhistx", trmstrings, fixed = TRUE))){
686686
for(j in seq_along(trmstrings)){
687-
if(any(grepl("bhistx", trmstrings[j]))){
688-
if(grepl("%X", trmstrings[j]) ){
687+
if(any(grepl("bhistx", trmstrings[j], fixed = TRUE))){
688+
if(grepl("%X", trmstrings[j], fixed = TRUE) ){
689689
temp <- strsplit(trmstrings[[j]], "%X.*%")[[1]]
690-
temp <- temp[ grepl("bhistx", temp) ]
690+
temp <- temp[ grepl("bhistx", temp, fixed = TRUE) ]
691691
## pryr::standardise_call(quote(bhistx(X1h, df=3)))
692692
temp_name <- all.vars(formula(paste("~", temp)))[1]
693693
}else{
@@ -707,13 +707,13 @@ FDboost <- function(formula, ### response ~ xvars
707707
}
708708
}
709709

710-
yfm <- strsplit(cfm[1], "\\+")[[1]] ## name of response
710+
yfm <- strsplit(cfm[1], "+", fixed = TRUE)[[1]] ## name of response
711711

712712
## set up formula for effects constant in time
713713
if(length(where.c) > 0){
714714
# set c_df to the df/lambda in timeformula
715-
if( grepl("lambda", tfm) ||
716-
( grepl("bols", tfm) && !grepl("df", tfm)) ){
715+
if( grepl("lambda", tfm, fixed = TRUE) ||
716+
( grepl("bols", tfm, fixed = TRUE) && !grepl("df", tfm, fixed = TRUE)) ){
717717
c_lambda <- eval(parse(text = paste0(tfm, "$dpp(rep(1.0,", length(time), "))$df()")))["lambda"]
718718
cfm <- paste("bols(ONEtime, intercept = FALSE, lambda = ", c_lambda ,")")
719719
} else{
@@ -745,20 +745,20 @@ FDboost <- function(formula, ### response ~ xvars
745745
}
746746

747747
# do not expand an effect bconcurrent() or bhist() with timeformula
748-
if( length(c(grep("bconcurrent", tmp), grep("bhis", tmp)) ) > 0 )
749-
tmp[c(grep("bconcurrent", tmp), grep("bhist", tmp))] <- xfm[c(grep("bconcurrent", tmp), grep("bhist", tmp))]
748+
if (any(grepl("bconcurrent|bhis", tmp)))
749+
tmp[c(grep("bconcurrent", tmp, fixed = TRUE), grep("bhist", tmp, fixed = TRUE))] <- xfm[c(grep("bconcurrent", tmp, fixed = TRUE), grep("bhist", tmp, fixed = TRUE))]
750750

751751
## do not expand effects in formula including %A% with timeformula
752-
if( length(grep("%A%", xfm)) > 0 )
753-
tmp[grep("%A%", xfm)] <- xfm[grep("%A%", xfm)]
752+
if( any(grepl("%A%", xfm, fixed = TRUE)) )
753+
tmp[grep("%A%", xfm, fixed = TRUE)] <- xfm[grep("%A%", xfm, fixed = TRUE)]
754754

755755
## do not expand effects in formula including %A0% with timeformula
756-
if( length(grep("%A0%", xfm)) > 0 )
757-
tmp[grep("%A0%", xfm)] <- xfm[grep("%A0%", xfm)]
756+
if( any(grepl("%A0%", xfm, fixed = TRUE)) )
757+
tmp[grep("%A0%", xfm, fixed = TRUE)] <- xfm[grep("%A0%", xfm, fixed = TRUE)]
758758

759759
## do not expand effects in formula including %O% with timeformula
760-
if( length(grep("%O%", xfm)) > 0 )
761-
tmp[grep("%O%", xfm)] <- xfm[grep("%O%", xfm)]
760+
if( any(grepl("%O%", xfm, fixed = TRUE)) )
761+
tmp[grep("%O%", xfm, fixed = TRUE)] <- xfm[grep("%O%", xfm, fixed = TRUE)]
762762

763763
## expand with a constant effect in t-direction
764764
if(length(where.c) > 0){
@@ -833,11 +833,11 @@ FDboost <- function(formula, ### response ~ xvars
833833

834834
### replace "1" with intercept base learner
835835
formula_intercept <- FALSE
836-
if ( any( gsub(" ", "", strsplit(cfm0[2], "\\+")[[1]]) == "1")){
836+
if ( any( gsub(" ", "", strsplit(cfm0[2], "+", fixed = TRUE)[[1]], fixed = TRUE) == "1")){
837837
formula_intercept <- TRUE
838838
## use df or lambda as in timeformula
839-
if( any(grepl("lambda", deparse(timeformula))) ||
840-
any(( grepl("bols", deparse(timeformula)) & !grepl("df", deparse(timeformula)))) ){
839+
if( any(grepl("lambda", deparse(timeformula), fixed = TRUE)) ||
840+
any(( grepl("bols", deparse(timeformula), fixed = TRUE) & !grepl("df", deparse(timeformula), fixed = TRUE))) ){
841841
tmp <- c("bols(ONEx, intercept = FALSE, lambda = 0)", tmp)
842842
} else{
843843
tmp <- c("bols(ONEx, intercept = FALSE, df = 1)", tmp)
@@ -879,9 +879,9 @@ FDboost <- function(formula, ### response ~ xvars
879879
## get the limits argument
880880
current_bl <- attr(terms_fm_bhist, "variables")[[places_bhist[pl] + 1]]
881881
# for base-learner with interaction, find bhistx / bhist
882-
if(any(grepl("%X", current_bl))){
882+
if(any(grepl("%X", current_bl, fixed = TRUE))){
883883
#current_bl <- current_bl[ grepl("bhist", current_bl) ]
884-
arg_limits <- eval(as.call(as.list(current_bl[grepl("bhist", current_bl)])[[1]])$limits)
884+
arg_limits <- eval(as.call(as.list(current_bl[grepl("bhist", current_bl, fixed = TRUE)])[[1]])$limits)
885885
}else{
886886
# limits argument of bhist / bhistx
887887
arg_limits <- eval(as.call(current_bl)$limits)
@@ -1163,7 +1163,7 @@ FDboost <- function(formula, ### response ~ xvars
11631163
if(check0 && length(ret$baselearner) > 1 && is.null(id) && dim(response)[2] != 1){
11641164

11651165
# do not check the smooth intercept
1166-
if(any( gsub(" ", "", strsplit(cfm[2], "\\+")[[1]]) == "1")){
1166+
if(any( gsub(" ", "", strsplit(cfm[2], "+", fixed = TRUE)[[1]], fixed = TRUE) == "1")){
11671167
effectsToCheck <- 2:length(ret$baselearner)
11681168
}else{
11691169
effectsToCheck <- seq_along(ret$baselearner)

R/crossvalidation.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ applyFolds <- function(object, folds = cv(rep(1, length(unique(object$id))), typ
235235

236236
# Function to suppress the warning of missings in the response
237237
h <- function(w){
238-
if( any( grepl( "response contains missing values;", w) ) )
238+
if( any( grepl( "response contains missing values;", w, fixed = TRUE) ) )
239239
invokeRestart( "muffleWarning" )
240240
}
241241

@@ -296,16 +296,16 @@ applyFolds <- function(object, folds = cv(rep(1, length(unique(object$id))), typ
296296
# the probelm with such base-learners is that their data is not contained in object$data
297297
# using object$baselearner[[j]]$get_data() is difficult as this can be blow up by index for %X%
298298
singleBls <- gsub("\\s", "", unlist(lapply(strsplit(
299-
strsplit(object$formulaFDboost, "~")[[1]][2], # split formula
300-
"\\+")[[1]], # split additive terms
299+
strsplit(object$formulaFDboost, "~", fixed = TRUE)[[1]][2], # split formula
300+
"+", fixed = TRUE)[[1]], # split additive terms
301301
function(y) strsplit(y, split = "%.{1,3}%")) # split single baselearners
302302
))
303303

304304
singleBls <- singleBls[singleBls != "1"]
305305

306-
if(any(!grepl("\\(", singleBls)))
306+
if(any(!grepl("(", singleBls, fixed = TRUE)))
307307
stop(paste0("applyFolds can not deal with the following base-learner(s) without brackets: ",
308-
toString(singleBls[!grepl("\\(", singleBls)])))
308+
toString(singleBls[!grepl("(", singleBls, fixed = TRUE)])))
309309

310310

311311
## check if data includes all variables
@@ -701,9 +701,9 @@ validateFDboost <- function(object, response = NULL,
701701
msg = "'validateFDboost' is deprecated. Use 'applyFolds' and 'bootstrapCI' instead.")
702702

703703
names_bl <- names(object$baselearner)
704-
if(any(grepl("brandomc", names_bl))) message("For brandomc, the transformation matrix Z is fixed over all folds.")
705-
if(any(grepl("bolsc", names_bl))) message("For bolsc, the transformation matrix Z is fixed over all folds.")
706-
if(any(grepl("bbsc", names_bl))) message("For bbsc, the transformation matrix Z is fixed over all folds.")
704+
if(any(grepl("brandomc", names_bl, fixed = TRUE))) message("For brandomc, the transformation matrix Z is fixed over all folds.")
705+
if(any(grepl("bolsc", names_bl, fixed = TRUE))) message("For bolsc, the transformation matrix Z is fixed over all folds.")
706+
if(any(grepl("bbsc", names_bl, fixed = TRUE))) message("For bbsc, the transformation matrix Z is fixed over all folds.")
707707

708708
type <- attr(folds, "type")
709709
if(is.null(type)) type <- "unknown"
@@ -755,7 +755,7 @@ validateFDboost <- function(object, response = NULL,
755755

756756
# Function to suppress the warning of missings in the response
757757
h <- function(w){
758-
if( any( grepl( "response contains missing values;", w) ) )
758+
if( any( grepl( "response contains missing values;", w, fixed = TRUE) ) )
759759
invokeRestart( "muffleWarning" )
760760
}
761761

@@ -956,7 +956,7 @@ validateFDboost <- function(object, response = NULL,
956956
}
957957

958958
## only makes sense for type="curves" with leaving-out one curve per fold!!
959-
if(grepl( "curves", type)){
959+
if(grepl( "curves", type, fixed = TRUE)){
960960
# predict response for all mstops in grid out of bag
961961
# predictions for each response are in a vector!
962962
oobpreds0 <- lapply(modRisk, function(x) x$predGrid)
@@ -1061,7 +1061,7 @@ validateFDboost <- function(object, response = NULL,
10611061

10621062
### predictions of terms based on the coefficients for each model
10631063
# only makes sense for type="curves" with leaving-out one curve per fold!!
1064-
if(grepl("curves", type)){
1064+
if(grepl("curves", type, fixed = TRUE)){
10651065
for(l in 1:(length(modRisk[[1]]$mod$baselearner)+1)){
10661066
predCV[[l]] <- t(sapply(seq_along(modRisk), function(g){
10671067
if(l == 1){ # save offset of model
@@ -1561,7 +1561,7 @@ plot_bootstrapped_coef <- function(temp, l,
15611561
quanty <- quantile(temp$y, probs=probs, type=1)
15621562

15631563
# set lower triangular matrix to NA for historic effect
1564-
if(grepl("bhist", temp$main)){
1564+
if(grepl("bhist", temp$main, fixed = TRUE)){
15651565
for(k in seq_along(temp$value)){
15661566
temp$value[[k]][temp$value[[k]]==0] <- NA
15671567
}
@@ -1675,9 +1675,9 @@ cvrisk.FDboost <- function(object, folds = cvLong(id=object$id, weights=model.we
16751675
if(!length(unique(object$offset)) == 1) message("The smooth offset is fixed over all folds.")
16761676

16771677
names_bl <- names(object$baselearner)
1678-
if(any(grepl("brandomc", names_bl))) message("For brandomc, the transformation matrix Z is fixed over all folds.")
1679-
if(any(grepl("bolsc", names_bl))) message("For bolsc, the transformation matrix Z is fixed over all folds.")
1680-
if(any(grepl("bbsc", names_bl))) message("For bbsc, the transformation matrix Z is fixed over all folds.")
1678+
if(any(grepl("brandomc", names_bl, fixed = TRUE))) message("For brandomc, the transformation matrix Z is fixed over all folds.")
1679+
if(any(grepl("bolsc", names_bl, fixed = TRUE))) message("For bolsc, the transformation matrix Z is fixed over all folds.")
1680+
if(any(grepl("bbsc", names_bl, fixed = TRUE))) message("For bbsc, the transformation matrix Z is fixed over all folds.")
16811681

16821682
class(object) <- "mboost"
16831683

R/factorize.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ factorize.FDboost <- function(x, newdata = NULL, newweights = 1, blwise = TRUE,
266266
e[[i]]$ens <- unlist(lapply(cf[[i]], asplit, 2), recursive = FALSE)
267267
e[[i]]$ens <- Map( function(x, cls) {
268268
bm <- list(model = x)
269-
class(bm) <- gsub("bl", "bm", cls)
269+
class(bm) <- gsub("bl", "bm", cls, fixed = TRUE)
270270
bm
271271
},
272272
x = e[[i]]$ens[bl_order[[i]]],
@@ -356,4 +356,4 @@ plot.FDboost_fac <- function(x, which = NULL, main = NULL, ...) {
356356
main <- names(x$baselearner)[w]
357357
for(i in seq_along(w))
358358
plot.mboost(x, which = w[i], main = main[i], ...)
359-
}
359+
}

0 commit comments

Comments
 (0)