【问题标题】:Getting a subset of variables in R summary在 R 摘要中获取变量的子集
【发布时间】:2017-08-11 01:21:45
【问题描述】:

当在 R 中使用 summary 函数时,我可以在其中传递一个选项以仅显示变量的子集吗?

在我的示例中,我运行了一个面板回归,我有几个解释变量,并且有许多我不想呈现其系数的虚拟变量。我想有一种简单的方法可以做到这一点,但在函数文档中找不到。谢谢

【问题讨论】:

  • 如果您只需要第 1、3 和 5 列,请使用 summary(df[, c(1,3,5)])
  • @G5W 我无法使用该语法。 “‘闭包’类型的对象不是可子集的”
  • summary 对象不是数据框,因此不能以这种方式作为子集。

标签: r linear-regression summary panel-data plm


【解决方案1】:

它在文档中,但您必须为summary.plm 寻找关联的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)
    

    【讨论】:

      猜你喜欢
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2022-06-27
      • 1970-01-01
      相关资源
      最近更新 更多