title: “2304220028_UAS_Komstat” output: html_document date: “2024-12-17”

#Import dataset airquality
data("airquality")

# (a) Menghitung statistik deskriptif untuk variabel Ozone
mean_ozone <- mean(airquality$Ozone, na.rm = TRUE)   # Rata-rata
median_ozone <- median(airquality$Ozone, na.rm = TRUE) # Median
sd_ozone <- sd(airquality$Ozone, na.rm = TRUE)         # Standar deviasi

# Menampilkan hasil
cat("Statistik Deskriptif Ozone:\n")
## Statistik Deskriptif Ozone:
cat("Mean:", mean_ozone, "\n")
## Mean: 42.12931
cat("Median:", median_ozone, "\n")
## Median: 31.5
cat("Standar Deviasi:", sd_ozone, "\n\n")
## Standar Deviasi: 32.98788
# (b) Membuat scatter plot antara Wind dan Temp
plot(airquality$Wind, airquality$Temp,
     main = "Scatter Plot antara Wind dan Temp",
     xlab = "Wind (Kecepatan Angin)",
     ylab = "Temp (Temperatur)",
     col = "blue", pch = 19)

#Import dataset mtcars
data("mtcars")

# Menghitung jumlah setiap kategori pada variabel cyl
cyl_count <- table(mtcars$cyl)

# Membuat bar chart
barplot(cyl_count,
        main = "Bar Chart Jumlah Mobil Berdasarkan Cylinders",
        xlab = "Jumlah Silinder (cyl)",
        ylab = "Jumlah Mobil",
        col = "lightblue")

# Menambahkan label jumlah pada setiap bar
text(x = 1:length(cyl_count), 
     y = cyl_count, 
     label = cyl_count, 
     pos = 3, cex = 1, col = "red")

# Load dataset iris
data("iris")

# (a) Membuat boxplot Petal.Width berdasarkan Species
boxplot(Petal.Width ~ Species, data = iris,
        main = "Boxplot Petal.Width Berdasarkan Species",
        xlab = "Species", 
        ylab = "Petal Width", 
        col = c("lightblue", "lightgreen", "pink"))

# (b) Menghitung korelasi antara Sepal.Length dan Petal.Length
correlation <- cor(iris$Sepal.Length, iris$Petal.Length)
cat("Korelasi antara Sepal.Length dan Petal.Length:", correlation, "\n\n")
## Korelasi antara Sepal.Length dan Petal.Length: 0.8717538
# (c) Membuat scatter plot dengan garis regresi untuk setiap Species
library(ggplot2)

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

# Uji Chi-Square untuk variabel vs dan am
chi_result <- chisq.test(table(mtcars$vs, mtcars$am))

# Menampilkan hasil uji
print(chi_result)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table(mtcars$vs, mtcars$am)
## X-squared = 0.34754, df = 1, p-value = 0.5555
# Membuat model regresi linear
model <- lm(Temp ~ Solar.R, data = airquality)

# (a) Menampilkan ringkasan model
summary(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
# (b) Scatter plot dengan garis regresi
plot(airquality$Solar.R, airquality$Temp,
     main = "Regresi Linear: Temp vs Solar.R",
     xlab = "Solar.R",
     ylab = "Temp",
     col = "blue", pch = 19)

abline(model, col = "red", lwd = 2)

# (c) Interpretasi koefisien regresi
cat("Koefisien Regresi:\n")
## Koefisien Regresi:
print(coef(model))
## (Intercept)     Solar.R 
## 72.86301176  0.02825463