user2685139
user2685139

Reputation: 111

covariance table for more variables

I've got three parameters a,b and c. Every parameter is a factor with three categories. I wanted to fit a multinomial regression with the car package.

require(car)
a <- sample(3, 100, TRUE)
b <- sample(3, 100, TRUE)
c <- sample(3, 100, TRUE)
a <- as.factor(a)
b <- as.factor(b)
c <- as.factor(c)
testus <- multinom(c ~ a + b)
predictors <-
expand.grid(b=c("1","2","3","4","5"),a=c("1","2","3","4","5"))
p.fit <- predict(testus, predictors, type='probs')
probabilities<-data.frame(predictors,p.fit)

Now I got the predicted probabilities for a under b and c.

>

`head(probabilities)
>  b a         X1         X2         X3         X4        X5
>1 1 1 0.10609054 0.22599152 0.20107167 0.21953158 0.2473147
>2 2 1 0.20886614 0.27207108 0.08613633 0.18276394 0.2501625
>3 3 1 0.17041268 0.24995975 0.16234240 0.13111518 0.2861700
>4 4 1 0.23704078 0.21179521 0.08493274 0.03135092 0.4348804
>5 5 1 0.09494071 0.09659144 0.24162612 0.21812449 0.3487172
>6 1 2 0.14059489 0.17793438 0.29272452 0.26104833 0.1276979`

The first two cols shows the categories of the independent variables a and b. the next five colums show the conditional probabilities (p.e. P(c=1|b==1&&a==1)=0,10609.

I need the variance covariance and did:

vcov(testus)
             2:(Intercept)        2:b2         2:b3         2:c2        2:c3 ....
2:(Intercept)   .......................................  
2:b2            ................................ 
2:b3             .................
2:c2             ..............
2:c3            .............
3:(Intercept)  .............
....

Sorry for pasting only a part of the matrix, but otherwise it would be to long. What I would like to have, is a variance covariance matrix for the simultaneous observation of two variables(vcov(a,b&c)). That means, that I would like to get variance (covariance)between my variable a and the simultaneous observation of b and c as I created with "probabilities". I would like to get the output

             2:(Intercept)        2:b2&c2         2:b2&c3         ....
2:(Intercept)   .......................................  
2:b2&c2            ................................ 
2:b3&c3             .................
3:(Intercept)  .............
....

Is this possible?

Upvotes: 1

Views: 227

Answers (1)

IRTFM
IRTFM

Reputation: 263382

Perhaps:

testus <- multinom(c ~ a : b)
vcov(testus)

I say 'perhaps' because there is also the possibility of using the c ~ a*b model and it's not clear what you want exactly. (The statistical question has not been defined and I would not think this to be a sufficient number of observations to a stable estimate.) At any rate:

 colnames( vcov(testus))
 #-----------
 [1] "2:(Intercept)" "2:a1:b1"       "2:a2:b1"      
 [4] "2:a3:b1"       "2:a1:b2"       "2:a2:b2"      
 [7] "2:a3:b2"       "2:a1:b3"       "2:a2:b3"      
[10] "2:a3:b3"       "3:(Intercept)" "3:a1:b1"      
[13] "3:a2:b1"       "3:a3:b1"       "3:a1:b2"      
[16] "3:a2:b2"       "3:a3:b2"       "3:a1:b3"      
[19] "3:a2:b3"       "3:a3:b3"     

rownames( vcov(testus))
#--------
 [1] "2:(Intercept)" "2:a1:b1"       "2:a2:b1"      
 [4] "2:a3:b1"       "2:a1:b2"       "2:a2:b2"      
 [7] "2:a3:b2"       "2:a1:b3"       "2:a2:b3"      
[10] "2:a3:b3"       "3:(Intercept)" "3:a1:b1"      
[13] "3:a2:b1"       "3:a3:b1"       "3:a1:b2"      
[16] "3:a2:b2"       "3:a3:b2"       "3:a1:b3"      
[19] "3:a2:b3"       "3:a3:b3"      

Upvotes: 1

Related Questions