我使用Tab_model输出GEE回归结果,默认阈值为0.5、0.01、0.001,长度必须为3,所有符号为“” 我想添加 0.1 作为“+”,0.5 作为“”,0.01 作为“”,0,001 作为“*” 谢谢
正如 PBulls 在评论中所说,3 个符号的限制有点“硬编码”到
sjPlot::tab_model()
中。
但是,我们可以通过创建一个函数来更改该函数的 HTML 输出来实现此目的:
#' Add a significance level to the output of sjPlot::tab_model()
#'
#' @param tab Output from sjPlot::tab_model()
#' @param model The model object fed to sjPlot::tab_model()
#' @param lvl The desired significance level to add; default = 0.1
#' @param symbol The symbol to use for that significance level; default = "+"
#' @return The altered table
add_sig_level = function(tab, model, lvl = 0.1, symbol = "+") {
## Make sure 'symbol' is escaped if need be
metas = c(".", "^", "$", "*", "+")
symbol = ifelse(symbol %in% metas, paste0("\\", symbol), symbol)
## Get "marginally significant" (i.e. 0.05 <= p < lvl) variables
coefs = summary(model)$coefficients
pvals = coefs[ , "Pr(>|t|)"]
vars = rownames(coefs)[which(pvals >= 0.05 & pvals < lvl)]
## For each of those variables
for ( j in vars ) {
## Define a regex to find the relevant table rows
pat = paste0("(col1\">", j, ".+?col2.+?<sup>)</sup>")
## & add a '+' (or whatever) in between the <sup></sup> for those rows
replacement = paste0("\\1", symbol, "</sup>")
tab$page.content = gsub(pat, replacement, tab$page.content)
tab$page.complete = gsub(pat, replacement, tab$page.complete)
}
## Add the new symbol to the table footnote
pat = "footnote\">"
replacement = paste0(pat, symbol, " p<", lvl, " ")
tab$page.content = gsub(pat, replacement, tab$page.content)
tab$page.complete = gsub(pat, replacement, tab$page.complete)
## Return result
return(tab)
}
为了说明这一点,我创建了一个示例数据集和模型,其中包含两个预测变量,这两个预测变量在 0.1 水平上显着,但在 0.05 水平上不显着:
## Simulate example data
set.seed(138)
n = 30
X = cbind(rnorm(n), rnorm(n), rnorm(n))
b = c(1, 1, 0.4, 0.4)
y = c(cbind(1, X) %*% b + rnorm(n))
## Fit the model
example_model = lm(y ~ X)
model_summary = summary(example_model)
model_summary
Call:
lm(formula = y ~ X)
Residuals:
Min 1Q Median 3Q Max
-1.5577 -0.5564 -0.1403 0.5669 1.6760
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.9253 0.1710 5.410 1.14e-05 ***
X1 1.0339 0.1648 6.274 1.22e-06 ***
X2 0.3097 0.1754 1.766 0.0891 .
X3 0.2693 0.1394 1.933 0.0642 .
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.8775 on 26 degrees of freedom
Multiple R-squared: 0.617, Adjusted R-squared: 0.5728
F-statistic: 13.96 on 3 and 26 DF, p-value: 1.281e-05
这是
sjPlot::tab_model()
的初始输出:
tab = sjPlot::tab_model(example_model, p.style = "numeric_stars")
tab
现在这是我们新函数的输出:
tab2 = add_sig_level(tab, example_model)
tab2
成功!我们可以看到
X2
和X3
都标有+
符号,
该符号的含义在表的脚注中进行了解释。
这是一个说明,我们可以根据函数的构造方式使用任何自定义显着性级别;如果我们使用 lvl = 0.08
,那么 X3
仍应与 +
符号一起弹出,但 X2
不应弹出(因为其 p 值为 0.089):
tab3 = add_sig_level(tab, example_model, lvl = 0.08)
tab3
注释和表格脚注符合预期。