【问题标题】:Extract standard errors from lm object从 lm 对象中提取标准错误
【发布时间】:2012-06-21 09:24:55
【问题描述】:

我们得到了一个 lm 对象并想要提取标准错误

lm_aaa <- lm(aaa ~ x + y + z)

我知道函数概要、名称和系数。

但是,summary 似乎是手动访问标准错误的唯一方法。

你知道我怎样才能输出se吗?

【问题讨论】:

    标签: r regression lm standard-error


    【解决方案1】:

    我认为以下几行也可以为您提供快速的答案:

    lm_aaa<- lm(aaa~x+y+z)
    se <- sqrt(diag(vcov(lm_aaa)))
    

    【讨论】:

      【解决方案2】:

      如果您不想得到模型的标准误差/偏差,而是个体系数的标准误差/偏差,请使用

      # some data (taken from Roland's example)
      x = c(1, 2, 3, 4)
      y = c(2.1, 3.9, 6.3, 7.8)
      
      # fitting a linear model
      fit = lm(y ~ x)
      
      # get vector of all standard errors of the coefficients
      coef(summary(fit))[, "Std. Error"] 
      

      有关模型标准误差/偏差的更多信息,请参阅here。有关系数的标准误差/偏差的更多信息,请参阅here

      【讨论】:

        【解决方案3】:

        要获取所有参数的标准错误列表,您可以使用

        summary(lm_aaa)$coefficients[, 2]
        

        正如其他人所指出的,str(lm_aaa) 会告诉您几乎所有可以从您的模型中提取的信息。

        【讨论】:

          【解决方案4】:

          summary 函数的输出只是一个 R list。所以你可以使用所有标准的列表操作。例如:

          #some data (taken from Roland's example)
          x = c(1,2,3,4)
          y = c(2.1,3.9,6.3,7.8)
          
          #fitting a linear model
          fit = lm(y~x)
          m = summary(fit)
          

          m 对象或列表具有许多属性。您可以使用括号或命名方法访问它们:

          m$sigma
          m[[6]]
          

          一个方便的函数是str。此函数提供对象属性的摘要,即

          str(m)
          

          【讨论】:

          • 然而,@csgillespie 指的是模型的 residual 标准差,而不是单个系数的标准差。函数m$sigma对应sigma(fit),见here。我相信这个问题实际上是关于个体系数的标准偏差。
          【解决方案5】:
          #some data
          x<-c(1,2,3,4)
          y<-c(2.1,3.9,6.3,7.8)
          
          #fitting a linear model
          fit<-lm(y~x)
          
          #look at the statistics summary
          summary(fit)
          
          #get the standard error of the slope
          se_slope<-summary(fit)$coef[[4]] 
          #the index depends on the model and which se you want to extract
          
          #get the residual standard error
          rse<-summary(fit)$sigma
          

          【讨论】:

            猜你喜欢
            • 2014-08-09
            • 2021-04-30
            • 2015-05-24
            • 1970-01-01
            • 1970-01-01
            • 2015-12-31
            • 1970-01-01
            • 1970-01-01
            • 2012-01-19
            相关资源
            最近更新 更多