我使用 tidymodels 框架拟合多项式逻辑回归模型来预测鸢尾花数据集中的物种。
library(tidymodels)
iris.lr = multinom_reg(
mode="classification",
penalty=NULL,
mixture=NULL
) %>%
set_engine("glmnet")
iris.fit = iris.lr %>%
fit(Species ~. , data = iris)
Run Code Online (Sandbox Code Playgroud)
然后我想查看模型的系数并写出公式。我的理解是我应该从 iris.fit 获取这个。
iris.fit 的输出有一个 100 行表,其中包含 Df、%Dev 、Lambda。iris 数据集只有 4 个预测变量。如何将此输出转换为系数?
转换回由配方转换的列outcome(在本例中为)的最优雅的方法是什么?mpg该解决方案可以是通用的(如果存在或仅适用于log和normalize步骤(如下编码)。
可能有用的链接:此处
讨论了一般解决方案,但我认为它尚未实施。这里提供了
R 函数的解决方案,但我不确定在这种情况下是否可以提供帮助。scale
library(recipes)
data <- tibble(mtcars) %>%
select(cyl, mpg)
rec <- recipe(mpg ~ ., data = data) %>%
step_log(all_numeric()) %>%
step_normalize(all_numeric()) %>%
prep()
data_baked <- bake(rec, new_data = data)
# model fitting, predictions, etc...
# how to invert/transform back predictions (estimates) and true outcomes
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Tidymodels 预测 R 中的房地产价格。我正在关注本教程。一切都很顺利,直到我尝试对测试数据进行预测时。
\n请参阅下面的代码示例和最后的错误。
\n我看了两个类似的问题(这里和这里),但似乎我已经定义了可变角色,并为我的工作流程提供了未准备好的配方。
\n\n # libraries ---------------------------------------------------------------\n library(tidymodels)\n #> \xe2\x94\x80\xe2\x94\x80 Attaching packages \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 tidymodels 0.1.2 \xe2\x94\x80\xe2\x94\x80\n #> \xe2\x9c\x93 broom 0.7.3 \xe2\x9c\x93 recipes 0.1.15\n #> \xe2\x9c\x93 dials 0.0.9 \xe2\x9c\x93 rsample 0.0.8 \n #> \xe2\x9c\x93 dplyr 1.0.3 \xe2\x9c\x93 tibble 3.0.5 \n #> \xe2\x9c\x93 ggplot2 3.3.3 \xe2\x9c\x93 tidyr 1.1.2 \n #> \xe2\x9c\x93 infer 0.5.4 \xe2\x9c\x93 tune 0.1.2 \n #> \xe2\x9c\x93 modeldata 0.1.0 \xe2\x9c\x93 workflows 0.2.1 \n #> \xe2\x9c\x93 parsnip 0.1.5 \xe2\x9c\x93 …Run Code Online (Sandbox Code Playgroud) 我有一个在 tidymodels (R) 中创建的逻辑回归模型。我正在尝试进行功能选择。如何使用 CRAN 上发布的包(请不要使用开发包)在 tidymodels 框架中进行功能选择?
每个人都说要做正则化逻辑回归,但我需要能够进行推理/具有参数置信区间,这是正则化无法做到的。
尝试使用交叉折叠重采样并拟合 Ranger 包中的随机森林。无需重新采样的拟合工作正常,但一旦我尝试重新采样拟合,它就会失败并出现以下错误。
考虑以下df
df<-structure(list(a = c(1379405931, 732812609, 18614430, 1961678341,
2362202769, 55687714, 72044715, 236503454, 61988734, 2524712675,
98081131, 1366513385, 48203585, 697397991, 28132854), b = structure(c(1L,
6L, 2L, 5L, 7L, 8L, 8L, 1L, 3L, 4L, 3L, 5L, 7L, 2L, 2L), .Label = c("CA",
"IA", "IL", "LA", "MA", "MN", "TX", "WI"), class = "factor"),
c = structure(c(2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 2L, 2L, 1L), .Label = c("R", "U"), class = "factor"),
d = structure(c(3L, 3L, …Run Code Online (Sandbox Code Playgroud) 最近我学习使用 tidymodels 来构建机器学习工作流程,但是当我使用该工作流程对测试集进行预测时,它会引发错误“列中缺少数据”,但我确信训练集和测试集都没有有缺失数据。这是我的代码和示例:
\n# Imformation of the data\xef\xbc\x9athe Primary_type in test set has several novel levels\nstr(train_sample)\ntibble [500,000 x 3] (S3: tbl_df/tbl/data.frame)\n $ ID : num [1:500000] 6590508 2902772 6162081 7777470 7134849 ...\n $ Primary_type: Factor w/ 29 levels "ARSON","ASSAULT",..: 16 8 3 3 28 7 3 4 25 15 ...\n $ Arrest : Factor w/ 2 levels "FALSE","TRUE": 2 1 1 1 1 2 1 1 1 1 ...\n\nstr(test_sample)\ntibble [300,000 x 3] (S3: tbl_df/tbl/data.frame)\n $ ID : num [1:300000] …Run Code Online (Sandbox Code Playgroud) 我尝试使用工作流程包或方法,但出现错误。这是 R 代码(抱歉,代码很长):
# Package ----
library(finetune)
library(themis)
library(tidymodels)
# Data ----
data("PimaIndiansDiabetes", package = "mlbench")
table(PimaIndiansDiabetes$diabetes)
str(PimaIndiansDiabetes)
PimaIndiansDiabetes <-
PimaIndiansDiabetes %>%
mutate(diabetes = relevel(diabetes, "pos"))
# Split ----
set.seed(123)
ind <- initial_split(PimaIndiansDiabetes, strata = diabetes)
dat_train <- training(ind)
dat_test <- testing(ind)
# CV ----
set.seed(123)
dat_cv <- vfold_cv(dat_train, v = 10)
# Recipe ----
dat_rec <-
dat_train %>%
recipe(diabetes ~.) %>%
step_normalize(all_numeric_predictors()) %>%
step_smote(diabetes)
# Model ----
parsnip_nn <-
mlp(hidden_units = tune(),
penalty = tune(),
epochs = tune()) %>% …Run Code Online (Sandbox Code Playgroud) 使用该partykit::ctree函数创建 ctree:
ctree = ctree(formula, # Outcome variables and features
data=sample, # Name of dataset
control=ctree_control(mincriterion=1-0.05, # 1 minus p-value
testtype="Bonferroni", # Corrects for multiple hypothesis testing
maxdepth=10)) # The maximum number of steps from the top to the bottom of the tree
Run Code Online (Sandbox Code Playgroud)
使用 tidymodels 和 bonsai 包创建 ctree:
model_ctree <- decision_tree() %>%
set_mode("regression") %>%
set_engine("partykit") %>%
fit(formula, data = sample)
Run Code Online (Sandbox Code Playgroud)
我注意到使用 tidymodels (通过 partykit 的决策树)似乎只能设置 3 个参数,我可以更改参数mincriterion=0.95和吗testtype="Bonferroni"?