R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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.

# No 1 a Mengimpor dataset mtcars
data(mtcars)

# Menghitung statistik deskriptif untuk variabel mpg
mean_mpg <- mean(mtcars$mpg)
median_mpg <- median(mtcars$mpg)
sd_mpg <- sd(mtcars$mpg)

# Menampilkan hasil
mean_mpg
## [1] 20.09062
median_mpg
## [1] 19.2
sd_mpg
## [1] 6.026948
# No 1 b Menampilkan hasil
cat("Mean MPG:", mean_mpg, "\n")
## Mean MPG: 20.09062
cat("Median MPG:", median_mpg, "\n")
## Median MPG: 19.2
cat("Standar Deviasi MPG:", sd_mpg, "\n")
## Standar Deviasi MPG: 6.026948
# No 2 Membuat boxplot mpg berdasarkan jumlah silinder (cyl)
boxplot(mpg ~ cyl, data = mtcars,
        main = "Boxplot MPG Berdasarkan Jumlah Silinder",
        xlab = "Jumlah Silinder",
        ylab = "Miles Per Gallon (MPG)",
        col = c("PINK", "LIGHTBLUE", "PURPLE"))

# No 3 Membuat histogram untuk variabel hp
hist(mtcars$hp, breaks = 10, probability = TRUE,
     main = "Histogram dan Garis Densitas Horsepower",
     xlab = "Horsepower (hp)", col = "PINK", border = "black")

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

# No 3 Melakukan uji ANOVA
anova_result <- aov(Petal.Length ~ Species, data = iris)

# Menampilkan hasil uji ANOVA
summary(anova_result)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Species       2  437.1  218.55    1180 <2e-16 ***
## Residuals   147   27.2    0.19                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# No 4 Memfilter data untuk spesies setosa dan versicolor
setosa <- subset(iris, Species == "setosa")
versicolor <- subset(iris, Species == "versicolor")

# Melakukan uji t-test
t_test_result <- t.test(setosa$Sepal.Length, versicolor$Sepal.Length)

# Menampilkan hasil uji t-test
t_test_result
## 
##  Welch Two Sample t-test
## 
## data:  setosa$Sepal.Length and versicolor$Sepal.Length
## t = -10.521, df = 86.538, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.1057074 -0.7542926
## sample estimates:
## mean of x mean of y 
##     5.006     5.936
# No 5 a Model regresi linier sederhana
reg_model <- lm(mpg ~ wt, data = mtcars)

# Menampilkan ringkasan model
summary(reg_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
# No 5 b Scatter plot
plot(mtcars$wt, mtcars$mpg,
     main = "Relasi Antara Berat Mobil (wt) dan Efisiensi Bahan Bakar (mpg)",
     xlab = "Berat Mobil (wt)", ylab = "Miles Per Gallon (mpg)",
     col = "pink", pch = 16)

# Menambahkan garis regresi ke plot
abline(reg_model, col = "blue", lwd = 2)

Interpretasi Hasil

Intercept: Prediksi nilai mpg saat berat mobil (wt) sama dengan 0.

Koefisien Slope (wt): Nilai negatif berarti peningkatan berat mobil menyebabkan penurunan efisiensi bahan bakar.Slope = -4, setiap kenaikan 1 unit berat mobil menyebabkan penurunan efisiensi bahan bakar sebesar 4 mpg.

R2 =0.75, berarti 75% variabilitas mpg dijelaskan oleh model, sedangkan 25% lainnya disebabkan oleh variabel lain.

p-value untuk variabel wt lebih kecil dari 0.05, hubungan antara berat mobil dan efisiensi bahan bakar signifikan secara statistik.