我正在寻找一个函数或代码,以检查在任何给定时刻如何在R中全局设置对比度,以便在运行某些代码后可以将其重置。
所以我正在寻找这样的东西:
# Identify current contrasts
original_contrasts <- get_contrasts()
# Change contrasts
options(contrasts=c("contr.sum","contr.poly"))
# Run some models ....
# Reset to original contrasts
options(contrasts=original_contrasts)
是否存在这样的功能?
答案 0 :(得分:1)
您可以使用options("contrasts")
存储当前对比度,并返回:
$contrasts
unordered ordered
"contr.treatment" "contr.poly"
# Identify current contrasts
original_contrasts <- options("contrasts")
# Change contrasts
options(contrasts=c("contr.sum","contr.poly"))
对比现在是:
$contrasts
[1] "contr.sum" "contr.poly"
# Run some models ....
# Reset to original contrasts
options(contrasts=unname(unlist(original_contrasts)))
这又给您:
$contrasts
[1] "contr.treatment" "contr.poly"