Home > Software engineering >  how are the p-values in lmrob() calculated?
how are the p-values in lmrob() calculated?

Time:08-22

I need to do a robust regression with lmrob(). For that I need to know how the p-value in the summary is calculated. I can not find anything in the documentation. Maybe anybody has an idea.

Thanks

CodePudding user response:

The p value is calculated from a Student's t distribution using the function stats::pt. The relevant code can be found in the body of the function robustbase:::summary.lmrob, which you can look at here

The line of the function where the coefficient table, including the p values are calculated, is:

ans$coefficients <- if (ans$converged) 
   cbind(est, se, tval, 2 * pt(abs(tval), df, lower.tail = FALSE))

Here, est is the vector of model estimates, se is the standard error (obtained from the square root of the covariance matrix) and tval is est / se. The variable df is the degrees of freedom.

Here's a simple example of replicating the p values using this method:

library(robustbase)

mod <- lmrob(mpg ~ wt, data = mtcars)

calculated_p_values <- 2 * pt(abs(mod$coefficients / sqrt(abs(diag(mod$cov)))), 
                       mod$df.residual, lower.tail = FALSE)

model_p_values <- summary(mod)$coefficients[, 4]

calculated_p_values
#>  (Intercept)           wt 
#> 2.588806e-16 1.283359e-08

model_p_values
#>  (Intercept)           wt 
#> 2.588806e-16 1.283359e-08

Created on 2022-08-20 with reprex v2.0.2

  • Related