在 R 摘要中获取变量子集
Getting a subset of variables in R summary
在 R 中使用 summary
函数时,是否有一个选项我可以传入其中以仅显示变量的子集?
在我的示例中,我 运行 面板回归我有几个解释变量,并且有许多我不想显示其系数的虚拟变量。我想有一种简单的方法可以做到这一点,但在函数文档中找不到它。谢谢
假设回归 运行 的行为与基本 lm()
模型的 summary()
相似:
# set up data
x <- 1:100 * runif(100, .01, .02)
y <- 1:100 * runif(100, .01, .03)
# run a very basic linear model
mylm <- lm(x ~ y)
summary(mylm)
# we can save summary of our linear model as a variable
mylm_summary <- summary(mylm)
# we can then isolate coefficients from this summary (summary is just a list)
mylm_summary$coefficients
#output:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.2007199 0.04352267 4.611846 1.206905e-05
y 0.5715838 0.03742379 15.273273 1.149594e-27
# note that the class of this "coefficients" object is a matrix
class(mylm_summ$coefficients)
# output
[1] "matrix"
# we can convert that matrix into a data frame so it is easier to work with and subset
mylm_df_coefficients <- data.frame(mylm_summary$coefficients)
它在文档中,但您必须寻找 summary.plm
的关联 print
方法。参数是subset
。在以下示例中使用它:
library(plm)
data("Grunfeld", package = "plm")
mod <- plm(inv ~ value + capital, data = Grunfeld)
print(summary(mod), subset = c("capital"))
在 R 中使用 summary
函数时,是否有一个选项我可以传入其中以仅显示变量的子集?
在我的示例中,我 运行 面板回归我有几个解释变量,并且有许多我不想显示其系数的虚拟变量。我想有一种简单的方法可以做到这一点,但在函数文档中找不到它。谢谢
假设回归 运行 的行为与基本 lm()
模型的 summary()
相似:
# set up data
x <- 1:100 * runif(100, .01, .02)
y <- 1:100 * runif(100, .01, .03)
# run a very basic linear model
mylm <- lm(x ~ y)
summary(mylm)
# we can save summary of our linear model as a variable
mylm_summary <- summary(mylm)
# we can then isolate coefficients from this summary (summary is just a list)
mylm_summary$coefficients
#output:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.2007199 0.04352267 4.611846 1.206905e-05
y 0.5715838 0.03742379 15.273273 1.149594e-27
# note that the class of this "coefficients" object is a matrix
class(mylm_summ$coefficients)
# output
[1] "matrix"
# we can convert that matrix into a data frame so it is easier to work with and subset
mylm_df_coefficients <- data.frame(mylm_summary$coefficients)
它在文档中,但您必须寻找 summary.plm
的关联 print
方法。参数是subset
。在以下示例中使用它:
library(plm)
data("Grunfeld", package = "plm")
mod <- plm(inv ~ value + capital, data = Grunfeld)
print(summary(mod), subset = c("capital"))