R语言 gg效果:原始数据丢失

62o28rlo  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(126)

在我的例子中:

library(dplyr)
library(readr)
library(ggplot2)
library(ggeffects)

# Read in the data
ds <- structure(list(Especie = c("C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", "C_externa_1", 
"C_externa_1", "C_externa_1", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", "C_cubana_2", 
"C_cubana_2", "C_cubana_2", "C_cubana_2"), 
Tentou_predar = c(3L, 25L, 20L, 36L, 12L, 0L, 1L, 10L, 
0L, 14L, 2L, 0L, 0L, 0L, 0L, 32L, 0L, 0L, 25L, 0L, 2L, 2L, 35L, 
0L, 0L, 0L, 22L, 0L, 2L, 9L, 54L, 57L, 26L, 17L, 18L, 34L, 2L, 
0L, 20L, 25L, 6L, 65L, 36L, 6L, 62L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 
1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L)), class = "data.frame", row.names = c(NA, 
-105L))
str(ds)

# Create a glm model
m_Pred <- glm(Tentou_predar ~ Especie, data = ds, 
                 family = "poisson")

# Organized data and plot
ds.1.1.1 <- ds %>% mutate(x_1=  1+(readr::parse_number(Especie)-2)*0.05, 
                    group = Especie)
df_gg <-ggeffects::ggpredict(m_Pred, terms = "Especie [all]")%>% 
  mutate(x_1=  1+(readr::parse_number(as.character(group))-2)*0.05)
str(df_gg)
df_gg %>% plot(add.data = TRUE)
# Raw data not available.
# Error in if (attr(x, "logistic", exact = TRUE) == "1" && attr(x, "is.trial",  : 
#   missing value where TRUE/FALSE needed

我不知道为什么不管用。拜托,有什么办法吗?

j5fpnvbx

j5fpnvbx1#

问题是mutateggeffects对象中剥离了所有的attributes,包括rawdata属性,这就是为什么会出现错误。
这可以通过在mutate之前和之后调用str看出:

library(dplyr)
library(ggeffects)

df_gg <- ggeffects::ggpredict(m_Pred, terms = "Especie [all]")

mutate步骤之前,ggeffects对象包括一组属性:

str(df_gg)
#> Classes 'ggeffects' and 'data.frame':    2 obs. of  6 variables:
#>  $ x        : Factor w/ 2 levels "C_cubana_2","C_externa_1": 1 2
#>  $ predicted: num  0.233 15.111
#>  $ std.error: num  0.2673 0.0383
#>  $ conf.low : num  0.138 14.017
#>  $ conf.high: num  0.394 16.291
#>  $ group    : Factor w/ 1 level "1": 1 1
#>  - attr(*, "legend.labels")= chr "1"
#>  - attr(*, "x.is.factor")= chr "1"
#>  - attr(*, "continuous.group")= logi FALSE
#>  - attr(*, "rawdata")='data.frame':  105 obs. of  4 variables:
#>   ..$ response: int [1:105] 3 25 20 36 12 0 1 10 0 14 ...
#>   ..$ x       : num [1:105] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ group   : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ facet   : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, "title")= chr "Predicted counts of Tentou_predar"
#>  - attr(*, "x.title")= chr "Especie"
#>  - attr(*, "y.title")= chr "Tentou_predar"
#>  - attr(*, "legend.title")= chr NA
#>  - attr(*, "x.axis.labels")= chr [1:2] "C_externa_1" "C_cubana_2"
#>  - attr(*, "constant.values")= Named list()
#>  - attr(*, "terms")= chr "Especie"
#>  - attr(*, "original.terms")= chr "Especie [all]"
#>  - attr(*, "at.list")=List of 1
#>   ..$ Especie: chr [1:2] "C_cubana_2" "C_externa_1"
#>  - attr(*, "ci.lvl")= num 0.95
#>  - attr(*, "type")= chr "fe"
#>  - attr(*, "response.name")= chr "Tentou_predar"
#>  - attr(*, "family")= chr "poisson"
#>  - attr(*, "link")= chr "log"
#>  - attr(*, "logistic")= chr "0"
#>  - attr(*, "link_inverse")=function (eta)  
#>  - attr(*, "link_function")=function (mu)  
#>  - attr(*, "is.trial")= chr "0"
#>  - attr(*, "fitfun")= chr "glm"
#>  - attr(*, "model.name")= chr "m_Pred"

但在mutate步骤之后没有:

df_gg |> mutate(x_1=  1+(readr::parse_number(as.character(group))-2)*0.05) |> str()
#> Classes 'ggeffects' and 'data.frame':    2 obs. of  7 variables:
#>  $ x        : Factor w/ 2 levels "C_cubana_2","C_externa_1": 1 2
#>  $ predicted: num  0.233 15.111
#>  $ std.error: num  0.2673 0.0383
#>  $ conf.low : num  0.138 14.017
#>  $ conf.high: num  0.394 16.291
#>  $ group    : Factor w/ 1 level "1": 1 1
#>  $ x_1      : num  0.95 0.95

要解决您的问题,您可以使用例如base R添加x1列,如下所示:

df_gg$x_1 <- 1 + (readr::parse_number(as.character(df_gg$group)) - 2) * 0.05

df_gg %>% plot(add.data = TRUE)

相关问题