Reputation: 187
How does one retain labels in linear models? How to have the slope labelled Bar
or xBar
in the coefficient table below?
dat <- data.frame(
y = c(1:3, 10:12),
x = gl(2, 3, labels = c('Foo', 'Bar'))
)
coef(summary(mod <- lm(y ~ x, dat)))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 6.5 0.4082483 15.92168 0.0000909619
# x1 -4.5 0.4082483 -11.02270 0.0003850677
Actual use case is more complicated, tens of linear models with tens of variables. I do realize that I could write my own function that both retrieves the labels and fits the model, but I wonder if I am missing something simpler.
Upvotes: 0
Views: 254
Reputation: 21992
It already works like that for me:
dat <- data.frame(
y = c(1:3, 10:12),
x = gl(2, 3, labels = c('Foo', 'Bar'))
)
coef(summary(mod <- lm(y ~ x, dat)))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 2 0.5773503 3.464102 0.0257214207
# xBar 9 0.8164966 11.022704 0.0003850677
Upvotes: 1