getwd()
[1] "/cloud/project"
launch<-read.csv("challenger2.csv")
View(launch)
#Estimate beta manually
b<-cov(launch$temperature,launch$distress_ct)/var(launch$temperature)
b
[1] -0.03364796
#Estimate alpha manually
a<-mean(launch$distress_ct)-b*mean((launch$temperature))
a
[1] 2.814585
#Let us get the correlation between temperature and distress
r<- cor(launch$temperature,launch$distress_ct)
r
[1] -0.3359996
#Computing the slope using correlation
r*(sd(launch$distress_ct)/sd(launch$temperature))
[1] -0.03364796
model<-lm(distress_ct~temperature,data = launch)
model

Call:
lm(formula = distress_ct ~ temperature, data = launch)

Coefficients:
(Intercept)  temperature  
    2.81458     -0.03365  

The values obtained via the LR model are very similar to the ones that we got manually.

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
#Creating a SLR 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)
}
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 ...
reg(y=launch$distress_ct,x=launch[2])
               estimate
intercept    2.81458456
temperature -0.03364796
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
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            flight_num  
           2.240e+00            -3.124e-02            -2.587e-05             2.762e-02  
summary(model)

Call:
lm(formula = distress_ct ~ temperature + field_check_pressure + 
    flight_num, data = launch)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.2744 -0.3335 -0.1657  0.2975  1.5284 

Coefficients:
                       Estimate Std. Error t value Pr(>|t|)  
(Intercept)           2.240e+00  1.267e+00   1.767   0.0894 .
temperature          -3.124e-02  1.787e-02  -1.748   0.0927 .
field_check_pressure -2.587e-05  2.383e-03  -0.011   0.9914  
flight_num            2.762e-02  1.798e-02   1.537   0.1369  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.6926 on 25 degrees of freedom
Multiple R-squared:  0.2132,    Adjusted R-squared:  0.1188 
F-statistic: 2.259 on 3 and 25 DF,  p-value: 0.1063
LS0tCnRpdGxlOiAiSW50cm8gdG8gTFIiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCgoKYGBge3J9CmdldHdkKCkKYGBgCgoKCmBgYHtyfQpsYXVuY2g8LXJlYWQuY3N2KCJjaGFsbGVuZ2VyMi5jc3YiKQpgYGAKCmBgYHtyfQojVmlldyhsYXVuY2gpCmBgYAoKCgpgYGB7cn0KI0VzdGltYXRlIGJldGEgbWFudWFsbHkKYjwtY292KGxhdW5jaCR0ZW1wZXJhdHVyZSxsYXVuY2gkZGlzdHJlc3NfY3QpL3ZhcihsYXVuY2gkdGVtcGVyYXR1cmUpCmIKYGBgCgoKCgpgYGB7cn0KI0VzdGltYXRlIGFscGhhIG1hbnVhbGx5CmE8LW1lYW4obGF1bmNoJGR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