gpt4 book ai didi

r - 抑制 R 中的固定效应系数

转载 作者:行者123 更新时间:2023-12-04 02:22:38 28 4
gpt4 key购买 nike

在使用 summary() 函数(例如,stata 中的 absorb() 函数的等价物)时,是否有一种方法可以抑制线性模型中固定效应的系数。例如,我希望汇总函数只输出截距和 x,而不是因子的系数和标准误差:

frame <- data.frame(x = rnorm(100), y = rnorm(100), z = rep(c("A", "B", "C", "D"),25))
summary(lm(y~x + as.factor(z), data = frame))

Call:
lm(formula = y ~ x + as.factor(z), data = frame)

Residuals:
Min 1Q Median 3Q Max
-2.2417 -0.6407 0.1783 0.5789 2.4347

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.25829 0.19196 -1.346 0.1816
x 0.09983 0.09788 1.020 0.3104

...

谢谢。

最佳答案

您可以执行以下任一操作:

mod <- lm(y~x + as.factor(z), data = frame)
coef(mod)[c("(Intercept)", "x")]
# (Intercept) x
# 0.12357491 -0.06430765
coef(mod)[grepl("Intercept|x", names(coef(mod)))]
# (Intercept) x
# 0.12357491 -0.06430765
coef(mod)[1:2]
# (Intercept) x
# 0.12357491 -0.06430765
mod$coefficients[1:2]
# (Intercept) x
# 0.12357491 -0.06430765

关于r - 抑制 R 中的固定效应系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26565879/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com