title: “Day 7 live” format: html editor: visual —

Read the data from the

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
url <- "https://raw.githubusercontent.com/jlacasa/stat705_fall2024/main/classes/data/lotus_hw1.csv"
data <- read.csv(url)
m <- lm(stm.length_cm ~ 1 + doy,  data = data)
confint(m)
##                   2.5 %      97.5 %
## (Intercept) -93.3506731 -57.5098063
## doy           0.5618765   0.7436752

Let’s estimate the fitted value and 95% (the default) estimation/prediction confidence intervals for a single point, when doy = 200.

dd_new <- data.frame(doy =200)

predict(m, newdata = dd_new, type = "response", 
        interval = "none")
##        1 
## 55.12493
predict(m, newdata = dd_new, type = "response", 
        interval = "confidence")
##        fit      lwr      upr
## 1 55.12493 52.44918 57.80067
predict(m, newdata = dd_new, type = "response", 
        interval = "prediction")
##        fit      lwr      upr
## 1 55.12493 36.66128 73.58857

Let’s estimate the fitted value and 95% (the default) estimation/prediction confidence intervals for a bunch of points, between doy = 160 and doy = 260.

dd_new <- data.frame(doy =160:260)

fitted <- predict(m, newdata = dd_new, type = "response", interval = "none")
plot(dd_new$doy, fitted, ty = 'l')

est_uncert <- predict(m, newdata = dd_new, type = "response",  interval = "confidence")
as.data.frame(est_uncert) %>% 
  bind_cols(dd_new) %>% 
  ggplot(aes(doy, fit))+
  geom_ribbon(aes(ymin = lwr, ymax = upr), fill = 'grey70')+
  geom_line()

pred_uncert <- predict(m, newdata = dd_new, type = "response", 
        interval = "prediction")

as.data.frame(pred_uncert) %>% 
  bind_cols(dd_new) %>% 
  ggplot(aes(doy, fit))+
  geom_ribbon(aes(ymin = lwr, ymax = upr), fill = 'grey70')+
  geom_line()