Skip to content

Commit

Permalink
Fix issue with sorting of predictor_types
Browse files Browse the repository at this point in the history
  • Loading branch information
mhesselbarth committed Nov 5, 2024
1 parent 162a9ec commit b2b6dbd
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions R/train.R
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,10 @@ methods::setMethod(
model[['biodiversity']][[id]][['predictors']] <- env
model[['biodiversity']][[id]][['predictors_names']] <- names(env)[names(env) %notin% c("ID", "x", "y", "Intercept")]
model[['biodiversity']][[id]][['predictors_types']] <- model[['predictors_types']][model[['predictors_types']][, "predictors"] %in% names(env), ]
}
# makes sure ordering is identical
sort_id <- match(model[['biodiversity']][[id]][['predictors_names']], model[['biodiversity']][[id]][['predictors_types']]$predictors)
model[['biodiversity']][[id]][['predictors_types']] <- model[['biodiversity']][[id]][['predictors_types']][sort_id, ]
}

# If the method of integration is weights and there are more than 2 datasets, combine
if(method_integration == "weight" && length(model$biodiversity)>=2){
Expand Down Expand Up @@ -914,7 +917,9 @@ methods::setMethod(
pred_prs <- model$predictors_object$get_names()
model$predictors_names <- pred_tmp
model$predictors_types <- model$predictors_types[model$predictors_type$predictors %in% pred_tmp, ]
model$predictors <- model$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model$predictors_types <- model$predictors_types[match(model$predictors_names, model$predictors_types$predictors), ]
model$predictors <- dplyr::select(model$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
model$predictors_object <- model$predictors_object$clone(deep = TRUE)
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
Expand Down Expand Up @@ -956,7 +961,9 @@ methods::setMethod(
pred_prs <- model$predictors_object$get_names()
model$predictors_names <- pred_tmp
model$predictors_types <- model$predictors_types[model$predictors_type$predictors %in% pred_tmp, ]
model$predictors <- model$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model$predictors_types <- model$predictors_types[match(model$predictors_names, model$predictors_types$predictors), ]
model$predictors <- dplyr::select(model$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
model$predictors_object <- model$predictors_object$clone(deep = TRUE)
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
Expand Down Expand Up @@ -990,7 +997,9 @@ methods::setMethod(
model2$predictors_object <- model$predictors_object$clone(deep = TRUE)
model2$predictors_names <- pred_tmp
model2$predictors_types <- model2$predictors_types[model2$predictors_type$predictors %in% pred_tmp, ]
model2$predictors <- model2$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model2$predictors_types <- model2$predictors_types[match(model2$predictors_names, model2$predictors_types$predictors), ]
model2$predictors <- dplyr::select(model2$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model2$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
}
Expand Down Expand Up @@ -1121,7 +1130,9 @@ methods::setMethod(
model2$predictors_object <- model$predictors_object$clone(deep = TRUE)
model2$predictors_names <- pred_tmp
model2$predictors_types <- model2$predictors_types[model2$predictors_type$predictors %in% pred_tmp, ]
model2$predictors <- model2$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model2$predictors_types <- model2$predictors_types[match(model2$predictors_names, model2$predictors_types$predictors), ]
model2$predictors <- dplyr::select(model2$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model2$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
}
Expand Down Expand Up @@ -1251,7 +1262,9 @@ methods::setMethod(
model2$predictors_object <- model$predictors_object$clone(deep = TRUE)
model2$predictors_names <- pred_tmp
model2$predictors_types <- model2$predictors_types[model2$predictors_type$predictors %in% pred_tmp, ]
model2$predictors <- model2$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model2$predictors_types <- model2$predictors_types[match(model2$predictors_names, model2$predictors_types$predictors), ]
model2$predictors <- dplyr::select(model2$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model2$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
}
Expand Down Expand Up @@ -1405,7 +1418,9 @@ methods::setMethod(
model2$predictors_object <- model$predictors_object$clone(deep = TRUE)
model2$predictors_names <- pred_tmp
model2$predictors_types <- model2$predictors_types[model2$predictors_type$predictors %in% pred_tmp, ]
model2$predictors <- model2$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model2$predictors_types <- model2$predictors_types[match(model2$predictors_names, model2$predictors_types$predictors), ]
model2$predictors <- dplyr::select(model2$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model2$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
}
Expand Down Expand Up @@ -1531,7 +1546,9 @@ methods::setMethod(
model2$predictors_object <- model$predictors_object$clone(deep = TRUE)
model2$predictors_names <- pred_tmp
model2$predictors_types <- model2$predictors_types[model2$predictors_type$predictors %in% pred_tmp, ]
model2$predictors <- model2$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model2$predictors_types <- model2$predictors_types[match(model2$predictors_names, model2$predictors_types$predictors), ]
model2$predictors <- dplyr::select(model2$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model2$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
}
Expand Down Expand Up @@ -1659,7 +1676,9 @@ methods::setMethod(
model2$predictors_object <- model$predictors_object$clone(deep = TRUE)
model2$predictors_names <- pred_tmp
model2$predictors_types <- model2$predictors_types[model2$predictors_type$predictors %in% pred_tmp, ]
model2$predictors <- model2$predictors |> dplyr::select(dplyr::any_of(c("x", "y", pred_tmp)))
# make sure all in same order
model2$predictors_types <- model2$predictors_types[match(model2$predictors_names, model2$predictors_types$predictors), ]
model2$predictors <- dplyr::select(model2$predictors, dplyr::any_of(c("x", "y", pred_tmp)))
if (length(pred_prs[!pred_prs %in% pred_tmp]) > 0){
model2$predictors_object$rm_data(pred_prs[!pred_prs %in% pred_tmp])
}
Expand Down

0 comments on commit b2b6dbd

Please sign in to comment.