Extract regression coefficient values
A summary.lm
object stores these values in a matrix
called 'coefficients'
. So the value you are after can be accessed with:
a2Pval <- summary(mg)$coefficients[2, 4]
Or, more generally/readably, coef(summary(mg))["a2","Pr(>|t|)"]
. See here for why this method is preferred.
The package broom
comes in handy here (it uses the "tidy" format).
tidy(mg)
will give a nicely formated data.frame with coefficients, t statistics etc. Works also for other models (e.g. plm, ...).
Example from broom
's github repo:
lmfit <- lm(mpg ~ wt, mtcars)require(broom) tidy(lmfit) term estimate std.error statistic p.value1 (Intercept) 37.285 1.8776 19.858 8.242e-192 wt -5.344 0.5591 -9.559 1.294e-10is.data.frame(tidy(lmfit))[1] TRUE
Just pass your regression model into the following function:
plot_coeffs <- function(mlr_model) { coeffs <- coefficients(mlr_model) mp <- barplot(coeffs, col="#3F97D0", xaxt='n', main="Regression Coefficients") lablist <- names(coeffs) text(mp, par("usr")[3], labels = lablist, srt = 45, adj = c(1.1,1.1), xpd = TRUE, cex=0.6) }
Use as follows:
model <- lm(Petal.Width ~ ., data = iris)plot_coeffs(model)