How to remove row of intercepts/only include the coefficients in a cbind of lm data by indexing? Why does it work?

StackOverflow https://stackoverflow.com/questions/18971287

  •  29-06-2022
  •  | 
  •  

Pregunta

I managed to remove the first row of the matrix from what I learned from digging around this website, but apparently there's an easier way to do it. This is what I currently have:

> prestige.income <- lm(prestige ~ log2(income), data=Prestige)
> prestige.edu <- lm(prestige ~ education, data=Prestige)
> prestige.women <- lm(prestige ~ women, data=Prestige)
> prestige.all <- lm(prestige ~ education + income + women, data=Prestige) 
> comparison <- cbind(INCOME=coef(prestige.income), EDUCATION=coef(prestige.edu),
  WOMEN=coef(prestige.women), ALL=coef(prestige.mod))
> comparison
                 INCOME  EDUCATION       WOMEN           ALL
(Intercept)  -139.85572 -10.731982 48.69299929 -110.96582409
education      14.94173   5.360878 -0.06417284    3.73050783
log2(income) -139.85572 -10.731982 48.69299929    9.31466643
women          14.94173   5.360878 -0.06417284    0.04689514
> remove <- rownames(comparison)[1]
> remove
[1] "(Intercept)"
> comp.noint <- comparison[!rownames(comparison) %in% remove, ]
> comp.noint
             INCOME  EDUCATION       WOMEN        ALL
education      14.94173   5.360878 -0.06417284 3.73050783
log2(income) -139.85572 -10.731982 48.69299929 9.31466643
women          14.94173   5.360878 -0.06417284 0.04689514

This is supposedly the easier way/way I was supposed to do it, but I don't understand why it works:

> comparison[-1,-5]
             INCOME  EDUCATION       WOMEN        ALL
education      14.94173   5.360878 -0.06417284 3.73050783
log2(income) -139.85572 -10.731982 48.69299929 9.31466643
women          14.94173   5.360878 -0.06417284 0.04689514

I was told the -1 gets rid of the first row, but it doesn't actually do that..

> comparison[-1]
 [1]   14.94173191 -139.85572422   14.94173191  -10.73198197    5.36087773
 [6]  -10.73198197    5.36087773   48.69299929   -0.06417284   48.69299929
[11]   -0.06417284 -110.96582409    3.73050783    9.31466643    0.04689514

And I was given no explanation for why the -5 does anything. I know this is probably a really basic question, but my teacher couldn't explain to me why this works and I can't find anything on Google or in the textbooks. Any help would be great. Thanks!

¿Fue útil?

Solución

Using comparison[-1] with a single argument for [], you no longer have a matrix, but a vector of length 16 (4 x 4), formed by all columns of comparison concatenated one after the other. A negative argument drops the first element (-139.85572) and you are left with the remaining 15. Note that they are printed left to right, not top to bottom like columns of a matrix.

Using comparison[-1,-5] you still have a matrix, where the first row and fifth column are droped. Oh, wait, there is no fifth column, so -5 is ignored. Try comparison[-1,-4] for instance.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top