airquality
Ozone
# Load dataset airquality
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
cat("Mean Ozone:", mean_ozone, "\n")
## Mean Ozone: 42.12931
cat("Median Ozone:", median_ozone, "\n")
## Median Ozone: 31.5
cat("Standar Deviasi Ozone:", sd_ozone, "\n")
## Standar Deviasi Ozone: 32.98788
# Buat scatter plot
plot(airquality$Wind, airquality$Temp,
main = "Scatter Plot Wind vs Temp",
xlab = "Wind",
ylab = "Temperature",
pch = 19, col = "blue")
# Load dataset mtcars
data("mtcars")
# Buat bar chart dengan label jumlah setiap kategori
barplot(table(mtcars$cyl),
main = "Bar Chart Variabel cyl",
xlab = "Number of Cylinders",
ylab = "Frequency",
col = "lightblue")
# Tambahkan label jumlah
text(x = 1:length(table(mtcars$cyl)),
y = table(mtcars$cyl),
labels = table(mtcars$cyl),
pos = 3, col = "red")
# Load dataset iris
data("iris")
# Buat boxplot
boxplot(Petal.Width ~ Species, data = iris,
main = "Boxplot Petal.Width Berdasarkan Species",
xlab = "Species",
ylab = "Petal.Width",
col = c("pink", "lightgreen", "lightblue"))
# Hitung korelasi
correlation <- cor(iris$Sepal.Length, iris$Petal.Length)
# Tampilkan hasil korelasi
cat("Korelasi antara Sepal.Length dan Petal.Length:", correlation, "\n")
## Korelasi antara Sepal.Length dan Petal.Length: 0.8717538
# Load library
library(ggplot2)
# Buat scatter plot dengan garis regresi berdasarkan Species
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Scatter Plot Sepal.Length vs Sepal.Width",
x = "Sepal.Length",
y = "Sepal.Width") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
# Uji Chi-Square
chisq_test <- chisq.test(table(mtcars$vs, mtcars$am))
# Tampilkan hasil
print(chisq_test)
##
## 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
# Bangun model regresi: Temp ~ Solar.R
model <- lm(Temp ~ Solar.R, data = airquality)
# Tampilkan 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
# Buat scatter plot dengan garis regresi
plot(airquality$Solar.R, airquality$Temp,
main = "Scatter Plot Solar.R vs Temp dengan Garis Regresi",
xlab = "Solar.R",
ylab = "Temperature",
pch = 19, col = "blue")
# Tambahkan garis regresi
abline(model, col = "red", lwd = 2)
# Ekstrak koefisien regresi dan nilai R^2
coef_model <- summary(model)$coefficients
r_squared <- summary(model)$r.squared
# Tampilkan hasil
cat("Koefisien Regresi (Intercept dan Slope):\n")
## Koefisien Regresi (Intercept dan Slope):
print(coef_model)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 72.86301176 1.693950864 43.013651 4.541566e-84
## Solar.R 0.02825463 0.008204764 3.443686 7.517729e-04
cat("\nNilai R-squared:", r_squared, "\n")
##
## Nilai R-squared: 0.07608786
KESIMPULAN: Koefisien regresi yang dihasilkan menunjukkan hubungan antara variabel independen dan dependen dalam model. Koefisien Intercept sebesar 72.86 menunjukkan nilai prediksi suhu (Temp) saat radiasi matahari (Solar.R) bernilai nol. Koefisien Solar.R sebesar 0.0282 menunjukkan bahwa setiap peningkatan satu unit pada Solar.R akan meningkatkan suhu (Temp) sekitar 0.0282 derajat. Kedua koefisien tersebut signifikan secara statistik, karena nilai t-value untuk keduanya sangat besar (lebih dari 2) dan p-value sangat kecil, menunjukkan bahwa pengaruh kedua variabel ini pada model tidak terjadi karena kebetulan. Namun, meskipun model ini signifikan, nilai R-squared yang hanya sebesar 7.6% menunjukkan bahwa model ini hanya menjelaskan sebagian kecil variasi suhu yang dipengaruhi oleh radiasi matahari, dan banyak faktor lain yang memengaruhi suhu yang tidak tercakup dalam model.