You are on page 1of 7

Nama : Fisty Adistinov Budiyanto

NIM : 22/493959/PA/21232

PEMBAHASAN KUIS 1 PRATIKUM KOMPUTASI STATISTIKA I


1. Syntax:
#------------NOMOR 1-----------
#membuat persegi orange
x=c(-8,-8,8,8)
y=c(-8,8,8,-8)
plot(x,y,type="n",ylim=c(-15,15),xlim=c(-
15,15),main="Logo Matahari")
polygon(x,y,col="orange",border = "orange")
#membuat belah ketupat orange
x=c(0,11.3137,0,-11.3137)
y=c(11.3137,0,-11.3137,0)
polygon(x,y,col="orange",border = "orange")
#membuat persegi kuning
x=c(-7.7,-7.7,7.7,7.7)
y=c(-7.7,7.7,7.7,-7.7)
polygon(x,y,col="yellow",border = "yellow")
#membuat belah ketupat kuning
x=c(0,10.9,0,-10.9)
y=c(10.9,0,-10.9,0)
polygon(x,y,col="yellow",border = "yellow")
#membuat persegi orange
x=c(-6.7,-6.7,6.7,6.7)
y=c(-6.7,6.7,6.7,-6.7)
polygon(x,y,col="orange",border = "orange")
#membuat belah ketupat orange
x=c(0,9.475,0,-9.475)
y=c(9.475,0,-9.475,0)
polygon(x,y,col="orange",border = "orange")

#membuat 8 segitiga kuning


x=c(-2,0,2)
y=c(6.7,8.7,6.7)
polygon(x,y,col="yellow",border = "yellow")

x=c(3.2,6.2,6.2)
y=c(6.2,6.2,3.2)
polygon(x,y,col="yellow",border = "yellow")
x=c(6.7,8.7,6.7)
y=c(2,0,-2)
polygon(x,y,col="yellow",border = "yellow")

x=c(6.2,6.2,3.2)
y=c(-3.2,-6.2,-6.2)
polygon(x,y,col="yellow",border = "yellow")

x=c(2,0,-2)
y=c(-6.7,-8.7,-6.7)
polygon(x,y,col="yellow",border = "yellow")

x=c(-3.2,-6.2,-6.2)
y=c(-6.2,-6.2,-3.2)
polygon(x,y,col="yellow",border = "yellow")

x=c(-6.7,-8.7,-6.7)
y=c(-2,0,2)
polygon(x,y,col="yellow",border = "yellow")

x=c(-6.2,-6.2,-3.2)
y=c(3.2,6.2,6.2)
polygon(x,y,col="yellow",border = "yellow")
#membuat lingkaran kuning
r=4.5
x0=0
y0=0
theta=seq(0,2*pi,length=1000)
a=x0+r*cos(theta)
b=y0+r*sin(theta)
polygon(a,b,col="yellow",border = "yellow")
#membuat setengah lingkaran merah
r=2.5
x0=0
y0=-1
theta=seq(0,pi,length=1000)
a=x0+r*cos(-theta)
b=y0+r*sin(-theta)
polygon(a,b,col="red",border = "red")
#membuat 2 setengah lingkaran hitam
r=1.6
x0=-2
y0=0.8
theta=seq(0,pi,length=1000)
a=x0+r*cos(theta)
b=y0+r*sin(theta)
polygon(a,b,col="black",border = "black")

r=1.6
x0=2
y0=0.8
theta=seq(0,pi,length=1000)
a=x0+r*cos(theta)
b=y0+r*sin(theta)
polygon(a,b,col="black",border = "black")

Output:

2. Syntax:
Bangkit_Weibull <- function(n, alpha, beta){
set.seed(123)
U = runif(n,0,1)
x=(-log(1-U)/alpha)^(1/beta)
return(x)
}

plot(NULL, xlim = c(0, 2.5), ylim = c(0, 1.5), xlab =


"x", ylab = "y")
lines(density(Bangkit_Weibull(100, 1, 1)), col =
"royalblue")
lines(density(Bangkit_Weibull(100, 1, 2)), col =
"salmon")
lines(density(Bangkit_Weibull(100, 1, 3.5)), col =
"rosybrown")
legend(legend=c("beta = 1",'beta = 2', "beta =
3.5"),lty=c(1,1,1,1,1),

col=c('royalblue','salmon','rosybrown'),'topright')

Output:

3. Syntax:
#-----NOMOR 3--------
# Define the data
data <- array(data = c(109, 4, 116, 84), dim = c(2, 2))
data

# Function : find expected frequency


expected_frequency <- function(data){
n <- rowSums(data)
m <- colSums(data)
N <- sum(data)
Fe <- matrix(0, nrow = 2, ncol = 2)

for (i in 1:2) {
for (j in 1:2) {
Fe[i,j] <- (n[i]* m[j])/N
}
}
return(Fe)
}

# Function : find chi-square value


chi_square <- function(data){
FE <- expected_frequency(data)
chi_Sq <- sum((data - FE)^2 / FE)

return(chi_Sq)
}

# Function : find p-value


p_value <- function(data){
1 - pchisq(chi_square(data), df = 1)
}

# Function : find critical value


alpha <- 0.05
critical_value <- function(data,alpha){
qchisq(1 - alpha, 1)
}

# Function : find Odds Ratio


odds_ratio <- function(data) {
a <- data[1,1]
b <- data[2,1]
c <- data[1,2]
d <- data[2,2]
return((a * d) / (b * c))
}

# Function : find Confidence Interval for Odds Ratio


confidence_interval <- function(data, alpha = 0.05) {
# Menghitung OR
OR <- odds_ratio(data)

# Menghitung Standard Error (SE) dari log(OR)


SE <- sqrt(1 / a + 1 / b + 1 / c + 1 / d)

# Menghitung Lower Confidence Interval (Lower CI)


lower_ci <- exp(log(OR) - qnorm(1 - alpha / 2) * SE)

# Menghitung Upper Confidence Interval (Upper CI)


upper_ci <- exp(log(OR) + qnorm(1 - alpha / 2) * SE)

return(list(LowerCI = lower_ci, UpperCI = upper_ci))


}

# Function : print the output


output <- function(){
# Print the expected frequency
cat("Expected Frequency :\n")
cat(expected_frequency(data)[1], "\t",
expected_frequency(data)[3], "\n")
cat(expected_frequency(data)[2], "\t",
expected_frequency(data)[4], "\n")

# Hasil Pengujian

cat("====================================================
==========\n")
cat("Hipotesis Uji\n")
cat("Hipotesis_0: Tidak ada hubungan antara makan
sambal dan sakit perut\n")
cat("Hipotesis_1: Ada hubungan antara makan sambal dan
sakit perut\n")

cat("====================================================
==========\n")
cat("Tingkat Signifikansi\n")
cat("alpha = 0.05\n")

cat("====================================================
==========\n")
cat("Statistik Uji\n")
cat("Chi-Square =", chi_square(data), "\n")
cat("P-Value =", p_value(data), "\n")
cat("Critical Value =", critical_value(data, 0.05),
"\n")
cat("Odds Ratio =", odds_ratio(data), "\n")
cat("Lower Confidence Interval =",
confidence_interval(data, 0.05)[[1]], "\n")
cat("Upper Confidence Interval =",
confidence_interval(data, 0.05)[[2]], "\n")

cat("====================================================
==========\n")
cat("Kesimpulan\n")
if(p_value(data) < 0.05){
cat("Karena P-Value < alpha, maka hipotesis nol
ditolak\n")
cat("Maka ada hubungan antara makan sambal dan sakit
perut\n")
} else{
cat("Karena P-Value > alpha, maka hipotesis nol
diterima\n")
cat("Maka tidak ada hubungan antara makan sambal dan
sakit perut\n")
}

cat("====================================================
==========")
}

# Call the output function


alpha <- 0.05
output()

Output:

You might also like