Home > Software design >  Argument "by" is missing, with no default
Argument "by" is missing, with no default

Time:02-25

Here is my reprodicible sample and code:

set.seed(42)
n <- 1000
dat <- data.frame(Confidence=1:5, 
                  Type = ("Your confidence"),
                  Correct=sample(0:1, n, replace=TRUE)
)

dat %>%
  group_by(Confidence,Type) %>%
  summarize(Accuracy = mean(Correct),
            se = (sd(Correct, na.rm = TRUE)/sqrt(n())))%>%
  ggplot(aes(x=Confidence, y= Accuracy, color = Type, group = Type))  
  geom_line()   
  geom_errorbar(aes(ymin = Accuracy - se, ymax = Accuracy   se), color = "Black", size = .15, width = .3)   
  geom_point(size = 2)  
  scale_y_continuous(labels = scales::percent)  
  labs(y= "YOU ARE RIGHT ___% OF THE TIME.", x = "WHEN YOU ARE ___ % CONFIDENT IN YOUR ANSWER...") 
  theme_minimal()   
  scale_color_brewer(palette = "Set1")

When I run this i get this error:

Error in summarize(., Accuracy = mean(Correct), se = (sd(Correct, na.rm = TRUE)/sqrt(n()))) : 
  argument "by" is missing, with no default

Has anyone seen this error before and know how to fix it? Thank you!

CodePudding user response:

summarize is found in multiple packages and if we load many packages that have the summarize (depending on the order of loading the package) it could mask the function. e.g. the error seems to be coming from Hmisc::summarize

> dat %>%
    group_by(Confidence,Type) %>%
    Hmisc::summarize(Accuracy = mean(Correct),
              se = (sd(Correct, na.rm = TRUE)/sqrt(n())))%>%
    ggplot(aes(x=Confidence, y= Accuracy, color = Type, group = Type))  
    geom_line()   
    geom_errorbar(aes(ymin = Accuracy - se, ymax = Accuracy   se), color = "Black", size = .15, width = .3)   
    geom_point(size = 2)  
    scale_y_continuous(labels = scales::percent)  
    labs(y= "YOU ARE RIGHT ___% OF THE TIME.", x = "WHEN YOU ARE ___ % CONFIDENT IN YOUR ANSWER...") 
    theme_minimal()   
    scale_color_brewer(palette = "Set1")
Error in Hmisc::summarize(., Accuracy = mean(Correct), se = (sd(Correct,  : 
  argument "by" is missing, with no default

If we change it to dplyr::summarize

dat %>%
  group_by(Confidence,Type) %>%
  dplyr::summarize(Accuracy = mean(Correct),
            se = (sd(Correct, na.rm = TRUE)/sqrt(n())))%>%
  ggplot(aes(x=Confidence, y= Accuracy, color = Type, group = Type))  
  geom_line()   
  geom_errorbar(aes(ymin = Accuracy - se, ymax = Accuracy   se), color = "Black", size = .15, width = .3)   
  geom_point(size = 2)  
  scale_y_continuous(labels = scales::percent)  
  labs(y= "YOU ARE RIGHT ___% OF THE TIME.", x = "WHEN YOU ARE ___ % CONFIDENT IN YOUR ANSWER...") 
  theme_minimal()   
  scale_color_brewer(palette = "Set1")

-output

enter image description here

  • Related