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

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

  1. a
# Memuat dataset mtcars
data(mtcars)

# Menghitung statistik deskriptif untuk mpg
mean_mpg <- mean(mtcars$mpg)          # Rata-rata
median_mpg <- median(mtcars$mpg)      # Median
sd_mpg <- sd(mtcars$mpg)              # Standar deviasi

# Menampilkan hasil
cat("Mean mpg:", mean_mpg, "\n")
## Mean mpg: 20.09062
cat("Median mpg:", median_mpg, "\n")
## Median mpg: 19.2
cat("Standard Deviation mpg:", sd_mpg, "\n")
## Standard Deviation mpg: 6.026948
  1. b
# Memuat library ggplot2
library(ggplot2)

# Membuat boxplot dengan ggplot2
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
  geom_boxplot() +
  labs(title = "Boxplot of MPG by Cylinder",
       x = "Number of Cylinders",
       y = "Miles per Gallon (MPG)") +
  theme_minimal() +
  scale_fill_brewer(palette = "Pastel1")

2.

# Histogram dan garis densitas untuk hp
hist(mtcars$hp, breaks = 10, probability = TRUE, 
     main = "Histogram of Horsepower (hp)", 
     xlab = "Horsepower (hp)", 
     col = "pink", border = "black")
lines(density(mtcars$hp), col = "darkblue", lwd = 2)

Data (hp) dalam dataset mtcars memiliki distribusi yang skewed ke kanan, dengan konsentrasi nilai pada rentang 100–150 horsepower. Nilai horsepower yang sangat tinggi (> 300) jarang terjadi dan bisa memengaruhi perhitungan statistik seperti rata-rata.

# Uji ANOVA
data(iris)
anova_model <- aov(Sepal.Length ~ Species, data = iris)
summary(anova_model)
##              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 dua sampel
setosa <- subset(iris, Species == "setosa")$Petal.Length
versicolor <- subset(iris, Species == "versicolor")$Petal.Length
t_test <- t.test(setosa, versicolor, var.equal = TRUE)
print(t_test)
## 
##  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
  1. a
# Model regresi
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
  1. b
# Scatter plot dengan garis regresi
plot(mtcars$wt, mtcars$mpg, 
     main = "Scatter Plot of MPG vs WT", 
     xlab = "Weight (WT)", 
     ylab = "Miles per Gallon (MPG)", 
     pch = 16, col = "blue")
abline(model, col = "pink", lwd = 2)

5. c. interpretasi hasil Koefisien Regresi: - Intercept (beta 0) = Nilai rata-rata (mpg) saat (wt) = 0. - Slope (beta 1): Penurunan rata-rata (mpg) untuk setiap kenaikan 1 unit (wt).

Nilai R*2: - Mengukur seberapa besar variasi dalam (mpg) yang dijelaskan oleh (wt). Nilai antara 0 dan 1, semakin besar semakin baik.

Jika ada p-value untuk koefisien <0.05, maka (wt) secara signifikan memengaruhi (mpg).