Obtain standard errors of regression coefficients for an “mlm” object returned by `lm()`

后端 未结 3 1106
执笔经年
执笔经年 2020-12-21 05:08

I\'d like to run 10 regressions against the same regressor, then pull all the standard errors without using a loop.

depVars <- as.matrix(         


        
3条回答
  •  醉酒成梦
    2020-12-21 05:34

    Given an "mlm" model object model, you can use the below function written by me to get standard errors of coefficients. This is very efficient: no loop, and no access to summary.mlm().

    std_mlm <- function (model) {
      Rinv <- with(model$qr, backsolve(qr, diag(rank)))
      ## unscaled standard error
      std_unscaled <- sqrt(rowSums(Rinv ^ 2)[order(model$qr$pivot)])
      ## residual standard error
      sigma <- sqrt(colSums(model$residuals ^ 2) / model$df.residual)
      ## return final standard error
      ## each column corresponds to a model
      "dimnames<-"(outer(std_unscaled, sigma), list = dimnames(model$coefficients))
      }
    

    A simple, reproducible example

    set.seed(0)
    Y <- matrix(rnorm(50 * 5), 50)    ## assume there are 5 responses
    X <- rnorm(50)    ## covariate
    
    fit <- lm(Y ~ X)
    

    We all know that it is simple to extract estimated coefficients via:

    fit$coefficients    ## or `coef(fit)`
    #                   [,1]       [,2]        [,3]        [,4]        [,5]
    #(Intercept) -0.21013925  0.1162145  0.04470235  0.08785647  0.02146662
    #X            0.04110489 -0.1954611 -0.07979964 -0.02325163 -0.17854525
    

    Now let's apply our std_mlm:

    std_mlm(fit)
    #                 [,1]      [,2]      [,3]      [,4]      [,5]
    #(Intercept) 0.1297150 0.1400600 0.1558927 0.1456127 0.1186233
    #X           0.1259283 0.1359712 0.1513418 0.1413618 0.1151603
    

    We can of course, call summary.mlm just to check our result is correct:

    coef(summary(fit))
    #Response Y1 :
    #               Estimate Std. Error    t value  Pr(>|t|)
    #(Intercept) -0.21013925  0.1297150 -1.6200072 0.1117830
    #X            0.04110489  0.1259283  0.3264151 0.7455293
    #
    #Response Y2 :
    #              Estimate Std. Error    t value  Pr(>|t|)
    #(Intercept)  0.1162145  0.1400600  0.8297485 0.4107887
    #X           -0.1954611  0.1359712 -1.4375183 0.1570583
    #
    #Response Y3 :
    #               Estimate Std. Error    t value  Pr(>|t|)
    #(Intercept)  0.04470235  0.1558927  0.2867508 0.7755373
    #X           -0.07979964  0.1513418 -0.5272811 0.6004272
    #
    #Response Y4 :
    #               Estimate Std. Error    t value  Pr(>|t|)
    #(Intercept)  0.08785647  0.1456127  0.6033574 0.5491116
    #X           -0.02325163  0.1413618 -0.1644831 0.8700415
    #
    #Response Y5 :
    #               Estimate Std. Error    t value  Pr(>|t|)
    #(Intercept)  0.02146662  0.1186233  0.1809646 0.8571573
    #X           -0.17854525  0.1151603 -1.5504057 0.1276132
    

    Yes, all correct!

提交回复
热议问题