Skip to content

Commit e2d3d1b

Browse files
committed
refactor: replace 1:nrow(x) with safer seq_len(nrow(x))
1 parent c57e67a commit e2d3d1b

11 files changed

Lines changed: 30 additions & 30 deletions

R/FDboost.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ FDboost <- function(formula, ### response ~ xvars
11881188
## generate an id-variable for a regular response
11891189
if(is.null(id)){
11901190
if(scalarResponse){
1191-
id <- 1:NROW(response)
1191+
id <- seq_len(NROW(response))
11921192
}else{
11931193
id <- rep(1:ydim[1], times = ydim[2])
11941194
}

R/baselearners.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ integrationWeights <- function(X1, xind, id = NULL){
125125

126126
# taking into account missing values
127127
if(anyNA(X1)){
128-
Lneu <- sapply(1:nrow(X1), function(i){
128+
Lneu <- sapply(seq_len(nrow(X1)), function(i){
129129
x <- X1[i,]
130130

131131
if(!anyNA(x)){
@@ -840,7 +840,7 @@ X_conc <- function(mf, vary, args) {
840840

841841
## <FIXME> is that line still necessary?
842842
## important for prediction, otherwise id=NULL and yind is multiplied accordingly
843-
if(is.null(id)) id <- 1:nrow(X1)
843+
if(is.null(id)) id <- seq_len(nrow(X1))
844844

845845
## check yind
846846
if(args$format=="long" && length(yind)!=length(id)) stop(xname, ": Index of response and id do not have the same length")
@@ -1156,7 +1156,7 @@ X_hist <- function(mf, vary, args) {
11561156
## <FIXME> is that line still necessary? should it be there in long and wide format?
11571157
###### EXTRA LINE in comparison to X_hist
11581158
## important for prediction, otherwise id=NULL and yind is multiplied accordingly
1159-
if(is.null(id)) id <- 1:nrow(X1)
1159+
if(is.null(id)) id <- seq_len(nrow(X1))
11601160

11611161
## check yind
11621162
if(args$format=="long" && length(yind)!=length(id)) stop(xname, ": Index of response and id do not have the same length")
@@ -2519,9 +2519,9 @@ bolsc <- function(..., by = NULL, index = NULL, intercept = TRUE, df = NULL,
25192519
if(is.null(index)){
25202520

25212521
if(is.null(weights)){ ## use weights
2522-
w <- 1:nrow(mf)
2522+
w <- seq_len(nrow(mf))
25232523
}else{
2524-
w <- rep(1:nrow(mf), weights)
2524+
w <- rep(seq_len(nrow(mf)), weights)
25252525
}
25262526

25272527
temp <- X_olsc(mf[w, , drop = FALSE], vary,
@@ -2531,9 +2531,9 @@ bolsc <- function(..., by = NULL, index = NULL, intercept = TRUE, df = NULL,
25312531
}else{
25322532

25332533
if(is.null(weights)){ ## use weights
2534-
w <- 1:nrow(mf[index, , drop = FALSE])
2534+
w <- seq_len(nrow(mf[index, , drop = FALSE]))
25352535
}else{
2536-
w <- rep(1:nrow(mf[index, , drop = FALSE]), weights)
2536+
w <- rep(seq_len(nrow(mf[index, , drop = FALSE])), weights)
25372537
}
25382538

25392539
temp <- X_olsc(mf = (mf[index, , drop = FALSE])[w, , drop = FALSE], vary = vary,

R/bootstrapCIs.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ bootstrapCI <- function(object, which = NULL,
404404
lapply(listOfQuantiles[isSurface],
405405
function(x){
406406

407-
retL <- lapply(1:nrow(x), function(i)
407+
retL <- lapply(seq_len(nrow(x)), function(i)
408408
matrix(x[i,], nrow = length(attr(x, "y"))))
409409
names(retL) <- levels
410410
return(retL)

R/constrainedX.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@
104104
mboost_intern(bl2, fun = "model.frame.blg") )
105105
index1 <- bl1$get_index()
106106
index2 <- bl2$get_index()
107-
if (is.null(index1)) index1 <- 1:nrow(mf)
108-
if (is.null(index2)) index2 <- 1:nrow(mf)
107+
if (is.null(index1)) index1 <- seq_len(nrow(mf))
108+
if (is.null(index2)) index2 <- seq_len(nrow(mf))
109109

110110
mfindex <- cbind(index1, index2)
111111
index <- NULL
@@ -312,8 +312,8 @@ bl_lin_matrix_a <- function(blg, Xfun, args) {
312312
# K2 <- args$K2
313313

314314
# ## per default do not expand the marginal design matrices
315-
# expand_index1 <- 1:nrow(X$X1)
316-
# expand_index2 <- 1:nrow(X$X2)
315+
# expand_index1 <- seq_len(nrow(X$X1))
316+
# expand_index2 <- seq_len(nrow(X$X2))
317317

318318
## weights-matrix W: weights are for single observations in the matrix Y
319319
## but the marginal bl work either on columns or rows of Y
@@ -375,8 +375,8 @@ bl_lin_matrix_a <- function(blg, Xfun, args) {
375375
### but: this does not work correctly: problem with factor remains
376376
# ## W cannot be computed from w1 and w2,
377377
# ## -> blow up the marginal design matrices and use W with them,
378-
# expand_index1 <- rep(1:nrow(X$X1), times = nrow(X$X2))
379-
# expand_index2 <- rep(1:nrow(X$X2), each = nrow(X$X1))
378+
# expand_index1 <- rep(seq_len(nrow(X$X1)), times = nrow(X$X2))
379+
# expand_index2 <- rep(seq_len(nrow(X$X2)), each = nrow(X$X1))
380380
# ## all( c(W) == weights) is TRUE, ordering of weights must match to blown-up marginal design matrices
381381
# ## standardize weights to compensate for the blow-up of the marginal design-matrices
382382
# #w1 <- c(W) / mean(rowSums(W)) ## for some special cases (e.g. BS on rows): mean(rowSums(W)) == nrow(X$X2)
@@ -986,8 +986,8 @@ NULL
986986

987987
index1 <- bl1$get_index()
988988
index2 <- bl2$get_index()
989-
if (is.null(index1)) index1 <- 1:nrow(mf)
990-
if (is.null(index2)) index2 <- 1:nrow(mf)
989+
if (is.null(index1)) index1 <- seq_len(nrow(mf))
990+
if (is.null(index2)) index2 <- seq_len(nrow(mf))
991991

992992
mfindex <- cbind(index1, index2)
993993
index <- NULL

R/crossvalidation.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
#' cvr <- applyFolds(mod, folds = folds_bs, grid = 1:75)
136136
#'
137137
#' ## weights per observation point
138-
#' folds_bs_long <- folds_bs[rep(1:nrow(folds_bs), times = mod$ydim[2]), ]
138+
#' folds_bs_long <- folds_bs[rep(seq_len(nrow(folds_bs)), times = mod$ydim[2]), ]
139139
#' attr(folds_bs_long, "type") <- "3-fold bootstrap"
140140
#' ## compute out-of-bag risk on the 3 folds for 1 to 75 boosting iterations
141141
#' cvr3 <- cvrisk(mod, folds = folds_bs_long, grid = 1:75)
@@ -541,7 +541,7 @@ applyFolds <- function(object, folds = cv(rep(1, length(unique(object$id))), typ
541541
oobrisk <- t(as.data.frame(oobrisk))
542542
## oobrisk <- oobrisk / colSums(OOBweights[object$id, ]) # is done in dummyfct()
543543
colnames(oobrisk) <- grid
544-
rownames(oobrisk) <- 1:nrow(oobrisk)
544+
rownames(oobrisk) <- seq_len(nrow(oobrisk))
545545
attr(oobrisk, "risk") <- fam_name
546546
attr(oobrisk, "call") <- call
547547
attr(oobrisk, "mstop") <- grid
@@ -656,7 +656,7 @@ applyFolds <- function(object, folds = cv(rep(1, length(unique(object$id))), typ
656656
#' cvr2 <- validateFDboost(mod, folds = folds_bs, grid = 1:75)
657657
#'
658658
#' ## weights per observation point
659-
#' folds_bs_long <- folds_bs[rep(1:nrow(folds_bs), times = mod$ydim[2]), ]
659+
#' folds_bs_long <- folds_bs[rep(seq_len(nrow(folds_bs)), times = mod$ydim[2]), ]
660660
#' attr(folds_bs_long, "type") <- "3-fold bootstrap"
661661
#' ## compute out-of-bag risk on the 3 folds for 1 to 75 boosting iterations
662662
#' cvr3 <- cvrisk(mod, folds = folds_bs_long, grid = 1:75)
@@ -1739,7 +1739,7 @@ cvMa <- function(ydim, weights = rep(1, l = ydim[1] * ydim[2]),
17391739
if ( (nrowY * ncolY) != n) stop("The arguments weights and ydim do not match.")
17401740

17411741
## cvMa is only a wrapper for cvLong
1742-
foldsMa <- cvLong(id = rep(1:nrowY, times = ncolY), weights = weights,
1742+
foldsMa <- cvLong(id = rep(seq_len(nrowY), times = ncolY), weights = weights,
17431743
type = type, B=B, prob = 0.5, strata = NULL)
17441744
return(foldsMa)
17451745
}

R/hmatrix.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#' # ids and times in the time id matrix
5858
#' # for bhistx baselearner, there may be an additional id variable for the tensor product
5959
#' newdat <- reweightData(data = list(hmat = myhmatrix,
60-
#' repIDx = rep(1:nrow(attr(myhmatrix,'x')), length(attr(myhmatrix,"argvals")))),
60+
#' repIDx = rep(seq_len(nrow(attr(myhmatrix,'x'))), length(attr(myhmatrix,"argvals")))),
6161
#' vars = "hmat", index = c(1,1,2), idvars="repIDx")
6262
#' length(newdat$repIDx)
6363
#'
@@ -75,7 +75,7 @@ hmatrix <- function(time, id, x, argvals=seq_len(ncol(x)),
7575

7676
## check that id is integer valued containing 1, 2, 3, ..., n
7777
## and that x has n rows
78-
stopifnot( all(sort(unique(id)) == 1:nrow(x)) )
78+
stopifnot( all(sort(unique(id)) == seq_len(nrow(x))) )
7979
stopifnot(length(time)==length(id))
8080

8181
# convert x to a matrix, especially if x is of class AsIs

R/methods.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,19 +1189,19 @@ coef.FDboost <- function(object, raw = FALSE, which = NULL,
11891189
#print(attr(d, "varnms"))
11901190
vari <- names(d)[1]
11911191
if(is.factor(d[[vari]])){
1192-
d[[vari]] <- d[[vari]][ rep(1:NROW(d[[vari]]), times=length(d[[attr(object$yind ,"nameyind")]]) ) ]
1192+
d[[vari]] <- d[[vari]][ rep(seq_len(NROW(d[[vari]])), times=length(d[[attr(object$yind ,"nameyind")]]) ) ]
11931193
if(trm$dim>1) d[[attr(object$yind ,"nameyind")]] <- rep(d[[attr(object$yind ,"nameyind")]],
11941194
each=length(unique(d[[vari]])) )
11951195
}else{
11961196
# expand signal variable
11971197
if( grepl("bhist(", trm$get_call(), fixed = TRUE) |
11981198
grepl("bsignal", trm$get_call()) | grepl("bfpc", trm$get_call()) ){
11991199
vari <- names(d)[!names(d) %in% attr(d, "varnms")]
1200-
d[[vari]] <- d[[vari]][ rep(1:NROW(d[[vari]]), times=NROW(d[[vari]])), ]
1200+
d[[vari]] <- d[[vari]][ rep(seq_len(NROW(d[[vari]])), times=NROW(d[[vari]])), ]
12011201

12021202
}else{ # expand scalar variable
12031203
vari <- names(d)[1]
1204-
if(vari!=attr(object$yind ,"nameyind")) d[[vari]] <- d[[vari]][ rep(1:NROW(d[[vari]]), times=NROW(d[[vari]])) ]
1204+
if(vari!=attr(object$yind ,"nameyind")) d[[vari]] <- d[[vari]][ rep(seq_len(NROW(d[[vari]])), times=NROW(d[[vari]])) ]
12051205
}
12061206
# expand yind
12071207
if(trm$dim>1) d[[attr(object$yind ,"nameyind")]] <- rep(d[[attr(object$yind ,"nameyind")]],

man/applyFolds.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/hmatrix.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/validateFDboost.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)