Home > database >  ggeffects: raw data loss
ggeffects: raw data loss

Time:12-01

In my example:

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

I don't know why it's not working. Please, any ideas?

CodePudding user response:

The issue is that mutate strips off all attributes from your ggeffects object, including the rawdata attribute. That's why you get an error.

This can seen by calling str before and after the mutate:

library(dplyr)
library(ggeffects)

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

Before the mutate step the ggeffects object includes a bunch of attributes:

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"

But none after the mutate step:

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

To fix your issue you could add your x1 column using e.g. base R like so:

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

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

  • Related