【问题标题】:gam plots with ggplot用 ggplot 绘制游戏图
【发布时间】:2018-03-25 00:12:51
【问题描述】:

我需要在 ggplot 中创建一些游戏图。我可以使用通用绘图功能来完成它们,但不确定如何使用 ggplot。这是我的代码和带有常规绘图功能的绘图。我正在使用 ISLR 包中的 College 数据集。

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")

【问题讨论】:

  • 查看 help("plot.gam") 并在 Value 下查看。

标签: r ggplot2 gam


【解决方案1】:

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

旧答案:

voxel 库中有一个使用 ggplot2 的 GAM 绘图实现。以下是你的做法:

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比较:

par(mfrow=c(2,3))
plot(gam.college, se=TRUE,col="blue")

您可能还想绘制观察值:

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 参数(游戏中的交互)尤其重要。

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 日更新。

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

library(mgcViz)
viz <- getViz(gam.college)
print(plot(viz, allTerms = T), pages = 1)

情节自定义类似 go ggplot2 语法:

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 展示了更多示例。

【讨论】:

  • 运行该代码后出现此错误。 plotGAM(gam.college, smooth.cov = x) 中的错误:gamFit 不是 gam 类型的对象
  • 您使用的gam 方法是什么库?如果你可以换成library(mgcv)mgcv::gam,它可能会起作用。
  • @missuse,是否可以在同一个图上绘制两个不同的模型,一个用于“Private”的每个级别,而不是使用交互?
  • @fibar 是的。生成两个模型,然后生成两个图,然后使用 gridExtracowplot 或其他几个包中的任何一个组合成一个图。
  • @misuse 我注意到plotGAMplot 有不同的y 轴。 plotGAM 中的 y 轴是因变量的预测值。 plot 中的 y 轴是什么意思?它们的值不相同(例如, 8000~16000 V.S. -6000~6000 )。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 2014-04-12
  • 2016-04-06
  • 2018-11-18
相关资源
最近更新 更多