# p value for the last level using lm with contrast sum in R

How do I get the p-value for the last level in a categorical variable from a linear regression model in R when contrast is set to contr.sum

set.seed(1)
x = rep(c('a', 'b', 'c'), c(10, 10, 10))
y = c(runif(10, 1, 2), runif(10, 2, 3), runif(10, 3, 4))
m = lm(y ~ x, contrasts = list(x = contr.sum))

summary(m)
# Call:
#   lm(formula = y ~ x, contrasts = list(x = contr.sum))
#
# Residuals:
#   Min       1Q   Median       3Q      Max
# -0.48973 -0.19948 -0.03407  0.21672  0.51638
#
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)
# (Intercept)  2.50955    0.05447   46.07  < 2e-16 ***
#   x1          -0.95804    0.07703  -12.44 1.09e-12 ***
#   x2           0.04927    0.07703    0.64    0.528
# ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 0.2984 on 27 degrees of freedom
# Multiple R-squared:  0.879,   Adjusted R-squared:   0.87
# F-statistic: 98.08 on 2 and 27 DF,  p-value: 4.141e-13


I understand the Intercept 2.50955 is the mean of means of all 3 levels:

• The mean of level a is Intercept + x1 (2.50955 + -0.95804) = 1.551514
• The mean of level b is Intercept + x2 (2.50955 + 0.04927) = 2.55882
• The mean of level c is Intercept - x1 - x2 = (2.50955 - -0.95804 - 0.04927) = 3.418323

How do I get the Std. Error, t value and P for level c?

You should use R package "lsmeans"

And you can get the Std. Errors and intervals. See the below results:

library(lsmeans)

(lsmeans(m,c("x"))->hh)

x lsmean SE df lower.CL upper.CL

a 1.490133 0.0852562 27 1.315201 1.665064

b 2.588532 0.0852562 27 2.413601 2.763464

c 3.634533 0.0852562 27 3.459602 3.809464

Confidence level used: 0.95

After then, we can calculate t value and P-value.

• Thanks, what type of p value adjustment should be used in this case for the multiple calculation? – Green Dec 15 '17 at 3:41
• Try this R code:"(contrast(hh,method="revpairwise")->ii)". And then, you can obtain results of the p-value adjustement according to tukey method. – J-H Yoon Dec 15 '17 at 3:54