#Launch Data 2

## Example: Space Shuttle Launch Data
launch <- read.csv("challenger2.csv")

#Beta Calculation #These #’s are relaitvely similar #In Challenger 1 - 0.047

# estimate beta manually
b <- cov(launch$temperature, launch$distress_ct) / var(launch$temperature)
b
## [1] -0.03364796

#Alpha Calculation - These are important for understanding the Basics behind the DATA #Both Numbers are Significant when correlating the data #this is likely occuring becasue the data sets are so similar, I anticipate the trend will continue while only using 2 variables, when we add more I predict the number difference’s will increase #In Challenger 1 - 3.698

# estimate alpha manually
a <- mean(launch$distress_ct) - b * mean(launch$temperature)
a
## [1] 2.814585

#Correlation between temp and the likelihood of an incident #this correlation is not significant because it is less than (-2,-,2) #Compared to the first data set - this number is somehow the same exact number #I am unsure how this is possible, when looking into the correlation #I would assume they would all be different #Ask why this is

# calculate the correlation of launch data
r <- cov(launch$temperature, launch$distress_ct) /
       (sd(launch$temperature) * sd(launch$distress_ct))
r
## [1] -0.3359996

#This data however is different. In Challenger one these two chunks displayed the same Value #That value being the same -0.511 #However in this instance the number increased to -0.335 #It is important to realize this is increased when it gets closer to 0 being a negative number #this means that the temperature does not affect the distress level as much as it did in the initial data

cor(launch$temperature, launch$distress_ct)
## [1] -0.3359996

#this means that everytime there is a degree change the distress level either goes up or down. #in this instance if the temp increases the distress level decreases #if the temp decreases than the distress level % increases #this number is significant similarly to the Challenger 1 data #this is because it is between the range of [-0.05 ~ 0.05]

# computing the slope using correlation
r * (sd(launch$distress_ct) / sd(launch$temperature))
## [1] -0.03364796

#This shows the linear regression coeffinicents #Essientially plotting out the line in simplistic #’s #would be easier to just make this a graph to show the visual… #Once Again the numbers remain similar, both displaying the same significant values

# confirming the regression line using the lm function (not in text)
model <- lm(distress_ct ~ temperature, data = launch)
model
## 
## Call:
## lm(formula = distress_ct ~ temperature, data = launch)
## 
## Coefficients:
## (Intercept)  temperature  
##     2.81458     -0.03365

#This is where we see the first difference in significant Values #The P value, is now not significant where it was in the initial data #Additionally, the R Sq numbers, are much lower, and closer to 0 in this dataset #this would represent an obnormaality, #traditionally the more data points given the higher the number would be #however, this is likely because we just plugged random #’s in to see the true difference

summary(model)
## 
## Call:
## lm(formula = distress_ct ~ temperature, data = launch)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.0649 -0.4929 -0.2573  0.3052  1.7090 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  2.81458    1.24629   2.258   0.0322 *
## temperature -0.03365    0.01815  -1.854   0.0747 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7076 on 27 degrees of freedom
## Multiple R-squared:  0.1129, Adjusted R-squared:  0.08004 
## F-statistic: 3.436 on 1 and 27 DF,  p-value: 0.07474

#Cbind

# creating a simple multiple regression function
reg <- function(y, x) {
  x <- as.matrix(x)
  x <- cbind(Intercept = 1, x)
  b <- solve(t(x) %*% x) %*% t(x) %*% y
  colnames(b) <- "estimate"
  print(b)
}

#Identifying mulitple variables and putting them into the data frame #Examining Launch Data

# examine the launch data
str(launch)
## 'data.frame':    29 obs. of  4 variables:
##  $ distress_ct         : int  0 1 0 0 0 0 0 0 1 1 ...
##  $ temperature         : int  66 70 69 68 67 72 73 70 57 63 ...
##  $ field_check_pressure: int  50 50 50 50 50 50 100 100 200 200 ...
##  $ flight_num          : int  1 2 3 4 5 6 7 8 9 10 ...

temp v Change in danger

#Same Data was spoke about earlier #Intercept data is significant #Temperate is significant #Similar Results to the Challenger 1 Flight Data

# test regression model with simple linear regression
reg(y = launch$distress_ct, x = launch[2])
##                estimate
## Intercept    2.81458456
## temperature -0.03364796

#All of these numbers changed drastically #in the initial test only the intercept was significant #However, Now all of these data poitns are significant #This means it has more of an effect with the added data

# use regression model with multiple regression
reg(y = launch$distress_ct, x = launch[2:4])
##                           estimate
## Intercept             2.239817e+00
## temperature          -3.124185e-02
## field_check_pressure -2.586765e-05
## flight_num            2.762455e-02

#Looking at the difference in the coefficients with the new data #we are also looking at new data like the Field Check Pressure #Same numbers as up-top previously #Still drastic changes to the overall data

# confirming the multiple regression result using the lm function (not in text)
model <- lm(distress_ct ~ temperature + field_check_pressure + flight_num, data = launch)
model
## 
## Call:
## lm(formula = distress_ct ~ temperature + field_check_pressure + 
##     flight_num, data = launch)
## 
## Coefficients:
##          (Intercept)           temperature  field_check_pressure  
##            2.240e+00            -3.124e-02            -2.587e-05  
##           flight_num  
##            2.762e-02

```