我有個問題。我對 R 比較陌生。我正在將一些代碼從另一個應用程式轉換到 R。在該代碼中,我能夠遍歷一個表并根據 p 值和幾率的大小僅挑選出重要的變數邏輯回歸的比率。然后,當 p 小于或等于 0.05 并且優勢比高于 1.00 時,我可以說“x 與 y 有顯著聯系”之類的話,并且當 p 時,我可以說“x 與 y 有顯著的負相關”值小于 0.05,優勢比小于 1.00。然后,我能夠做到我從 gtsummary 文獻中了解到的就是 inline_text 這些陳述句。當我試圖了解 R 時,我想知道如何使用 gtsummary 表來完成此任務?我的可重現代碼不起作用,但它在下面:
# install.packages("gtsummary")
library(gtsummary)
library(tidyverse)
#simulated data
gender <- sample(c(0,1), size = 1000, replace = TRUE)
age <- round(runif(1000, 18, 80))
xb <- -9 3.5*gender 0.2*age
p <- 1/(1 exp(-xb))
y <- rbinom(n = 1000, size = 1, prob = p)
mod <- glm(y ~ gender age, family = "binomial")
summary(mod)
#create the gtsummary table
tab1 = mod %>%
tbl_regression(exponentiate = TRUE) %>%
as_gt() %>%
gt::tab_source_note(gt::md("*This data is simulated*"))
#attempt of going through the gtsummary table
for (i in 1:nrow(tab1[1:3,])) { # does one row at a time
pv = tab1[["_data"]]$p.value
num = tab1[i, "pv"]
name = tab1[i, "variable"]
if(pv <=0.05 ){
cat("The link between", name, "and is significant. ")
}
}
我詢問 gtsummary 回歸表是因為,我也必須對 tbl_summary 做同樣的事情。我想我會從回歸版本開始。這個想法是通過 if else 獲得華麗的 inline_text。所有這一切都是由向下p-value列觸發的,然后將變數的名稱和驚人的inline_text資訊拉到句子中。我已經查看了其他人提出的可用問題,但我還沒有找到任何能夠觸及這個問題的核心。如果我錯過了,請指出正確的方向。
uj5u.com熱心網友回復:
每個 gtsummary 表中都有一個名為 的資料框x$table_body。我認為從那里提取您需要的資訊更容易。下面的例子!inline_text()(如果這對您更好,您也可以將最后一行包裝在一個中)。
# install.packages("gtsummary")
library(gtsummary)
#> #BlackLivesMatter
library(tidyverse)
#simulated data
gender <- sample(c(0,1), size = 1000, replace = TRUE)
age <- round(runif(1000, 18, 80))
xb <- -9 3.5*gender 0.2*age
p <- 1/(1 exp(-xb))
y <- rbinom(n = 1000, size = 1, prob = p)
mod <- glm(y ~ gender age, family = "binomial")
#create the gtsummary table
tab1 = mod %>% tbl_regression(exponentiate = TRUE)
# extract the variable names and the pvalues
tab1$table_body %>%
select(variable, p.value) %>%
filter(p.value <= 0.05) %>% # only keep the sig pvalues
deframe() %>%
imap(~str_glue("The link between 'y' and {.y} is significant ({style_pvalue(.x, prepend_p = TRUE)})."))
#> $gender
#> The link between 'y' and gender is significant (p<0.001).
#>
#> $age
#> The link between 'y' and age is significant (p<0.001).
使用reprex v2.0.2創建于 2022-11-07
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/530441.html
標籤:r循环逻辑回归总结
