s****y 发帖数: 297 | 1 In my dataset, I have several variables (Y, A, B, C...) and one factor.
I'm investigating the regression using the command
lm(Y ~ A+ B + C +...)
but now I want to know the regression coefficients of A, B, and C... for
every factor levels.
The factor have more than 20 levels...
Is there any good way to do this in R?
Thanks in advance!! |
s****y 发帖数: 297 | |
O*****y 发帖数: 222 | 3 If you put your data into a data frame named df, then
summary(lm(Y~ ., data=df))
will print out all the coefficients.
【在 s****y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : In my dataset, I have several variables (Y, A, B, C...) and one factor. : I'm investigating the regression using the command : lm(Y ~ A+ B + C +...) : but now I want to know the regression coefficients of A, B, and C... for : every factor levels. : The factor have more than 20 levels... : Is there any good way to do this in R? : Thanks in advance!!
|
l*********s 发帖数: 5409 | 4 lm does handle factors.
【在 s****y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : In my dataset, I have several variables (Y, A, B, C...) and one factor. : I'm investigating the regression using the command : lm(Y ~ A+ B + C +...) : but now I want to know the regression coefficients of A, B, and C... for : every factor levels. : The factor have more than 20 levels... : Is there any good way to do this in R? : Thanks in advance!!
|
s****y 发帖数: 297 | 5 多谢留上两位,可能是我的问题没有说明白,我的意思是我需要在每一个factor level
都fit一次 lm (y~A+B+C...), 这样如果factor有20多个level,我就需要20多次
summary,
可有简便一点的方法? 我知道在SAS 里有proc reg, by factor, 这样会给出每个
level的结果, R里有类似的办法吗?
谢谢啦!! |
l*********s 发帖数: 5409 | 6 What is not clear is not your statement but your understanding :-)
You only need to fit lm once with the categorical variable, say D, which is a
factor of 20 lvls. lm function will give you coeff estimate for 19 lvls except the base lvl.
level
【在 s****y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 多谢留上两位,可能是我的问题没有说明白,我的意思是我需要在每一个factor level : 都fit一次 lm (y~A+B+C...), 这样如果factor有20多个level,我就需要20多次 : summary, : 可有简便一点的方法? 我知道在SAS 里有proc reg, by factor, 这样会给出每个 : level的结果, R里有类似的办法吗? : 谢谢啦!!
|
s****y 发帖数: 297 | 7 可是我感兴趣的是在每个factor level的A, B,C的coeff extimate 啊,呵呵
用for 循环固然可以,可是有没有更简便的办法呢?谢谢了... |
n*****t 发帖数: 41 | 8 lm (y~(A+B+C...)*factor)不就可以吗 |
s****y 发帖数: 297 | 9 不行啊,这样只有一个sigma,一个intercept啊,我需要在每个factor level都有一个
值啊,呵呵
【在 n*****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : lm (y~(A+B+C...)*factor)不就可以吗
|
D******n 发帖数: 2836 | 10 ?by
level
【在 s****y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 多谢留上两位,可能是我的问题没有说明白,我的意思是我需要在每一个factor level : 都fit一次 lm (y~A+B+C...), 这样如果factor有20多个level,我就需要20多次 : summary, : 可有简便一点的方法? 我知道在SAS 里有proc reg, by factor, 这样会给出每个 : level的结果, R里有类似的办法吗? : 谢谢啦!!
|
s****y 发帖数: 297 | 11 啥意思?
【在 D******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : ?by : : level
|
D******n 发帖数: 2836 | 12 type this in R and you will know.
there is an example in the end which is exactly describing your case.
【在 s****y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 啥意思?
|