# Load dataset
data(airquality)
# Hitung statistik deskriptif untuk variabel Ozone
mean_ozone <- mean(airquality$Ozone, na.rm = TRUE)
median_ozone <- median(airquality$Ozone, na.rm = TRUE)
sd_ozone <- sd(airquality$Ozone, na.rm = TRUE)
# Tampilkan hasil
mean_ozone
## [1] 42.12931
median_ozone
## [1] 31.5
sd_ozone
## [1] 32.98788
# Scatter plot antara Wind dan Temp
plot(airquality$Wind, airquality$Temp,
main = "Scatter Plot antara Wind dan Temp",
xlab = "Wind", ylab = "Temp",
col = "blue", pch = 19)

# Load dataset
data(mtcars)
# Buat bar chart untuk variabel cyl
barplot(table(mtcars$cyl),
main = "Bar Chart Variabel cyl",
xlab = "Jumlah Silinder (cyl)",
ylab = "Frekuensi",
col = "skyblue")
# Tambahkan label jumlah pada masing-masing kategori
text(x = 1:length(table(mtcars$cyl)),
y = table(mtcars$cyl),
labels = table(mtcars$cyl), pos = 3)

# Load dataset
data(iris)
# Buat boxplot
boxplot(Petal.Width ~ Species, data = iris,
main = "Boxplot Petal.Width Berdasarkan Species",
xlab = "Species", ylab = "Petal.Width",
col = c("red", "green", "blue"))

# Hitung korelasi
correlation <- cor(iris$Sepal.Length, iris$Petal.Length)
correlation
## [1] 0.8717538
# Interpretasi hasil
if (correlation > 0) {
print("Korelasi positif: Sepal.Length dan Petal.Length meningkat bersama.")
} else {
print("Korelasi negatif: Sepal.Length meningkat, tetapi Petal.Length menurun.")
}
## [1] "Korelasi positif: Sepal.Length dan Petal.Length meningkat bersama."
# Buat scatter plot dengan warna berdasarkan Species
plot(iris$Sepal.Length, iris$Sepal.Width,
col = as.factor(iris$Species),
pch = 19,
main = "Scatter Plot Sepal.Length vs Sepal.Width",
xlab = "Sepal.Length", ylab = "Sepal.Width")
# Tambahkan legenda
legend("topright", legend = levels(iris$Species),
col = 1:3, pch = 19)
# Tambahkan garis regresi untuk masing-masing spesies
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
ggtitle("Scatter Plot dengan Garis Regresi per Species")
## `geom_smooth()` using formula = 'y ~ x'

# Membuat tabel kontingensi
contingency_table <- table(mtcars$vs, mtcars$am)
# Lakukan uji Chi-Square
chi_test <- chisq.test(contingency_table)
# Tampilkan hasil
chi_test
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: contingency_table
## X-squared = 0.34754, df = 1, p-value = 0.5555
# Bangun model regresi: Temp ~ Solar.R
linear_model <- lm(Temp ~ Solar.R, data = airquality)
# Tampilkan ringkasan model
summary(linear_model)
##
## Call:
## lm(formula = Temp ~ Solar.R, data = airquality)
##
## Residuals:
## Min 1Q Median 3Q Max
## -22.3787 -4.9572 0.8932 5.9111 18.4013
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 72.863012 1.693951 43.014 < 2e-16 ***
## Solar.R 0.028255 0.008205 3.444 0.000752 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.898 on 144 degrees of freedom
## (7 observations deleted due to missingness)
## Multiple R-squared: 0.07609, Adjusted R-squared: 0.06967
## F-statistic: 11.86 on 1 and 144 DF, p-value: 0.0007518
# Scatter plot
plot(airquality$Solar.R, airquality$Temp,
main = "Scatter Plot Solar.R vs Temp dengan Garis Regresi",
xlab = "Solar.R", ylab = "Temp",
col = "blue", pch = 19)
# Tambahkan garis regresi
abline(linear_model, col = "red", lwd = 2)
