Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

r - Retain factor labels in linear model results

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...