Loading Exam data set

library(tidyverse
        )
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data1 <- read_csv('/Users/souleymanedoumbia/Library/Mobile Documents/com~apple~CloudDocs/CUNY SPS CLASSES/MSDS CLASSES/DATA 605 Spring2024 /Week 11/Discussion board/Exam_data.csv')
## Rows: 100 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (2): Hours_Studied, Exam_Score
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary(data1)
##  Hours_Studied     Exam_Score   
##  Min.   :1.050   Min.   :12.44  
##  1st Qu.:2.739   1st Qu.:24.40  
##  Median :5.177   Median :34.14  
##  Mean   :5.232   Mean   :36.15  
##  3rd Qu.:7.572   3rd Qu.:48.02  
##  Max.   :9.882   Max.   :63.32
head(data1)
## # A tibble: 6 × 2
##   Hours_Studied Exam_Score
##           <dbl>      <dbl>
## 1          4.37       32.3
## 2          9.56       56.3
## 3          7.59       48.4
## 4          6.39       32.0
## 5          2.40       20.9
## 6          2.40       23.8

Building a Linear Model

#the linear model
model1 <- lm(Exam_Score ~ Hours_Studied, data=data1)

#Summarizing the model to get the coefficients and other statistics
summary(model1)
## 
## Call:
## lm(formula = Exam_Score ~ Hours_Studied, data = data1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.6371 -3.5937  0.2384  2.3378 11.4653 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    11.3309     0.9997   11.34   <2e-16 ***
## Hours_Studied   4.7446     0.1703   27.86   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.536 on 98 degrees of freedom
## Multiple R-squared:  0.8879, Adjusted R-squared:  0.8868 
## F-statistic: 776.4 on 1 and 98 DF,  p-value: < 2.2e-16

Quality evaluation of the model, and Analyzing Residual

library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
# Checking for linearity and homoscedasticity visually
plot(model1, which=1) # Residuals vs Fitted

plot(model1, which=3) # Scale-Location (also for checking homoscedasticity)

# Residual Analysis
par(mfrow=c(2,2)) 
plot(model1) 

# Normality of residuals
hist(residuals(model1), breaks=10, main="Histogram of Residuals", xlab="Residuals") # Histogram of residuals
shapiro.test(residuals(model1)) # Shapiro-Wilk test for normality
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(model1)
## W = 0.98463, p-value = 0.2984
# Independence of residuals
durbinWatsonTest(model1) 
##  lag Autocorrelation D-W Statistic p-value
##    1       -0.145225      2.284998   0.158
##  Alternative hypothesis: rho != 0

Conclusion:

It seems like the linear model is quite appropriate given the high R-squared and the significant predictor variable.