R 向返回错误的函数添加if语句
这是我之前发布的一篇文章的后续文章。代码如下:R 向返回错误的函数添加if语句,r,R,这是我之前发布的一篇文章的后续文章。代码如下: data(iris) df <- iris %>% filter(Species != "setosa") %>% mutate(Species = +(Species == "virginica")) %>% sample_n(10) ########################################## var_combos <- expand.grid(colnames(df[,
data(iris)
df <- iris %>%
filter(Species != "setosa") %>%
mutate(Species = +(Species == "virginica")) %>%
sample_n(10)
##########################################
var_combos <- expand.grid(colnames(df[,1:4]), colnames(df[,1:4])) %>%
filter(!Var1 == Var2)
boundary_lists <- map2(
.x = var_combos$Var1,
.y = var_combos$Var2,
~select(df, .x, .y) %>%
summarise(
minX = min(.[[1]], na.rm = TRUE),
maxX = max(.[[1]], na.rm = TRUE),
minY = min(.[[2]], na.rm = TRUE),
maxY = max(.[[2]], na.rm = TRUE)
)
) %>%
map(.,
~tibble(
x = seq(.x$minX, .x$maxX, length.out = 200),
y = seq(.x$minY, .x$maxY, length.out = 200),
)
) %>%
map(.,
~tibble(
xx = rep(.x$x, each = 200),
yy = rep(.x$y, time = 200)
)
) %>%
map2(.,
asplit(var_combos, 1), ~ .x %>%
set_names(.y))
# xgboost(
# objective='binary:logistic',
# eval_metric = 'auc',
# data = as.matrix(df[, 1:2]),
# label = as.matrix(df[, 5]), # binary variable
# nrounds = 10
# )
models_list <- map2(
var_combos$Var1,
var_combos$Var2,
~df %>%
select(Species, .x, .y) %>%
group_by(grp = 'grp') %>%
nest() %>%
mutate(
models = map(
data,
~{
list(
glm(Species ~ ., data = .x, family = binomial(link='logit')),
e1071::svm(Species ~ ., data = .x, type = 'C-classification', kernel = 'linear'),
randomForest::randomForest(formula = as.factor(Species) ~ ., data = .),
xgboost(
objective='binary:logistic',
eval_metric = 'auc',
data = as.matrix(.x[, 2:3]),
label = as.matrix(.x$Species), # binary variable
nrounds = 10
)
)
}
)
)
) %>%
map(
., ~unlist(., recursive = FALSE)
)
这给了我一个错误:
Error: unexpected ')' in " )"
> }
Error: unexpected '}' in " }"
> )
Error: unexpected ')' in " )"
我似乎找不到代码中意外的部分。我“假设”传递
~if
语句时出现问题。如何在此方式中传递多个if
语句?以下是有效的方法:
library(tidyverse)
output <- map2_df(models_list, boundary_lists, ~{
mods <- purrr::pluck(.x, "models")
dat <- .y
map_df(mods, function(x)
tryCatch({
if(attr(x, "class")[1] == "glm"){
# predict the logistic model
tibble(
modelname = attr(x, "class")[1],
prediction = predict(x, newdata = dat)
)
}
else if(attr(x, "class")[1] == "svm.formula"){
# predict the SVM model
tibble(
modelname = attr(x, "class")[1],
prediction = as.numeric(as.character(predict(x, newdata = dat)))
)
}
else if(attr(x, "class")[1] == "randomForest.formula"){
# predict the RF model
tibble(
modelname = attr(x, "class")[1],
prediction = as.numeric(as.character(predict(x, newdata = dat)))
)
}
else if(attr(x, "class")[1] == "xgb.Booster"){
# predict the XGBoost model
tibble(
modelname = attr(x, "class")[1],
prediction = predict(x, newdata = as.matrix(dat), type = 'prob')
)
}
}, error = function(e) { print('skipping\n')})
)
})
库(tidyverse)
输出我认为您需要else if
,最后只需要else
,而不是在初始值之后的串行if
。是的,您似乎只有这些浮动~if
语句。在map\u dfr
中应该只有一个~
来定义函数。把这些陈述合并成一个陈述。谢谢,我明白你的观点。当
时是否有类似于的功能。也就是说,当(attr(.x,“class”)[1]==“glm”){…
-这样做…,当(attr(.x,“class”)[1]==“svm.formula”){…
-这样做…,我想明确说明标准,并告诉函数在满足标准时该怎么做?或者这违反了最佳做法?@user113156检查开关。您可以通过将~if
命令更改为来查看它做了什么,否则如果给出错误:错误:No co>
.1$prediction`和.2$prediction
的AMON类型`
library(tidyverse)
output <- map2_df(models_list, boundary_lists, ~{
mods <- purrr::pluck(.x, "models")
dat <- .y
map_df(mods, function(x)
tryCatch({
if(attr(x, "class")[1] == "glm"){
# predict the logistic model
tibble(
modelname = attr(x, "class")[1],
prediction = predict(x, newdata = dat)
)
}
else if(attr(x, "class")[1] == "svm.formula"){
# predict the SVM model
tibble(
modelname = attr(x, "class")[1],
prediction = as.numeric(as.character(predict(x, newdata = dat)))
)
}
else if(attr(x, "class")[1] == "randomForest.formula"){
# predict the RF model
tibble(
modelname = attr(x, "class")[1],
prediction = as.numeric(as.character(predict(x, newdata = dat)))
)
}
else if(attr(x, "class")[1] == "xgb.Booster"){
# predict the XGBoost model
tibble(
modelname = attr(x, "class")[1],
prediction = predict(x, newdata = as.matrix(dat), type = 'prob')
)
}
}, error = function(e) { print('skipping\n')})
)
})