---
title: "Challenger data"
author: "Nancy"
date: "18/10/2022"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
# knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(
  echo = TRUE,
  message = FALSE,
  warning = FALSE,
  error = FALSE, 
  out.width = "100%",
  fig.width = 10,
  fig.height = 6.7, 
  fig.retina = 3,
  cache = FALSE)
```

## Analysis of the Challenger O-Ring Failure Data
```{r load data}
library(faraway)
data(orings)
#View(orings)
library(SMPracticals)
data(shuttle)
#View(shuttle)
```

There is a discrepancy between ELM and SM about the data. Going back to the original source (Dalal et al., 1989), and also the Rogers Commission Report (1986), it seems that SM has the correct numbers. I think they are counting slightly different things. There is also a weird error in the `shuttle` data frame, that is solved by
```{r fix}
shuttle2 <- data.frame(shuttle)
```

<!-- Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. -->

I'll fit and then plot both sets of data; note I've also used two different summaries; `sumary` provided by Faraway and the usual one.

```{r fitting}
# Faraway's fitting

logitmod <- glm(cbind(damage,6-damage) ~ temp, family = binomial,
                data = orings)
sumary(logitmod)

# Davison's fitting

logitmodcorrect <- glm(cbind(r,m-r) ~ temperature, family = binomial,
                       data = shuttle2)
summary(logitmodcorrect)
```
Now I'll compute and then plot the fitted curves. The function `ilogit` is provided by Faraway, it's the inverse.
```{r curve}
tempvals <- seq(30,80,length=100)

# predicting prob of O-ring damage at temperature = 31 degrees F

ilogit(5.085 -.11560*31); ilogit(11.663-.2162*31)

## adding the fitted curves to the plots 
tempvals <- seq(30,80,length=100)


par(mfrow=c(2,2)) # 4 plots on one page
with(orings, plot(temp,damage,main="Faraway",xlim=c(31,80)))
with(shuttle2,plot(temperature,r,main="Davison",xlim=c(31,80),ylim=c(0,5)))



with(orings, plot(temp,damage/6,main="Faraway",xlim=c(31,80), ylim=c(0,1)))
lines(tempvals,ilogit(11.6630-.2162*tempvals))
abline(v=32, lty=2)


with(shuttle2,plot(temperature,r/6,main="Davison",xlim=c(31,80),ylim=c(0,1)))
lines(tempvals,ilogit(5.08498-.1156*tempvals))
abline(v=32, lty=2)

```
