所以我試圖通過自定義函式輸入 tbl_uvregression 函式(gt_summary 包)的 Y 引數。這個想法是在我的函式中創建多個 tbl 并回傳合并的不同表。
這是我正在使用的代碼示例:
#Loading libraries example dataset from questionr package
library(haven)
library(tidyverse)
library(finalfit)
library(dplyr)
library(survey)
library(srvyr)
library(gtsummary)
library(glue)
library(gt)
library(knitr)
library(questionr)
data(hdv2003)
這是我遇到問題的部分:
reg_log <- function(dataframew, variables, by) {
#@param1 : weighted dataframe
#@param2 : vector containing variables we want in our graph
#@param3 : the variable or column we want as our Y argument
Table <- tbl_uvregression(data = dataframew, include = variables, exponentiate = TRUE, method.args = list(family = quasibinomial()), y = by, method = survey::svyglm)
return(Table)
}
當我在 reg_log 之外運行這個函式時,我沒有問題,但似乎在函式內部,tbl_uvregression 的 Y 引數不評估引數,而是從字面上讀取它。這是我在呼叫函式時遇到的錯誤:
hdv2003w <- svydesign(ids = ~1, data = hdv2003, weights = ~hdv2003$poids) #setting the survey.design object
reg_log(hdv2003w, c("age", "sexe", "hard.rock", "sport"), "sport")
x 構建模型時
survey::svyglm(formula = by ~ age, design = ., family = quasibinomial())出錯 請參閱下面的錯誤。錯誤:mutate()列問題model。我model = map(...)。x svyglm.survey.design 中的錯誤(公式 = 按~年齡,設計 = 結構(串列(:所有變數都必須在設計 = 引數中)
我知道 Y 引數需要不帶引號的語法,但即使我使用了替換 () 函式它也不起作用。我已經決定自己使用 switch 功能做出幾種可能性,但如果有人知道如何解決這個問題,那就太棒了。
謝謝。
uj5u.com熱心網友回復:
該tbl_uvregression()函式需要一個不帶引號的輸入y=,而不是帶有結果名稱的字串。我更新了您的函式以說明字串輸入。
library(gtsummary)
library(questionr)
data(hdv2003)
reg_log <- function(dataframew, variables, by) {
tbl_uvregression(
data = dataframew,
include = all_of(variables),
exponentiate = TRUE,
method.args = list(family = quasibinomial()),
y = !!rlang::sym(by),
method = survey::svyglm
)
}
hdv2003w <- survey::svydesign(ids = ~1, data = hdv2003, weights = ~hdv2003$poids) #setting the survey.design object
tbl <-
reg_log(hdv2003w, c("age", "sexe", "hard.rock"), "sport")
由reprex 包(v2.0.1)于 2021 年 11 月 12 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358038.html
上一篇:如何用for回圈重復一個函式
下一篇:如何在組中計算特定值?
