Forum

Linear and Logistic...
 
Notifications
Clear all

Linear and Logistic Regression in R with Real-Life Examples

1 Posts
1 Users
0 Reactions
125 Views
(@rahima-noor)
Member Moderator
Joined: 11 months ago
Posts: 85
Topic starter  

🔍 Introduction: Why Regression Matters in Research

Regression models help us understand relationships between variables and make predictions. In medical, social, or business research, they are fundamental tools for data analysis. In this forum, we’ll explore Linear and Logistic Regression using R, with easy-to-understand real-life examples.

📈 What is Linear Regression?

Linear regression predicts a continuous outcome based on one or more predictor variables. It assumes a straight-line relationship.
🧪 Example: Predicting systolic blood pressure based on age and BMI.

r
model <- lm(BP ~ age + BMI, data = patient_data)
summary(model)

🧪 Real-Life Use Case: Predicting Exam Scores

Suppose you're analyzing whether study hours and sleep duration impact students' exam scores.

r
model <- lm(score ~ study_hours + sleep_hours, data = students)
plot(students$study_hours, students$score)
abline(model, col="blue")

This helps identify how much scores improve with each additional hour of study.

⚠️ Assumptions of Linear Regression

Before trusting results, check:

  • Linearity between variables

  • Normal distribution of residuals

  • Homoscedasticity (equal variance)
    Use plot(model) and shapiro.test() to verify these.

🚦 What is Logistic Regression?

Unlike linear regression, logistic regression is used when the outcome is binary (e.g., yes/no, disease/no disease). It estimates the probability of an event occurring.
🧪 Example: Predicting whether a patient has diabetes (yes/no) based on BMI and age.

r
model <- glm(diabetes ~ age + BMI, data = patient_data, family = binomial)
summary(model)

🏥 Real-Life Use Case: Heart Disease Risk

You work with a hospital dataset and want to predict if a patient has heart disease based on cholesterol and smoking status.

r
model <- glm(heart_disease ~ cholesterol + smoker, data = hospital_data, family = "binomial")
predict(model, type = "response")

Interpret the odds ratios using exp(coef(model)).

🛠 Visualizing Logistic Regression Results

Use ggplot2 and ggpubr to visualize probabilities and fitted curves:

r
library(ggplot2)
ggplot(patient_data, aes(x = BMI, y = diabetes)) +
geom_point() +
stat_smooth(method = "glm", method.args = list(family = "binomial"))

🤔 When to Use Linear vs Logistic Regression?

  • Use linear regression for continuous outcomes like weight, score, or cost.

  • Use logistic regression for categorical outcomes like disease status or approval (yes/no).
    Both help you model, predict, and understand data relationships clearly.

🧠 Bonus Tip: Use tidymodels for Clean Code

The tidymodels framework in R allows for structured and modern modeling workflows:

r
library(tidymodels)
model <- logistic_reg() %>%
set_engine("glm") %>%
fit(diabetes ~ age + BMI, data = patient_data)


   
Quote
Share:
error: Content is protected !!