4 Maret 2024
Berikut contoh code regresi linear dengan RStudio menggunakan dataset BMI, Usia, dan Tekanan Darah.
File > New File > R Script
atau tekan tombol CTRL/Cmd + Shift + N
CTRL/Cmd + Shift + Enter
untuk menjalankan seluruh code# Install and load the required libraries
install.packages("ggplot2")
library(ggplot2)
# Load the CSV data from Risetku's Dataset
data_url = 'https://static.risetku.com/blog/dataset_bmi_age_blood_pressure.csv'
data <- read.csv(data_url)
# Create a linear regression model
model <- lm(BloodPressure ~ BMI + Age, data=data)
# Create a scatter plot with the regression line
ggplot(data, aes(x=BMI, y=BloodPressure)) +
geom_point() +
geom_smooth(method="lm", se=FALSE, color="blue") +
labs(x="BMI", y="Blood Pressure") +
ggtitle("Linear Regression: Blood Pressure vs BMI")
Baca juga