首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ggplot绘制gam图

使用ggplot绘制gam图
EN

Stack Overflow用户
提问于 2018-03-25 08:12:51
回答 1查看 11K关注 0票数 8

我需要在ggplot中创建一些gam图。我可以用一般的plot函数来做这些事情,但是我不确定如何使用ggplot。下面是我的代码和使用常规plot函数绘制的图。我使用的是ISLR包中的学院数据集。

代码语言:javascript
复制
train.2 <- sample(dim(College)[1],2*dim(College)[1]/3)
train.college <- College[train.2,]
test.college <- College[-train.2,]
gam.college <- gam(Outstate~Private+s(Room.Board)+s(Personal)+s(PhD)+s(perc.alumni)+s(Expend)+s(Grad.Rate), data=train.college)
par(mfrow=c(2,2))
plot(gam.college, se=TRUE,col="blue")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-25 16:26:25

请参阅旧答案下面的更新。

老生常谈:

voxel库中有一个使用ggplot2实现GAM绘图的方法。下面是你该怎么做的:

代码语言:javascript
复制
library(ISLR)
library(mgcv)
library(voxel)
library(tidyverse)
library(gridExtra)
data(College)

set.seed(1)
train.2 <- sample(dim(College)[1],2*dim(College)[1]/3)
train.college <- College[train.2,]
test.college <- College[-train.2,]
gam.college <- gam(Outstate~Private+s(Room.Board)+s(Personal)+s(PhD)+s(perc.alumni)+s(Expend)+s(Grad.Rate), data=train.college)

vars <- c("Room.Board", "Personal", "PhD", "perc.alumni","Expend", "Grad.Rate")

map(vars, function(x){
  p <- plotGAM(gam.college, smooth.cov = x) #plot customization goes here
  g <- ggplotGrob(p)
}) %>%
  {grid.arrange(grobs = (.), ncol = 2, nrow = 3)}

在一堆错误之后:In plotGAM(gam.college, smooth.cov = x) : There are one or more factors in the model fit, please consider plotting by group since plot might be unprecise

plot.gam进行比较

代码语言:javascript
复制
par(mfrow=c(2,3))
plot(gam.college, se=TRUE,col="blue")

您可能还希望绘制观察值:

代码语言:javascript
复制
map(vars, function(x){
  p <- plotGAM(gam.college, smooth.cov = x) +
    geom_point(data = train.college, aes_string(y = "Outstate", x = x ), alpha = 0.2) +
    geom_rug(data = train.college, aes_string(y = "Outstate", x = x ), alpha = 0.2)
  g <- ggplotGrob(p)
}) %>%
  {grid.arrange(grobs = (.), ncol = 3, nrow = 2)}

或每个组(如果您使用by参数(gam中的交互),则尤其重要。

代码语言:javascript
复制
map(vars, function(x){
  p <- plotGAM(gam.college, smooth.cov = x, groupCovs = "Private") +
    geom_point(data = train.college, aes_string(y = "Outstate", x = x, color= "Private"), alpha = 0.2) +
    geom_rug(data = train.college, aes_string(y = "Outstate", x = x, color= "Private"  ), alpha = 0.2) +
    scale_color_manual("Private", values = c("#868686FF", "#0073C2FF")) +
    theme(legend.position="none")
  g <- ggplotGrob(p)
}) %>%
  {grid.arrange(grobs = (.), ncol = 3, nrow = 2)}

更新时间: 2020年1月8日

我目前认为与voxel::plotGAM函数相比,mgcViz包提供了更好的功能。使用上述数据集和模型的示例:

代码语言:javascript
复制
library(mgcViz)
viz <- getViz(gam.college)
print(plot(viz, allTerms = T), pages = 1)

绘图定制类似于go ggplot2语法:

代码语言:javascript
复制
trt <- plot(viz, allTerms = T) +
  l_points() +
  l_fitLine(linetype = 1)  +
  l_ciLine(linetype = 3) +
  l_ciBar() +
  l_rug() +
  theme_grey() 

print(trt, pages = 1)

这个vignette展示了更多的例子。

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49471300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档