R: Get p-value for all coefficients in multiple linear regression (incl. reference level)

StackOverflow https://stackoverflow.com//questions/23044830

  •  21-12-2019
  •  | 
  •  

Question

Example

I have a linear regression, which fits a numerical dependent variable with 3 explanatory factor variables. Each of the factor variables has 2 levels.

install.packages("robustbase")
install.packages("MASS")
require(robustbase)
require(MASS)

# Example data
data(npk)
df= npk[,-1]
str(df) 

# 'data.frame':  24 obs. of  4 variables:
# $ N    : Factor w/ 2 levels "0","1": 1 2 1 2 2 2 1 1 1 2 ...
# $ P    : Factor w/ 2 levels "0","1": 2 2 1 1 1 2 1 2 2 2 ...
# $ K    : Factor w/ 2 levels "0","1": 2 1 1 2 1 2 2 1 1 2 ...
# $ yield: num  49.5 62.8 46.8 57 59.8 58.5 55.5 56 62.8 55.8 ...

set.seed(0)
model <- lmrob(yield ~ N + P + K - 1, data= df)

Task

I want to access the p-values for each coefficient of the model object. To avoid an unnecessary intercept I am already using - 1 in the formula.

summary(model)$coefficients

# Estimate Std. Error    t value     Pr(>|t|)
# N0 54.644672   2.400075 22.7678995 8.972084e-16
# N1 60.166737   1.966661 30.5933467 2.858276e-18
# P1 -1.059299   2.139443 -0.4951286 6.259053e-01
# K1 -3.905052   2.226012 -1.7542822 9.469295e-02

Seems that the baseline (reference) levels for P and K are hidden.

Question

How can I change the code to access the p-values also for P0and K0 as coefficients for the model object?

Note: I am not sure if it makes a difference for the solution, that I am actually using in my real problem lmrob as robust regression function, so I decided to better keep it in this reproducible example.

Was it helpful?

Solution

The p-values that are estimated are:

coef(summary(model))[, 4]

Regarding the reference levels, the model is using treatment contrasts so the values of the reference levels are all zero thus its not meaningful to ask for their p-values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top