- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 tidyeval 进行编程。
我想编写一个函数来为选定的结果变量运行逻辑回归模型:
library(tidyverse)
set.seed(1234)
df <- tibble(id = 1:1000,
group = sample(c("Group 1", "Group 2", "Group 3"), 1000, replace = TRUE),
died = sample(c(0,1), 1000, replace = TRUE))
myfunc <- function(data, outcome){
enquo_var <- enquo(outcome)
fit <- tidy(glm(!!enquo_var ~ group, data=data,
family = binomial(link = "logit")),
exponentiate = TRUE, conf.int=TRUE)
fit
}
myfunc(df, died)
但是得到:
Error in !enquo_outcome : invalid argument type
(注意实际场景涉及更复杂的功能)。
这可能吗?
最佳答案
我们需要为 glm
创建一个公式来选取它。一种选择是粘贴
myfunc <- function(data, outcome){
enquo_var <- enquo(outcome)
fit <- tidy(glm(paste(quo_name(enquo_var), "group", sep="~"), data=data,
family = binomial(link = "logit")),
exponentiate = TRUE, conf.int=TRUE)
fit
}
myfunc(df, died)
# term estimate std.error statistic p.value conf.low conf.high
#1 (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185 1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512 1.253959
#3 groupGroup 3 1.3692735 0.1557241 2.0181864 0.04357185 1.0095739 1.859403
<小时/>
如果我们还需要使用tidyverse函数
myfunc <- function(data, outcome){
quo_var <- quo_name(enquo(outcome))
fit <- tidy(glm(rlang::expr(!! rlang::sym(quo_var) ~ group), data=data,
family = binomial(link = "logit")),
exponentiate = TRUE, conf.int=TRUE)
fit
}
myfunc(df, died)
# term estimate std.error statistic p.value conf.low conf.high
#1 (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185 1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512 1.253959
#3 groupGroup 3 1.3692735 0.1557241 2.0181864 0.04357185 1.0095739 1.859403
<小时/>
或者正如@lionel在评论中提到的get_expr
可以使用
myfunc <- function(data, outcome){
quo_var <- enquo(outcome)
fit <- tidy(glm(rlang::expr(!! rlang::get_expr(quo_var) ~ group), data=data,
family = binomial(link = "logit")),
exponentiate = TRUE, conf.int=TRUE)
fit
}
myfunc(df, died)
# term estimate std.error statistic p.value conf.low conf.high
#1 (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185 1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512 1.253959
#3 groupGroup 3 1.3692735 0.1557241 2.0181864 0.04357185 1.0095739 1.859403
<小时/>
或者 @lionel 建议的更紧凑的方法,它避免了 enquo/quo_name/sym
转换,而是直接采用 enexpr
中的参数
myfunc <- function(data, outcome){
fit <- tidy(glm(rlang::expr(!! rlang::enexpr(outcome) ~ group), data=data,
family = binomial(link = "logit")),
exponentiate = TRUE, conf.int=TRUE)
fit
}
myfunc(df, died)
# term estimate std.error statistic p.value conf.low conf.high
#1 (Intercept) 0.8715084 0.1095300 -1.2556359 0.20924801 0.7026185 1.079852
#2 groupGroup 2 0.9253515 0.1550473 -0.5003736 0.61681204 0.6826512 1.253959
#3 groupGroup 3 1.3692735 0.1557241 2.0181864 0.04357185 1.0095739 1.859403
关于r - 使用 tidyeval 进行编程回归建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439477/
我正在寻找可以从 Eclipse 的 UML 图生成代码的工具。该工具应该从 UML 图生成代码,进行逆向工程以查看代码更改,并提供合并选项以防止因任何更改而删除文件。 是否有工具可以做到这一点,或者
我正在寻找有关建模的见解。我有一个关于设计模式和基本类图、序列图和用例的介绍类(class)。 我发现类图作为我编程中的组织工具非常宝贵。到目前为止,用例还算有用。 本学期我正在上课,更深入地学习 U
如何使用 flutter 数据建模技术访问“HH”和“mm”。我一直在尝试按以下方式使用它,但是下面给出了错误。 我的数据模型当前为它的简化版本。 class Week { final Strin
我正在使用 Neo4j 和 PHP。在我的项目中,我有餐厅节点。每个节点都有纬度、经度和分类属性。 我需要返回与用户给定分类相匹配的餐厅节点,其结果按距用户位置的距离排序(即第一个最近的餐厅)。 最简
我希望 POJO 包含一个 Status 字段(只有两个可能的值 SUCCESS、FAILED),并且根据此状态,第二个字段需要为 POJO 对象(如果 Status 为 SUCCESS) 或只是字符
我正在尝试将我的办公室迁移到数据库应用程序中。这是我得到的: (来源:phunkei.de) 公司和个人从客户继承(1:1关系)。这是因为他们都可以是“客户”并收到账单,我想引用订单中的单个表。公司和
我目前有这个数据库结构: 一个条目可以有多个"file"、“文本”和“url”类型的项目。 这些项目中的每一项在文本、网址或文件表(存储数据的位置)中都有一个对应的项目。 我需要一个查询来有效地选择一
下面的代码不应该像“if(condition)”语句一样工作吗,当它在另一个循环中并且“body”有自己的 break 或 continue 语句时,会出现明显的错误行为: for( ; condit
我一直在像这样存储和递增下载计数器: INCRBY downloads: 1 但现在我想要下载:* 按值排序,以便显示热门下载列表。 我觉得我可以更好地存储它。我愚弄了: ZINCRBY downlo
我开始使用图形数据库,在我的团队中,我们已经开始为我们的软件建模图形。当我们尝试“记录”模型以查看数据库的结构时,问题就出现了。对于 SQL 数据库,您只需查看 SQL 模式。 我们花了一些时间阅读
我正在做一个固定效应回归并且遇到了自相关问题,为了解决这个问题,我正在使用预测、lmtest 和 plm 包进行 ARIMA 建模。我的数据是一般面板数据,looks like this ,我正在尝试
我想知道是否有任何工具可以帮助我对 C 应用程序(即函数式编程)进行建模。例如。我目前正在构建一个共享库。但是为了直观地传达我的设计,我需要类似 UML 的东西。我想这样做,以便审查我的设计的人不需要
我正在尝试将 JSON 转换为 GSON ,但我无法建模。谁能给我举个例子。 [ { "id": "1", "name": "lalala",
正如标题所说,在 中建模 optional 参数的最佳方法是什么?斯卡拉 ? 对于 optional 参数,我的意思是执行函数体不需要的值。 要么因为该参数存在默认值,要么根本不需要该参数本身(例如配
我发现 UML 可用于记录 OO 系统的各个方面,尤其是用于整体架构的类图和用于说明特定例程的序列图。我想为我的 clojure 应用程序做同样的事情。我目前对模型驱动开发不感兴趣,只是在交流应用程序
我想知道是否有人知道如何使用 UML 在属性中建模 DateTime 基元类型? 最佳答案 我想这取决于日期的格式......请参阅下面的示例,其中我使用两个整数(分别为日和年)和一个名为“Month
我有一些与我的问题建模相关的问题。我正在研究基于模型的测试的论文项目。还想从专家的角度了解我是否采用正确的方法来建模我的场景。我正在对 Android 应用程序的 UI 进行建模,遍历它们,生成测试用
我正在尝试建立对 CouchDB 以及如何为某些现实世界场景建模数据的理解。我现在已经尽可能多地“按日期获取我的博客文章”;) 给定这样的文件: { "_id": "couch1",
一个模型资源是如何分层的?例如假设一个人有一个“留言板”并且“消息”是一种资源。假设“消息”可以有回复,从而形成一个讨论线程。如何模拟线程的概念? “消息”是否包含它的子项? “线程”是它自己的资源吗
我正在尝试创建一个像通用 POJO 一样工作的对象,因为我必须通过不同的对象传递它,并且我需要一个接口(interface)来访问其属性。 目前,我使用具有 getField 方法的基础对象来完成此操
我是一名优秀的程序员,十分优秀!