Search code examples
rlinear-regression

How do I extract the coefficient names from the lm coefficients?


I have the following code which displays some coefficients from lm

fit <-lm(Petal.Width ~ Petal.Length, data=iris) 
cf <-coef(summary(fit,complete = TRUE)) 
colnames(cf)[4] <- "pval"
cf<- data.frame(cf)
cf <-cf[cf$pval < 0.05,]
cf <-cf[order(-cf$pval), ]
head(cf)
cf[1,1]

I want to extract the names in the left column ie (intercept) and petal length. I thought I could use cf[1,1] but it shows the estimate


Solution

  • Those are extracted using rownames :

    fit <-lm(Petal.Width ~ Petal.Length, data=iris) 
    cf <-coef(summary(fit,complete = TRUE)) 
    rownames(cf)
    #[1] "(Intercept)"  "Petal.Length"