Part 1: Linear Regression

Understanding regression

getwd()
## [1] "C:/Users/Dell/Downloads"
## Example: Space Shuttle Launch Data
launch <- read.csv("challenger2.csv")
# estimate beta manually
b <- cov(launch$temperature, launch$distress_ct) / var(launch$temperature)
b
## [1] -0.03364796
#A one-unit increase in temperature is associated with an average decrease of 0.03364796 units in the distress count.
# estimate alpha manually
a <- mean(launch$distress_ct) - b * mean(launch$temperature)
a
## [1] 2.814585
#Baseline Distress: This value suggests a baseline distress count of 2.814585, even when the temperature is zero. This means that even in ideal conditions (no temperature stress), some level of distress is expected
# 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 correlation is less significant than in challenger.csv (51%). Moderate correlation. Meaning: the variable that we chose to explain distress events (temperature),explains distress in 33.59%. 
cor(launch$temperature, launch$distress_ct)
## [1] -0.3359996
# computing the slope using correlation
r * (sd(launch$distress_ct) / sd(launch$temperature))
## [1] -0.03364796
# 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
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
# Indicates that only about 11% of the variability in distress count is explained by temperature. Other factors likely play a role, compared to first database where R-squared explained 26% of the variability in distress count.

# p-value of 0.0747 There might be a weak negative relationship between temperature and distress count. However, the evidence is not strong enough to conclude this definitively.
# 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)
}
# 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 ...
# test regression model with simple linear regression
reg(y = launch$distress_ct, x = launch[2])
##                estimate
## Intercept    2.81458456
## temperature -0.03364796
# 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
# 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