Creating a matrix of coefficients from coef function in R
Pergunta
From this code:
treatment <- factor(rep(c(1, 2), c(43, 41)),
levels = c(1, 2),
labels = c("placebo", "treated"))
improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)),
levels = c(1, 2, 3),
labels = c("none", "some", "marked"))
numberofdrugs <- rpois(84, 10) + 1
healthvalue <- rpois(84, 5)
y <- data.frame(healthvalue, numberofdrugs, treatment, improved)
test <- glm(healthvalue~numberofdrugs+treatment+improved + treatment:improved, y, family=poisson)
summary(test)
we get the following using the coef() function:
> coef(test)
(Intercept) numberofdrugs treatmenttreated
1.549172817 0.004261529 0.014634807
improvedsome improvedmarked treatmenttreated:improvedsome
0.201150827 -0.129251907 -0.258841251
treatmenttreated:improvedmarked
0.051326071
I wish to arrange the coefficients of the variables into vectors and the coefficients of the interactions into a matrix, to be able to more easily work with them.
For example:
coef.intercept=(1.5491)
coef.numberofdrugs=(0.00426)
coef.treatment=(0, 0.01463)
coef.improved=(0, 0.2011, -0.1292)
and a correlation matrix with treatment as rows and improved as columns, like this:
coef.correlation=
(0 0 0 )
(0 -0.2588 -0.2588)
Is there any efficient way to do this?
Note that any of the coefficients that are not listed in coef() function shall be set to zero and I have shortened some of the numbers above.
Solução
I believe allEffects
from the effects
package may be of interest:
library(effects)
allEffects(test)
model: healthvalue ~ numberofdrugs + treatment + improved + treatment:improved
numberofdrugs effect
numberofdrugs
6 8 10 12 14 16 18 20
4.050962 4.322559 4.612365 4.921601 5.251570 5.603662 5.979360 6.380247
treatment*improved effect
improved
treatment none some marked
placebo 4.416773 3.713517 5.461153
treated 4.596433 4.902746 5.309627
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow