1. Analisis Dataset MTCARS
# a. Statistik Deskriptif Variabel MPG
summary_mpg <- summary(mtcars$mpg)
cat("Statistik Deskriptif MPG:\n")
## Statistik Deskriptif MPG:
print(summary_mpg)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.40 15.43 19.20 20.09 22.80 33.90
sd_mpg <- sd(mtcars$mpg)
cat("\nStandar Deviasi MPG:", sd_mpg)
##
## Standar Deviasi MPG: 6.026948
# b. Boxplot MPG berdasarkan Jumlah Silinder
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
geom_boxplot() +
labs(title = "Distribusi MPG berdasarkan Jumlah Silinder",
x = "Jumlah Silinder",
y = "Miles per Gallon (MPG)") +
theme_minimal()

2. Histogram Horsepower dengan Densitas
ggplot(mtcars, aes(x = hp)) +
geom_histogram(aes(y = ..density..),
bins = 15,
fill = "blue",
alpha = 0.7) +
geom_density(color = "red") +
labs(title = "Distribusi Horsepower",
x = "Horsepower",
y = "Densitas") +
theme_minimal()
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

3. ANOVA pada Dataset Iris
# Melakukan ANOVA untuk Sepal Length antar spesies
anova_result <- aov(Sepal.Length ~ Species, data = iris)
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
4. T-Test Panjang Petal Iris
# Uji t-test untuk Petal Length antara setosa dan versicolor
t_test_result <- t.test(Petal.Length ~ Species,
data = subset(iris, Species %in% c("setosa", "versicolor")))
print(t_test_result)
##
## Welch Two Sample t-test
##
## data: Petal.Length by Species
## t = -39.493, df = 62.14, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group setosa and group versicolor is not equal to 0
## 95 percent confidence interval:
## -2.939618 -2.656382
## sample estimates:
## mean in group setosa mean in group versicolor
## 1.462 4.260
5. Regresi Linear Sederhana MTCARS
# Model Regresi MPG berdasarkan Berat (WT)
model <- lm(mpg ~ wt, data = mtcars)
# a. 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
# b. Scatter Plot dengan Garis Regresi
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue") +
geom_smooth(method = "lm", color = "red") +
labs(title = "Regresi Linear: MPG vs Berat",
x = "Berat (1000 lbs)",
y = "Miles per Gallon") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# Mengambil koefisien regresi dari model
coefficients <- summary(model)$coefficients
cat("Koefisien Regresi:\n")
## Koefisien Regresi:
print(coefficients)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.285126 1.877627 19.857575 8.241799e-19
## wt -5.344472 0.559101 -9.559044 1.293959e-10
# Mengambil nilai R-squared
r_squared <- summary(model)$r.squared
cat("\nNilai R-squared:", r_squared)
##
## Nilai R-squared: 0.7528328