2017-03-19 25 views
0

在R中使用summary函数时,是否有一个选项可以传入,仅显示变量的子集?获取R中变量的子集汇总

在我的例子中,我运行了一个面板回归,我有几个解释变量,并且有许多虚拟变量,其系数我不想提出。我想有一个简单的方法来做到这一点,但在功能文档中找不到它。由于

+1

如果你只是想,3列1和5,使用'汇总(DF [,C(1,3,5))' – G5W

+0

@ G5W我不能让这个语法起作用。 “'闭包'类型的对象不是子集合” – noumenal

+1

“汇总”对象不是数据帧,因此不能以这种方式进行子集化。 – neilfws

回答

2

它是在文档中,但你必须找对summary.plm的associacted print方法。参数是subset。使用它作为在下面的示例:

library(plm) 
data("Grunfeld", package = "plm") 
mod <- plm(inv ~ value + capital, data = Grunfeld) 
print(summary(mod), subset = c("capital")) 
2

假设你跑的回归同样表现为基本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)