library(ggplot2)
#8.2 Baby weights

##a) baby_weight = 120.07 - 1.93*parity
##b) 
x=0
y<- 120.07-1.93*x
y
## [1] 120.07
x=1
y
## [1] 120.07
##Average birth weight decreases by 1.93 ounces if the baby is not a newborn.
##c)P value of .1052 is greater than .05, so there is no statistically significant relationship.

#8.4 Absenteeism

##a) y= 18.93 -9.11*eth +3.11*sex +2.15*lrn
##b) eth: Average no of days decreases by 9.11 if ethnicity is not aboriginal
##   sex: Average no of days increases by 3.11 if gender is male
##   lrn: Average no of days increases by 2.15 if person is a slow learner
##c) 
a<- 18.93-9.11*0+3.11*1+2.15*1
residual<- 2-a
residual
## [1] -22.19
##d) 
Rsq<- 1-240.57/264.17
Rsq
## [1] 0.08933641
Rsq_adj<- 1-(240.57/264.17)*(145/(146-3-1))
Rsq_adj
## [1] 0.07009704
#8.8 Absenteeism part 2

##Adjusted Rsquare improves when learner status is removed, so this variable should be removed first.

#8.16 Challenger Disaster

##a) Lower pemperatures seem to lead to more damages.
##b) Increase in temperature decreases the probability of O Ring failure. P value is close to 0 so the relationship is significant.
##c) ln(p/(1-p)) = 11.663 - 0.2162*Temperature
##d) Yes it is justified as the results are statistically significant.

#8.18 Challenger Disaster part 2

##a)
Phat51 = exp(11.663-0.2162*51)/(1+exp(11.663-0.2162*51))
Phat51
## [1] 0.6540297
Phat53 = exp(11.663-0.2162*53)/(1+exp(11.663-0.2162*53))
Phat53
## [1] 0.5509228
Phat55 = exp(11.663-0.2162*55)/(1+exp(11.663-0.2162*55))
Phat55
## [1] 0.4432456
##b)
data<- data.frame(shuttle=seq(1:23),
                  temperature=c(53,57,58,63,66,67,67,67,68,69,70,70,70,70,72,73,75,75,76,76,78,79,81),
                  damaged=c(5,1,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0),
                  undamaged=c(c(1,5,5,5,6,6,6,6,6,6,5,6,5,6,6,6,6,5,6,6,6,6,6)))
data$rate <- data$damaged / (data$damaged + data$undamaged)
ggplot(data,aes(x=temperature,y=damaged)) + geom_point() +  stat_smooth(method = 'glm', family ='binomial')
## Warning: Ignoring unknown parameters: family

##c) The model assumes linearity of independent variables and log odds.