Solved – How to plot correlation coefficients from multiple regressions while leaving out some of the variables from the plot

data visualizationmultiple regressionr

Lets say that I have "y" that I want to model with linear regression. "x" and "z" are the things I'm interrested in showing folks, but I also have things that I want to adjust for, but not really show in my plot.

Now, I would like to show my coefficients in a plot, but I would like to keep the things I adjusted for out of it. So perhaps this could be coronary artery calcification modeled as "CAC ~ SomeBloodStuff + Bloodpressure + BMI + Smoking_status". The BMI and Smoking_status would be something that I would want to take into account, but just note that I have adjusted for them.

I gather this is how to do the model:

MyModel <- lm( CAC ~ SomeBloodstuff + BP + BMI + Smoking_status, data=MyData)
summary(MyModel) 
coefficients(MyModel)
confint(MyModel, level=0.95) # Seems like a legit model.

The coefplot function in arm library gives just the sort of presentation that I want, but shows all the dimensions.

library(arm)
coefplot(MyModel)

How could I leave those few variables out of the plot while keeping them in the regression and get a plot that looks like what the coefplot produces?

Best Answer

You can do what you're looking to do if you make fuller use of the coefplot package's optional arguments. In documentation, three relevant arguments are "predictors," "coefficients," and "strict":

predictors - A character vector specifying which variables to keep. Each individual variable has to be specfied [sic], so individual levels of factors must be specified. We are working on making this easier to implement, but this is the only option for now.

coefficients - A character vector specifying which factor variables to keep. It will keep all levels and any interactions, even if those are not listed.

strict - If TRUE then predictors will only be matched to its own coefficients, not its interactions [sic]

The following examples are selected from p. 8.

model1 <- lm(price ~ carat + cut*color, data=diamonds)

model2 <- lm(price ~ carat*color, data=diamonds)

model3 <- glm(price > 10000 ~ carat*color, data=diamonds)

coefplot(model1, predictors="color")

coefplot(model1, predictors="color", strict=TRUE)

coefplot(model1, coefficients=c("(Intercept)", "color.Q"))

coefplot(model1, predictors="cut", coefficients=c("(Intercept)", "color.Q"), strict=TRUE)
Related Question