# Memuat library yang diperlukan
library(tidyverse)
library(ggplot2)
library(dplyr)
library(datasets)
# Memuat dataset
data(mtcars)
data(iris)
# Menghitung statistik deskriptif untuk variabel mpg
mpg_stats <- summary(mtcars$mpg)
print("Statistik Deskriptif MPG:")
## [1] "Statistik Deskriptif MPG:"
print(mpg_stats)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.40 15.43 19.20 20.09 22.80 33.90
# Standar deviasi
mpg_sd <- sd(mtcars$mpg)
print(paste("Standar Deviasi MPG:", round(mpg_sd, 2)))
## [1] "Standar Deviasi MPG: 6.03"
# Membuat boxplot
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()
# Histogram dengan garis densitas
ggplot(mtcars, aes(x = hp)) +
geom_histogram(aes(y = ..density..), bins = 15, fill = "skyblue", color = "black") +
geom_density(color = "red", size = 1) +
labs(title = "Distribusi Horsepower",
x = "Horsepower",
y = "Densitas") +
theme_minimal()
### Interpretasi Distribusi - Distribusi horsepower tampak right-skewed
- Mayoritas mobil memiliki horsepower antara 100-200 - Beberapa mobil
memiliki horsepower tinggi di atas 300
# Uji ANOVA untuk panjang sepal 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
# 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
# Model regresi linear
model <- lm(mpg ~ wt, data = mtcars)
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 dengan garis regresi
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue") +
geom_smooth(method = "lm", color = "red") +
labs(title = "Hubungan Berat Mobil dengan Miles per Gallon",
x = "Berat (1000 lbs)",
y = "Miles per Gallon") +
theme_minimal()