我正打算去画一条长颈鹿。使用ggplot对多个模型进行估算。
我想估计一些模型,然后将它们的点估计值同时绘制到一个图中。
使用简单的示例:
library(ggplot2)
model1 = lm(hp ~ gear + am + mpg + carb, data = mtcars)
model2 = lm(hp ~ gear + am + I(am^2) + mpg + carb, data = mtcars)
model3 = lm(hp ~ gear + am + I(am^2) + mpg + carb, data = mtcars)
model4 = lm(hp ~ gear + am + I(am^2) + mpg , data = mtcars)
model5 = lm(hp ~ gear + am + I(am^2) + log(mpg) + log(carb), data = mtcars)
ce = function(model.obj) {
extract = summary(get(model.obj))$coefficients[ ,1:2]
return(data.frame(extract, vars=row.names(extract), model=model.obj))
}
# Run function on the three models and bind into single data frame
coefs = do.call(rbind, sapply(paste0(list("model1", "model2", "model3", "model4", "model5")), ce, simplify=FALSE))
names(coefs)[2] = "se"
ggplot(coefs, aes(vars, Estimate)) +
geom_hline(yintercept=0, lty=2, lwd=1, colour="red") +
geom_errorbar(aes(ymin=Estimate - se, ymax=Estimate + se, colour=vars),
lwd=1, width=0) +
geom_point(size=3, aes(colour=vars)) +
facet_grid(. ~ model) +
coord_flip() +
guides(colour=FALSE) +
labs(x="Coefficient", y="Value")
我们将获得5个地块(1行和5列),有没有一种简单的方法可以转换地块,因此我们将同时拥有5个地块,但是维数是5行和1列?