Search code examples
rcoefplot

Subset of predictors using coefplot()


I'd like to do a plot of coefficients using coefplot() that only takes into account a subset of the predictors that I'm using. For example, if you have the code

 y1 <- rnorm(1000,50,23)
 x1 <- rnorm(1000,50,2) 
 x2 <- rbinom(1000,1,prob=0.63) 
 x3 <- rpois(1000, 2) 
 fit1 <- lm(y1 ~ x1 + x2 + x3)

and then ran

 coefplot(fit1)

it would give you a plot displaying the coefficients of the intercept, x1, x2 and x3. How can I modify this so I only get the coefficients for say, x1 and x2?


Solution

  • You can use the argument predictors and it will only plot the coefficients you need:

    library(coefplot)
    coefplot(fit1, predictors=c('x1','x2'))
    

    Output:

    enter image description here