geom_smooth () с facet_grid () против mgcv ::: gam с by = "" - PullRequest
0 голосов
/ 27 октября 2018

Я сравниваю выходные данные игры, выполненной через geom_smooth ggplot2 с использованием facet_grid, и игру с использованием mgcv ::: gam с указанным фактором «by», визуализированным через visreg. При этом данные и код:

library(dplyr)
set.seed(1)
dat <- iris %>% mutate(response = sample(rep(c(0,1),length.out=150/2),150, replace=T))

#Just the output from geom_smooth
library(ggplot2)
ggplot(dat, aes(Sepal.Length,response)) +
  geom_point() +
  geom_smooth(method="gam", formula = y~s(x, bs="cs"), method.args=list("binomial")) +
  facet_grid(.~Species)
#Now performing the gam through mgcv:::gam specifying by=Species
library(mgcv)
gam <- gam(dat, formula = response~s(Sepal.Length, bs="cs", by=Species),family = binomial())
#Comparing the two different outputs
library(visreg)
visreg(gam, "Sepal.Length", by="Species", scale="response", gg=T)  +
  guides(color=F)+
  geom_smooth(data=dat,aes(Sepal.Length,response),
              method="gam", formula = y~s(x, bs="cs"), method.args=list("binomial"), color="red", fill="green")

geom_smooth output shown with red smooth and green CI, mgcv with blue smooth and grey CI

По сути, я думаю, что mgmv ::: gam основывает свои сглаживания гаммы на неком типе «вмененных» данных для областей, где фактически нет ни одного для каждого уровня видов. Кажется, установка в geom_smooth () избегает этого. Кто-нибудь знает, как противодействовать этому, чтобы выходные данные geom_smooth и mgcv ::: gam были идентичны?

EDIT:

На основании ответа пользователя 20650 код был обновлен до:

library(mgcv)
gam <- gam(dat, formula = response~Species + s(Sepal.Length, bs="cs", by=Species),family = binomial()) 
library(visreg)
visreg(gam, "Sepal.Length", by="Species", scale="response", gg=T)  +
  guides(color=F)+
  geom_smooth(data=dat,aes(Sepal.Length,response),
              method="gam", formula = y~s(x, bs="cs"), method.args=list("binomial"), color="red", fill="green",
              fullrange=T)

updated plot

Как видно на приведенном выше графике, между этими двумя методами есть тонкие различия (в основном в КИ). Это подчеркивается, если мы посмотрим, например, set.seed (100): * * один тысяча двадцать-одна

library(dplyr)
set.seed(100)
dat <- iris %>% mutate(response = sample(rep(c(0,1),length.out=150/2),150, replace=T))

library(mgcv)
gam <- gam(dat, formula = response~Species + s(Sepal.Length, bs="cs", by=Species),family = binomial()) 
library(visreg)
visreg(gam, "Sepal.Length", by="Species", scale="response", gg=T)  +
  guides(color=F)+
  geom_smooth(data=dat,aes(Sepal.Length,response),
              method="gam", formula = y~s(x, bs="cs"), method.args=list("binomial"), color="red", fill="green",
              fullrange=T) 

new seed plot

Может кто-нибудь объяснить различия в двух методах и как сгенерировать идентичный вывод из geom_smooth () в mgcv ::: gam и наоборот (как в случае fullrange = T, так и fullrange = F)?

...