summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
data(mtcars)
mean(mtcars$mpg)
## [1] 20.09062
median(mtcars$mpg)
## [1] 19.2
sd(mtcars$mpg)
## [1] 6.026948
# Memuat library ggplot2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
# Membuat boxplot mpg berdasarkan cyl
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
  geom_boxplot(fill = "lightblue", color = "black") +
  labs(
    title = "Boxplot MPG berdasarkan Cylinders",
    x = "Jumlah Silinder (cyl)",
    y = "Miles Per Gallon (mpg)"
  ) +
  theme_minimal()

# Membuat histogram dengan garis densitas
library(ggplot2)
ggplot(mtcars, aes(x = hp)) +
  geom_histogram(aes(y = ..density..), bins = 10, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) +
  labs(
    title = "Histogram dan Densitas Horsepower (hp)",
    x = "Horsepower (hp)",
    y = "Density"
  ) +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## 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.

# Memuat dataset bawaan iris
data(iris)

# Uji ANOVA
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
# Filter data untuk spesies setosa dan versicolor
setosa <- iris$Petal.Length[iris$Species == "setosa"]
versicolor <- iris$Petal.Length[iris$Species == "versicolor"]

# Uji t-test
t_test_result <- t.test(setosa, versicolor, var.equal = TRUE)
t_test_result
## 
##  Two Sample t-test
## 
## data:  setosa and versicolor
## t = -39.493, df = 98, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.938597 -2.657403
## sample estimates:
## mean of x mean of y 
##     1.462     4.260
# Membuat 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", se = FALSE) +
  labs(
    title = "Scatter Plot MPG vs WT dengan Garis Regresi",
    x = "Berat Mobil (wt)",
    y = "Miles Per Gallon (mpg)"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'