## 1. Import Dataset mtcars
#(a) Hitung Statistik Deskriptif (mean, median, dan standar deviasi) untuk variabel mpg

# Import dataset
data(mtcars)

# Hitung statistik deskriptif untuk mpg
mean_mpg <- mean(mtcars$mpg)
median_mpg <- median(mtcars$mpg)
sd_mpg <- sd(mtcars$mpg)
mean_mpg
## [1] 20.09062
median_mpg
## [1] 19.2
sd_mpg
## [1] 6.026948
##(b) Buat Boxplot Variabel mpg Berdasarkan Variabel cyl

# Boxplot mpg berdasarkan cyl
boxplot(mpg ~ cyl, data = mtcars,
        main = "Boxplot MPG Berdasarkan Jumlah Silinder",
        xlab = "Jumlah Silinder (cyl)",
        ylab = "Miles Per Gallon (mpg)",
        col = c("lightblue", "lightgreen", "lightpink"))

##2. Histogram untuk Variabel hp (Horsepower) dengan Garis Densitas
# Histogram untuk hp
hist(mtcars$hp, freq = FALSE, 
     main = "Histogram Horsepower (hp) dengan Garis Densitas",
     xlab = "Horsepower (hp)",
     col = "lightblue", border = "black")

# Tambahkan garis densitas
lines(density(mtcars$hp), col = "red", lwd = 2)

# Import dataset iris
data(iris)

# Melihat struktur data untuk memastikan kolom yang relevan
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
# Uji ANOVA pada Sepal.Length berdasarkan Species
anova_result <- aov(Sepal.Length ~ Species, data = iris)

# Menampilkan hasil ANOVA
summary(anova_result)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Species       2  63.21  31.606   119.3 <2e-16 ***
## Residuals   147  38.96   0.265                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Load dataset iris
data(iris)

# Filter data untuk spesies setosa dan versicolor
setosa <- subset(iris, Species == "setosa")
versicolor <- subset(iris, Species == "versicolor")

# Lakukan uji t-test dua sampel
t_test_result <- t.test(setosa$Petal.Length, versicolor$Petal.Length, 
                        alternative = "two.sided")

# Cetak hasil uji t-test
print(t_test_result)
## 
##  Welch Two Sample t-test
## 
## data:  setosa$Petal.Length and versicolor$Petal.Length
## t = -39.493, df = 62.14, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.939618 -2.656382
## sample estimates:
## mean of x mean of y 
##     1.462     4.260
# Import dataset mtcars
data(mtcars)

# Bangun model regresi linear sederhana
model <- lm(mpg ~ wt, data = mtcars)

# Tampilkan ringkasan model
summary(model)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10
# Scatter plot dan garis regresi
plot(mtcars$wt, mtcars$mpg,
     main = "Scatter Plot mpg vs wt",
     xlab = "Berat Mobil (wt)",
     ylab = "Miles per Gallon (mpg)",
     pch = 19, col = "blue")

# Tambahkan garis regresi ke scatter plot
abline(model, col = "red", lwd = 2)

C. Interpretasi hasil

Intercept: 37.285. Saat wt = 0, prediksi mpg adalah 37.285. Koefisien wt: -5.344. Setiap kenaikan 1 unit wt (berat mobil) mengurangi rata-rata mpg sebesar 5.344. Nilai R²: 0.7528. Sekitar 75.28% variabilitas mpg dapat dijelaskan oleh wt. Signifikansi: Karena nilai p < 0.05, hubungan antara wt dan mpg signifikan secara statistik