# Membuat data
df <- read.csv(file.choose())

# Membuat data frame 
data <- df[,c("weight","mpg")] 
 
# Menampilkan data 
tail(data)
##     weight mpg
## 393   2950  27
## 394   2790  27
## 395   2130  44
## 396   2295  32
## 397   2625  28
## 398   2720  31
# Statistik deskriptif sederhana 
summary(data) 
##      weight          mpg       
##  Min.   :1613   Min.   : 9.00  
##  1st Qu.:2224   1st Qu.:17.50  
##  Median :2804   Median :23.00  
##  Mean   :2970   Mean   :23.51  
##  3rd Qu.:3608   3rd Qu.:29.00  
##  Max.   :5140   Max.   :46.60
# Standar deviasi 
sd(data$weight) 
## [1] 846.8418
## [1] 1.932184 
sd(data$mpg) 
## [1] 7.815984
## [1] 9.554522 
# Uji korelasi Pearson 
hasil_korelasi <- cor.test(data$weight, data$mpg, method = "pearson") 
 
# Menampilkan hasil 
print(hasil_korelasi)
## 
##  Pearson's product-moment correlation
## 
## data:  data$weight and data$mpg
## t = -29.814, df = 396, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.8597473 -0.7987473
## sample estimates:
##        cor 
## -0.8317409
# Membuat scatter plot 
plot(data$weight, data$mpg, 
     main = "Scatter Plot weight vs mpg", 
     xlab = "weight", 
     ylab = "mpg", 
     pch = 19, 
     col = "blue") 
 
# Menambahkan garis regresi 
abline(lm(data$weight ~ data$mpg), col = "red", lwd = 2)

library(ggplot2) 
## Warning: package 'ggplot2' was built under R version 4.5.2
ggplot(data, aes(x = weight, y = mpg)) + 
  geom_point(size = 3) + 
  geom_smooth(method = "lm", se = TRUE) + 
  labs(title = "Hubungan berat mobil dan efesiensi bahan bakar", 
    x = "weight", 
    y = "mpg") + 
  theme_minimal() 
## `geom_smooth()` using formula = 'y ~ x'