Create a new function, but always showed the error

Originally, I just create a ggplot to add prediction interval, like:

# 0. Build linear model 
# load data
food <- read_csv("~/Desktop/fiber_test.csv")
  
model <- lm(fiber0812 ~ month, data = food)
summary(model)
newmodel <- augment(model)


# tidy your model and include 95% confidence intervals
tidy(model, conf.int = T)

# 1. Add predictions 
pred.int <- predict(model, interval = "prediction")
mydata <- cbind(food, pred.int)

# 2. Regression line + confidence intervals
p <- ggplot(mydata, aes(month, fiber0812)) +
  geom_point(aes(x = month, y = fiber0812), color = "grey25", size = 0.8) +
  stat_smooth(aes(colour = "Fitted Line"), method = "lm", size = 0.8, se = FALSE) + 
  geom_line(data = newmodel, aes(y = .fitted + 1.96*.se.fit, colour = "95% CI"), size = 0.5) +
  geom_line(data = newmodel, aes(y = .fitted - 1.96*.se.fit, colour = "95% CI"), size = 0.5)+
  scale_x_continuous(breaks = c(seq(0, 15, by = 1))) +
  xlab("Month") +
  ylab("Content (%)") +
  ggtitle("Batch Number 0812")

# 3. Add prediction intervals
p + 
  geom_line(aes(y = lwr, colour = "95% PI"), linetype = "dashed") +
  geom_line(aes(y = upr, colour = "95% PI"), linetype = "dashed") +
  theme_set(theme_bw()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  scale_colour_manual(name = "Legend", 
                      values=c("Fitted Line" = "black", "95% CI" = "blue4", "95% PI" = "red"))

But I have a lot of data to run and show the results, I want to create the function like this:

# load data
food <- read_csv("~/Desktop/fiber_test.csv")


prediction <- function(food, sample){
  # 0. Build linear model 
  
  model <- lm(sample ~ month, data = food)
  summary(model)
  newmodel <- augment(model)


  # tidy your model and include 95% confidence intervals
  tidy(model, conf.int = T)

  # 1. Add predictions 
  pred.int <- predict(model, interval = "prediction")
  mydata <- cbind(food, pred.int)

  # 2. Regression line + confidence intervals
  p <- ggplot(mydata, aes(month, sample)) +
    geom_point(aes(x = month, y = sample), color = "grey25", size = 0.8) +
    stat_smooth(aes(colour = "Fitted Line"), method = "lm", size = 0.8, se = FALSE) + 
    geom_line(data = newmodel, aes(y = .fitted + 1.96*.se.fit, colour = "95% CI"), size = 0.5) +
    geom_line(data = newmodel, aes(y = .fitted - 1.96*.se.fit, colour = "95% CI"), size = 0.5)+
    scale_x_continuous(breaks = c(seq(0, 15, by = 1))) +
    xlab("Month") +
    ylab("Content (%)") +
    ggtitle("Batch Number 0812")

  # 3. Add prediction intervals
  p + 
    geom_line(aes(y = lwr, colour = "95% PI"), linetype = "dashed") +
    geom_line(aes(y = upr, colour = "95% PI"), linetype = "dashed") +
    theme_set(theme_bw()) +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
          panel.background = element_blank(), axis.line = element_line(colour = "black")) +
   scale_colour_manual(name = "Legend", 
                       values=c("Fitted Line" = "black", "95% CI" = "blue4", "95% PI" = "red"))
}

But it always show "Error in eval(predvars, data, env) : object 'fiber0812' not found" when I ran the code "prediction(food, sample = fiber0812)"

This is a namespace problem. It's easy to fix. Restart your R session, run the code and then check what's there with

ls()

fiber0812 possibly wants to be something like food$fiber0812

Thank you! The function works when I typed food$fiber0812.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.