library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ 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
library(ggthemes)
library(ggrepel)
library(effsize)
library(pwrss)
##
## Attaching package: 'pwrss'
##
## The following object is masked from 'package:stats':
##
## power.t.test
library(boot)
library(broom)
library(lindia)
library(dplyr)
library(ggplot2)
library(dplyr)
library(glmnet)
## Warning: package 'glmnet' was built under R version 4.3.2
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
## Loaded glmnet 4.1-8
library(caret)
## Warning: package 'caret' was built under R version 4.3.2
## Loading required package: lattice
##
## Attaching package: 'lattice'
##
## The following object is masked from 'package:boot':
##
## melanoma
##
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
mpg<- read_delim("C:/Users/Jamil Shaikh/Desktop/heart1.csv", delim = ",",show_col_types = FALSE)
glimpse(mpg)
## Rows: 303
## Columns: 13
## $ age <dbl> 63, 37, 41, 56, 57, 57, 56, 44, 52, 57, 54, 48, 49, 64, 58, 5…
## $ sex <dbl> 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1…
## $ cp <dbl> 3, 2, 1, 1, 0, 0, 1, 1, 2, 2, 0, 2, 1, 3, 3, 2, 2, 3, 0, 3, 0…
## $ trestbps <dbl> 145, 130, 130, 120, 120, 140, 140, 120, 172, 150, 140, 130, 1…
## $ chol <dbl> 233, 250, 204, 236, 354, 192, 294, 263, 199, 168, 239, 275, 2…
## $ fbs <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0…
## $ restecg <dbl> 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1…
## $ thalach <dbl> 150, 187, 172, 178, 163, 148, 153, 173, 162, 174, 160, 139, 1…
## $ exang <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0…
## $ oldpeak <dbl> 2.3, 3.5, 1.4, 0.8, 0.6, 0.4, 1.3, 0.0, 0.5, 1.6, 1.2, 0.2, 0…
## $ slope <dbl> 0, 0, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 0, 2, 2, 1…
## $ ca <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0…
## $ target <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
Tasks : Build a linear (or generalized linear) model as you like Use whatever response variable and explanatory variables you prefer Use the tools from previous weeks to diagnose the model Highlight any issues with the model Interpret at least one of the coefficients
model <- lm(`age` ~ `chol` + `thalach`, data = mpg)
summary(model)
##
## Call:
## lm(formula = age ~ chol + thalach, data = mpg)
##
## Residuals:
## Min 1Q Median 3Q Max
## -20.6809 -6.2925 0.1868 6.2999 22.4536
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 68.838893 3.828730 17.980 < 2e-16 ***
## chol 0.036751 0.009033 4.068 6.06e-05 ***
## thalach -0.157191 0.020441 -7.690 2.12e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.136 on 300 degrees of freedom
## Multiple R-squared: 0.2028, Adjusted R-squared: 0.1975
## F-statistic: 38.16 on 2 and 300 DF, p-value: 1.717e-15
Here, we’re predicting “Admission grade” using three explanatory variables: “Age at enrollment,” “Tuition fees up to date,” and “GDP.”
selected_vars <- c("target", "age", "oldpeak", "cp", "sex")
data_subset <- mpg %>%
select(all_of(selected_vars))
set.seed(123)
train_index <- createDataPartition(data_subset$target, p = 0.7, list = FALSE)
train_data <- data_subset[train_index, ]
test_data <- data_subset[-train_index, ]
glm_model <- glm(as.factor(target) ~ ., data = train_data, family = binomial)
summary(glm_model)
##
## Call:
## glm(formula = as.factor(target) ~ ., family = binomial, data = train_data)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 4.20105 1.29259 3.250 0.00115 **
## age -0.05269 0.02159 -2.441 0.01467 *
## oldpeak -1.00542 0.18861 -5.331 9.78e-08 ***
## cp 0.92521 0.17737 5.216 1.83e-07 ***
## sex -1.32300 0.41551 -3.184 0.00145 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 292.79 on 212 degrees of freedom
## Residual deviance: 195.68 on 208 degrees of freedom
## AIC: 205.68
##
## Number of Fisher Scoring iterations: 5
predictions <- predict(glm_model, newdata = test_data, type = "response")
predicted_classes <- ifelse(predictions > 0.5, "trestbps", "chol")
confusion_matrix <- table(Predicted = predicted_classes, Actual = test_data$target)
accuracy <- (confusion_matrix[1, 1] + confusion_matrix[2, 2]) / sum(confusion_matrix)
precision <- confusion_matrix[2, 2] / sum(confusion_matrix[, 2])
recall <- confusion_matrix[2, 2] / sum(confusion_matrix[2, ])
f1_score <- 2 * (precision * recall) / (precision + recall)
confusion_matrix
## Actual
## Predicted 0 1
## chol 30 5
## trestbps 13 42
cat("Accuracy:", accuracy, "\n")
## Accuracy: 0.8
cat("Precision:", precision, "\n")
## Precision: 0.893617
cat("Recall:", recall, "\n")
## Recall: 0.7636364
cat("F1 Score:", f1_score, "\n")
## F1 Score: 0.8235294
coef(glm_model)
## (Intercept) age oldpeak cp sex
## 4.20104819 -0.05269378 -1.00542077 0.92521175 -1.32300314
slope <- coef(glm_model)["slope"]
exp(slope)
## <NA>
## NA